function deleteLightbox(message) {
	if (message == null){
		message = "Are you sure you wish to delete the current lightbox?";
	}
	if (confirm(message)) {
		location.href="lightbox.do?action=deleteLightbox";
	}
}
function showImageInfoBar(bgUrl, infobarDiv){
	document.getElementById(infobarDiv).style.background = "url("+bgUrl+") 0px 0px no-repeat";
}
function hideImageInfoBar(infobarDiv){
	document.getElementById(infobarDiv).style.background = "#FFF";
}
function preview(query, imgServlet, zoomServlet, imageId) {
	q="";
	q += "./imagePreview.do?";
	q += "bvImageQuery=" + encodeURIComponent(query);
	q += "&bvImageServlet=" + encodeURIComponent(imgServlet);
	q += "&bvZoomServlet=" + encodeURIComponent(zoomServlet);
	q += "&imageId=" + encodeURIComponent(imageId);
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(q,"winPreview",helpStr);
}
function previewHere(query, imgServlet, zoomServlet, imageId) {
	q="";
	q += "./imagePreview.do?";
	q += "bvImageQuery=" + encodeURIComponent(query);
	q += "&bvImageServlet=" + encodeURIComponent(imgServlet);
	q += "&bvZoomServlet=" + encodeURIComponent(zoomServlet);
	q += "&imageId=" + encodeURIComponent(imageId);
	document.location.href=q;
}
function previewSubscription(query, imgServlet, zoomServlet, imageId) {
	q="";
	q += "./imagePreviewSubscription.do?";
	q += "bvImageQuery=" + encodeURIComponent(query);
	q += "&bvImageServlet=" + encodeURIComponent(imgServlet);
	q += "&bvZoomServlet=" + encodeURIComponent(zoomServlet);
	q += "&imageId=" + encodeURIComponent(imageId);
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(q,"winPreview",helpStr);
}
function previewHereSubscription(query, imgServlet, zoomServlet, imageId) {
	q="";
	q += "./imagePreviewSubscription.do?";
	q += "bvImageQuery=" + encodeURIComponent(query);
	q += "&bvImageServlet=" + encodeURIComponent(imgServlet);
	q += "&bvZoomServlet=" + encodeURIComponent(zoomServlet);
	q += "&imageId=" + encodeURIComponent(imageId);
	document.location.href=q;
}
function showAlamyImageDetails(assetId) {
	previewPage = './alamyImagePreview.do?imageId=' + assetId + "&tab=1";
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(previewPage, "", helpStr);
}
function calculateAlamyPrice(assetId) {
	previewPage = './alamyImagePreview.do?imageId=' + assetId + "&tab=2";
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(previewPage,"winPreview",helpStr);
}

function showImageDetails(imageCode) {
	var url = "./imageDetails.do?imageCode=" + imageCode;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function showImageDetailsById(imageId) {
	var url = "./imageDetails.do?imageId=" + imageId;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function showImageDetailsByIdSubscription(imageId) {
	var url = "./imageDetailsSubscription.do?imageId=" + imageId;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function showUserAssetPreview(assetId, tab) {
	var previewPage = "./userAssetPreview.do?assetId=" + assetId;
	if (tab) {
		previewPage += "&tab=" + tab;
	}
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(previewPage, "", helpStr);
}

function showSubscriptionImageDetails(imageCode) {
	var url = "./imageDetailsSubscription.do?imageCode=" + imageCode;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function showSubscriptionImageDetailsById(imageId) {
	var url = "./imageDetailsSubscription.do?imageId=" + imageId;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function showVideoPreview(videoCode) {
	var url = "./videoPreview.do?videoCode=" + videoCode;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function showVideoPrice(videoCode) {
	var url = "./videoPrice.do?videoCode=" + videoCode;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function calculatePrice(imageId,imageCode) {
	var url = "./calculatePrice.do?imageId=" + imageId+"&imageCode="+imageCode;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function recalculatePrice(imageId,imageCode, tpf, unitPrice){
	var url = "./recalculatePrice.do?imageId=" + imageId+"&imageCode="+imageCode+"&tempPriceFactors="+tpf+"&unitPrice="+unitPrice;
	leftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	topPosition = (screen.height) ? (screen.height-640)/2 : 0;
	helpStr = "alwaysRaised=yes,resizable=0,scrollbars=1,width=700,height=640,left=" + leftPosition + ",top=" + topPosition;
	window.open(url, "", helpStr);
}

function eraseCookies(){
	var cookieString = document.cookie;
	var cookieArray = cookieString.split(";");
	for (var i=0;i<cookieArray.length;i++){
		var c = cookieArray[i].substring(0,cookieArray[i].indexOf("="));
		eraseCookie(c);
	}
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}

function openClose(elem, theLabel, param, linkTmp){
	elem = document.getElementById(elem);
//	theLabel = document.getElementById(theLabel);
	if (elem) {
		elem.style.display = (elem.style.display != "block") ? "block"  : "none";
		if (param){
			var theLink ="/searchOptions.do?";
			if (linkTmp) {
				theLink = linkTmp + "?";
			}
			theLink += param + "=";
			theLink = (elem.style.display != "block") ? theLink + "false" : theLink + "true";
			//document.getElementById('searchOptionsStatus').src = theLink;
			setSearchOptions(theLink);
		}
	}
}

function nameValuePairToSession (paramName, paramValue){
	document.getElementById('refreshLocale').src = "nameValuePairToSession.do?name=" + paramName + "&value=" + paramValue;
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);


function getClientHeight() {
	return YAHOO.util.Dom.getViewportHeight();
}

function setMainDivHeight(lightboxMinimized) {
	var mainDiv = document.getElementById("mainDiv");
	if (lightboxMinimized) {
		//mainDiv.style.height = (getClientHeight() - 34) + "px";
		mainDiv.style.height = (getClientHeight() - 21) + "px";
	} else {
		if(document.getElementById("minMaxLghtBox")&& document.getElementById("minMaxLghtBox").firstChild.nodeValue == "Minimize Lightbox"){
			// if lightbox maximized
			mainDiv.style.height = (YAHOO.util.Dom.getViewportHeight() - 178) + "px";
		} else {
			mainDiv.style.height = (getClientHeight() - 110) + "px";
		}
	}
}
var time = 600000; // 10 minutes
function initHeartbeat() {
	document.getElementById('refreshObject').src = "403.jsp";
	setTimeout("initHeartbeat()", time);
}

function switchAJAXPage(location) {
	resourceLoader.getInclude(location, {
		callback: function(data) {
			dwr.util.setValue("mainContent", data, {escapeHtml:false});
			// select lightbox assets on search page
			if (location.indexOf("ajaxImageSearch.do") > -1) {
				// scroll to top
				document.getElementById("mainDiv").scrollTop = 0 + "px";
				if(lightbox != null){
					lightbox.checkLightboxAssets();
					setDragableElements(lightbox.writePermission);
				} else {
					// concurent ajax requests
					//when search result is returned initialize lightbox
					initLightbox();
				}
				layoutCheck();
				postSwitchAction();
			}
		},
		errorHandler: function(errorString) {
			var messageZone = $('messageZone');
			document.body.removeChild(messageZone);
			var disabledZone = $('disabledZone');
			document.body.removeChild(disabledZone);
			
			dwrShowMessage();
		},
		warningHandler: function(errorString) {
			dwrShowMessage();
		},
		timeout:40000
	});
}

function setSearchOptions(location){ 
	var ts = "&ts=" + new Date().getTime();
	document.getElementById('searchOptionsStatus').src = location + ts;
}

function setSearchOptionsAJAX(location){
	resourceLoader.getInclude(location, {
		callback: function(data) {},
		errorHandler: function(errorString) {
			dwrShowMessage();
		},
		warningHandler: function(errorString) {
			dwrShowMessage();
		}
	});
}

function dwrShowMessage(){
		var dwrMessage = document.getElementById('dwrMessage');
	    if (!dwrMessage) {
	    	dwrMessage = document.createElement('div');
		    dwrMessage.setAttribute('id', 'dwrMessage');
		    dwrMessage.style.position = "absolute";
		    dwrMessage.style.top = "0px";
		    dwrMessage.style.right = "0px";
		    dwrMessage.style.padding = "0px";
		    dwrMessage.style.margin = "0px";
		    document.body.appendChild(dwrMessage);
		    var p = document.createElement('p');
		    p.className = "messageP";
		    var text = document.createTextNode("Connection problem occured! Please try again...");
		    p.appendChild(text);
		    dwrMessage.appendChild(p);
		    //dwrMessage.appendChild(text);
		    if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 7) {
			    // Create an iframe mask
		        var mask = document.createElement('iframe');
		        mask.src = 'about:blank';
		        mask.className = 'messageShim';
		        YAHOO.util.Dom.setStyle(mask, 'height',dwrMessage.offsetHeight);
		        dwrMessage.appendChild(mask);
		    } 
	    }
	    
	    dwrMessage.style.visibility = "visible";
	    setTimeout("dwrHideMessage()", 3000);
}

function dwrHideMessage() {
	document.getElementById('dwrMessage').style.visibility = "hidden";
}

function postSwitchAction() {
}
function setDragableElements(writePermission) {
	var images = YAHOO.util.Dom.getElementsByClassName("dragable", "img");
	if (images && images.length > 0) {
		for (var i = 0; i < images.length; i++) {
			ddImage = new AssetDrag(images[i].id, "lbImage", {dragElId: "dragImage", resizeFrame: false, centerFrame: true, scroll: false});
		}
	}
}

// define trim() function
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}