//cufon
Cufon.replace('h1, .home h2, .replaced', { fontFamily: 'zapotec', hover: true });

//add this
var addthis_config = {
	ui_click: true,
	ui_cobrand: "Motorweek",
	ui_header_color: "#fff",
    ui_header_background: "#EE3B34",
	services_compact: 'facebook,twitter,stumbleupon,digg,delicious,linkedin,google,print,email,favorites'
}
var addthis_share = {
	templates: { 
		twitter: 'Take a look at {{title}} (from @motorweek) at {{url}}' 
	}
}

$(document).ready( function() {
	//$('p').css('border', '8px solid red');
	var keywords = '';
	if ( undefined !== window.searchResults ) 
	{
		keywords = searchResults;
	}
	setupSearch( 'main_keywords', 'main_search_form', keywords);

	selectCurrentMenu();

	homeVideoPlayer();

	setupIconRollovers();
});

function selectCurrentMenu()
{
	$( '#menu li ul li a').each( function( index, element )
	{
		if ( location.href.indexOf( $( element ).attr( 'href' ) ) != -1 )
		{
			$( element ).parent( 'li' ).addClass( 'selected ');
		}
	});
}

function setupSearch( textbox_id, form_id, keywords )
{	
	var search_text = '';
	if ( $('#' + textbox_id) )
	{
		//setup search box text
		if ( keywords != '' ) 
		{
			$('#' + textbox_id).val( keywords );
		}
		else
		{
			search_text = $('#' + textbox_id).val();
		}
				
		search_text = htmlspecialchars_decode( search_text );
						
		$('#' + textbox_id).blur( 
						function(e) 
						{
							if ( jQuery.trim( $(this).val() ) == '')
							{
								$(this).val( search_text );
							}
						}
					).focus( 
						function(e) 
						{
							var textBoxValue = $( this ).val();
							if ( jQuery.trim( textBoxValue ) == search_text)
							{
								$(this).val('');
							}
						}
					)
		
		//setup submit event
		$('#' + form_id).submit(
			function(e)
			{
				var temp = jQuery.trim( $('#' + textbox_id).val() );
				if ( (temp == search_text ) || (temp == '') )
				{
					alert('You must enter a search term');
					return false;
				}
			}
		);
	}
}

function htmlspecialchars_decode(text)
{
	var stub_object = $('<span></span>').html( text );
	var ret_val = stub_object.text();
	stub_object.remove();
	delete stub_object;
	return ret_val;
}

var currentVid = '';
var currentVidIndex = 0;
var videoPlaying = false;
var vidTimer = '';

var videoListLinks;
function homeVideoPlayer()
{
	videoListLinks = $('a.youtube_video');
	
	if ( $('#home_video_player').length > 0 )
	{
		$('#home_video_player').click(
			function(e)
			{
				videoPlaying = true;
			}	
		);
		//set the current video to the first index
		currentVid = $('a.youtube_video').eq(0).attr('id').replace('video_', '');

		//TODO hide all images and have the video show up instead
		//TODO make the images themselves clickable
		videoListLinks.click(
			function(e)
			{
				videoPlaying = true;
				
				//prevent default
				e.preventDefault();

				$('#video_embed').html('').css('z-index', 0);

				$('#video_player_inner .image').removeClass( 'selected' ).eq( videoListLinks.index( $(this) ) ).addClass( 'selected' );
				
				//get the video's id
				var vidId = $(this).attr('id').replace('video_', '');
				if ( currentVid != vidId )
				{
					currentVid = vidId;					

					//set the selected class
					$('#video_list li').removeClass( 'selected' );
					$(this).parent('li').addClass( 'selected' );
				}
			}
		);


		$('#video_player_inner .image').click(
			function(e)
			{
				videoPlaying = true;

				//prevent default
				e.preventDefault();

				//get the video's id
				var vidId = $(this).attr('data-video_id');

				console.log( vidId );

				//if ( currentVid != vidId )
				//{
					currentVid = vidId;

					//create the video source html
					var source = '<object width="640" height="390" type="application/x-shockwave-flash" data="http://www.youtube.com/v/' + vidId + '?fs=1&amp;hl=en_US&amp;rel=0&amp;hd=0&amp;autoplay=1"><param name="movie" value="http://www.youtube.com/v/' + vidId + '?fs=1&amp;hl=en_US&amp;rel=0&amp;hd=0&amp;autoplay=1" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="allowscriptaccess" value="always" /><embed src="http://www.youtube.com/v/' + vidId + '?fs=1&amp;hl=en_US&amp;rel=0&amp;hd=0&amp;autoplay=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="640" height="390" /></object>'

					//swap the player html
					$('#video_embed').css('z-index', 300).html( source );
				//}
			}
		);
		
		//set the timeout
		vidTimer = setTimeout( "showNextVideo()", 7000 );




		//center the info in the buttons
		$('#video_list li a').each( function( ind, el ) {
			$( el ).css( {'height': 'auto', 'paddingTop': 0 } );
			var height = $( el ).outerHeight();
			var padding = Math.round( ( 74 - height ) * .5 );
			$(el).css( {'paddingTop': padding, 'height': 74 - padding } );
		});  
	}
}

function showNextVideo()
{
	if ( videoPlaying == false ) //a video has not been clicked
	{
		if ( currentVidIndex < 4 )
		{
			currentVidIndex++;
		}
		else
		{
			currentVidIndex = 0;
		}

		/*
		var vidId = $('a.youtube_video').eq(currentVidIndex).attr('id').replace('video_', '');
		
		currentVid = vidId;					


		//create the video source html
		var source = '<object width="640" height="390" type="application/x-shockwave-flash" data="http://www.youtube.com/v/' + vidId + '?fs=1&amp;hl=en_US&amp;rel=0&amp;hd=0&amp;autoplay=0"><param name="movie" value="http://www.youtube.com/v/' + vidId + '?fs=1&amp;hl=en_US&amp;rel=0&amp;hd=0&amp;autoplay=0" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="allowscriptaccess" value="always" /><embed src="http://www.youtube.com/v/' + vidId + '?fs=1&amp;hl=en_US&amp;rel=0&amp;hd=0&amp;autoplay=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="640" height="390" /></object>'
		
		//swap the player html
		$('#video_player_inner').html( source );
		*/

		//hide all of the images except the current one
		$('#video_player_inner .image').removeClass( 'selected' ).eq(currentVidIndex).addClass( 'selected' );

		
		//remove selected classes
		$('#video_list li').removeClass( 'selected' );
		
		//set the selected class
		$('a.youtube_video').eq(currentVidIndex).parent('li').addClass( 'selected' );
		
		//set the timeout
		vidTimer = setTimeout( "showNextVideo()", 7000 );
	}
	else
	{
		clearTimeout( vidTimer );
	}
}

function setupIconRollovers()
{
	$('#social_icons_top a span').css({display: 'block', opacity: 0});
	$('#social_icons_top a span').mouseenter(
			function()
			{
					$( this ).first( 'span' ).fadeTo( 100, 1 );
			}
	);
	$('#social_icons_top a span').mouseleave(
			function()
			{
					$( this ).first( 'span' ).fadeTo( 400, 0);
			}
	);
}
