//togglePopup
//Displays/Hides a popup
//Inputs:
//  Name            The name of the layer you have created
function togglePopup(name) {
	var popup = document.getElementById(name);
	 
	if (popup.style.visibility == "visible") {
		popup.style.visibility = "hidden";
		popup.style.display = "none";
	} else {
		popup.style.visibility = "visible";
		popup.style.display = "inline";
	}
}

//changeUrl(name, url)
//Changes the url of the iframe in the popup
//Inputs:
//  name        The name of the layer you created (must have inline src content to use this function);
//  url         the file or url you wish to display in the popup
function changeUrl(name, url) {
   name = "iframe_"+name;
   frames[name].location.href = url;
}
 

var popupOriginPositions = new Array();

function getScrollWidth() {
	var w = window.pageXOffset ||
		document.body.scrollLeft ||
		document.documentElement.scrollLeft;

	return w ? w : 0;
}

function getScrollHeight() {
	var h = window.pageYOffset ||
		document.body.scrollTop ||
		document.documentElement.scrollTop;

	return h ? h : 0;
}

function setPopupPosition(name) {
	var popup = document.getElementById(name);

	LeftPosition = (screen.width) ? (screen.width-350)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-350)/2 : 0;
	popup.style.top = TopPosition + "px";
	popup.style.left = LeftPosition + "px";

	popup.style.top = (popupOriginPositions[name]["top"] + getScrollHeight()) + 265 + "px";
	popup.style.left = (popupOriginPositions[name]["left"] + getScrollWidth()) + 250 + "px";
}

//object to be referenced for href redirection
var obj = null;


 //showPopup(name)
 //Shows the popup by name
 function fShowPopup(pParam1) {
	var vName = 'WindowDiv';
	 var popup = document.getElementById(vName);
	 if (!popupOriginPositions[vName]) {
		 popupOriginPositions[vName] = new Array();
		 popupOriginPositions[vName]["top"] = (isNaN(parseInt(popup.style.top))) ? 0 : parseInt(popup.style.top);
		 popupOriginPositions[vName]["left"] = (isNaN(parseInt(popup.style.left))) ? 0 : parseInt(popup.style.left);
	 }
	 
	 setPopupPosition(vName);
     popup.style.visibility = "visible";    
     popup.style.display = "inline";

	 // :NOTE: please do not put page specific JS code in here, it will cause errors on pages
	 //        that display popups but do not have the same HTML elements and/or included functions

	 // make sure we are resetting a login popup,
	 // removing this check will break 4x8 cards, photogifts and 
	 // possibly other pages we haven't noticed yet
	resetPopup(pParam1);

}
//pParam1 = value
//pParam2 = URL
//pParam3 = Targets
//pParam4 = Field
//pParam5 = Method

function resetPopup(pParam1) {
	
	document.getElementById("WindowDivContent").innerHTML = "";
	document.getElementById("WindowDiv").style.height = "200px";

	document.getElementById("WindowDivTitleLabel").innerHTML = "ADD TO CART";
	fSendInfos('-1','http://www.montrealsalsaconvention.com/en/add-products.php?ProductId='+pParam1,'WindowDivContent','-1','GET');
	document.getElementById("WindowDiv").style.height = "200px";
	document.getElementById("WindowDiv").style.width = "650px";

	document.getElementById("WindowDiv").style.left = getScrollHeight() + 250 + "px";
	document.getElementById("WindowDiv").style.top = getScrollWidth() + 150 + "px";
	//document.getElementById("WindowDiv").style.filter='alpha(opacity=95)';
	//document.getElementById("WindowDiv").style.opacity='0.95'; 
	document.getElementById("WindowDiv").style.zIndex='2'; 
}

//hidePopup(name)
 //Hides the popup by name
function hidePopup(name) {
	var popup = document.getElementById(name);
	popup.style.visibility = "hidden";    
	popup.style.display = "none";
}

 
function hidePopupAndBlank(name) {
	var popup = document.getElementById(name);
	
	popup.style.visibility = "hidden";    
	popup.style.display = "none";
	changeUrl(name, "blank.htm");
}
//changeUrl(name, url)
//change the url of the iframe and maintain the visibility of the iframe
//Inputs:
//  name        The name of the layer you created (must have inline src content to use this function);
//  url         the file or url you wish to display in the popup
function changeUrlAndShow(name, url) {
	var popup = document.getElementById(name);
	frames["iframe_"+name].location.href = url;
	popup.style.visibility = "visible";    
	popup.style.display = "inline";
}

//drag functions
var TitleBar = document.getElementById('WindowDivTitle');
var WindowPopUp = document.getElementById('WindowDiv');



var dragging = false;
var oldx = 0;
var oldy = 0;
var oldmousemove = document.onmousemove;
var isIE=document.all;

function newmousemove (e) {
	if (!isIE) event = e;

	var offsetx = oldx - event.clientX;
	var offsety = oldy - event.clientY;
	oldx = event.clientX;
	oldy = event.clientY;
	var newleft = parseInt(WindowPopUp.style.left) - offsetx;
	var newtop =  parseInt(WindowPopUp.style.top) - offsety;

	WindowPopUp.style.left = newleft + "px";
	WindowPopUp.style.top = newtop + "px";

	return false;
}

function newmousedown (e) {
	if (!isIE) event = e;

	dragging = true;
	document.onmousemove = newmousemove;
	
	oldx = event.clientX;
    oldy = event.clientY;
	
	return false;
}

function newmouseup () {
	dragging = false;
	document.onmousemove = oldmousemove;
	return false;
}

if (TitleBar != null) {
    TitleBar.onmouseup = newmouseup;
    TitleBar.onmousedown = newmousedown;
}
// end drag functions