function fade_vid_to(to)
{
	if( to > vid_itemcount )
	{
		to = 1;
	}
	
	if( current_vid == to )
	{
		// same item, do nothing
	}
	else
	{
		$('.vid_item').each(function(i){
			if( $(this).attr('id') == "VI-"+current_vid )
			{
				$(this).css('z-index', 5001);
			}
			else if( $(this).attr('id') == "VI-"+to )
			{
				$(this).css('z-index', 5002);
			}
			else
			{
				$(this).css('z-index', (5000-i)).hide();
			}
		});
		
		old_vid = current_vid;
		$('#vid_nav').css('background-position',(to-1)*dot_nav_width+'px 0px');
		$('#VI-'+to).fadeIn(1000, function(){
				$('#VI-'+old_vid).hide();
		});
		current_vid = to;
		
		if( vid_auto_play == 1 )
		{
			auto_vid = setTimeout(function(){ fade_vid_to(current_vid+1) },5000);
		}
	}
}


function show_next_sponsor()
{
	next_sponsor = current_sponsor+1;
	if( next_sponsor > sponsor_count )
	{
		next_sponsor = 1;
	}
	
	$('#sponsor_cycle').children('.sponsor_img').eq((current_sponsor-1))
		.animate({ top: '220px', opacity: 'hide' }, 1000, 'easeInCirc', function(){
			$('#sponsor_cycle').children('.sponsor_img').eq((next_sponsor-1))
				.animate({opacity: 'show', top: '0px' }, 1000, 'easeOutCirc', function(){
					$('#sponsor_cycle').children('.sponsor_img').eq((current_sponsor-1)).css('top','-220px');
					current_sponsor = next_sponsor;
				});
		});
	
	setTimeout("show_next_sponsor()", 4000);
}

$(document).ready(function(){
	
	// LOAD FOOTER
	if( $('#footer_target').length > 0 )
	{
		$('#footer_target').load("/index #footer");
	}
	
	// REMOVE EMPTY PARAGRAPHS FROM TEASER TEXT
	$('.teaser_text p').each(function(){
		var textVal = $(this).text();
		if (textVal.length < 10) { // Empty string outputs length of 9 due to additional elements inside p tags
			$(this).remove();
		};
		
	});
	
	// FONT REPLACEMENT
	Cufon.replace('h2', { fontFamily: 'GothamLight' });
	Cufon.replace('h1, #menu_main ul li a, #hero_text, .teaser_headline, .teaser_text p, .vid_item_text p, .teaser_item h2, #lb_gallery_info, .twocol h2, .twocol p, .teaser_bar h3', { fontFamily: 'GothamBold', hover : true });
	
	
	
	
	// MAIN NAV
	var submenu_item_count = 0;
	$('#menu_sub').children('.menu_sub_item').children().each(function(){
		submenu_item_count++;
	});
	
	if( submenu_item_count > 0 )
	{
		var this_path = window.location.pathname;
		this_path = this_path.slice(1);
		var reg_url1 = /^index/i;
		var reg_url2 = /^home/i;
		var submenu_H = $('#menu_sub').height() + 20;
		var submenu_show = 0;

		if( reg_url1.test(this_path) || reg_url2.test(this_path) || this_path == '' )
		{
			$('#menu_sub').css('height', '0px');
		}
		else
		{
			$('#menu_sub').css('height', submenu_H+'px');
			submenu_show = 1;
		}


		$('#menu_main ul li a').click(function(){

			if( submenu_show == 0 )
			{
				$('#menu_sub').animate({'height' : submenu_H+'px'}, 600, 'easeOutCirc');
				submenu_show = 1;
			}
			else
			{
				$('#menu_sub').animate({'height' : '0px'}, 600, 'easeInCirc');
				submenu_show = 0;
			}	

			return false;	
		});
	}
	else
	{
		$('#menu_sub').css('height', '0px');
	}
	
	
	
	// TEMPORARILY CLOSED
	//$('#menu_sub').css('height', '0px');
	
	
	
	
	// VID PLAYER on INDEX
	dot_nav_width = 15;
	vid_itemcount = $('#vid_viewport').children('.vid_item').length;
	$('#vid_viewport').children('.vid_item').eq(0).show();
	$('#vid_viewport').children('.vid_item').each(function(i){
		$(this).attr('id', 'VI-'+(i+1)).css('z-index',(5000-i));
	});
	
	old_vid = 0;
	current_vid  = 1;
	
	vid_auto_play = 1;
	if( vid_auto_play == 1 )
	{
		auto_vid = setTimeout(function(){ fade_vid_to(current_vid+1) },5000);
	}
	
	
	// Set dot-nav to corresponding width and add event
	$('#vid_nav').css('width', (vid_itemcount*dot_nav_width)+'px').click(function(e){
		vid_auto_play = 0;
		clearTimeout(auto_vid);
		var this_offset = $(this).offset();
		var dot_number = (e.pageX - this_offset.left)/dot_nav_width;
		dot_number = Math.ceil(dot_number);
		$(this).css('background-position',(dot_number-1)*dot_nav_width+'px 0px');
		
		fade_vid_to(dot_number);
	});
	
	
	// SPONSOR CYCLE
	current_sponsor = 1;
	next_sponsor = 1;
	
	if( $('#sponsor_cycle_target').length > 0 )
	{
		$('#sponsor_cycle_target').load("/index #sponsor_cycle", function(){
			sponsor_count = $('#sponsor_cycle').children('.sponsor_img').length;
			$('#sponsor_cycle').children('.sponsor_img').hide().eq(0).show().css({ top: '0px' });

			if( sponsor_count > 1 )
			{
				setTimeout("show_next_sponsor()", 4000);
			}
			
			$('#sponsor_cycle_target').prepend('<h2>Business3000 Partners</h2>');
			
		});
	}
	else
	{
		sponsor_count = $('#sponsor_cycle').children('.sponsor_img').length;
		$('#sponsor_cycle').children('.sponsor_img').hide().eq(0).show().css({ top: '0px' });

		if( sponsor_count > 1 )
		{
			setTimeout("show_next_sponsor()", 4000);
		}
	}
	
	
	$('.vid_item_text')
		.css('cursor', 'pointer')
		.click(function(){
			var link_to = $(this).children('.vtext_link').children('a').attr('href');
			window.location = link_to;
		});
	
	//Social sharing links
	$('.socmed').live('click', function(){
		var socMedId = $(this).attr('id'); 
		window.open('http://b3000.public.blockshome.com/shares/new?platform='+socMedId+'&page_address='+location.href,'Social Media','width=800,height=400,location=no');
		return false;
	});
	
});
