var queryString = "";
var redirectPageLoc='http://home.disney.go.com/guestservices/disclaimers/redirect?destination=';

var today = new Date();
var streetDate		= new Date();
var userDateAt		= location.search.substring().indexOf('today=');
var userDateStr		= location.search.substr(userDateAt+6);
var userDateEnd		= userDateStr.indexOf('&');
var PSTOffset		= 7;

var hasSuper = false;
if (location.search) {
	queryString = location.search.substring(1, location.search.length);
}


/**** GEO Tracking ****/
if (country == "united kingdom" || country == "ireland") {
	document.location.replace("http://www.disney.co.uk/muppets");
}



/****  BROWSER DETECTION  ****/
// var chBrwsrVer=parseFloat(chAppVersion[1]);
var isIE=false;
// var chIsIE7=false, chIsIE8=false, chIsFirefox=false, chIsOpera=false, chIsSafari=false, chIsMac=false, chIsPC=false, chromeDebug=false;
if(navigator.userAgent.indexOf('MSIE')!=-1)
{
	isIE=true;
}



/**** Common scripts for page loads & popups ****/
function loadPage (targURL) { //v2.0
	window.location.href = targURL;
}
function newWindow(targURL, disclaimer) { //v2.0
	if (disclaimer=='true') {
		MuppetsWin = window.open(redirectPageLoc+ targURL,'MuppetsPop');
	} else {
		MuppetsWin = window.open(targURL,'MuppetsPop');
	}
	MuppetsWin.window.focus();
}
function popup(targURL, w, h, disclaimer) { //v2.0
	var winl = (screen.width - w) / 3;
	var wint = (screen.height - h) / 3;
	coords = 'width='+w+',height='+h+',top='+wint+',left='+winl;
		
	if (disclaimer=='true') {
		MuppetsWin = window.open(redirectPageLoc+ targURL,'MuppetsPop', coords,'scrollbars=no,resizable=no');
	} else {
		MuppetsWin = window.open(targURL,'MuppetsPop', coords,'scrollbars=no,resizable=no');
	}
	MuppetsWin.window.focus(); 
}

function removeLineBreaks (str)
{
	var forcedLineBreakAt = str.indexOf("\n");
	if (forcedLineBreakAt >= 0)
	{
		str = (str.substring (0,forcedLineBreakAt)+" "+removeLineBreaks (str.substring (forcedLineBreakAt + 1)));
	}
	return str;
}

function loadTwitterFeed ()
{
	$.getJSON("http://twitter.com/status/user_timeline/muppetsstudio.json?count=20&callback=?",
		function(data) {
			var d = new Array ();
			$.each(data, function(i,item){
				var twText = removeLineBreaks (item.text);
				d[i] = {id_str:item.id_str,text:twText,thumb_url:"http://a.dolimg.com/franchise/muppets/3.3/media/images/social/twitter_thumb.jpg",screen_name:item.user.screen_name,created_at:item.created_at}
		});
		var swf = document.getElementById("mainSite");
		swf.loadTwitterData (d);
	});
}


function launchWpaper(str)
{
	newWindow("http://disney.go.com/muppets/popups/popup_wpaper.html?"+str);
}
function launchBuddy (str)
{
	popup("http://disney.go.com/muppets/popups/popup_buddy.html?"+str, 320, 320, false)
}
function launchSsaver (str)
{
	popup("http://disney.go.com/muppets/popups/popup_screensaver.html?id="+str, 320, 320, false)
}




function launchLTMVideo (str)
{
	$('#modalContent #btnClose').css("top","50px");
	$('#modalContent #btnClose').css("left","834px");
	openModal ("http://disney.go.com/muppets/modal/ltm_video.html?config="+str);
}

function launchLTMGame (str)
{
	openModal ("http://disney.go.com/muppets/modal/ltm_game.html?config="+str);
}

function launchGame (str)
{
	$('#modalContent #btnClose').css("top","53px");
	$('#modalContent').addClass('prevented');
	openModal ("http://disney.go.com/muppets/modal/game.html?swf="+str);
}

function launchLTMGameKermit (str)
{
	openModal ("http://disney.go.com/muppets/modal/game_Kermit/ltm_game.html?config="+str);
}

function launchGameKermit (str)
{
	$('#modalContent #btnClose').css("top","53px");
	$('#modalContent').addClass('prevented');
	openModal ("http://disney.go.com/muppets/modal/game_Kermit/game.html?swf="+str);
}

function launchLTMGameFozzie (str)
{
	openModal ("http://disney.go.com/muppets/modal/game_Fozzie/ltm_game.html");
}

function launchGameFozzie (str)
{
	$('#modalContent #btnClose').css("top","53px");
	$('#modalContent').addClass('prevented');
	openModal ("http://disney.go.com/muppets/modal/game_Fozzie/game.html");
}

function launchLTMGameGonzo (str)
{
	openModal ("http://disney.go.com/muppets/modal/game_Gonzo/ltm_game.html");
}

function launchGameGonzo (str)
{
	$('#modalContent #btnClose').css("top","53px");
	$('#modalContent').addClass('prevented');
	openModal ("http://disney.go.com/muppets/modal/game_Gonzo/game.html");
}

function launchGameSearch (str)
{
	$('#modalContent #btnClose').css("top","53px");
	$('#modalContent').addClass('prevented');
	openModal ("http://disney.go.com/muppets/modal/game_Search/game.html");
}

function openModal (url)
{
	$('#modal').show();
	document.getElementById('modalWin').src = url;
	if (isIE)
	{
		$('html').css('overflow','hidden');
	}
}

function closeModal ()
{
	$('#modal').hide();
	$('#modalWin').hide();
	document.getElementById('modalWin').src = "";
	var swf = document.getElementById("mainSite");
	swf.onModalClose (hasSuper);
	if (isIE)
	{
		$('html').css('overflow','auto');
	}
}

function displayModal()
{
	$('#modalWin').show();
}

function showSuperAd ()
{
	$('#adSuper').fadeIn();
	hasSuper = true;
}

function hideSuperAd ()
{
	$('#adSuper').fadeOut();
	hasSuper = false;
}

function showSponsorAd (pos)
{
	$('#adSponsor').css("left",(Number(pos[0])-40)+"px");
	$('#adSponsor').css("top",(Number(pos[1])+$('#mainContainer').position().top)+"px");
	$('#adSponsor').fadeIn();
}
	
function removeSponsorAd ()
{
	$('#adSponsor').css("top","-120px");
	$('#adSponsor').hide();
}

function hideSponsorAd ()
{
	$('#adSponsor').hide();
}

function restoreSponsorAd ()
{
	$('#adSponsor').fadeIn();
}

function disableSite ()
{
	$('#adSponsor').hide();
	if (hasSuper)
	{
		$('#adSuper').fadeTo(400,0.2);
	}
}

function enableSite ()
{
	// use after site dims
	$('#adSponsor').fadeIn();
	if (hasSuper)
	{
		$('#adSuper').fadeTo(400,1);
	}
}



function setPromosActive (isActive)
{
	var swf = document.getElementById("footerPromos");
	swf.setPromosActive (isActive);
}

function checkDate(yr,mo,dt,hr) {
	compareDates (yr,mo,dt,hr);
	if (today >= streetDate) {
		return true;
	}else{
		return false;
	}
}

function compareDates(yr,mo,dt,hr) {
	mo=mo-1;
	if (userDateAt > 0) {
		if (userDateEnd > 0) {
			userDateStr = userDateStr.substring (0,userDateEnd);
		}
		userDate = userDateStr.split(',');
		today.setFullYear(userDate[0]);
		today.setMonth(Number(userDate[1])-1);
		today.setDate(userDate[2]);
		if (userDate.length>3) {
			today.setHours(Number(userDate[3])+PSTOffset);
		} else {
			hr = PSTOffset;
		}
	}
	today.setMinutes(today.getMinutes());
	streetDate.setFullYear(yr);
	streetDate.setMonth(mo);
	streetDate.setDate(dt);
	// alert ("userDateStr:"+userDateStr);
	if (hr) {
		streetDate.setHours(hr);
	}
}
function addTheaterDates (targ) {
	var mos = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	var ticketDay = new Date (2011,10,23);
	if (checkDate (2011,11,23))
	{
		ticketDay = today;
	}
	var dtRead = "";
	for (var d = 0; d < 14; d++) {
		var dtTxt = (mos[ticketDay.getMonth()]+" "+ticketDay.getDate()+", "+ticketDay.getFullYear());
		var mo = ticketDay.getMonth()+1;
		var dt = ticketDay.getDate();
		if (mo < 10)
		{
			mo = "0"+mo;
		}
		if (dt < 10)
		{
			dt = "0"+dt;
		}
		if (dtTxt == "Nov 23, 2011")
		{
			$(targ+' #id_date').append('<option value="'+ticketDay.getFullYear()+'-'+mo+'-'+dt+'" selected>'+dtTxt+'</option>');
		} else {
			$(targ+' #id_date').append('<option value="'+ticketDay.getFullYear()+'-'+mo+'-'+dt+'">'+dtTxt+'</option>');
		}
		ticketDay = new Date(ticketDay.getTime() + 24*60*60*1000);
	}
}

var hashLoc = location.hash.substr(2);
function initFlashData (startPage)
{
	if (startPage)
	{
		hashLoc = startPage;
	}
   	var queryString = location.search.substring(1, location.search.length);
	var qStrAt = hashLoc.indexOf('?');
	var qString = "";
	if (qStrAt > 0) {
		if (queryString)
		{
			queryString = queryString + "&" + hashLoc.substring(qStrAt + 1);
		}
		else {
			queryString = hashLoc.substring(qStrAt + 1);
		}
		hashLoc = hashLoc.substring (0,qStrAt);
	}
	var _deeplink = hashLoc;
	var ltmCookies = (getCookie('ltmKermit')+","+getCookie('ltmPiggy')+","+getCookie('ltmAnimal')+","+getCookie('ltmGonzo')+","+getCookie('ltmFozzie'));
	
	mainFlashVars = {
		as_swf_name			:	"muppetsSite",
		cdnLoc				:	"http://a.dolimg.com/franchise/muppets/3.3/",
		xmlLoc				:	"http://disney.go.com/muppets/xml/",
		configLoc			:	"http://disney.go.com/muppets/xml/config.xml",
		ageGated			:	getCookie('ageAllowed'),
		ltmStatus			:	ltmCookies,
		qString				:	queryString,
		deeplink			:	_deeplink
		};
	mainParams = {
		menu				:	"false",
		scale				:	"noscale",
		allowScriptAccess	:	"always",
		allowFullScreen		:	"true",
		wmode				:	"opaque",
		salign				:	"t"
		};
	mainAttributes = {
		id					:	"mainSite"
		};
}

function ticketWidgetSlideUp ()
{
	$('#tixWidget').stop().animate(
		{top:'0'},
		{duration: 400, easing: 'easeInQuart'});
	$('#orderWidget').stop().animate(
		{top:'0'},
		{duration: 400, easing: 'easeInQuart'});
}

function ticketWidgetSlideDown ()
{
	$('#tixWidget').stop().animate(
		{top:'48'},
		{duration: 400, easing: 'easeOutQuart'});
	$('#orderWidget').stop().animate(
		{top:'48'},
		{duration: 400, easing: 'easeOutQuart'});
}

function initShell ()
{
	// called once on main shell build
	// only applies on transition from Splash
	$('#mainContainer').animate(
		{height:'910'},
		{duration: 600, easing: 'easeInOutQuad'});
	$('#orderWidget').animate(
		{top:'48'},
		{duration: 400, easing: 'easeInQuart'});
}

