// overlay player config
feedUrl = "http://www.sonypictures.com/previews/movies/battlelosangeles.xml"; 
autocloseToggle = true; 
singlevideoToggle = false; 
callbackToggle = true;
clipName = 1757;
parentSwf = 'videobutton';
var htmlVideoScreenshot = "http://www.sonypictures.com/previews/movies/images/battlelosangeles_clip_2914.jpg";
var htmlVideoMP4Lo = "http://flash.sonypictures.com/video/movies/battlela/battlela_dtr2_iphone_low.mp4";
var htmlVideoMP4Hi = "http://flash.sonypictures.com/video/movies/battlela/BLA_DTR2_1-18-11_ipad.mp4";


var spheSplash = function() {
/****************IMPORTANT********************/
/*
Do NOT use rel="external" for nav links that have been cufonized.
Doing so will cause an error in ie.

USE rel="nav-external".
*/
	
	//PRIVATE
	
	//cufon init and other methods to be called on load
	function initStyles() {
		//hide submenus
		$('#maincontent > ul').children('li').hide();
		
		//fix for cufon js bug ie for rel external
		$('a[rel="nav-external"]').bind('click', function() {
			window.open($(this).attr('href'), '_blank');
			return false;
		});
		
		//cufon
		if($('.cufon-title').length > 0) {
			Cufon.replace('.cufon-title', {
				fontFamily: 'Helvetica Neue Black Condensed',
				textShadow: '0 -2px #2F2F2F',
				hover: true
			});
		}
		if($('.cufon-subtitle').length > 0) {
			Cufon.replace('.cufon-subtitle', {
				fontFamily: 'Helvetica Neue Medium Condensed',
				hover: true
			});
		}
		if($('.cufon-quote').length > 0) {
			Cufon.replace('.cufon-quote', {
				fontFamily: 'Helvetica Neue Black Condensed',
				textShadow: '2px 2px #291B10'
			});
		}
		
		//pop new windows if common.js is not loaded
		if(typeof popUpsAndTrackingFunctions != 'function') {
			$('a[rel="external"]').bind('click', function() {
				window.open($(this).attr('href'), '_blank');
				return false;
			});
		}	
	}
	
	function toggleNav() {		
		$('.button','#nav').bind('click',function(){
			thisSubmenu = $('.submenu',$(this).parent());
			if(!thisSubmenu.is(':animated')) {
				if(thisSubmenu.is(':hidden')) {
					toggleSubmenu($(this),$(this).attr('rel'));
				}
			}
			//store current page in ul#nav rel
			var currentPage = $(this).parent().index();
			$('ul#nav').attr('rel',currentPage);
			
			$('#fullpage-pagination ul li a').each(function(){
				$(this).removeClass('selected');
			});
			$('#fullpage-pagination ul li a').eq(currentPage).addClass('selected');
			Cufon.refresh();
			return false;
		});
	}
	
	function toggleSubmenu(o,submenuIndex) {
		$('.button','#nav').each(function(){
			$(this).removeClass('button-selected');
			//ie7 fix for cufon animation
			if ($.browser.msie && $.browser.version <= 7) {
				$('.submenu ul',$(this).parent()).css('visibility','hidden');
			}
			$('.submenu',$(this).parent()).slideUp(function(){
			});								
		});
		o.addClass('button-selected');		
		$('.submenu',o.parent()).slideDown(function(){
			//ie7 fix for cufon animation
			if ($.browser.msie && $.browser.version <= 7) {
				$('.submenu ul',$(this).parent()).css('visibility','visible');
			}
			var index;
			if(submenuIndex == '') {
				index = 0;				
			}
				else {
					index = submenuIndex;
				}				
			$('a',$(this)).not('a[rel="external"]').eq(index).trigger('click');				
		});
	}
	
	function submenuNav() {
		var internalLinks = $('#nav li .submenu a').not('a[rel="nav-external"]').not('a[rel^="playvideo"]');
		internalLinks.bind('click',function(){
			internalLinks.each(function(){
				$(this).removeClass('selected');
			});
			$(this).addClass('selected');
			Cufon.refresh();
			return false;
		});
	}
	
	function mainContentNav() {
		$('.contentnav').bind('click',function(){
			//var activeSection = new String(); 
			var activeSection = $($(this).attr('href'));
			if(activeSection.is(':hidden')) {
				$('#maincontent > ul').children('li').hide();
				if($.browser.msie) {
					activeSection.show();
				}
				else {
					activeSection.fadeIn();
				}
			}
			return false;
		});
	}
	
	function generatePagination(id,container,numberOfPages) {
		$(document.createElement('div')).attr('id',id).appendTo('#' + container);
		$(document.createElement('ul')).appendTo('#' + id);
		
		for (i=0;i<numberOfPages;i++){
			var li = $(document.createElement('li'));
			$(document.createElement('a')).addClass('_' + i).addClass('sprite').attr('rel',i).appendTo(li);
			li.appendTo('#' + id +' ul');
		}
		$('#' + id).css('width',$('#' + id +' ul').width() + 'px');
		
		//select first page by default
		$('#' + id + ' ul li a').eq(0).addClass('selected');
				
		$('#' + id + ' ul li a').bind('click',function(){
			//toggle selected state
			$('#' + id + ' ul li a').each(function(){
				$(this).removeClass('selected');
			});
			$(this).addClass('selected');
		});
	}

	function fullPagePagination() {
		generatePagination('fullpage-pagination','inner-content',$('ul#nav').children().length);
		var marginLeft = (parseInt($('#fullpage-pagination').parent().width()) - parseInt($('#fullpage-pagination').width())) / 2;
		$('#fullpage-pagination').css({
			'margin-left': marginLeft
		});		
		$('#fullpage-pagination ul li a').bind('click',function(){	
			//trigger nav item
			var page = $(this).parent().index();
			$('a.button','ul#nav li').eq(page).trigger('click');
			return false;
		});
	}
	
	function fullPageNextButton() {		
		var currentPage = 0;
		$('#button-page-next').bind('click',function(){
			currentPage = parseInt($('ul#nav').attr('rel'));			
			if(currentPage < ($('#nav').children().length - 1)) {
				currentPage++;
			}
				else{
					currentPage = 0;
				}			
			$('#fullpage-pagination ul li a').eq(currentPage).trigger('click');
			return false;
		});
	}	
	
	function photoGalleryFunctions() {
		//init gallery pagination
		$('#photogallery').attr('rel','0');		
		imgCount = $('#gallery-carousel ul li').length;
		generatePagination('pagination','gallerycontainer',imgCount);
		var marginLeft = (parseInt($('#pagination').parent().width()) - parseInt($('#pagination').width())) / 2;
		$('#pagination').css({
			'margin-left': marginLeft,
			'width' : 'auto'
		});

		var overlayLayer = $(document.createElement('div')).attr('id','sphe-overlayLayer')
		.css({
			'background': '#000',
			//'display': 'none',
			'height': $(document).height() + 'px',
			'left': '0',
			'opacity': '0.95',
			'outline': 'none',
			'overflow': 'hidden',
			'position': 'fixed',
			'top': '0',
			'width': $(document).width() + 'px',
			'z-index': '100'
		});
		
		overlayLayer.prependTo('body');			
		
		$(document.createElement('div')).attr('id','fullscreen-gallerycontainer')
		.css({
			//'display': 'none',
			'height': '667px',
			'left': '50%',
			'margin-left': '-500px',
			'margin-top': '7%',
			'outline': 'none',
			'overflow': 'hidden',
			'position': 'fixed',
			'width': '1000px',
			'z-index': '200'
		}).insertAfter('#sphe-overlayLayer');
		
		//create full screen gallery elements
		$(document.createElement('div')).attr('id','fullscreen-gallery-innercontainer').appendTo('#fullscreen-gallerycontainer');
		
		//previous button
		$(document.createElement('a')).attr('id','fullscreen-button-previous').addClass('sprite').addClass('navlink').text('Previous').appendTo('#fullscreen-gallery-innercontainer');
		
		//next button
		$(document.createElement('a')).attr('id','fullscreen-button-next').addClass('sprite').addClass('navlink').text('Next').appendTo('#fullscreen-gallery-innercontainer');
		
		//close button
		$(document.createElement('a')).attr('id','button-close').addClass('sprite').text('Close').appendTo('#fullscreen-gallery-innercontainer');
		
		//gallery container
		$(document.createElement('div')).attr('id','fullscreen-gallery-carousel').appendTo('#fullscreen-gallery-innercontainer');		

		//generate gallery ul
		$(document.createElement('ul')).appendTo('#fullscreen-gallery-carousel');
		
		for(i=0;i<imgCount;i++) {
		
			//get img filename 
			var imgsrc = $('#gallery-carousel ul li img').eq(i).attr('src');
			var splitImgSrc = imgsrc.split('/');
			var filename = splitImgSrc[(splitImgSrc.length - 1)];
		
			var li = $(document.createElement('li')).appendTo('#fullscreen-gallery-carousel ul');
			$(document.createElement('img')).attr({
				'src': 'images/photogallery/large/' + filename,
				'alt': 'Photo ' + (i+1),
				'height': '667',
				'width': '1000'
			}).
			appendTo(li);			
		}
		
		//full gallery pagination
		generatePagination('fullgallery-pagination','fullscreen-gallery-innercontainer',imgCount);
		var fullmarginLeft = (parseInt($('#fullgallery-pagination').parent().width()) - parseInt($('#fullgallery-pagination').width())) / 2;
		$('#fullgallery-pagination').css({
			'margin-left': fullmarginLeft,
			'width' : 'auto'
		});
		
		//generate pages array for pagination
		var pages = new Array();
		for (i=0;i<imgCount;i++) {
			pages[i] = '#pagination ul li ._' + i;
			$('#gallery-carousel ul li').eq(i).attr('rel',i);
		}
		
		//for fullscreen
		var fullpages = new Array();
		for (i=0;i<imgCount;i++) {
			fullpages[i] = '#fullgallery-pagination ul li ._' + i;
			$('#fullscreen-gallery-carousel ul li').eq(i).attr('rel',i);
		}

		
		//init photo gallery carousel 
		$('#gallery-carousel').jCarouselLite({
			btnPrev: "#photogallery #button-previous",
			btnNext: "#photogallery #button-next",
			speed: 500,
			btnGo: pages,
			afterEnd: function(a) {
				$('#pagination ul li a._' + a.attr('rel')).trigger('click');
				$('#photogallery').attr('rel',a.attr('rel'));
			}			
		});
		
		$('#fullscreen-gallery-carousel').jCarouselLite({
			btnPrev: "#fullscreen-button-previous",
			btnNext: "#fullscreen-button-next",
			speed: 500,
			btnGo: fullpages,
			afterEnd: function(a) {
				$('#fullgallery-pagination ul li a._' + a.attr('rel')).trigger('click');
				$('#photogallery').attr('rel',a.attr('rel'));
			}		
		});		
		
		$('#fullscreen-gallerycontainer').hide();
		$('#sphe-overlayLayer').hide();
	
		
		$('a#fullscreen').bind('click',function(){
			openFullGalleryOverlay();
			return false;
		});
		
		$('a#button-close').bind('click',function(){
			closeFullGalleryOverlay();
			return false;
		});
		
		$('#sphe-overlayLayer').bind('click',function(){
			closeFullGalleryOverlay();
			return false;
		});
	}
	
	function toggleOverlay(callback) {
		if($('#sphe-overlayLayer').is(':hidden')){
			$('#sphe-overlayLayer').fadeIn('fast',callback)
		}
		else {
			$('#sphe-overlayLayer').fadeOut('fast');
		}
	}
	
	function showFullGallery() {
		$('#fullscreen-gallerycontainer').fadeIn('fast');
	}
	
	function openFullGalleryOverlay() {
		if($('#fullscreen-gallerycontainer').is(':hidden')) {
			$('#fullgallery-pagination ul li a._'+ $('#photogallery').attr('rel')).trigger('click');
			toggleOverlay(showFullGallery);
		}
	}
	
	function closeFullGalleryOverlay() {
		if($('#fullscreen-gallerycontainer').is(':visible')) {
			$('#pagination ul li a._'+ $('#photogallery').attr('rel')).trigger('click');
			$('#fullscreen-gallerycontainer').fadeOut('fast');
			toggleOverlay();
		}
	}
	
	function rotateQuotes() {
		var rotateIndex = 0;
		var quotations = $('#quotations ul li');
		if (quotations.length > 1) {
			quotations.each(function(){
				$(this).hide();
			});
			quotations.eq(rotateIndex).fadeIn('slow',function(){
				setInterval(function(){		
					quotations.eq(rotateIndex).hide()
					if(rotateIndex < 1 ) {
						rotateIndex++;
					}
						else {
							rotateIndex = 0;
						}
					quotations.eq(rotateIndex).fadeIn('slow');
				},5000);
			});
			
		}
	}
	
	function promoPhotoGalleryNav() {
		$('a.photonav','#promo-photogallery').bind('click',function(){
			var index = $(this).parent().index();
			$('#pagination ul li a._' + index).trigger('click');
			$('#nav-aboutthefilm a.button').attr('rel',1);
			$('#nav-aboutthefilm a.button').trigger('click');
			$('#nav-photogallery').trigger('click');
			$('#nav-aboutthefilm a.button').removeAttr('rel');			
			return false;
		});
	}

	//open nav item specified by index on load
	function openDefaultNavItem(index) {
		$('#nav li a.button').eq(index).trigger('click');		
	}
	
	//embed movie {
	function embedMovieButton(swfName) {
		var flashvars = {};
		var params = {
			wmode: 'opaque',
			allowscriptaccess: true
		};	
		var attributes = {
			id: 'videobutton'
		};
		swfobject.embedSWF('media/' + swfName, 'videobutton', '232', '94', '10', '', flashvars, params, attributes);
	}
	
	//register popup
	function registerPopup() {
		$('#share-register').bind('click', function() {
			window.open(this.href, '', 'width=446, height=500, scrollbars=no, resizable=no');
			return false;
		});
	}
	
	// plays videos with rel tag that starts with "playvideo". To play specific clip, add clipName to href. 
	function playVideos() {
		$('a[rel="playvideo"]').bind('click', function() {
			var clipId = parseInt($(this).attr('href'));
			
			if(swfobject.hasFlashPlayerVersion("10")) {
				document.getElementById('videobutton').pause();
			}
				else {
				}
				
			if(typeof(clipId) == 'number') {
				openOverlay(clipId);				
			} else {
				openOverlay();
			}
			
			return false;
		});
	}
	
	//fix for cufon flash of unstyled text(fout)
	function fixFoutBeforeCufon() {
		$('<style type="text/css">.cufonised { text-indent : -9000px; } .submenu, #maincontent > ul > li { display: none; } </style>').appendTo('head');
	}
	function fixFoutAfterCufon() {
		$('.cufonised').removeClass('cufonised');
	}
		
	//PUBLIC/PRIVILEDGED
	this.init = function() {
		//init photo gallery before container is hidden;
		photoGalleryFunctions();
		fixFoutBeforeCufon();
		initStyles();
		fixFoutAfterCufon();
		toggleNav();
		submenuNav();
		mainContentNav();
		fullPagePagination();
		rotateQuotes();
		promoPhotoGalleryNav();	
		openDefaultNavItem(0);
		fullPageNextButton();
		embedMovieButton('video.swf');
		registerPopup();
		playVideos();
	}
}

var blaSplash = new spheSplash();

$(document).ready(function(){
	blaSplash.init();
});
