function Microsite()
{
	unFocus.History.addEventListener( "historyChange", Microsite.onHistoryChange );
}

Microsite.last_community_vid = 0;
Microsite.last_community_tab = "community_rating"

Microsite.addToHistory = function( hash )
{
	if ( hash )
		unFocus.History.addHistory( hash );
}

Microsite.onHistoryChange = function( hash )
{
	if ( hash )
		VideoLoad( parseInt( hash ) );
}

Microsite.communityTab = function( enabled_tab )
{
	var tabs = [ 'community_rating', 'community_comments', 'community_tags' ];
	
	Microsite.last_community_tab = enabled_tab;
	
	for ( var i = 0; i < tabs.length; i++ )
		$(tabs[i]).style.display = tabs[i] == enabled_tab ? 'block' : 'none';

	Microsite.fixLayout( ['content','left','right'] );
}

Microsite.community_handler = 
{
	onSuccess: function(data)
	{
		$('community').innerHTML = data;
		Microsite.communityTab( Microsite.last_community_tab );
	}
}

Microsite.loadCommunity = function( video_id, tab )
{
	var ajax = new Ajax( "/microsite/render_block.php" );
	ajax.SetData( "block_id", "community" );
	ajax.SetData( "site_id", USER_ID );
	ajax.SetData( "video_id", video_id );
	ajax.SetData( "tab", tab );
	ajax.Register( Microsite.community_handler );
	ajax.SendRequest("GET");
	
	Microsite.last_community_vid = video_id;	
}

Microsite.comment_handler =
{
	onCreate: function(data)
	{
		$('comment_message').innerHTML = '<img align="absmiddle" src="' + Microsite.loading_image.src + '" /> Saving...';
		$('comment').disabled = true;
		$('submit_comment').disabled = true;
	},

	onSuccess: function( data )
	{
		Microsite.loadCommunity( Microsite.last_community_vid, "comments" );
		$('comment').disabled = false;
		$('submit_comment').disabled = false;
		$('comment').value = '';
		$('comment_message').innerHTML = '';
		AjaxDialog.closeDialog();
	}
}

Microsite.addComment = function( video_id, comment )
{
	var ajax = new Ajax( "/index.php" );
	ajax.SetData( "m", comment );
	ajax.SetData( "d", video_id );
	ajax.SetData( "type", "video" );
	ajax.SetData( "action", 105 );
	ajax.SetData( "controller", "video" );
	ajax.Register( Microsite.comment_handler );
	ajax.SendRequest("POST");	
}

Microsite.tag_handler =
{
	onCreate: function(data)
	{
		$('tag_message').innerHTML = '<img align="absmiddle" src="' + Microsite.loading_image.src + '" /> Saving...';
		$('tags').disabled = true;
		$('submit_tag').disabled = true;
	},

	onSuccess: function( data )
	{
		Microsite.loadCommunity( Microsite.last_community_vid, "tags" );
		$('tags').disabled = false;
		$('submit_tag').disabled = false;
		$('tags').value = '';
		$('tag_message').innerHTML = '';
		
		AjaxDialog.closeDialog();
	}
}

Microsite.addTag = function( video_id, tags )
{
	var ajax = new Ajax( "/index.php" );
	ajax.SetData( "m", tags );
	ajax.SetData( "d", video_id );
	ajax.SetData( "type", "video" );
	ajax.SetData( "action", 106 );
	ajax.SetData( "controller", "video" );
	ajax.Register( Microsite.tag_handler );
	ajax.SendRequest("POST");	
}

Microsite.auth_handler =
{
	onCreate: function(data)
	{
		$('login_message').innerHTML = '<img align="absmiddle" src="' + Microsite.loading_image.src + '" /> Authenticating...';
	},
	
	onSuccess: function( data )
	{
		data = parseInt( data );
		
		if ( data == 1 )
		{
			AjaxDialog.closeDialog();
			Microsite.reloadPubBar();
			Microsite.loadCommunity( Microsite.last_community_vid );
		}
		else
		{
			$('login_message').innerHTML = "Invalid login";
			$('password').value = '';
		}
	}
}

Microsite.authenticate = function( username, password )
{
	var ajax = new Ajax( "/index.php" );
	ajax.SetData( "action", 113 );
	ajax.SetData( "User", username );
	ajax.SetData( "Pass", password );
	ajax.Register( Microsite.auth_handler );
	ajax.SendRequest("POST");	
}

Microsite.join_handler =
{
	onCreate: function(data)
	{
		$('join_message').innerHTML = '<img align="absmiddle" src="' + Microsite.loading_image.src + '" /> Joining...';
	},
	
	onSuccess: function( data )
	{
		data = parseInt( data );
		
		if ( data == 0 )
		{
			Microsite.reloadPubBar();
			Microsite.loadCommunity( Microsite.last_community_vid );
		}
		else
		{
			// User wasn't logged in, so show the login box
			AjaxDialog.createDialog( {title: 'Community Login', width: 400, height: 0, classname: 'vsn_dialog'} );
			AjaxDialog.attachContent( 'login_dialog' ); 
			AjaxDialog.showDialog();
		}
	}
}

Microsite.joinCommunity = function()
{
	var ajax = new Ajax( "/microsite/publisher.php" );
	ajax.SetData( "action", 21 );
	ajax.SetData( "site_id", USER_ID );
	ajax.Register( Microsite.join_handler );
	ajax.SendRequest("GET");
}

Microsite.favorite_handler = 
{
	onCreate: function(data)
	{
		$('fav_message').innerHTML = '';
		$('fav_wait').innerHTML = 'Saving...';
		//$('fav_icon').src = Microsite.loading_image.src;
	},
	
	onSuccess: function( data )
	{
		data = parseInt( data );
		
		if ( data == 1 )
		{
			$('fav_message').innerHTML = 'Remove from favorites';
			$('fav_wait').innerHTML = '';
			$('fav_icon').src = Microsite.favorite_image.src;
		}
		else
		{
			$('fav_message').innerHTML = 'Add to favorites';
			$('fav_wait').innerHTML = '';
			$('fav_icon').src = Microsite.no_favorite_image.src;
		}

		$('fav_message').blur();
		
	}	
}

Microsite.toggleFavorite = function( video_id )
{
	var ajax = new Ajax( "/microsite/publisher.php" );
	ajax.SetData( "action", 23 );
	ajax.SetData( "video_id", video_id );
	ajax.Register( Microsite.favorite_handler );
	ajax.SendRequest("GET");
}

Microsite.rating_handler =
{
	onCreate: function()
	{
		$('community_rating').innerHTML = '<img align="absmiddle" src="' + Microsite.loading_image.src + '" /> Updating...'; 		
	},
	
	onSuccess: function( data )
	{
		Microsite.loadCommunity( Microsite.last_community_vid, "rating" );		
	}
}

Microsite.star_click = function( rating )
{
	var ajax = new Ajax( "/index.php" );
	ajax.SetData( "action", 110 );
	ajax.SetData( "s", rating );
	ajax.SetData( "m", Microsite.last_community_vid );
	ajax.SetData( "controller", "video" );
	ajax.Register( Microsite.rating_handler );
	ajax.SendRequest("GET");	
}

Microsite.star_handler = function(id)
{
	a = id.split('_');
	w = a.pop();
	p = a.join('_') + '_';
	
	i = 1;
	while( (e = document.getElementById(p + i)) )
	{
		e.src = ( i <= w ) ? Microsite.star_image.src : Microsite.star_dim_image.src;
		i++;
	}		
}

Microsite.fixLayout = function( elements )
{
	var max_height = 0;
	
	var element_info = {};
	
	// First find the element information
	for ( var i = 0; i < elements.length; i++ )
	{
		var cur_element = $(elements[i]);
		
		if ( !cur_element )
			continue;

		cur_element.style.height = "";
		cur_element.style.minHeight = "";

		var height_info = { 
							height: 		Microsite.getStyle( cur_element, "height" ) || "0px",
							paddingTop:		Microsite.getStyle( cur_element, "paddingTop" ) || "0px",
							paddingBottom:	Microsite.getStyle( cur_element, "paddingBottom" ) || "0px",
							marginTop:		Microsite.getStyle( cur_element, "marginTop" ) || "0px",
							marginBottom:	Microsite.getStyle( cur_element, "marginBottom" ) || "0px",
							borderTop:		Microsite.getStyle( cur_element, "borderTop" ) || "0px",
							borderBottom:	Microsite.getStyle( cur_element, "borderBottom" ) || "0px"
						  }
						  
		element_info[elements[i]] = height_info;
	}
	
	// Now find the highest element
	for ( var i in element_info )
	{
		total_height = 0;
		for ( j in element_info[i] )
			total_height += parseInt( element_info[i][j].replace( "px", "" ) );
		
		element_info[i].totalHeight = total_height;
		
		max_height = total_height > max_height ? total_height : max_height;
	}
	
	// Now set all elements to max_height taking into account padding, etc
	for ( var i in element_info )
		if ( cur_element = $(i) )
		{
			old_height = parseInt( element_info[i].height.replace( "px", "" ) );
			cur_element.style.height = ( old_height + (max_height - element_info[i].totalHeight) ) + "px"; 
			cur_element.style.minHeight = cur_element.style.height;
		}
}

/* getStyle: http://squidfingers.com/code/snippets/?id=getcssprop */
Microsite.getStyle = function(element, prop) 
{
	if (typeof element == 'string') element = document.getElementById(element);
	
	if (element.style[prop])
	{
		// inline style property
		return element.style[prop];
	} 
	else if (element.currentStyle) 
	{
		// external stylesheet for Explorer
		return element.currentStyle[prop];
	}
	else if (document.defaultView && document.defaultView.getComputedStyle) 
	{
		// external stylesheet for Mozilla and Safari 1.3+
		prop = prop.replace(/([A-Z])/g, "-$1");
		prop = prop.toLowerCase();
		
		var style = document.defaultView.getComputedStyle(element,null);
		if ( style )
			return style.getPropertyValue( prop );
		
		return null;
	}
	else 
	{
		return null;
	}
}

Microsite.updateFilterLinks = function()
{
	links = ['new', 'pop', 'dis'];
	
	for( i = 0; i < links.length; i++ )
		if ( (e = $(links[i])) )
			if ( links[i] == CUR_SORT )
			{
				if ( e.className.indexOf( "active_link" ) == -1 )
					e.className = e.className + " active_link";
			}
			else
				e.className = e.className.replace( /[ ]*active_link/g, "");
}

Microsite.pubbar_handler =
{
	onSuccess: function(data)
	{
		$('pp_bar').innerHTML = data;
	}
}

Microsite.reloadPubBar = function()
{
	var ajax = new Ajax( "/microsite/render_block.php" );
	ajax.SetData( "block_id", "pp_bar" );
	ajax.SetData( "site_id", USER_ID );
	ajax.Register( Microsite.pubbar_handler );
	ajax.SendRequest("GET");
}

Microsite.signup_handler = 
{
	onSuccess: function(data)
	{
		if ( data.toUpperCase() == "OK" )
		{
			AjaxDialog.closeDialog();
			Microsite.reloadPubBar();
			Microsite.loadCommunity( Microsite.last_community_vid );
		}
		else
			$('signup_message').innerHTML = data;
	}
}

Microsite.processSignup = function( username, password, password_c, email )
{
	var ajax = new Ajax( "/index.php" );
	ajax.SetData( "action", 100 );
	ajax.SetData( "controller", "propublisher/microsite" );
	ajax.SetData( "user", username );
	ajax.SetData( "pass", password );
	ajax.SetData( "pass_confirm", password_c );
	ajax.SetData( "email", email );

	ajax.Register( Microsite.signup_handler );

	ajax.SendRequest("GET");	
}

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

function process_ajax_commands( str )
{
	retstr = str;
	
	setvar_pattern = /<setvar>([^=]+)=([^<]+)<\/setvar>/g
	
	while ( (res = setvar_pattern.exec( str ) ) )
	{
		key = res[1];
		value = res[2];
		
		eval( key.toUpperCase() + "=" + value );
		
		retstr = retstr.replace( res[0], "" );
	}
		
	return retstr;
}

var video_handler =
{
	onCreate: function() 
	{
	},
	
	onSuccess: function(data) 
	{
		$("video_info").innerHTML = data;
	}
}

function VideoLoad( video_id ) 
{
	Microsite.addToHistory( video_id );
	
	var player = $('embed_object');
	
	url = player.getAttribute( "src" );
	
	if ( url == "" )
		url = 'http://static.vsocial.com/flash/ups.swf?d=' + video_id + '&a=1';
	else
		url = url.replace( /d=[0-9]+&/g, "d="+video_id+"&" );
	
	$('video').innerHTML = '<embed id="embed_object" height="' + player.getAttribute( "height" ) + '" width="' + player.getAttribute( "width" ) + '" src="'+url+'"></embed>';

	Microsite.loadCommunity( video_id );
	
	var ajax = new Ajax( "/microsite/render_block.php" );
	ajax.SetData( "block_id", "videodata" );
	ajax.SetData( "site_id", USER_ID );
	ajax.SetData( "video_id", video_id ); 
	ajax.Register( video_handler );
	ajax.SendRequest("GET");
}

var browse_handler =
{
	onCreate: function() 
	{
		$("thumbnails").innerHTML = "<h2 class='linktext'>Loading...</h2>";
	},
	
	onSuccess: function(data) 
	{
		$("thumbnails").innerHTML = process_ajax_commands( data );
		RefreshArrows();
	}
}

var upload_handler =
{
	onCreate: function()
	{
	},
	
	onSuccess: function(data)
	{
		data = parseInt( data );
		
		$('upload_status').innerHTML = '<strong>Uploading:</strong> ' + data + '%';
		
		if ( data == 0 && LAST_DATA > 0 )
		{
			LAST_DATA = 0;
			upload_done();
		}
		else
		{
			LAST_DATA = data;
			setTimeout( 'track_upload(\'' + GUID + '\');', 1000 );
		}
		/*
		var bar = $('upload_inner');
		
		
		if ( bar && data > 0 && data > bar.style.width.replace('%', '') )
		bar.style.width = data + "%";
		
		if ( data == 0 && bar.style.width.replace('%','') > 0 )
			upload_done();
		else
			setTimeout( 'track_upload(\'' + GUID + '\');', 1000 );
		*/
	}
}

function track_upload( id )
{
	GUID = id;
	
	var ajax = new Ajax( "/index.php" );
	ajax.SetData( "action", 111 );
	ajax.SetData( "uid", id );
	ajax.SetData( "controller", "vSocial" );
	ajax.Register( upload_handler );
	ajax.SendRequest( "GET" );
}

function upload_start(id)
{
	if ( (e = $('upload_status') ) )
	{
		e.innerHTML = '<strong>Uploading:</strong> Connecting...';
		
		e.style.display = 'block';
		$('launch_upload').style.display = 'none';
		track_upload(id);
	}
}

function upload_done()
{
	$('upload_status').innerHTML = '<em>Upload complete!</em>';
	$('launch_upload').style.display = '';
}

function toggle_fade( show )
{
	player = $('embed_object');
	
	if ( player )
		player.style.height = (show ? 0 : player.getAttribute("height")) + "px";
	
	$('fade_layer').style.display = show ? 'block' : 'none';
}

function RefreshArrows()
{
	LAST_PAGE = Math.ceil( TOTAL_FOUND / ( ROWS * COLUMNS ) ) - 1;
	
	first = $('nav_first');
	prev  = $('nav_prev');
	next  = $('nav_next');
	last  = $('nav_last');
	
	
	if ( CUR_PAGE == 0 )
	{
		first.src =  'http://static.vsocial.com/images/arrow_first_dim.gif';
		first.setAttribute( "active", 'false' );
		first.className = '';

		prev.src =  'http://static.vsocial.com/images/arrow_back_dim.gif';
		prev.setAttribute( "active", "false" );
		prev.className = '';
	}
	else
	{
		first.src =  'http://static.vsocial.com/images/arrow_first.gif';
		first.setAttribute( "active", "true" );
		first.className = 'active';

		prev.src =  'http://static.vsocial.com/images/arrow_back.gif';
		prev.setAttribute( "active", "true" );
		prev.className = 'active';
	}
		
	if ( CUR_PAGE == LAST_PAGE )
	{
		last.src =  'http://static.vsocial.com/images/arrow_last_dim.gif';
		last.setAttribute( "active", "false" );
		last.className = '';

		next.src =  'http://static.vsocial.com/images/arrow_forward_dim.gif';
		next.setAttribute( "active", "false" );
		next.className = '';
	}
	else
	{
		next.src =  'http://static.vsocial.com/images/arrow_forward.gif';
		next.setAttribute( "active", "true" );
		next.className = 'active';

		last.src =  'http://static.vsocial.com/images/arrow_last.gif';
		last.setAttribute( "active", "true" );
		last.className = 'active';
	}
	
	// Update page counters if they are there
	if ( (e = $('cur_page')) )
		e.innerHTML = CUR_PAGE + 1;
	
	if ( (e = $('last_page')) && e.innerHTML != (LAST_PAGE + 1) )
		e.innerHTML = LAST_PAGE + 1;
}

function ReloadVids()
{
	Microsite.updateFilterLinks();	

	var search = $('search');
	
	var ajax = new Ajax( "/microsite/render_block.php" );	
	ajax.SetData( "block_id", "vidblox" );
	
	
	if ( search && search.value )
	{
		ajax.SetData( "search_term", search.value ? search.value : '' );
		ajax.SetData( "type", "search" );
	}
	else
		ajax.SetData( "type", "user" );
	
	ajax.SetData( "section", CUR_SECTION );
	ajax.SetData( "sort", CUR_SORT );
	ajax.SetData( "page", CUR_PAGE );
	ajax.SetData( "rows", ROWS );
	ajax.SetData( "site_id", USER_ID );
	ajax.SetData( "columns", COLUMNS );	
	ajax.Register( browse_handler );
	ajax.SendRequest("GET");
}

var USER_ID = 0;
var CUR_PAGE = 0;
var LAST_PAGE = 0;
var TOTAL_FOUND = 0;
var CUR_SECTION = 'all';
var CUR_SORT = 'new';
var ROWS = 0;
var COLUMNS = 0;

var GUID = 0;
var LAST_DATA = 0;

