// Global vars
var cycle_current = 1;
var cycle_total = 0;
var hash = "MT";
var goTo = "-1";
var quickJump = false;
var pageLoaded = false;
var pageTitle = "";
var newTitle = "";
var whichPage, whichLi;

var GB_ANIMATION = true;

$(document).ready(function(){

	// Greybox
	$("a.greybox").click(function(){
		var t = this.title || this.innerHTML || this.href;
		GB_show(t,this.href,430,530);
		return false;
	});
	
	// Google Search
	/*$(".gsc-input:first input").live("focus",function(e){
		$("#search").css("background-position","-100px -100px");
	});*/

	// Carousel
	cycle_current = 1;
	cycle_total = $(".carousel li").size();

	$(".carousel").css("width", (cycle_total * 770) + "px");
	
	createDotcom(cycle_total);
	
	$("#carousel_left_arrow").live("click",function(e){

		moveTheCarousel("left");
		return false;
	});

	$("#carousel_right_arrow").live("click",function(e){

		moveTheCarousel("right");
		return false;
	});

	$(".dotcom").live("click",function(e){

		moveTheCarousel($(this).attr("rel"));
		return false;
	});

	/////////////////////////////////////////////////////////////
	// Sub nav hover
	$("#nav a").hover(function(){

		if ( $(this).attr("rel") != "0" ){

			$("#"+$(this).attr("rel")).show();
			
			whichPage = $(this).attr("alt");

			if ( $("body").attr("id") != whichPage ){

				$("#"+$(this).attr("rel")).css("z-index","600");
			}
		}
	},function(){
		$(".sub_nav").hide();
	});

	// Sub nav stayin alive
	$(".sub_nav").hover(function(){
		
		$(this).show();

		whichPage = $(this).attr("alt");

		if ( $("body").attr("id") != whichPage ){

			$("#nav a[alt="+whichPage+"]").addClass( $(this).attr("rel") );
		}

	},function(){
		
		$("#nav a[alt="+whichPage+"]").removeClass( $(this).attr("rel") );
		$(this).hide();
	});

	// Sub nav click
	$(".subnav_link").live('click',function(){
		
		whichPage = $(this).parent().parent().attr("rel");

		if ( $("body").attr("id") == whichPage ){

			whichLi = $(this).attr("rel");	
			$("#"+whichLi).click();
		}
	});

	/////////////////////////////////////////////////////////////
	// Twitter
	$("#twitter a").live("click",function(e){
		window.open(this.href);
		return false;
	});

	// SMF Twitter
	var newsFeed, newsFeedHTML = "";

	$.getJSON('http://smfsvc.com/websvc/smf/portfolio/get_smf_twitter_rss.php?callback=?',null,function(newsFeed){

		for ( i = 0; i < 11; i++){
			newsFeedHTML += '<li id="ssc_' + i + '" class="">' + newsFeed[i].description + '<br /><br /><a href="' + newsFeed[i].link + '" class="date" target="_blank">' + newsFeed[i].date + '</a></li>';
		}

		newsFeedHTML += '<li id="ssc_twitter" class="">For more news, follow<br />@sanbornmedia on Twitter:<br /><br /><a href="http://twitter.com/sanbornmedia" target="_blank">twitter.com/sanbornmedia</a></li>';

		$("#twitter_update_list").customFadeOut(400, function(){
			$("#twitter_update_list").html(newsFeedHTML).delay(400).customFadeIn(1000, function(){
				
				$("#twitter_update_list").sscarouselvert(1);

			});
		});
	});
	
	////////////////////////////////////////////////////////
	//Switch videos into flash player in about section
	var v1 = "<script type=\"text/javascript\">loadVideoPlayer(\'Entertainment_Tonight\',false);</script><div id=\"Entertainment_Tonight\"><img src=\"images/clear.gif\" height=\"320\" width=\"470\" /></div>";
	var v2 = "<script type=\"text/javascript\">loadVideoPlayer(\'smf_vf_hollywood_ABCnews\',false);</script><div id=\"smf_vf_hollywood_ABCnews\"><img src=\"images/clear.gif\" height=\"320\" width=\"470\" /></div>";
	
	$('a#ET').click(function(){
		//alert('hey mom');
		//$('div.vplayer').html("id");
		$('div.flashplayer').html("").html("<div class=\"vplayer\"></div>");
		$('div.vplayer').attr("id", "Entertainment_Tonight");
		loadVideoPlayer('Entertainment_Tonight',false);

	});
	$('a#VF').click(function(){
		$('div.flashplayer').html("").html("<div class=\"vplayer\"></div>");
		$('div.vplayer').attr("id", "smf_vf_hollywood_ABCnews");
		loadVideoPlayer('smf_vf_hollywood_ABCnews',false);

	});
	
}); // End (document).ready

/////////////////////////////////////////////////////////////////

function updateVideoID(value){

	value++;
	videoID_current = $('.carousel li:nth-child('+value+')').children('.flashplayer').children('object').attr('id');
}

/////////////////////////////////////////////////////////////////

function setLocationHash(value){
	window.location.hash = "0"+value;
	return false;
}

/////////////////////////////////////////////////////////////////

function moveTheCarousel(thisWay){

		updateVideoID( $(".current_li").attr("alt") );

		switchPage();	// pause video

		$(".current_li").removeClass("current_li");

		if ( thisWay == "left" ){
			cycle_current--;
			if ( cycle_current == 0 ) { cycle_current = cycle_total; }
			$(".carousel").animate({ left: "-"+(770 * (cycle_current-1) )+"px"}, 800);
		} else if ( thisWay == "right" ){
			cycle_current++;
			if ( cycle_current > cycle_total ) { cycle_current = 1; }
			$(".carousel").animate({ left: "-"+(770 * (cycle_current-1) )+"px"}, 800);
		} else {

			cycle_current = thisWay;

			if ( quickJump ){
				$(".carousel").animate({ left: "-"+(770 * cycle_current )+"px"}, 0);
				quickJump = false;
			} else {	
				$(".carousel").animate({ left: "-"+(770 * cycle_current )+"px"}, 800);
			}
		}
		
		$("#wawa a").each(function(intIndex){
	
			if ( thisWay == "left" || thisWay == "right" ){

					if ( $(this).attr("rel") == (cycle_current - 1) ){
						$(this).addClass("current_li");
						setLocationHash( ( cycle_current - 1) );
						return false;
					}
			} else {

					if ( $(this).attr("rel") == (cycle_current) ){
						$(this).addClass("current_li");
						cycle_current++;
						setLocationHash( ( cycle_current - 1) );
						return false;
					}
			}
		});
}

/////////////////////////////////////////////////////////////////

function createDotcom(liTotal){

	if ( liTotal == 0 || liTotal == 1 ){
		
		$(".carousel").css("border-bottom","1px solid #fff");
		$("#carousel_left_arrow").css("display","none");
		$("#wawa").css("display","none");
		$("#carousel_right_arrow").css("display","none");

	} else {
		
		$("#carousel_left_arrow").fadeIn(10);
		$("#wawa").customFadeIn(10,function(){
		
			// Deep-linking
			if (window.location.hash != null){
				hash = window.location.hash;
			}
				
			if( hash.length >= 2  ) {
				quickJump = true;
				goTo = hash.substring(2);
				$("#"+goTo).click();
			}	
		
		});
		$("#carousel_right_arrow").fadeIn(10);
	}
}

/////////////////////////////////////////////////////////////////

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

/////////////////////////////////////////////////////////////////

function switchPage(){

		try {
			pauseMovie("pause");
		} catch (err) {
			1==1;
		}
}

function pauseMovie(choice){

	try {
		
		thisMovie(videoID_current).pauseMovie(choice);

	} catch(err) {
		//console.log("stop: " + err);
	}
}

function thisMovie(movieName) {

	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

/////////////////////////////////////////////////////////////////

var baseurl = 'http://smfportfolio.s3.amazonaws.com/video/';

function loadVideoPlayer(videoID, start) {
	
	var videoPath = baseurl + videoID + '.flv';

	var flashvars = {
		videoIn: videoPath,
		autostart:start
	};

	var params = {
		wmode: "transparent",
		allowFullScreen: "true"
	};

	var attributes = {
		id: videoID,
		name: videoID,
		bgcolor: '#000000',
		quality: 'high',
		allowScriptAccess: 'always'
	};
	
	swfobject.embedSWF("flash/videoplayer.swf", videoID, "470", "320", "9.0.0", "", flashvars, params, attributes);
}


