var actionBase;
var MAX_Z_INDEX = 2147483648;
var hideAds = function(){};
var showAds = function(){};
var dom = YAHOO.util.Dom;
//modal dialog setup
var peModalDialog = null;
//Depends on menuPiczo.jsp
function setModalTitleToIFrameTitle(backupTitle) {
	try {
		var newTitle = document.getElementById("modalIFrame").contentWindow.document.title;
		if (newTitle != undefined && trim(newTitle) != "")
			document.getElementById("modalTitle").innerHTML = newTitle.toUpperCase();
		else
			document.getElementById("modalTitle").innerHTML = backupTitle.toUpperCase(); 
	} catch (e) {
		document.getElementById("modalTitle").innerHTML = backupTitle.toUpperCase();
	}
}
//Depends on YUI
var modalShown = false;
function showModalDialog(url, width, height, backupTitle) {
	var panelWidth = width+"px";
	var iframeWidth = "100%";
	if (width > dom.getViewportWidth()) {
		panelWidth = (dom.getViewportWidth() - 50) +"px";
		//iframeWidth = (document.body.clientWidth - 75) +"px";
	} 
	var panelHeight = height+"px";
	var iframeHeight="100%";
	if (height > dom.getViewportHeight()) {
		panelHeight = (dom.getViewportHeight() - 50) + "px";
	}
	iframeHeight = (parseInt(panelHeight) - 30) + "px";
	if (width != null && height != null) {
		peModalDialog = new YAHOO.widget.Panel("peModalDialog", {width: panelWidth, height: panelHeight, fixedcenter:true,  close:false,  draggable:false,  modal:true,  visible:false,  zIndex: 2147483647 } );
		peModalDialog.setHeader('<a id="modalX" class="modalDialogClose" href="javascript:void(0)">&nbsp;</a><span id="modalTitle">Loading...</span>');
		peModalDialog.setBody('<iframe onload="setModalTitleToIFrameTitle(\''+backupTitle+'\')" id="modalIFrame" style="width: '+iframeWidth+'; height: '+iframeHeight+'; margin:0px;" scrolling="auto" frameborder="0" src=""></iframe>');
		peModalDialog.render(document.body);
		peModalDialog.element.style.display="block";
		peModalDialog.show();
		YAHOO.util.Event.addListener("modalX", "click", hideModalDialog, peModalDialog, true);
		document.getElementById("modalIFrame").src=url;
	} else {
		var mif = document.createElement("iframe");
		mif.id="modalIFrame";
		var mifHolder = document.getElementById("mifHolder");
		if (!mifHolder) {
			mifHolder = document.createElement("div");
			mifHolder.id="mifHolder";
			mifHolder.style.visibility="hidden";
			document.body.appendChild(mifHolder);
		}
		mifHolder.appendChild(mif);
		var mifLoadCheckInterval = setInterval(function(mif) {
			if (mif.contentWindow.finished) {
				clearInterval(mifLoadCheckInterval);
				mif.width = mif.contentWindow.document.body.scrollWidth+20;
				mif.height = mif.contentWindow.document.body.scrollHeight+20;
				peModalDialog = new YAHOO.widget.Panel("peModalDialog", {width: mif.width, height: mif.height, fixedcenter:true,  close:false,  draggable:false,  modal:true,  visible:false,  zIndex: 2147483647 } );
				peModalDialog.setHeader('<a id="modalX" class="modalDialogClose" href="javascript:void(0)">&nbsp;</a><span id="modalTitle">Loading...</span>');
				peModalDialog.setBody(mifHolder.innerHTML);
				peModalDialog.render(document.body);
				peModalDialog.element.style.display="block";
				peModalDialog.show();
				parent.setModalTitleToIFrameTitle(backupTitle);
				parent.YAHOO.util.Event.addListener("modalX", "click", hideModalDialog, peModalDialog, true);
			}
			
		}, 500, mif);
		mif.src=url;

	}
	modalShown = true;
	try {
		hideAds();
	} catch (e) {
		//hideAds failed because ad frame has probably not loaded yet
		top.onLoadHideAds = true;
	}

}
// Depends on YahooUI
function showModalDialog2(content, header, width, height, hideOnClose) {
	var panelWidth = width+"px";
	if (width > dom.getViewportWidth()) {
		panelWidth = (dom.getViewportWidth() - 50) +"px";
	} 
	var panelHeight = height+"px";
	if (height > dom.getViewportHeight()) {
		panelHeight = (dom.getViewpoerHeight() - 50) + "px";
	}
	if (!hideOnClose) {
		hideOnClose = false;
	}
	peModalDialog = new YAHOO.widget.Panel("peModalDialog", {width: panelWidth, height: panelHeight, fixedcenter:true,  close:!hideOnClose,  draggable:false,  modal:true,  visible:false,  zIndex: 2147483647 } );

	if (!hideOnClose) {
		if (header) {
			peModalDialog.setHeader(header);
		}
	} else {
		if (!header) {
			header="";
		}
		peModalDialog.setHeader('<a id="modalX" class="modalDialogClose" href="javascript:void(0)">&nbsp;</a><span id="modalTitle">'+header+'</span>');
		YAHOO.util.Event.addListener("modalX", "click", hideModalDialog, peModalDialog, true);
	}

	peModalDialog.setBody(content);
	peModalDialog.render(document.body);
	peModalDialog.element.style.display="block";
	peModalDialog.show();
	try {
		hideAds();
	} catch (e) {
		//hideAds failed because ad frame has probably not loaded yet
		top.onLoadHideAds = true;
	}
	modalShown = true;
}
//Depends on YahooUI
function hideModalDialog() {
	modalShown = false;
	showAds();
	peModalDialog.element.style.display="none";
	peModalDialog.hide();
	peModalDialog.element.parentNode.removeChild(peModalDialog.element);
	peModalDialog = null;
}

//dynamic image sizing
function sizeImage(img, maxDim, display) {
	var ratio = img.height / img.width;
   	if (img.width > img.height) {
   		img.width = maxDim;
   		img.height = ratio * maxDim;
   	} else {
   		img.height = maxDim;
   		img.width = maxDim / ratio;
   	}
   	if (display != null)
   		img.style.display=display;

}
//@Piczo menu
function swapDropdownBg(id, hexColor) {
	document.getElementById(id).style.backgroundColor = hexColor;
}
var showTimer = null;
var hideTimer = null;
function showAtPiczo() {
	try {
		if (top != self && top.showAtPiczo != null) {
				top.showAtPiczo();
				return;
		}
	} catch (e) {
	}
	clearTimeout(hideTimer);
	clearTimeout(showTimer);
		showTimer = setTimeout( function() {
			var atPiczoDropDown = document.getElementById("atPiczoDropDown");
			var atPiczo = document.getElementById("atpiczo");
			if (atPiczo == null && em != null) {
				atPiczo = em.document.getElementById("atpiczo");
			}
			atPiczoDropDown.style.top = dom.getY(atPiczo)+25 + "px";
			atPiczoDropDown.style.left = dom.getX(atPiczo)-9 +"px";			
			atPiczoDropDown.style.display="block";

		}, 200);

}
function hideAtPiczo() {
	try {
		if (top != self && top.hideAtPiczo != null) {
			top.hideAtPiczo();
			return;
		}
	} catch (e) {
	}
	clearTimeout(showTimer);
	hideTimer = setTimeout( function() {
		var atPiczoDropDown = document.getElementById("atPiczoDropDown");
		atPiczoDropDown.style.display="none";
	}, 200);
}

function showPiczoLanguage() {
	if (top != self && top.showAtPiczo != null) {
		top.showPiczoLanguage();
		return;
	}
	clearTimeout(hideTimer);
	clearTimeout(showTimer);
		showTimer = setTimeout( function() {
			var piczoLanguageDropDown = document.getElementById("piczoLanguageDropDown");
			var piczoLanguageSelected = document.getElementById("piczoLanguageSelected");
			if (piczoLanguageSelected == null && em != null) {
				piczoLanguageSelected = em.document.getElementById("piczoLanguageSelected");
			}
							
			var centeredTableParent = document.getElementById("Content");
			var leftOffset = 0;
			if (centeredTableParent && centeredTableParent.align == "center"
				&& document.body.clientWidth > parseInt(centeredTableParent.style.width)) {
				leftOffset = -10 + ((document.body.clientWidth - parseInt(centeredTableParent.style.width)) /2);
			}
			
			if (! piczoLanguageSelected.offsetParent || piczoLanguageSelected.offsetParent.tagName == "TD") { //firefox
				piczoLanguageDropDown.style.top = dom.getY(piczoLanguageSelected) + 18 +"px";
				piczoLanguageDropDown.style.left= dom.getX(piczoLanguageSelected) - 9 + "px";
			} else {
				var totalTop = piczoLanguageSelected.offsetTop;
				var totalLeft = piczoLanguageSelected.offsetLeft;
				var p = piczoLanguageSelected;
				while ( p = p.offsetParent) {
					if (p.tagName != "BODY") {
						totalTop += p.offsetTop;
						totalLeft += p.offsetLeft;
					} else {
						break;
					}
				}

				piczoLanguageDropDown.style.top = totalTop + 18 +"px";
				piczoLanguageDropDown.style.left = totalLeft - 9 +"px";

			}			
			piczoLanguageDropDown.style.display="block";
				
		}, 200);

}
function hidePiczoLanguage(){

	if (top != self && top.hidePiczoLanguage != null) {
		top.hidePiczoLanguage();
		return;
	}
	clearTimeout(showTimer);
	hideTimer = setTimeout( function() {
		var piczoLanguageDropDown = document.getElementById("piczoLanguageDropDown");
		piczoLanguageDropDown.style.display="none";
	}, 200);

}



//cookie handling courtesy W3Schools
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
var cookie=c_name+ "=" +escape(value)+ ";path=/;domain=.piczo.com" +
((expiredays==null) ? "" : (";expires="+(expiredays < 0 ? "-1" : exdate.toGMTString())));
document.cookie = cookie;
}
function trim(str) {
	return str.replace(/^\s*|\s*$/g,"");	
}
function abbrev(str, maxlen) {
	if (str.length <= maxlen) {
		return str;
	} else {
		return str.substring(0,maxlen-3)+"...";
	}
}
function openPopup(url, name, w, h, toolbar, scrollbars, location, resizable) {
	if (toolbar == null) {
		toolbar = "no";
	}
	if (scrollbars == null) {
		scrollbars = "yes";
	}
	if (location == null) {
		location = "no";
	}
	if (resizable == null) {
		resizable = "yes";
	}
	if (isNaN(w) && w.indexOf("%") > 0) {
		w = screen.width * parseInt(w)/100;
	}
	if (isNaN(h) && h.indexOf("%") > 0) {
		h = screen.height * parseInt(h)/100;
	}

	var newPopup = window.open ( url, name, 
	    "toolbar="+toolbar+",scrollbars="+scrollbars+",location="+location+",resizable="+resizable+",width=" + w + ",height=" + h + "," +
	    "top=" + ((screen.height-h)/2) + ",left=" + ((screen.width-w)/2));
	if (newPopup != null) {
		newPopup.focus();
	}
	return newPopup;
}
var closingPopups = [];
var ajaxRunning = false;
function closePopup(popupToClose) {
	if (ajaxRunning) {
		closingPopups.push(popupToClose);
	} else {
		popupToClose.close();
	}
}
//get the only child of the html element. If more than one child, return null. Ignore empty text and all comment children
function getOnlyChild(element) {
	var childNodes = element.childNodes;
	if (childNodes.length == 1) {
		return childNodes[0];
	}
	var onlyChild = null;
	for (var i = 0; i < childNodes.length; i++) {
		var node = childNodes[i];
		if (node.nodeType == 3) { //text node
			if(trim(node.nodeValue) == "") {
				continue;
			}
		} else if (node.nodeType == 8) { //comment
			continue;
		}
		if (onlyChild == null) {
			onlyChild = node;
		} else {
			return null;
		}
	}
	return onlyChild;
	
}
var iframeIdsInYUIPopups = ["modalIFrame","picList","uploadFrame","emailFriendsPopupIframe", "_yuiResizeMonitor"];

var originalEmbedWmodes = [];
var originalObjectWmodes = [];
function hideAds() {
    	var embeds = document.getElementsByTagName("embed");
    	for (var i = 0; i < embeds.length; i++) {
    		embeds[i].style.visiblity="hidden";
    		embeds[i].parentNode.style.visibility="hidden";
    		if (embeds[i].attributes != undefined) {
    			if (embeds[i].getAttribute("wmode") != undefined) {
    				originalEmbedWmodes[i] = embeds[i].getAttribute("wmode");
    			}
    			embeds[i].setAttribute("wmode", "opaque");
    		}
    	}
    	var objects = document.getElementsByTagName("object");
    	for (var i = 0; i < objects.length; i++) {
    		objects[i].parentNode.style.visibility="hidden";
    		if (objects[i].wmode != undefined) {
    			originalObjectWmodes[i] = objects[i].wmode;
    			objects[i].wmode = "opaque";
    		}
    	}
    	
    	//hide non-login popup iframes
		var iframes = document.getElementsByTagName("iframe");
		for (var i = 0; i < iframes.length; i++) {
			var hiding = true;
			for (var j = 0; j < iframeIdsInYUIPopups.length && hiding; j++) {
				if (iframes[i].id == iframeIdsInYUIPopups[j]) {
					hiding = false;
				}
			}
			if (hiding) {
				iframes[i].style.visibility = "hidden";
			}
		}
}
function showAds(exceptionId) {
	   	var embeds = document.getElementsByTagName("embed");
    	for (var i = 0; i < embeds.length; i++) {
    		embeds[i].style.visibility="visible";
    		embeds[i].parentNode.style.visibility="visible";
    		if (embeds[i].attributes != undefined && originalEmbedWmodes[i] != null) {
    			embeds[i].setAttribute("wmode", originalEmbedWmodes[i]);
    		}
    	}
    	var objects = document.getElementsByTagName("object");
    	for (var i = 0; i < objects.length; i++) {
    		objects[i].parentNode.style.visibility="visible";
    		if (objects[i].wmode != undefined) {
    			objects[i].wmode = originalObjectWmodes[i];
    		}
    	}

		var iframes = document.getElementsByTagName("iframe");
		for (var i = 0; i < iframes.length; i++) {
			var showing = true;
			for (var j = 0; j < iframeIdsInYUIPopups.length && showing; j++) {
				if (iframes[i].id == iframeIdsInYUIPopups[j]) {
					showing = false;
				}
			}
			if (exceptionId != null && iframes[i].parentNode.id == exceptionId) {
				showing = false;
			}
			if (showing) {
				iframes[i].style.visibility = "visible";
			}
		}
}
function safeTextForHtmlAttribute(text) {
	return text.replace(/"/g, "&quot;");
}

