var film; 
var index;
var q;  
var sid;
var tid; 
var count;
var total;
var over;
var HDfilm;
var HDtrailer;
var SDtrailer;
$(document).ready(function(){
    HDfilm = document.getElementsByTagName('video')[2];
	HDtrailer = document.getElementsByTagName('video')[1];
	SDtrailer = document.getElementsByTagName('video')[0];
   	lastBlock = $("1");
	lastDisplay = 1;
    maxWidth = 210; 
    minWidth = 21;	
    $("li").hover(function(){
		maxWidth = $(this).find('p').attr('rel');
	}); 
	$('.choose img').hover(function(){
		$(this).css("opacity", ".65");
	}, function(){
		$(this).css("opacity", 1);
	});   
	film = $('body').attr('class');
	if ($('div.body').hasClass('francis')){
		slideNav();
	} else {
		staticNav();
		openFilm('li#'+film);
	}
	thumb_scrolley();
	addJavascript('http://s3.amazonaws.com/new.cetrk.com/pages/scripts/0010/5953.js', 'body');  
	detectBrowserVersion();
	
});

function detectBrowserVersion(){
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	var version = 0;
	// Is this a version of IE?
	if($.browser.msie){
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));	
		version = userAgent;
	}
	// Is this a version of Chrome?
	if($.browser.chrome){
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));	
		version = userAgent;
		// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
		$.browser.safari = false;
	}

	// Is this a version of Safari?
	if($.browser.safari){
		userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);	
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;	
	}

	// Is this a version of Mozilla?
	if($.browser.mozilla){
		//Is it Firefox?
		if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
			userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
			userAgent = userAgent.substring(0,userAgent.indexOf('.'));
			version = userAgent;
		} else{
		}
	}

	// Is this a version of Opera?
	if($.browser.opera){
		userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}
	return version;
}

function hiliteYoutube(){
	$('div.social').append('<p class="hilite">Check Out Basic Series Extras on Youtube.</p>');
	$('a.youtube').addClass('hilite');
	setTimeout(closehilite, 10000);
	function closehilite(){
		$('p.hilite').hide(); 
		$('.hilite').removeClass('hilite');
		
	}
	
}
 
function staticNav(){
	$('div#filmSelect ul').css({marginLeft: '0px'});
	$('div#filmSelect li').css({marginLeft: '4px'});
}

function slideNav(){
	$('#filmSelect p.loading').hide();
	$('div#filmSelect ul').animate({
		marginLeft: '0px'
	},2500, 'easeInOutExpo');
	
	$('div#filmSelect li').animate({
		marginLeft: '4px'
	}, 2000);
}

function thumb_scrolley(){
	count = 0;
	total = $('#'+film+' div.stills_thumb').children().size();
	over = total-6;
	$('#'+film+' div.stills_thumb').css({width: total*52});
	//go back/prev 
	$('#'+film+ ' div.still_prev').bind('click', stillPrev);
	//go forward/next 
	$('#'+film+' div.still_next').bind('click', stillNext);
}

function stillNext(){
	if(count < total && count < over){
		//alert('count of '+count+' must be less than '+total+' which is the total');
		$('#'+film+' .stills_thumb').animate({
			left: '-=52px'
		}, "slow", 'easeOutExpo');
		count = count+1;
	}
}              

function stillPrev(){
	if(count > 0){		
		$('#'+film+' .stills_thumb').animate({
			left: '+=52px'
		}, 'slow', 'easeOutExpo');
		count = count-1;
	}
}

$('div.stills img').click(function(){
	$(this).hide();
	index = $('div.stills img').index(this);
	sid= $('div.stills img').eq(index).attr('id');
	tid= sid.replace('thumbid', 'stillid');
	document.getElementById(tid).scrollIntoView(true);
	q = $(this).parent().children().size();
	if(index<(q-1)){ 
		//alert('index is '+index+', which is less than '+(q+1)+'"quantity" minus 1');
		//$('img.still').removeClass('selected');
		//$('img.still[ref=index+1]').addClass('selected');
		$(this).next().show();
	} else{  
		//alert('back to beginning');
		//$('img.still').removeClass('selected');
		//$('img.still[ref="30"]').addClass('selected');
		$(this).parent().children().eq(0).show();
	}
});

//function that closes the FILM
function closeFilm(_this){
	_this.animate({width: minWidth+"px"}, { queue:false, duration: 400, ease: 'easeInOutExpo'});
	_this.removeClass('selected');
	$('div._'+lastDisplay).hide();
	$('div.francisInfo').fadeIn('slow');
	$('div.body').addClass('francis');
}

//function that opens the FILM
function openFilm(_this){
	maxWidth = $(_this).find('p').attr('rel');
    //$('div._'+lastDisplay).hide();
	//lastDisplay = $(_this).find('img').attr('ref');
	//$('div._'+lastDisplay).fadeIn();
	//film = $('div._'+lastDisplay).attr('id');
	$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400, ease: 'easeInOutExpo'});
	$(_this).animate({width: maxWidth+"px"}, { queue:false, duration:400, ease: 'easeInOutExpo'});
	lastBlock = _this;
	$(_this).addClass('selected');
	var stillsIMG = $('div.stills img');
	
	if ($('div.trailer').hasClass('hidden')){
		$('div.trailer').hide();
		stillsIMG.hide();
		$('div.stills img:first-child').show();
	} else { 
		stillsIMG.hide();
		$('div.trailer').show();
        $('div.trailersd').hide();
        $('div.trailerfilm').hide();
        $('div.trailerhd').show();
	}		
	
    //select a STILL
	$('div.choose img.still').click(function(){
		imgSelect = $(this).attr('ref');
		$('div.stills img').hide();
		$('div.trailer div').hide();
		$('div.stills img._'+imgSelect).show();
	    HDfilm.pause();
		HDtrailer.pause();
		SDtrailer.pause();
	});
	
	//select a TRAILER 
	
	$('div.choose img.showtrailer').click(function(){
		trailerSelect = $(this).attr('ref');
	   	$('div.stills img').hide();
		$('div.trailer div').hide();
		$('div.trailer'+trailerSelect).show();
		if(trailerSelect == 'film'){
	        SDtrailer.pause();
	        HDtrailer.pause();
	        HDfilm.play();
	    } else if(trailerSelect == 'hd'){
			SDtrailer.pause();
			HDfilm.pause();
			HDtrailer.play();
		} else if(trailerSelect == 'sd'){
			HDtrailer.pause();
			HDfilm.pause();
			SDtrailer.play();
		}
	});
	$('.filmContents').show();
	
}

/*Load External JS*/
function addJavascript(jsname,pos) {
	var th = document.getElementsByTagName(pos)[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',jsname);
	th.appendChild(s);
}
