//overlayPlayer config+
feedUrl = "http://www.sonypictures.com/previews/movies/underworldawakening.xml"; 
autocloseToggle = true;
htmlVideoScreenshot = "http://www.sonypictures.com/movies/underworldawakening/images/ua_trailer_mobile_screenshot.jpg";
htmlVideoMP4Lo = "http://flash.sonypictures.com/video/movies/underworldawakening/mobile/Underworld4_DTR1_Eng_EAGL_high.mp4";
htmlVideoMP4Hi = "http://flash.sonypictures.com/video/movies/underworldawakening/mobile/Underworld4_DTR1_Eng_EAGL_full.mp4";

// share configuration
share.url = "http://www.EntertheUnderworld.com";
share.title = "Underworld Awakening - Official Site";
share.description = "Underworld Awakening - in theaters January 2012. Kate Beckinsale returns as the vampire warrioress Selene.";
share.tweet = "Underworld Awakening in theaters Jan 2012. Kate Beckinsale is back as vampire warrior Selene. http://www.EntertheUnderworld.com";




var SplashUI = function() {
	
	// PRIVATE
	
	// any special styling that need to be done on load should be in this function
	function initStylesAndMethods() {	
		Cufon.replace('.cufon', { fontFamily: 'Trajan Pro'});
		
		if(typeof popUpsAndTrackingFunctions != 'function') {
			$('a[rel="external"]').bind('click', function() {
				window.open($(this).attr('href'), '_blank');
				return false;
			});
		}
	}
	
	// use it only if you want to animate things
	function animation() {
	}
	
	// opens full screen window for the immersive - only used when enter the site button is active
	function enterTheSite() {
		$('#title-treatment').bind('click', function() {
			var link = 'http://www.EnterTheUnderworld.com/site/';
			window.open(link, '');
			sCode.trackFeaturedContentClick(link, 'entersite_keyart');
			//window.open(link, '', 'fullscreen=yes, scrollbars=yes, resizable=no, status=no, directories=no, toolbar=no');
			/*if(typeof sCode == 'object') {
				if($(this).attr('id') == 'title-treatment') {
					sCode.trackFeaturedContentClick(link, 'entersite_button');
				}	else {
					sCode.trackFeaturedContentClick(link, 'entersite_keyart');
				}			
			}*/
			return false;
		});
	}
	
	// video that goes into enter the site button - only used when enter the site button is active
	function embedVideo($width, $height) {
		var flashvars = {};
		var params = {
			wmode: 'opaque',
			allowscriptaccess: true
		};	
		var attributes = {};
		swfobject.embedSWF('media/video.swf', 'enterthesite-video', $width, $height, '10', '', flashvars, params, attributes);
	}
	
	// plays videos with rel tag that starts with "playvideo". To play specific clip, add dash then number. Example: rel="playvideo-3144"
	function playVideos() {
		$('#menu a[rel^="playvideo"]').bind('click', function() {
			var clipId = $(this).attr('rel').split('-')[1];
			if(document.getElementById('enterthesite-video')) {
				document.getElementById('enterthesite-video').pause();
			}
			if(clipId) {
				openOverlay(clipId);				
			} else {
				openOverlay();
			}
			return false;
		});
	}
	
	//opens the overlay player and plays a specified clip
	function autoPlay(clipId) {
		var autoPlayTrailer = setTimeout("openOverlay(" + clipId + ");", 5000);
		$('#menu, a').bind('click', function(){
			clearTimeout(autoPlayTrailer);
		});			
	}
	
	function connectAndShare(e) {
		$('#connect-share').bind('click', function(e) {				
			if (!$('#connect-share-extras').is(':animated')) {
				if($('#connect-share-extras').is(':hidden')) {
					connectAndShareSlide();
				} else {	
					$('#share').fadeTo('', 0.3);
					$('#connect-share-extras').slideToggle('normal', function() {
						$('#connect-share').removeClass('connect-expanded');
						$('#arrow').hide();						
					});	
				}			
				return false;
			}
		});			
		
		// hover state binded to li element.
		$('#connect-share').hover(
			function () { 
				$(this).addClass('connect-hover'); 
			}, function () { 
				$(this).removeClass('connect-hover');
			}
		);
		
		// stops triggering the events binded to parent DOM elements
		$('#connect li a, #share li a, #getglue-container').bind('click', function(e) {
			e.stopPropagation();
		});	
				
		// share integration
		$('#share li a').bind('click', function() {
			if($('#connect-share-extras').is(':hidden')) {						
				connectAndShareSlide();		
			} else {
				share.send($(this).attr('rel'));	
				if(typeof sCode == 'object') {
					sCode.trackOutboundClick($(this).attr('href'), $(this).attr('class'));				
				}
			}
			return false;
		});			
	}
	
	
	function getTickets() {
		$('#gettickets').bind('click', function() {				
			if($('#getticketswidget-container').is(':hidden')) {
				getTicketsSlide();
			}
			else {
				$('#getticketswidget-container').parent().parent().css('padding', '0px').parent().css('padding', '0px');
				$('#gettickets').removeClass('gettickets-expanded');
				//$('#gettickets p.cufon').css('margin-top', '0');
				$('#gettickets p.cufon').show();
				$('#gettickets #arrow').hide();
				$('#getticketswidget-container').slideToggle();
			}			
			return false;
		});
		
		$('#gettickets').hover(
			function () { 
				if($('#getticketswidget-container').is(':hidden')) {
					$(this).addClass('gettickets-hover'); 
				}
			}, function () { 
				$(this).removeClass('gettickets-hover');
			}
		);
		// stops triggering the events binded to parent DOM elements
		$('#getticketswidget-container').bind('click', function(e) {
			e.stopPropagation();
		});
	}
	
	// sliding animation for connect and share section, apply this when slided state is the default layout
	function connectAndShareSlide() {
		$('#connect-share').addClass('connect-expanded');					
		$('#arrow').show();
		$('#share').fadeTo('', 1);
		$('#connect-share-extras').slideToggle();
	}
	
	// sliding animation for get tickets and showtimes section, apply this when expanded state is the default layout
	function getTicketsSlide() {
		$('#gettickets').addClass('gettickets-expanded');					
		//$('#gettickets p.cufon').css('margin-top', '-2px');
		$('#gettickets p.cufon').hide();
		$('#gettickets #arrow').show();
		$('#getticketswidget-container').slideToggle();
	}
	
	function embedTicketsWidget() {
		var MOVIE_ID = 52,
			RELEASE_DATE = '01/19/2012'; // A day before release date
		
		$('#search-results').jScrollPane({
			scrollbarWidth: 5, 
			dragMinHeight: 23,
			dragMaxHeight: 23
		});		

		var api = $('#search-results').data('jsp'),
			throttleTimeout;
			
		function reinitializeScroll(a) {
			if ($.browser.msie) {
				if (!throttleTimeout) {
					throttleTimeout = setTimeout(
						function()
						{
							a.reinitialise();
							throttleTimeout = null;
						},
						50
					);
				}
			} else {
				a.reinitialise();
			}
		}
		
		Date.prototype.defaultView = function() {
			var day   = this.getDate(),
				month = this.getMonth() + 1,
				year  = this.getFullYear();
			if(day < 10)
				day = '0' + day;
			if(month < 10)
				month = '0' + month;
			return String(month + "\/" + day + "\/" + year);
		}	

		function compareDate() {
			var monthArr = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
				today = new Date(),	
				releaseArr = RELEASE_DATE.split('/'),
				releaseDate = new Date(monthArr[Number(releaseArr[0]) - 1] + ' ' + releaseArr[1]+ ', ' + releaseArr[2] + ' 00:00:00');
			if(today < releaseDate) {
				return RELEASE_DATE;
			} else if (today > releaseDate) {
				return today.defaultView();
			} else {
				return today.defaultView();
			}
		}
			
		$('#tickets-zipcode').bind('focus', function() {
			if($(this).val() == 'Zip Code') {
				document.getElementById('tickets-zipcode').value = '';
			}
		}).bind('blur', function() {
			if($(this).val() == '') {
				document.getElementById('tickets-zipcode').value = 'Zip Code';
			}
		});
		
		$('#tickets-date p').text(compareDate());
		
		// Datepicker
		$('#datepicker').datepicker({
			showOtherMonths: true,
			minDate: compareDate(),
			onSelect: function(dateText, inst){
				if($.browser.msie && $.browser.version == 7) {
					$('#search-results-container .jspPane').css('left', '0');		
				}
				$('#datepicker').hide();
				$('#tickets-date p').text(dateText);				
			}
		});
		
		$('#datepicker').bind('click', function(e) {
			e.stopPropagation();
		});
		
		$('#tickets-date').bind('click', function() {
			if($.browser.msie && $.browser.version == 7) {
				$('#search-results-container .jspPane').css('left', '-9999px');	
			}
			$('#datepicker').show();			
		});
		
		$('#tickets-search-button').bind('click', function() {
			sCode.trackFeaturedContentClick('', 'showtimeswidget_search_button');
			if(document.getElementById('tickets-zipcode').value == 'Zip Code') {
				alert('Please enter zip code');
				return false;
			}
			
			var date  = $('#tickets-date p').text().split('/');
				
				
			$.ajax({
			
				type: 'POST',
				url: '/global/php/showtimes/fetch.php',
				dataType: 'xml',	
				
				data:  
					'zip=' + $('#tickets-zipcode').val() +
					'&month=' + date[0] +
					'&day=' + date[1] +
					'&year=' + date[2] +
					'&movie=' + MOVIE_ID
				,		
				
				success: function(xml){
					$('#search-results .jspPane').empty();
					if($(xml).find('theater').length > 0) {						
						$(xml).find('theater').each(function(){
							var name     = $(this).find('name').text(),
								showtime = $(this).find('showtime'),
								times    = '';
							showtime.each(function(i) {
								var type = $(this).attr('type'),
									url  = $(this).attr('url'),								
								    time = $(this).text(),
									source = $(this).parent().attr('source'),
									typeOutput;
								if(type == 'imax') {
									typeOutput = ' (IMAX 3D) ';
								} else if (type == '3d') {
									typeOutput = ' (3D) ';
								} else {
									typeOutput = '';
								}		
								times += '<li><p><a href="' + url + '" target="_blank" rel="' + source + '" class="' + type + '">' + time + '</a>' + typeOutput + '</p></li>';									
							});							
							$('<div class="theater"></div>').append(
								'<p class="theater-name">' + name + '</p>' +
								'<ul class="theater-showtimes">' + times + '</ul>'
							).appendTo('#search-results .jspPane');
						});						
						$('.theater-showtimes li a').unbind();
						$('.theater-showtimes li a').bind('click', function() {						
							sCode.trackOutboundClickToBuy(this.href, $(this).attr('rel') + '_showtimeswidget');
						});
						reinitializeScroll(api);
					} else {
						$('#search-results .jspPane').html('<p>No results found.</p>');
						reinitializeScroll(api);
					}
				},
				
				beforeSend: function() {
					$('#search-results .jspPane').empty();
					$('#search-results .jspPane').append('<p>Loading...</p>');
				},
				
					
				
				error: function(httpRequest, textStatus, errorThrown) { 
					alert("status=" + textStatus + ",error=" + errorThrown);
				}
				
			}); 
		});
	}
	
	/* function embedTicketsWidget($widgetDivId, $width, $height) {
		var flashvars;
		var params = {
			allowscriptaccess:"always",
			allowfullscreen:"true",
			base:"media/",
			wmode:"transparent",
			salign: "left"
		};					
		var attributes = {
			id: $widgetDivId
		};										
		swfobject.embedSWF(params.base + "sa_widget.swf", $widgetDivId, $width, $height, "9.0.115", "", flashvars, params, attributes);
	} */
	
	function registerPopup() {
		$('#register').bind('click', function() {
			window.open(this.href, '', 'width=446, height=500, scrollbars=no, resizable=no');
			return false;
		});
	}
	
	function shareFade() {
	$('#share').fadeTo('', 0.3);
	}
	
	function openConnectAndShare() {
		$('#connect-share').trigger('click');
	}
	
	//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/PRIVILEGED 

	// Only include the functions that are going to get used
	this.init = function() {
		fixFoutBeforeCufon()
		//animation();
		initStylesAndMethods();
		fixFoutAfterCufon()
		enterTheSite();
		//getTickets();
		//embedVideo(232,94);
		playVideos();		
		//autoPlay();
		connectAndShare();
		openConnectAndShare();
		embedTicketsWidget();
		//registerPopup();			
	}	
		
}

var fwbSplash = new SplashUI();

$(function() {	
	fwbSplash.init();
});
