// browser detection
var $j=jQuery.noConflict();
var firefox = navigator.userAgent.indexOf("Firefox") != -1
var explorer = navigator.userAgent.indexOf("MSIE") != -1
var explorer7 = navigator.userAgent.indexOf("MSIE 7.0") != -1
var netscape = navigator.userAgent.indexOf("Netscape") != -1
var safari = navigator.userAgent.indexOf("Safari") != -1
var opera = navigator.userAgent.indexOf("Opera") != -1
var camino = navigator.userAgent.indexOf("Camino") != -1
var macintosh = navigator.platform.indexOf("Mac") != -1
var dialog = null;

// fix for transparent .png on IE6
function correctPNG() {
	for(var i=0; i<document.images.length; i++) {
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			//alert(img.src)
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:block;" + img.style.cssText 
			if (img.align == "left") imgStyle = "float:left;" + imgStyle
			if (img.align == "right") imgStyle = "float:right;" + imgStyle
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			var strNewHTML = "<span class=\"pngFix\" " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}
if (explorer && !explorer7) {
	window.attachEvent("onload", correctPNG);
}

// embed a flash file
function insertFlash(path, id, width, height, flashvars, description, parent) {
	document.write('<object width="' + width + '" height="' + height + '" id="' + id + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">\n');
	document.write('<![CDATA[<p>' + description + '</p>]]>\n');
	document.write('<param name="allowScriptAccess" value="always" />\n');
	document.write('<param name="movie" value="' + path + '" />\n');
	document.write('<param name="menu" value="false" />\n');
	document.write('<param name="allowFullScreen" value="true" />\n');
	document.write('<param name="flashvars" value="' + flashvars + '">\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="scale" value="noscale" />\n');
	document.write('<param name="bgcolor" value="transparent" />\n');
	document.write('<param name="wmode" value="transparent" />\n');
	document.write('<embed width="' + width + '" height="' + height + '" name="' + id + '" src="' + path + '" flashvars="' + flashvars + '" wmode="transparent" menu="false" quality="high" scale="noscale" allowFullScreen="true" bgcolor="transparent" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>\n');
}

// images rollover (3 states: of, on, in)
function changeImage(which) { 
	var sr = $j(which);
	var st = sr.src.substring(sr.src.length -6,sr.src.length -4);
	var e = sr.src.substring(sr.src.length -3,sr.src.length);
	if (st == 'of') sr.src = sr.src.substring(sr.src.length -6,0)+'on.'+e;
	else if (st == 'on') sr.src = sr.src.substring(sr.src.length -6,0)+'of.'+e;
}



function updateCartAsync(url, resultPlace) {
	$j.ajax({
	    type: "GET",
	    url: url,
	    success: function(data) {
	        $j(resultPlace).html(data);
	        // $("#dialog").dialog('open');
	      }
	});
}

function login(ctx){
	closeDialogIfOpen();
	var loginURL = ctx + '/user/login.jsp';
	
	var frameInfo = '<iframe src="' + loginURL + '" id="loginDialog">';
	dialog = $j(frameInfo).dialog({
		title: 'Sign in with your Cineplex account',
		height: 446,
		width: 470,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('width','470px');
			$j(this).css('overflow','hidden');
			if ($j.browser.msie || $j.browser.webkit){
				$j(this).css('height', '396px');
			} else {
				$j(this).css('height', '392px');
			}
			
			
		},
		buttons: {
		//Cancel: function(){ $j(this).dialog('close'); }
		}
		});
	//dialog.style.height="392px";
}

function loginCart(ctx){
	closeDialogIfOpen();
	var loginURL = ctx + '/user/login.jsp?cart=1';
	
	var frameInfo = '<iframe src="' + loginURL + '" id="loginDialog">';
	dialog = $j(frameInfo).dialog({
		title: 'Sign in with your Cineplex account',
		height: 445,
		width: 698,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('width','701px');
			$j(this).css('overflow','hidden');
			if ($j.browser.msie ){
				$j(this).css('height', '396px');
			} else {
				$j(this).css('height', '392px');
			}
		},
		buttons: {
		
		}
		});
}

function forgotPassword(ctx){
	closeDialogIfOpen();
	var forgotPasswordURL = ctx + '/global/popups/forgotPassword.jsp';
	
	var frameInfo = '<iframe src="' + forgotPasswordURL + '" id="forgotPasswordDialog">';
	dialog = $j(frameInfo).dialog({
		title: 'Forgot Password',
		height: 210,
		width: 465,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('overflow','hidden');
			if ($j.browser.msie || $j.browser.webkit){
				$j(this).css('width','469px');
				//$j(this).css('height', '170px');
			} else {
				$j(this).css('width','465px');
				//$j(this).css('height', '170px');
			}
		}
		});
}

function giftCardBalance(ctx){
	closeDialogIfOpen();
	var giftCardBalanceURL = ctx + '/global/popups/giftCardBalance.jsp';
	
	var frameInfo = '<iframe src="' + giftCardBalanceURL + '" id="giftCardBalanceDialog">';
	dialog = $j(frameInfo).dialog({
		title: 'Pay with Cineplex Gift Card <span style="color:#FFFFFF; font-size:0.7em; text-decoration:underline; padding-left:15px;"><a href="#" onclick="window.open(\'' + ctx + '/faq.jsp?fCatId=900036\', \'faq\');" title="Find out more about gift cards">Find out more</a></span>',
		height: 170,
		width: 580,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('overflow','hidden');
			if ($j.browser.msie){
				$j(this).css('width','584px');
				$j(this).css('height', '130px');
			} else if ($j.browser.webkit){
				$j(this).css('width','580px');
				$j(this).css('height', '125px');
			} else {
				$j(this).css('width','580px');
				$j(this).css('height', '120px');
			}
		}
		});
	hidePopupCloseButton();
}

function scenePointsBalance(ctx){
	closeDialogIfOpen();
	var scenePointsBalanceURL = ctx + '/global/popups/scenePointsBalance.jsp';

	var frameInfo = '<iframe src="' + scenePointsBalanceURL + '" id="scenePointsBalanceDialog">';
	dialog = $j(frameInfo).dialog({
		title: 'Check my SCENE points balance',
		height: 170,
		width: 580,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('overflow','hidden');
			if ($j.browser.msie || $j.browser.webkit){
				$j(this).css('width','605px');
				$j(this).css('height','160px');
			} else {
				$j(this).css('width','580px');
				$j(this).css('height','160px');
			}
		}
		});
}

function sceneCardBalanceResult() {
	if (dialog){
		if ($j.browser.msie || $j.browser.webkit){
			dialog.css('height', '125px');
			dialog.css('width', '335px');
			$j(".ui-dialog").css('height', '125px');
			$j(".ui-dialog").css('width', '335px');
		} else {
			dialog.css('height', '144px');
			dialog.css('width', '330px');
			$j(".ui-dialog").css('height', '144px');
			$j(".ui-dialog").css('width', '330px');
		}
	}
}

function closeDialogIfOpen(){
	if (dialog){
		dialog.dialog('close');
	}
}

function viewTrailer(trailer, title){
	closeDialogIfOpen();
	var trailerUrl = 'http://www.totaleclips.com/Player/Splash.aspx?eclipid=' + trailer + '&vendorid=1021&playerid=19';

	var frameInfo = '<iframe src="' + trailerUrl + '" id="viewTrailerDialog">';
	title = title.replace("&apos;", "'");
	dialog = $j(frameInfo).dialog({
		title: title,
		height: 600,
		width: 580,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('overflow','hidden');
			$j(this).css('width', '580px');
		}
		});
}

function resetShoppingCart(url){
	closeDialogIfOpen();
	refreshCartCb(url)
}

function giftCardMessage(ctx, itemId, skuId){
	closeDialogIfOpen();
	var giftCardMessageURL = ctx + '/global/popups/giftCardMessage.jsp?itemId=' + itemId + '&skuId=' + skuId;

	var frameInfo = '<iframe src="' + giftCardMessageURL + '" id="giftCardMessageDialog">';
	dialog = $j(frameInfo).dialog({
		title: 'Add a personal message to your Cineplex Gift Card',
		height: 350,
		width: 580,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('overflow','hidden');
			if ($j.browser.msie){
				$j(this).css('width','585px');
				if (parseInt($j.browser.version)>=8){
					$j(this).css('height','248px');
				} else {
					$j(this).css('height','270px');
				}
			} else if ($j.browser.webkit){
				$j(this).css('width','585px');
				$j(this).css('height','270px');
			} else {
				$j(this).css('width','580px');
				$j(this).css('height','262px');
			}
		}
		});
}

function goTo(url){
	top.location = url;
}

function hidePopupCloseButton(){
	var closeThick = $j('.ui-icon-closethick');
	closeThick.css('display','none');
}

function showPopupCloseButton(){
	var closeThick = $j('.ui-icon-closethick');
	closeThick.css('display','block');
}

function showAddToCartPopup(ctx, skuId){
	var popupURL = ctx + '/global/popups/addedToCartPopup.jsp?skuId=' + skuId;
	doAddToCartPopup(popupURL)
}
function showAddGiftcardToCartPopup(ctx, skuId){
	var popupURL = ctx + '/global/popups/addedGiftcardToCartPopup.jsp?skuId=' + skuId;
	doAddToCartPopup(popupURL)
}

function doAddToCartPopup(popupURL){
	closeDialogIfOpen();
	var frameInfo = '<iframe src="' + popupURL + '" id="addToCartPopup">';
	dialog = $j(frameInfo).dialog({
		title: 'Item Added',
		height: 162,
		width: 466,
		modal: true,
		draggable: false,
		position: [400, 100],
		open: function(){
			$j(this).css('overflow','hidden');
			if ($j.browser.msie ){
				$j(this).css('width','470px');
				$j(this).css('height', '167px');
			} else {
				$j(this).css('width','466px');
				$j(this).css('height', '162px');
			}
		}
		});
	hidePopupCloseButton();
}

function loginPopupToCartMerged(){
	if (dialog){
		if ($j.browser.msie || $j.browser.webkit){
			dialog.css('height', '130px');
		} else {
			dialog.css('height', '125px');
		}
		dialog.css('width', '470px');
		$j(".ui-dialog").css('width', '470px');
	}
	$j('#ui-dialog-title-loginDialog').html('Your cart has been modified');
	hidePopupCloseButton();
	
}

function showCartMergedPopup(ctx, skuId){
	closeDialogIfOpen();
	var popupURL = ctx + '/global/popups/cartMergedNotification.jsp';
	
	var frameInfo = '<iframe src="' + popupURL + '" id="cartMergedDialog">';
	dialog = $j(frameInfo).dialog({
		title: 'Cart Merged',
		height: 125,
		width: 466,
		modal: true,
		draggable: false,
		position: ['middle','middle'],
		open: function(){
			$j(this).css('overflow','hidden');
			if ($j.browser.msie || $j.browser.webkit){
				$j(this).css('width','470px');
				$j(this).css('height', '130px');
			} else {
				$j(this).css('width','466px');
				$j(this).css('height', '125px');
			}
		}
		});
}

function showPrintableTerms(context){
	window.open(context + "/checkout/termsPrintable.jsp");
}

function verifyOrderTotal(orderTotal, orderLimit) {
	if (orderTotal > orderLimit) {
		top.jAlert('Your order is over the order limit ($' + orderLimit + ').  Please reduce your order.', 'Alert');
		return false;
	}
	return true;
}

/* // OLD CODE - for reference
// change an iframe's src
function changeFrame(frameId, url) {
	var oIframe = document.getElementById([frameId]);
	oIframe.src = url;
}

// put an image over sifr elements
function getPositionObject(obj) {
	var obj = $(obj);
	var o = function(){var curtop = 0; var curleft = 0;};
	o.curleft = 0;
	o.curtop = 0;
	if (obj.offsetParent) {
		o.curleft = obj.offsetLeft
		o.curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			o.curleft += obj.offsetLeft
			o.curtop += obj.offsetTop
		}
	}
	return o;
}

function createDiv(itm) {
	if (!explorer) {
		var itm = $(itm).id;
		var e = $('container');
		var p = getPositionObject(itm); 
		var d = Element.getDimensions(itm);
		var w = d.width+'px'; var h = d.height+'px';
		var l = p.curleft+'px'; var t = p.curtop+'px';
		var lnk = $(itm+'_alternate').select('a');
		if(lnk != "") { var img = '<a href="'+lnk+'"><img src="/store/images/common/spacer.gif" style="position:relative; width:'+w+'; height:'+h+'; border:solid 0px #666;" /></a>'; }
		else {var img = '<img src="/store/images/common/spacer.gif" style="position:relative; width:'+w+'; height:'+h+'; border:solid 0px #666;" />'; }
		Insertion.Top(e,'<div style="position:absolute; left:'+l+'; top:'+t+'; width:'+w+'; height:'+h+'; z-index:100000;">'+img+'</div>');
	}
}

// hide the search tips over the textbox in the header and set the focus on the textbox
function searchBox() {
	$('examples').hide();
	$('txt_searchBox').focus();
}

// hide and show the lightbox
function lightBox(url, title) {
	var lbPos = (document.viewport.getDimensions().height / 2).ceil() + document.viewport.getScrollOffsets().top - 340 + 'px';
	$('lb_player').setStyle({'top':lbPos});
	if ($('lb_background').visible()) {
		top.changeFrame('moviePlayer', '/store/html/emptyLB.html');
		$('lb_title').innerHTML = '';
		Effect.Fade('lb_player', {duration:0.5, from:1, to:0, queue: {position:'end', scope:'lightbox', limit:2}});
		Effect.Fade('lb_background', {duration:1.0, from:0.92, to:0, queue: {position:'end', scope:'lightbox', limit:2}});
		
		if (explorer){
			var selList = $$('select');
			for(i=0;i<selList.length;i++){
				mySel = selList[i];
				mySel.show();
			}
		}
		
		
	}
	else {
		//alert(lbPos);
		var url = url.replace(/&amp;/g,'&');
		top.changeFrame('moviePlayer', url);
		$('lb_title').innerHTML = title;
		Effect.Appear('lb_background', {duration:1.0, from:0, to:0.92, queue: {position:'end', scope:'lightbox', limit:2}});
		Effect.Appear('lb_player', {duration:0.5, from:0, to:1, queue: {position:'end', scope:'lightbox', limit:2}});
		if (explorer){
			var selList = $$('select');
			for(i=0;i<selList.length;i++){
				mySel = selList[i];
				mySel.hide();
			}
		}
	}
}

// hide and show the lightbox
function openLightbox3(whichLB, url, title) {
	var lbPos = (document.viewport.getDimensions().height / 2).ceil() + document.viewport.getScrollOffsets().top - 340 + 'px';
	var playerL = $$('#' + whichLB  + " .player");
	playerL[0].setStyle({'top':lbPos});
	top.changeFrame('loginBox2', url);
	$('lb_loginTitle2').innerHTML = title;
	var th = $('container').getHeight() + 'px';
	$('lb_background2').setStyle({'height':th});
	
	$(whichLB).setStyle({'display':'block'});
	
	if (explorer){
		var selList = $$('select');
		for(i=0;i<selList.length;i++){
			mySel = selList[i];
			mySel.hide();
		}
	}
	
	
}


function closeLightboxIframe(whichLB){
	top.document.getElementById(whichLB).hide();
	top.changeFrame('loginBox2', '/store/html/emptyLB.html');
	
}

function closeLightbox3(whichLB){
	$(whichLB).setStyle({'display':'none'});
	top.changeFrame('loginBox2', '/store/html/emptyLB.html');
	if (explorer){
		var selList = $$('select');
		for(i=0;i<selList.length;i++){
			mySel = selList[i];
			mySel.show();
		}
	}
}

function lightBoxLogin(url, title) {
	var lbPos = (document.viewport.getDimensions().height / 2).ceil() + document.viewport.getScrollOffsets().top - 340 + 'px';
	$('lb_login').setStyle({'top':lbPos});
	if ($('lb_loginBackground').visible()) {
		//top.changeFrame('loginBox', '/store/html/emptyLB.html');
		$('lb_loginTitle').innerHTML = '';
		// location.reload(true);
		Effect.Fade('lb_login', {duration:0.5, from:1, to:0, queue: {position:'end', scope:'lightboxLogin', limit:2}});
		Effect.Fade('lb_loginBackground', {duration:0.5, from:0.92, to:0, queue: {position:'end', scope:'lightboxLogin', limit:2}});
		//to show select
		if (explorer){
			var selList = $$('select');
			for(i=0;i<selList.length;i++){
				mySel = selList[i];
				mySel.show();
			}
		}
	}
	else {
		top.changeFrame('loginBox', url);
		$('lb_loginTitle').innerHTML = title;
		Effect.Appear('lb_loginBackground', {duration:0.5, from:0, to:0.92, queue: {position:'end', scope:'lightboxLogin', limit:2}});
		Effect.Appear('lb_login', {duration:0.5, from:0, to:1, queue: {position:'end', scope:'lightboxLogin', limit:2}});
		//to hide select
		if (explorer){
			var selList = $$('select');
			for(i=0;i<selList.length;i++){
				mySel = selList[i];
				mySel.hide();
			}
		}
	}
}


function lightBoxCart(url, title) {
  // add lightboxCart to the class for the light box login container
  $('lbLogin').addClassName('lightboxCart');

  // create light box
  return lightBoxLogin(url, title);
}

function closeLightbox(whichLightbox){
	Effect.Fade(whichLightbox, {duration:0.5, from:1, to:0, queue: {position:'end', scope:'lightboxLogin', limit:2}});
	Effect.Fade('lb_loginBackground', {duration:0.5, from:0.92, to:0, queue: {position:'end', scope:'lightboxLogin', limit:2}});
}

function postLightBoxLogin() {
	lightBoxLogin();
	location.href = location.href;
}

function postLightBoxCheckout() {
	lightBoxLogin();
	$('cart').submit();
}

function postShoppingCart() {
	lightBoxLogin();
	location.href = "/store/cart/shoppingCart.jsp";
}

function doRefresh() {
	document.billingForm.submit();
}

function doShippingRefresh() {
	document.shippingForm.submit();
}

function sifrLoad() {
	//sIFR.replace(imagoBook,{selector:'.content .right .cart p',transparent:'true',css:['.sIFR-root{color:#ffffff; font-size:17px; leading:0px;}, a{color:#ffffff; text-decoration:none;}, a:hover{color:#ffffff; text-decoration:underline;}'],onReplacement:function(obj){createDiv(this);}});
	//sIFR.replace(imagoMed,{selector:'.content .right .context p.title',transparent:'true',css:['.sIFR-root{color:#747474; font-size:14px; leading:0px;},a{color:#747474; text-decoration:none;},a:hover{color:#747474; text-decoration:underline;}'],onReplacement:function(obj){createDiv(this);}});
	//sIFR.replace(imagoBook,{selector:'.content .right .context .infos p',transparent:'true',css:['.sIFR-root{color:#747474; font-size:14px; leading:0px;},a{color:#747474; text-decoration:none;},a:hover{color:#747474; text-decoration:underline;}'],onReplacement:function(obj){createDiv(this);}});
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=800,height=600,left = 440,top = 225');");
}
*/
