var blank = new Image();
blank.src = 'images/blank.gif';

$(document).ready(function(){
	$('.goSection').click(function(){				 
		var href = $(this).attr('href');
		openSection(href);
		return false;
	});	

/*get quotes list
	var rotateItems = $('#quotes li');
	var rotateStarted = 0;*/
	
//identify IE, to filter for fixPng and animations
   var IE6 = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
   //treat IE7 and 8 the same
   var IE7 = (/MSIE (7|8)/.test(navigator.userAgent) && navigator.platform == "Win32");

//fixPng
	if (IE6){
		 // get all pngs on page
		 $('img[src$=.png]').each(function() {
		   if (!this.complete) {
			 this.onload = function() { fixPng(this) };
		   } else {
			 fixPng(this);
		   }
		 });
		 
		 flashcontentembed('videoplayer');
		 $('#bg_extended_right').addClass('ie');			
	}

//intro animations
	if (!IE6){
		
		setCSS_onload();
		
		if (!IE7) {
			$('#castshot').fadeIn(1200, function(){
				navAnimate();						   
			});
		} else {
			navAnimate();	
		}
	}
	
//imagemaps tracking and links
	/* $('.imagemaps area').click(function(){	
		var classname = $(this).attr('class');
		 $('a.'+classname).click();
		return false;
	}); */
	
$('#registerLink').click(function(){
	
	if(IE6){
		window.open($(this).attr('href'),'_blank','width=475,height=447');	
	} else if(!window.open($(this).attr('href'),'_blank','width=475,height=447'))
	alert('Please disable your popup blocker');

	return false;
		});
	
		
//open sections via deeplink
	if(pgname!=""){
		openSection(pgname);
		return false;
	}	

/** FUNCTIONS **/

	function openSection(href){
	
		if (IE6){
			$('.togglelayer').css('margin-left','-9999px'); //close current section
			$('.' + href + '').css('margin-left','128px');
		} else if (IE7){
			$('.togglelayer').css('display','none'); //close current section
			$('.' + href + '').css('display','block'); 
		} else {
			$('.togglelayer').css('display','none'); //close current section
			$('.' + href + '').fadeIn(400); 
		}
		
		if (href == 'dvd'){
			sCode.trackPageView('dvd.html', '');
			//flashcontentembed();
			rotateQuotes();
			//$('#castshotimg').css('display','none');
			//$('#castshotimg_expanded').css('display','block');
		} else if (href == 'about'){
			sCode.trackPageView('about.html', '');
			//flashcontentembed();
			loadScrollPane();
			//$('#castshotimg').css('display','block');
			//$('#castshotimg_expanded').css('display','none');
		} else if (href == 'home'){
			sCode.trackPageView('home.html', '');
			flashcontentembed('videoplayer');
			//$('#castshotimg').css('display','block');
			//$('#castshotimg_expanded').css('display','none');
		} 		
	};


	var promoimgs = $('#promos li');
	var noPromos = (promoimgs.length)-1;		
	
	function rotatePromos(j){
		var i;
		
		if(j == promoimgs.length) {
			i = 0;	
		} else {
			i = j;	
		}		
		
		$(promoimgs[i]).fadeIn('slow', function(){
			rotateTimer = setTimeout(function() {			
			$(promoimgs[i]).fadeOut('slow');
				i++;		
				rotatePromos(i);
			}, 6000);
		});
			
	};rotatePromos(0);

	//get quotes list
	var rotateItems = $('#quotes li');
	var rotateStarted = 0;

	function initQuotes(j){
		var i;
		
		if(j == rotateItems.length){
			i = 0;	
		} else {
			i = j;	
		}		
		
		if(rotateStarted != 1){
			rotateStarted == 1;
			$(rotateItems[i]).fadeIn('slow', function(){
				$('.rotating').addClass('rolling');									  
				rotateTimer = setTimeout(function() {			
				$(rotateItems[i]).fadeOut('slow');
					i++;		
					initQuotes(i);
				}, 6000);
			});
		}
		
	};
	
	function rotateQuotes(){
		if (!$('#quotes').hasClass('.rolling')){
			initQuotes(0);
		}
	};
	

	function loadScrollPane(){
		if (!(IE6 || IE7)){
			$('.scroll-pane').jScrollPane();	
		}
	};

	function setCSS_onload(){	
		$('.dvd').css({'display': 'none'});
		$('.about').css({'display': 'none'});
		$('#navmap_img').css({'display': 'none'});
		
		$('#nav_home').css({'opacity':0, 'left': '-100px'});	
		$('#nav_about').css({'display': 'none'});	
		$('#nav_dvd').css({'opacity':0, 'left': '100px'});
		if (!IE7) {
			$('#castshot').css({'display': 'none'});
		};
	};
	
	function navAnimate(){
		$('#nav_dvd').animate({opacity: 1, left: '-=100'}, 550, 'swing');
		$('#nav_home').animate({opacity: 1, left: '+=100'}, 550, 'swing');

		$('#nav_about').fadeIn(1000, function() {
			$('#navmap_img').show(200, function() {
				//load video
				flashcontentembed('videoplayer');
  			});
		});
		//$('#navmap_img').css('display','block');
		
	};
	
	function fixPng(png){
	   // get src
	   var src = png.src;
	   // set width and height
	   if (!png.style.width) { png.style.width = $(png).width(); }
	   if (!png.style.height) { png.style.height = $(png).height(); }
	   // replace by blank image
	   png.onload = function() { };
	   png.src = blank.src;
	   // set filter (display original image)
	   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	};


});// end doc.ready


//non jquery functions
function uriparse(name){
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
		return "";
	  else
		return results[1];
	}
	pgname = uriparse('p');
	
//flash embed
var flashversion = "9.0.115";

if(typeof(swfobject)=='object' && swfobject.hasFlashPlayerVersion(flashversion)){
	var head = document.getElementsByTagName('head');
	var flashstyle = document.createElement('style');
	var css = '#flashcontent h1, #flashcontent p {display: none}';
	flashstyle.setAttribute('type','text/css');
	if(head[0]){
		if(flashstyle.styleSheet){
			flashstyle.styleSheet.cssText = css;
		}else{
			flashstyle.appendChild(document.createTextNode(css));
		}
		head[0].appendChild(flashstyle);
	}
}
	
function flashcontentembed(loadme){
	
	var flashvars = {};
		
	var params = {
		allowscriptaccess:"always",
		wmode:"transparent",
		base:"media/"
	};
 
	var attributes = {
		id: "flashcontent",
		name: "flashcontent"
	};
											 
	swfobject.embedSWF("media/"+loadme+".swf", "flashcontent", "500", "275", "9.0.115", "", flashvars, params, attributes);
	swfobject.createCSS("#flashcontent", "border: 4px solid #bebabc;");
}
