/* This file contains various javascript functions used by preview.aspx and cart.aspx */

function checkFrameStatus(lbProtect)
{
    if (window.parent.myLytebox == null)
    {
	alert("This page must cannot be viewed directly. Please allow the gallery page to load before proceeding.");
	history.go(-1);
    }
    else
    {
	var objMain = window.parent.document.getElementById('lbMain');
	var objOverlay = window.parent.document.getElementById('lbOverlay');
	if (lbProtect)
	{
	    objMain.onclick = function(e) {  mainClick(e,lbProtect); }
	    objOverlay.onclick = function() { if (confirm("Close cart and continue shopping?")) {window.parent.myLytebox.end(); return false;} }
	    objMain.style.cursor = "not-allowed";
	    objOverlay.style.cursor = "not-allowed";
	}
	else
	{
	    objMain.onclick = function(e) { mainClick(e,lbProtect); }
	    objOverlay.onclick = function() { window.parent.myLytebox.end(); return false; }
	    objMain.style.cursor = "default";
	    objOverlay.style.cursor = "default";
	}
    }
}

var speed = 2000;
var control = 1;

function flashStatus()
{
    if (control == 1)
    {
	window.parent.status = "Please wait while we submit your order to PayPal";
	control=0;
    }  
    else
    {
	window.parent.status="";
	control=1;
    }
    setTimeout("flashStatus();",speed);
}

function setOpacity(id, opacity)
{
    var object = window.parent.document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + (opacity + 10) + ")";
}

function mainClick(e,lbProtect)
{
    if (!e)
    {
	if (window.parent.parent.frames[window.name] && (window.parent.document.getElementsByTagName('frameset').length <= 0))
	    e = window.parent.parent.window.event;
	else 
	    e = window.parent.event;
    }
    var id = (e.target ? e.target.id : e.srcElement.id);
    if (id == 'lbMain')
    {
	if (lbProtect)
	{
	    if (confirm("Close cart and continue shopping?"))
		window.parent.myLytebox.end();
	}
	else
	    window.parent.myLytebox.end();
    }
	return false;
}

function resizeParent(img)
{
    img.style.height = "auto";
    var newWidth;
    var newHeight;
    var resizeAdjacent;
    var lbCaption = window.parent.document.getElementById("lbCaption");
    var lbClose = window.parent.document.getElementById("lbClose");
    var caption = "";
    var brLoc = -1;
    var minWidth = 340;

    caption = lbCaption.innerHTML;

    brLoc = caption.indexOf("<BR>")
    caption = caption.substring(0,brLoc);

    lbCaption.style.width = "auto";
    lbCaption.style.whiteSpace = "nowrap";

    if (lbCaption.offsetWidth > screen.availWidth*0.8)
    {
	lbCaption.style.whiteSpace = "normal";
	lbCaption.style.width = screen.availWidth*0.8 + "px";
    }

    if (img.className == "preview")
    {
	minWidth = Math.max(minWidth, lbCaption.offsetWidth + lbClose.offsetWidth + 15);
	newWidth = Math.max(img.width + 2,minWidth);
	if (img.width > img.height)
	{
	    newHeight = img.height + 24;
	    newHeight = Math.max(newHeight,Math.min(newWidth,200));
	}
	else
	{
	    newHeight = img.height + 24;
	    newHeight =  Math.max(newHeight,Math.min(400,img.height*1.5));
	}
	resizeAdjacent = true;
    }
    else
    {
	newWidth = 640;
	newHeight = 480;
	resizeAdjacent = false;
    }
    doResize(newWidth, newHeight,true);
    document.body.style.overflowX = 'hidden';
}


function doResize(newWidth, newHeight, resizeAdjacent)
{
	var lyteBox = window.parent.myLytebox;
	var iFrame = lyteBox.doc.getElementById('lbIframe');
	if (lyteBox.frameArray[lyteBox.activeFrame][2] != "width: " + newWidth + "px; height: " + newHeight + "px; scrolling: auto;")
	{
		iFrame.width = newWidth;
		iFrame.height = newHeight;

		lyteBox.frameArray[lyteBox.activeFrame][2] = "width: " + newWidth + "px; height: " + newHeight + "px; scrolling: auto;";
		if (lyteBox.frameArray.length > 1)
		{
		  if (resizeAdjacent && lyteBox.activeFrame < lyteBox.frameArray.length - 1)
		    if (lyteBox.frameArray[lyteBox.activeFrame+1][2] == lyteBox.defRev)
			lyteBox.frameArray[lyteBox.activeFrame+1][2] = "width: " + newWidth + "px; height: " + newHeight + "px; scrolling: auto;";
		  if (resizeAdjacent && lyteBox.activeFrame > 0)
		    if (lyteBox.frameArray[lyteBox.activeFrame-1][2] == lyteBox.defRev)
			lyteBox.frameArray[lyteBox.activeFrame-1][2] = "width: " + newWidth + "px; height: " + newHeight + "px; scrolling: auto;";
		}

		lyteBox.wCur = lyteBox.doc.getElementById('lbOuterContainer').offsetWidth;
		lyteBox.hCur = lyteBox.doc.getElementById('lbOuterContainer').offsetHeight;
		lyteBox.xScale = ((newWidth  + (lyteBox.borderSize * 2)) / lyteBox.wCur) * 100;
		lyteBox.yScale = ((newHeight  + (lyteBox.borderSize * 2)) / lyteBox.hCur) * 100;
		var wDiff = (lyteBox.wCur - lyteBox.borderSize * 2) - newWidth;
		var hDiff = (lyteBox.hCur - lyteBox.borderSize * 2) - newHeight;
		if (!(hDiff == 0)) {
			lyteBox.hDone = false;
			lyteBox.resizeH('lbOuterContainer', lyteBox.hCur, newHeight + lyteBox.borderSize*2, lyteBox.getPixelRate(lyteBox.hCur, newHeight));
		} else {
			lyteBox.hDone = true;
		}
		if (!(wDiff == 0)) {
			lyteBox.wDone = false;
			lyteBox.resizeW('lbOuterContainer', lyteBox.wCur, newWidth + lyteBox.borderSize*2, lyteBox.getPixelRate(lyteBox.wCur, newWidth));
		} else {
			lyteBox.wDone = true;
		}
		if ((hDiff == 0) && (wDiff == 0)) {
			if (lyteBox.ie){ lyteBox.pause(250); } else { lyteBox.pause(100); } 
		}
		lyteBox.doc.getElementById('lbPrev').style.height = newHeight + "px";
		lyteBox.doc.getElementById('lbNext').style.height = newHeight + "px";
		lyteBox.doc.getElementById('lbDetailsContainer').style.width = (newWidth + (lyteBox.borderSize * 2) + (lyteBox.ie && lyteBox.doc.compatMode == "BackCompat" && lyteBox.outerBorder ? 2 : 0)) + "px";
	}
}
