var credits="<div class='Footer'>&copy;2007 MobileGates.com</div hackers='stephan.com mkantor'>";

// handle ajax errors
$(document).ajaxError(function(req,err,except) {
	if(except.dataType == "json") {
		setTimeout(function() {
			$.getJSON(except.url, except.success)
		},5000)		
	}
})

function Roller (text) {
	return $("<div/>").addClass("Roller").append($("<span/>").text(text))
}

$(function() {
	var updateLayout = function() {
		alert(window.innerWidth);
	};

//	alert("boing")
//	updateLayout();
//	window.addEventListener(’onresize’, updateLayout);
//	$(window).bind("onresize",updateLayout)
//	$(document).resize(function() {
//		alert("boing2")
//	})
//	updateLayout();
//	$(document).resize();
//	alert("boom")	
})

var gCurPage = "ul#Main.Menu";
function swipePage(fromPage,toPage,backwards) {


    if(toPage != gCurPage || toPage == "#searchResults") {
	var name;
	if (toPage == "#Main") {
	    name = "homepage";
	} else if (toPage == "#newReleases") {
	    name = "movies/new_releases";
	    /*
	    if($("#NewReleasesTopAd").length == 0)
		$("<li/>").attr("id","NewReleasesTopAd").addClass("TopAd").prependTo("ul#newReleases");
		prependAdd("06","movies/new_releases", "#NewReleasesTopAd");*/
	} else if (toPage == "#boxOffice") {
	    name = "movies/box_office";/*
	    if($("#BoxOfficeTopAd").length == 0)
		$("<li/>").attr("id","BoxOfficeTopAd").addClass("TopAd").prependTo("ul#boxOffice");
		prependAdd("06","movies/box_office", "#BoxOfficeTopAd");*/
	} else if (toPage == "#theaters") {
	    name = "theater/theater_listing";/*
	    if($("#TheaterListTopAd").length == 0)
		$("<li/>").attr("id","TheaterListTopAd").addClass("TopAd").prependTo("ul#theaters");
		prependAdd("06","theater/theater_listing", "#TheaterListTopAd");*/
	} else if (toPage == "#newsTitles") {
	    name = "news";/*
	    if($("#NewsTitlesTopAd").length == 0)
		$("<li/>").attr("id","NewsTitlesTopAd").addClass("TopAd").prependTo("ul#newsTitles");
		prependAdd("06","news", "#NewsTitlesTopAd");*/
	} else if (toPage == "#timesTickets") {
	    name = "movies/times_tix";/*
	    if($("#TimesTicketsTopAd").length == 0)
		$("<li/>").attr("id","TimesTicketsTopAd").addClass("TopAd").prependTo("ul#timesTickets");
		prependAdd("06","movies/times_tix", "#TimesTicketsTopAd");*/
	} else if (toPage == "#searchResults") {
	    name = "";
	}
	if (name) prependAdd("06", name, "#TopAd");
    }
    if(toPage==gCurPage) return;
    if (!fromPage) fromPage = gCurPage;
    gCurPage = toPage;

	if(toPage=="#undefined"){alert("under construction");return;}
	
	// get the current width
	width=window.innerWidth;
	
	if(backwards) {
		destStart = -width+"px";
		direction = "+="+width+"px";
	} else {
		destStart = width;
		direction = "-="+width+"px";		
	}

	$(fromPage).hide();
$(toPage).show();
return
	$(toPage)
		.css("left",destStart)
		.show().add($(fromPage).css("left",0))
		.animate({left: direction}, function() {
			$(fromPage).hide();
		})				
}

function createCookie(name,value,days) {
    if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}



function handleEmptyInput(textbox) {
    if($(textbox).is(".Empty")) {
	$(textbox).removeClass("Empty");
	textbox.value="";
    }
}

//var gStandardAdHeight = "40px";
var gStandardAdHeight = "";
function prependAdd(pos,page,where) {
    var pagename = page || "other";
    pagename += '@x' + pos;

    var whereObj = $(where);
    $.get("/loadAd.html", {"url" : OAS_url + pagename + "?phonetype=2&afid=" + gAFID}, function(msg) {
	if (msg.indexOf("empty.gif") >= 0) {
	    return;
	}

	if (where == "#TopAd") whereObj.css("height",gStandardAdHeight);
	whereObj.show();

	var appendObj = $(msg);
	var img = appendObj.find("img:not(.poster)");
	img.css("display","none");

	//img.load(function() {$(this).parent().fadeIn(2000);});


	if (whereObj.children().length > 0) {
	    whereObj.find("img:not(.poster)").fadeOut(400,function() {
		whereObj.empty();
		whereObj.append(appendObj);
		img.fadeIn(2000);
	    });    
	} else {
	    whereObj.append(appendObj);
	    img.fadeIn(2000);
	}
    });
}


function generateBuyLink(time,lasthour,lastampm,first,newShow,ticketing,houseID,showDate,movieID) {
    var matches = time.match(/(\d+)\:(\d+)/);
    var hour = matches[1];
    var comphour = hour % 12; // 12 is treated as 0 when comparing whether its greater or less than other times in the same AM/PM
    var minute = matches[2];
    var ampm;
    if (first) {
	if (time.match(/pm/i)) {
	    ampm = 1;
	} else if (hour <= 4) {
	    ampm = 2;
	} else {
	    ampm = 0;
	}
	first = false;
    } else if (lastampm == 0 && lasthour > comphour ) {
	ampm = 1;
    } else if (lastampm == 1 & lasthour > comphour) {
	ampm = 2;
    } else {
	ampm = lastampm;
    }
    
    var a = $("<a/>").text(time+" ");
	    if (ticketing == "Y") {
	a.click(function() {

		
		/*
		  console.log("theater id:", houseID)
		  console.log("movie id:", movieID)
		  console.log("date: ", showDate)
		  console.log("time: ", showTime)*/


		var newurl =  ((isTestMode) ? "http" : "https") + "://" + domain + "/iphone/buy1.html?"
		    +"&movieID="+movieID
		    +"&houseID="+houseID
		    +"&date="+showDate
		    +"&time="+hour + minute
		    +"&ispm="+ampm;
		newurl += gPropagateParams;
		window.location.href = newurl;
	})
	 .addClass("Ticketable")
}

   return [comphour,ampm,a];
}
