String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

function $(id)
{
	return document.getElementById( id ); 
}

function $N(name)
{
	return document.getElementsByName( name );
}

function vSocial()
{
}

vSocial.upload_showing = false;
vSocial.nsfw_showing = false;
vSocial.login_showing = false;
vSocial.background_hide = 0;
vSocial.called = false;
vSocial.video_block = false;
vSocial.newMessageWindowShowing = false;

vSocial.doAjax = function( url, params, handler, method )
{
	if ( method == undefined ) method = "get";
	
	new Ajax.Request(url, {
	  method: method,
	  parameters: params,
		
	  onSuccess: function( transport )
	  {
		  try
		  {
			  handler.onSuccess( transport.responseText );
		  } catch(e){}
	  }
  });
  
  try { handler.onCreate(); } catch(e){}
}

vSocial.leave_handler =
{
	onSuccess: function( data )
	{
		vsocial.loadUserpage('communities', Globals.visitor_id );
	}
}

vSocial.leaveCommunity = function( community_id )
{
	if ( !confirm( "Are you sure you wish to leave the community?" ) )
		return;
	
	vSocial.doAjax( "/index.php", { action: 128, community_id: community_id }, vSocial.leave_handler );
}

vSocial.friend_handler =
{
	onCreate: function()
	{
		$('friend_toggle').innerHTML = '<span class="saving">Saving...</span>';
	},
	
	onSuccess: function(data)
	{
		$('friend_toggle').innerHTML = data;
	}
}

vSocial.context_handler = {
	onCreate: function() {
		$('results').innerHTML = '<h1>Please wait...</h1>';
	},
	onSuccess: function(data) {
		$('results').innerHTML = data;
	}
}

vSocial.favorite_handler =
{
	onCreate: function()
	{
		$('favorite_toggle').innerHTML = '<span class="saving">Saving...</span>';
	},
	
	onSuccess: function(data)
	{
		$('favorite_toggle').innerHTML = data;
	}
}

vSocial.userpage_handler =
{
	onCreate: function()
	{
		$('home_left').innerHTML = '<div id="left_column"><h1>Please wait...</h1></div>';
	},
	
	onSuccess: function(data)
	{
		$('home_left').innerHTML = data;
	}	
}

vSocial.homepage_handler =
{
	onCreate: function()
	{
		$('home_left').innerHTML = '<div id="left_column"><h1>Please wait...</h1></div>';
	},
	
	onSuccess: function(data)
	{
		$('home_left').innerHTML = data;
		

		vsocial.recently_viewed();
	}	
}

vSocial.videoblock_handler =
{
	onCreate: function()
	{
		vSocial.video_block_loading = true;
		
		Effect.Opacity(vSocial.video_block, 40);
	},
	
	onSuccess: function(data)
	{
		Effect.Opacity(vSocial.video_block, 100);
		
		if (vSocial.video_block) $(vSocial.video_block).innerHTML = data;
		
		vSocial.video_block_loading = false;
	}
}

vSocial.recently_viewed_handler =
{
	onCreate: function()
	{
		
	},
	
	onSuccess: function(data) {
		$('staging_area').innerHTML = data;
		
		vSocial.recently_viewed_loader();
	}
}

vSocial.recently_viewed_loader = function() {
	img = $('staging_area').getElementsByTagName("img");
	
	not_loaded = false;
	
	for(i = 0; i < img.length; i++) {
		if (!DOM.ImageLoaded(img[i])) {
			not_loaded = true;
		}
	}

	if (!not_loaded) {
		$('recent').innerHTML = $('recent').innerHTML + $('staging_area').innerHTML;
		
		Effect.Scroll({id: 'recent', left: 400, time: 2});
		
		
		setTimeout(vsocial.recently_viewed, 5000);
	} else {
		setTimeout(vSocial.recently_viewed_loader, 200);
	}
}

vSocial.message_handler =
{
	onCreate: function()
	{
		$('private_messages').innerHTML = "<h1>Please wait...</h1>";
	},
	
	onSuccess: function(data)
	{
		$('private_messages').innerHTML = data;
	}
}

vSocial.comment_handler =
{
	onCreate: function()
	{
		$('video_comments').innerHTML = '<div id="left_column"><h1>Please wait...</h1></div>';
	},
	
	onSuccess: function(data)
	{
	
		$('video_comments').innerHTML = data;
		$('comment_input').disabled = false; 
		$('comment_submit').disabled = false; 
		
		$('comment_input').value = '';

		alert( "Your comment has been submitted" );
		
	}	
}

vSocial.tag_handler =
{
	onCreate: function()
	{
		if ( $('video_tags') )
			$('video_tags').innerHTML = '<div id="left_column"><h1>Please wait...</h1></div>';
		if ( $('video_tags_full') )
			$('video_tags_full').innerHTML = '<div id="left_column"><h1>Please wait...</h1></div>';
	},
	
	onSuccess: function(data)
	{
		if ( $('video_tags') )
			$('video_tags').innerHTML = data;
		if ( $('video_tags_full') )
			$('video_tags_full').innerHTML = data;

		if ( ( e = $('tag_input') ) )
		{
			e.disabled = false;
			e.value = '';
		}
		
		if ( ( e = $('tag_submit') ) )
			e.disabled = false;
	}	
}

vSocial.rate_handler =
{
	onCreate: function()
	{
	},
	
	onSuccess: function(data)
	{
		$('rating').innerHTML = data;
	}	
}
vSocial.adshare_handler = {
	onCreate: function() { },
	onSuccess: function( data ) {
		var rData = data.split("|");
		$( "adshare_display_" + rData[0] ).innerHTML = rData[1];
	}
}
vSocial.upload_handler =
{
	onCreate: function()
	{
	},
	
	onSuccess: function(data)
	{
		data = parseInt( data );
		
		var bar = $('upload_inner');
		
		if ( bar && data > 0 && data > bar.style.width.replace('%', '') )
		bar.style.width = data + "%";
		
		//bar = $('upload_outer');
		//bar.innerHTML = data;
		setTimeout( 'vsocial.uploadStatus(\'' + guid + '\');', 1000 );		
	}
}

vSocial.login_handler =
{
	onCreate: function()
	{
	},
	
	onSuccess: function( data )
	{
		data = parseInt( data );
		
		if ( data == 1 )
		{
			// Logged in, so refresh page
			//history.go();
			window.location = window.location;
		}
		else if ( data == 2 )
			window.location = "/user/?action=63";
		else
		{
			new Effect.Shake( $('lightbox_container') );
			
			$('login_message').innerHTML = "Invalid login";
			$('pass_overlay').value = "";
		}
	}		
}

vSocial.email_handler =
{
	onCreate: function() {},
	
	onSuccess: function( data )
	{ alert( "Your email(s) have been sent!" ); }		
}
vSocial.diggIt_handler = {
	onCreate: function() { 	},
	onSuccess: function( data ) {
		if (data == 'false') {
			$('diggIt').style.display = 'block';
		}
		else {
			$('diggIt').innerHTML = "<iframe onload=\"$('diggIt').style.display = 'block';\" id='diggItiframe' src='http://digg.com/api/diggthis.php?u=" + escape(data) + "' height='82' width='55' frameborder='0' scrolling='no'></iframe>";
		}
		
		
	}
}
vSocial.prototype.diggIt = function ( video_id ) {
	vSocial.doAjax( "/index.php", { action: 124, d: video_id }, vSocial.diggIt_handler );
}

vSocial.MakePrivate_handler =
{
	onCreate: function() {},
	onSuccess: function( data ) {
		if (data == 'false') {
			$('diggIt').style.display = 'block';
		}
		else {
			$('diggIt').innerHTML = "<iframe onload=\"$('diggIt').style.display = 'block';\" id='diggItiframe' src='http://digg.com/api/diggthis.php?u=" + escape(data) + "' height='82' width='55' frameborder='0' scrolling='no'></iframe>";
		}
		
		
	}
}
vSocial.prototype.MakePrivate = function( vid, action ) {
	vSocial.doAjax( "/index.php", { action: 123, v: vid, a: action }, vSocial.MakePrivate_handler );
}
vSocial.NetworkMessage_handler = 
{
	onCreate: function() {},
	onSuccess: function ( data )
	{
		$('NetMsg').style.display='none';
	}
}
vSocial.prototype.NetworkMessage = function ( user_id, network_message_id ) 
{
	vSocial.doAjax( "/index.php", { action: 122, u: user_id, n: network_message_id }, vSocial.NetworkMessage_handler );
}
vSocial.BlogPreview_handler = 
{
	onCreate: function() {},
	onSuccess: function ( data )
	{
		$('BlogPreview').innerHTML = data;
	}
}
vSocial.prototype.BlogPreview = function ( template, video_id, blog_id, title, post, tags )
{
	vSocial.doAjax( "/index.php", { action: 120, t: template, d: video_id, u: blog_id, p: post, c: tags, title: title }, vSocial.BlogPreview_handler );
}

vSocial.null_handler =
{
	onCreate: function() {},
	onSuccess: function() {}
}

vSocial.search_handler = {
	onCreate: function() {
		$('results').innerHTML = '<h1>Please wait...</h1>';
	},
	onSuccess: function(data) {
		$('results').innerHTML = data;
	}
}
vSocial.viewmore_handler = {
	onCreate: function() {
		$('friends').innerHTML = '<h1>Please wait...</h1>';
	},
	onSuccess: function(data) {
		$('friends').innerHTML = data;
	}
}
vSocial.validateForm = function( frm_name, use_asterisks )
{
    var invalid = 0;
	
	var form = frm_name;

    for ( var i = 0; i < form.length; i++ )
    {
        var cur_element = form.elements[i];
		
        // Text fields
        if ( cur_element.type == "text" || cur_element.type == "textarea" || cur_element.type == "password" || cur_element.tagName.toLowerCase() == "select" )
        {
            // Trim value
            cur_element.value = cur_element.value ? cur_element.value.trim() : "";

            if ( !cur_element.getAttribute( "optional" ) && ( ( cur_element.tagName.toLowerCase() == "select" && cur_element.value == 0 ) || (!cur_element.value || cur_element.value.length == 0 ) ) )
            {
                //alert( cur_element.error_message ? cur_element.error_message : "Field " + cur_element.name + " cannot be blank." );
				if ( use_asterisks )
					if ( !( cur_element.nextSibling && cur_element.nextSibling.nodeType == 1 && cur_element.nextSibling.className.indexOf( "__validation_element" ) != -1 ) )					
					{
						var span = document.createElement( "span" );
						span.innerHTML = "***";
						span.className = "__validation_element invalid";
						
						var ref = cur_element.nextSibling;
						
						if ( ref )
							cur_element.parentNode.insertBefore( span, ref );
						else
							cur_element.parentNode.appendChild( span );
					}
				
                cur_element.className = cur_element.className + " invalid";
                invalid++;
                continue;
            }
        }
		
		if ( cur_element.nextSibling && cur_element.nextSibling.nodeType == 1 && cur_element.nextSibling.className.indexOf( "__validation_element" ) != -1 )
			cur_element.parentNode.removeChild( cur_element.nextSibling );
		
		cur_element.className = cur_element.className.replace( /[ ]*invalid/gi, '' );
    }

    if ( invalid > 0 )
        alert( "Please complete the marked field" + ( invalid > 1 ? "s" : "" ) + " before continuing" );

    return invalid == 0;
}


vSocial.prototype.toggleFriend = function( user_id )
{
	vSocial.doAjax( "/user/index.php", { action: 200, user_id: user_id }, vSocial.friend_handler );
}

vSocial.prototype.toggleFavorite = function( video_id )
{
	vSocial.doAjax( "/video/index.php", { action: 102, video_id: video_id }, vSocial.favorite_handler );
}
vSocial.prototype.loadUserpage = function( which, user )
{
	vSocial.doAjax( "/index.php", { action: which == "pcomments" || which == "profile" || which == "communities" ? 112 : 102, s: which, d: user }, vSocial.userpage_handler );
}
vSocial.prototype.loadPendUserpage = function( which, user )
{
	vSocial.doAjax( "/index.php", { action: which == "pending" ? 112 : 102, s: which, d: user }, vSocial.userpage_handler );
}
vSocial.prototype.loadVideos = function( which, page, channel )
{
	// Close featured uploader div if its not 'hot' page
	if ( $('featured_uploader') ) {
		if ( which == "hot" ) {
			$('featured_uploader').style.display = 'block';
		} else
			$('featured_uploader').style.display = 'none';
	}
	
	var vars = {};
		
	// Set the parameters for the AJAX call
	vars.action = 102;
	vars.s = which;
	if ( page ) vars.page = page;
	if ( channel ) vars.d = channel;
	
	// Update the AJAX bookmarks
	AjaxNavigation.setParams( vars );
	AjaxNavigation.callFunction = "vsocial.loadVideos('"+which+"', '"+page+"', '"+channel+"')";
	AjaxNavigation.updateAddress();
	
	// Send the request
	vSocial.doAjax( "/index.php", vars, vSocial.homepage_handler );
}

vSocial.prototype.checkNewMessages = function()
{
	new Ajax.Request("/messages", {
	  method: "GET",
	  parameters: {action: 11},
		
	  onSuccess: function( transport )
		{
			eval("var obj = " + transport.responseText);
			
			if (obj.newMessages == true)
			{
				$('new_message').style.display = "inline";
				
				if ( obj.showWindow && obj.unreadMessageCount > 0 && obj.newMessageCount > 0 && obj.unreadMessageCount > obj.newMessageCount )
				{
					//$('message_overlay').style.display = "block";
					
					new Effect.Appear( $('message_overlay'), {queue: 'front'} );
					
					vSocial.newMessageWindowShowing = true;
					
					$('message_overlay_new_messages').innerHTML = obj.newMessageCount;
					$('message_overlay_unread_messages').innerHTML = obj.unreadMessageCount;
					
					$('message_overlay_subject').innerHTML = obj.message.subject;
					$('message_overlay_body').innerHTML = obj.message.body;
				}
			}
			else
			{
				$('new_message').style.display = "none";	
				$('message_overlay').style.display = "none";
			}
			
			setTimeout( vsocial.checkNewMessages, 5000 );
		}
	  });
}

vSocial.prototype.hideNewMessageWindow = function()
{
	new Ajax.Request( "/messages", {
			method: "GET",
			parameters: {action: 13}
	});
	
	//$('message_overlay').style.display = 'none';
	new Effect.Fade( $('message_overlay') );
	
	vSocial.newMessageWindowShowing = false;
}

vSocial.prototype.loadMessages = function( box, message_id )
{
	var vars = {};
	
	if (box == "compose")
		vars.action = 103;
	else if (box == "message")
		vars.action = 102;
	else
		vars.action = 101;
	
	if (!box) box = "inbox";
	
	vars.s = box
	if (message_id) vars.m = message_id;
	
	AjaxNavigation.setParams( vars );
	AjaxNavigation.callFunction = "vsocial.loadMessages('"+box+"', '"+message_id+"')";
	AjaxNavigation.updateAddress();
	
	
	vSocial.doAjax( "/messages", vars, vSocial.message_handler );
}

vSocial.prototype.moveMessages = function( messages, box )
{
	var message_array = new Array();
	var count = 0;
	var messages_str = "";
	
	for(i = 0; i < messages.length; i++) {
		if (messages[i].checked)
		{
			count++;
			messages_str += "messages[]=" + messages[i].value + "&";
		}
	}
	
	if (count == 0) return;
		
	vSocial.doAjax( "/messages?" + messages_str + "action=12&box=" + box,
					{},
					null
					);
					
	
	this.loadMessages(box);
}

vSocial.prototype.loadVideoBlock = function( which, page, channel )
{
	if (vSocial.video_block_loading) return;
	
	this.ajax.SetData( "action", 126 );
	this.ajax.SetData( "page", page );
	this.ajax.SetData( "s", which);
	if (channel) this.ajax.SetData( "d", channel );
	
	vSocial.video_block = which;
	
	this.ajax.Register( vSocial.videoblock_handler );
	this.ajax.SendRequest("GET");
}

vSocial.prototype.loadOtherVideos = function ( user_id, video_id, page )
{
	new Ajax.Request( "/video",
		{
			method: "GET",
			parameters: {
				action: 115,
				v: video_id,
				d: user_id,
				page: page
			},
			
			onSuccess: function( transport )
			{
				$('other_videos').innerHTML = transport.responseText;
			},
			
			onCreate: function()
			{
				$('other_videos').innerHTML = "loading...";	
			}
		}
	);
}

vSocial.prototype.recently_viewed = function()
{
	if (!$('recent')) return;
	
	this.recent_ajax = new Ajax("/widgets/json.php");
	
	this.recent_ajax.SetData( "action", 300 );
	this.recent_ajax.SetData( "page", 0 );
	this.recent_ajax.SetData( "s", "recent" );
	
	var channel = AjaxNavigation.getParam("d");
	
	if (channel) this.recent_ajax.SetData( "d", channel );
	
	this.recent_ajax.Register( vSocial.recently_viewed_handler );
	this.recent_ajax.SendRequest("GET");
}

vSocial.prototype.addComment = function( comment, meta_id, type )
{
	if ( !comment )
	{
		alert( "Please enter a comment to add." );
		return false;
	}
	
	$('comment_input').disabled = true; 
	$('comment_submit').disabled = true; 
	vSocial.doAjax( "/video/index.php", 
					{ 
						m: comment, 
						d: meta_id, 
						type: type, 
						action: 105 
					}, 
					vSocial.comment_handler, 
					"POST");
}
vSocial.prototype.addPhotoComment = function( comment, meta_id, type, track )
{
	$('comment_input').disabled = true; $('comment_submit').disabled = true; 
	
	vSocial.doAjax( "/comments/add", 
					{ 
						m: comment, 
						d: meta_id, 
						type: "image", 
						track: track
					}, 
					vSocial.comment_handler, 
					"POST");
}

vSocial.prototype.addTag = function( comment, meta_id, type )
{
	if ( !comment )
	{
		alert( "Please enter the tag to add." );
		return false;
	}
	
	if ( ( e = $('tag_input') ) )
		e.disabled = true;
	
	if ( ( e = $('tag_submit') ) )
		e.disabled = true;
	
	vSocial.doAjax( "/video/index.php",
					{
						action: 106,
						m: comment,
						d: meta_id
					},
					vSocial.tag_handler,
					"POST");
}

vSocial.prototype.deleteComment = function( comment_id )
{
	// Hide the comment instantly
	$('comment_' + comment_id).style.display = 'none';

	// Do the delete
	vSocial.doAjax( "/video/index.php",
					{
						action: 114,
						id: comment_id
					},
					vSocial.null_handler );
}
vSocial.prototype.approveComment = function( comment_id )
{
	// Hide the comment instantly
	$('comment_' + comment_id).style.display = 'none';

	// Do the delete
	vSocial.doAjax( "/video/index.php",
					{
						action: 127,
						id: comment_id
					},
					vSocial.null_handler);
}
vSocial.prototype.deleteCommentTracking = function ( tracking_id, type, meta_id)
{
	//Hide the anchor
	$('comment_tracking').style.display = 'none';
	
	//Do the ajax
	vSocial.doAjax( "/video/index.php",
					{
						action: 125,
						id: tracking_id,
						type: type,
						meta_id: meta_id
					},
					vSocial.null_handler,
					"POST"
					);
}
vSocial.prototype.deleteVCommentTracking = function ( comment_id )
{
	//Hide the anchor
	$('delt').style.display = 'none';
	
	//Do the ajax
	vSocial.doAjax( "/video/index.php",
					{
						action: 128,
						id: comment_id
			
					},
					vSocial.null_handler);
}

vSocial.prototype.nsfwEnable = function()
{
	this.ajax.SetData( "action", 115 );
	this.ajax.Register( vSocial.null_handler );
	this.ajax.SendRequest("GET");
}

vSocial.prototype.sendEmail = function( meta_id, type, addresses )
{
	vSocial.doAjax( "/video/index.php", { action: 117, id: meta_id, email: addresses, meta_type: type }, vSocial.email_handler );
}



vSocial.emailToggle = function()
{
	var obj = document.createElement( "div" );
	obj.innerHTML = $("email_box").innerHTML;
	
	Lightbox.show({content: obj});
	
	vSocial.email_showing = true;
	$('addresses').focus();
}

vSocial.blogItToggle = function()
{
	var obj = document.createElement( "div" );
	obj.innerHTML = $("blogit_box").innerHTML;
	
	Lightbox.show({content: obj});
	
	vSocial.email_showing = true;
	$('addresses').focus();
}

vSocial.loginToggle = function()
{
	showLogin();
}

vSocial.uploadToggle = function()
{
	if (!vSocial.upload_showing)
	{
		if ( vSocial.background_hide == 0 )
		{
			vSocial.toggleSelects(false);

			page_size = getPageSize();
			$('fade_div').style.height = page_size ? page_size[1] + "px" : "100%";
			$('fade_div').style.display = 'block';
		}

		if ( vSocial.background_hide < 75 )
		{
			vSocial.background_hide += 15;
			$('fade_div').style.MozOpacity = "0." + vSocial.background_hide;
			$('fade_div').style.opacity 	= "0." + vSocial.background_hide;
			$('fade_div').style.filter		= "alpha(opacity=" + vSocial.background_hide + ")";
			window.setTimeout( "vSocial.uploadToggle()", 10 );
		}
		else
		{
			vSocial.upload_showing = true;
			$('upload_status').style.display = 'block';
			$('upload_status').style.zIndex = 9999;
		}
	}
	else
	{
		$('upload_status').style.display = 'none';
		if ( vSocial.background_hide > 0 )
		{
			vSocial.background_hide -= 15;
			if ( vSocial.background_hide < 0 )
				vSocial.background_hide = 0;
			
			$("fade_div").style.MozOpacity = "0." + vSocial.background_hide;
			$("fade_div").style.opacity = "0." + vSocial.background_hide;
			$("fade_div").style.filter = "alpha(opacity=" + vSocial.background_hide + ")";
			window.setTimeout("vSocial.uploadToggle()", 10);
		}
		else
		{
			vSocial.upload_showing = false;
			$('fade_div').style.display = 'none';
			vSocial.toggleSelects( true );
		}
    }
}

vSocial.nsfwToggle = function()
{
	var flash_player = $('flash_player');

	if (!vSocial.nsfw_showing)
	{
		if ( flash_player )
			flash_player.style.height = '0px';
		
		if ( vSocial.background_hide == 0 )
		{
			page_size = getPageSize();
			$('fade_div').style.height = page_size ? page_size[1] + "px" : "100%";
			$('fade_div').style.display = 'block';
		}

		if ( vSocial.background_hide < 99 )
		{
			vSocial.background_hide += 20;
			if ( vSocial.background_hide >= 100 )
				vSocial.background_hide = 99;
			
			$('fade_div').style.MozOpacity = "0." + vSocial.background_hide;
			$('fade_div').style.opacity 	= "0." + vSocial.background_hide;
			$('fade_div').style.filter		= "alpha(opacity=" + vSocial.background_hide + ")";
			window.setTimeout( "vSocial.nsfwToggle()", 10 );
		}
		else
		{
			vSocial.nsfw_showing = true;
			$('nsfw_box').style.display = 'block';
			$('nsfw_box').style.zIndex = 9999;
		}
	}
	else
	{
		$('nsfw_box').style.display = 'none';
		if ( vSocial.background_hide > 0 )
		{
			vSocial.background_hide -= 20;
			$("fade_div").style.MozOpacity = "0." + vSocial.background_hide;
			$("fade_div").style.opacity = "0." + vSocial.background_hide;
			$("fade_div").style.filter = "alpha(opacity=" + vSocial.background_hide + ")";
			window.setTimeout("vSocial.nsfwToggle()", 10);
		}
		else
		{
			vSocial.nsfw_showing = false;
			$('fade_div').style.display = 'none';

			if ( flash_player )
				flash_player.style.height = '400px';
		}
    }
}


vSocial.toggleSelects = function( on )
{
	var selects = document.getElementsByTagName( "select" );
	
	for( var i = 0; i < selects.length; i++ )
		selects[i].style.display = on ? 'block' : 'none';
}


vSocial.profileNav = function( tab )
{
	try {
		$('link_share').className 	= ( tab == 0 ) ? 'active' : '';
		//('link_details').className = ( tab == 1 ) ? 'active' : '';
		$('link_tags').className 	= ( tab == 2 ) ? 'active' : '';
		$('link_admin').className 	= ( tab == 3 ) ? 'active' : '';
	} catch(e) {}

	try {
		$('tab_share').style.display 	= ( tab == 0 ) ? 'block' : 'none';
		//$('tab_details').style.display 	= ( tab == 1 ) ? 'block' : 'none';
		$('tab_tags').style.display 	= ( tab == 2 ) ? 'block' : 'none';
		$('tab_admin').style.display 	= ( tab == 3 ) ? 'block' : 'none';
	} catch(e) {}
}

vSocial.updateMyRating = function(rating)
{
	for( var i = 0; i < 5; i++ )
		$('rate_' + (i+1) ).src = 'http://origin-static.vsocial.com/vespa/images/star_' + ((i < rating) ? 'blue' : 'white') + '.jpg';
}

vSocial.prototype.authenticateUser = function( username, password )
{
	vSocial.doAjax( "/index.php", { action: 113, User: username, Pass: password }, vSocial.login_handler, "POST" );
}

vSocial.prototype.rateVideo = function( rating, video_id )
{
	vSocial.updateMyRating( rating );
	vSocial.doAjax( "/video", { action: 110, s: rating, m: video_id }, vSocial.rate_handler );
}

vSocial.prototype.ratePhoto = function( rating, image_id )
{
	vSocial.doAjax( "/photos/rate", { s: rating, m: image_id }, vSocial.rate_handler );
}

vSocial.prototype.ajaxPhotos = function( type, page, image_id )
{
	var func = new Function( "data", "$(\""+type+"_images\").innerHTML = data;" );
	
	vSocial.doAjax( "/photos/small-photo-list", { type: type, page: page, image_id: image_id }, { onSuccess: func } );	
}

vSocial.prototype.loadVideoPage = function( what, sort, type, page ) {
	
	new Ajax.Request('/video/index.php', {
		  method: 'get',
		  parameters: { 
				action: 101,
				s: what,
				sort: sort,
				type: type,
				page: page
			},
			
		  onSuccess: function( transport )
		  {
			$('results').innerHTML = transport.responseText;  
		  }
	  });
	
	
	
	AjaxNavigation.setParams( { s: what, type: type, sort: sort, p: page } );
	AjaxNavigation.callFunction = "vsocial.loadVideoPage('"+what+"', '"+sort+"', '"+type+"', '"+page+"')";
	AjaxNavigation.updateAddress();
	
	$('results').innerHTML = "<h1>Loading...</h1>";
	
	setTimeout('scrollUp()', 100 );
}

vSocial.prototype.loadUserPage = function( what, sort, type, page ) {
	
	new Ajax.Request('/user/index.php', {
		  method: 'get',
		  parameters: { 
				action: 101,
				s: what,
				sort: sort,
				type: type,
				page: page
			},
			
		  onSuccess: function( transport )
		  {
			$('results').innerHTML = transport.responseText;  
		  }
	  });
	
	AjaxNavigation.setParams( { s: what, type: type, sort: sort, p: page } );
	AjaxNavigation.callFunction = "vsocial.loadUserPage('"+what+"', '"+sort+"', '"+type+"', '"+page+"')";
	AjaxNavigation.updateAddress();
	
	$('results').innerHTML = "<h1>Loading...</h1>";
	
	setTimeout('scrollUp()', 100 );
}

vSocial.prototype.loadCommunitiesPage = function( what, sort, type, page ) {
	var query = { action: 101, s: what, sort: sort, page: page, type: type }
	
	AjaxNavigation.setParams( query );
	AjaxNavigation.updateAddress();
	
	vSocial.doAjax( "/communities", query, vSocial.search_handler );
	
	setTimeout('scrollUp()', 100 );
}

vSocial.prototype.loadPhotosPage = function( what, sort, type, page ) {
	var query = { ajax: true, s: what, sort: sort, page: page, type: type }
	
	AjaxNavigation.setParams( query );
	AjaxNavigation.updateAddress();
	
	vSocial.doAjax( "/photos/list", query, vSocial.search_handler );
	
	setTimeout('scrollUp()', 100 );
}

vSocial.prototype.loadSearchpage = function ( what, type, page ) 
{
	new Ajax.Request('/search/index.php', {
		  method: 'get',
		  parameters: { 
				action: 2,
				s: what,
				type: type,
				p: page
			},
			
		  onSuccess: function( transport )
		  {
			$('results').innerHTML = transport.responseText;  
		  }
	  });
	
	AjaxNavigation.setParams( { s: what, type: type, p: page } );
	AjaxNavigation.callFunction = "vsocial.loadSearchpage('"+what+"', '"+type+"', '"+page+"')";
	AjaxNavigation.updateAddress();
	
	$('results').innerHTML = "<h1>Loading...</h1>";
	  
	setTimeout( 'scrollUp()', 100 );
}

vSocial.prototype.uploadStatus = function( uid )
{
	guid = uid;


	vSocial.doAjax("/upload", { uid: uid, action: 'status'}, vSocial.upload_handler );
}

vSocial.swapImg = function( img, on )
{
	if ( on )
		img.src = img.src.replace( "_off", "_on" );
	else
		img.src = img.src.replace( "_on", "_off" );
}

function scrollUp() {
  var scroller = document.getElementsByTagName('html')[0];
  scroller.scrollTop = Math.floor( scroller.scrollTop / 2 );
  if ( scroller.scrollTop != 0 ) setTimeout( 'scrollUp()', 100 );
} 

// from lightbox.js
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}









vSocial.prototype.toggleBox = function( obj, id )
{
	//new Effect.toggle( $(id), 'slide' );
	new Effect.toggle( $(id), 'appear' );
	
	if ( obj.parentNode.className.indexOf( "up" ) == -1 )
		obj.parentNode.className = "title up";
	else
		obj.parentNode.className = "title";
}

vSocial.prototype.checkProfileForm = function( _form_ )
{
	var onlyNumbersRegex = /\D/g;

	_form_.mobile_number.value = _form_.mobile_number.value.replace( onlyNumbersRegex, "" );

	if ( _form_.mobile_number.value.length > 0 && _form_.mobile_number.value.length < 10 || _form_.mobile_number.value.length > 11 )
	{
		alert("Mobile number must be full 10 digits");
		return false;
	}
	
	if ( _form_.mobile_number.value.length > 0 && _form_.mobile_carrier.value == "" )
	{
		alert("You must specify a Mobile Carrier with your Mobile Number");
		return false;
	}
	
	return true;
}


vSocial.prototype.changeUploadForm = function( type )
{
	if ( type == "video" )
	{
		$("video-form").style.display = "block";
		$("image-form").style.display = "none";
	}
	else
	{
		$("video-form").style.display = "none";
		$("image-form").style.display = "block";
	}
}


guid = 0;
vsocial = new vSocial();
window.onerror=null;
setTimeout( vsocial.checkNewMessages, 4000 );

