//GLOBAL VARS
var menuHeight = 29;
var pagePadding = 20;
var repositionPopLayer = false;
var zIndexGlobal = 1000;
var imageDir = "image/";
// var serverUrl = getUrl(true, false);
var serverUrl = getUrlV2("http", "none");

// RESIZE PAGE
var bottomContentStartY = 0;

function resizePage()
{

    return;

    var frontPageBody = document.getElementById("frontPageBody");
	var clientH = document.body.clientHeight;
	var clientW = document.body.clientWidth;
	
	var contentDiv = document.getElementById("contentDiv");
	
	// Change content area size depending on lightbox bar is present or 
	if(document.getElementById('lightboxDiv'))
	{
		// resize lightbox if user tries to minimize to much
		if(lightboxbar_height > (clientH - lightboxMaxTopPos))
		{
			lightboxbar_height = (document.body.clientHeight - lightboxMaxTopPos);
				
			if(lightboxbar_height < (imageSize + lightboxMenuHeight + lightboxImageSpacing + dragbarHeight + lightboxRemoveHeight))
				lightboxbar_height = imageSize + lightboxMenuHeight + lightboxImageSpacing + dragbarHeight + lightboxRemoveHeight;
			
			renderLightbox();
		}
	
		// Only change if body is larger then lightbox the 92 is the top bar height!
		if(document.body.clientHeight > lightboxbar_height)
		{
			if(lightboxbar_isHidden)
				contentDiv.style.height = clientH - (lightboxMenuHeight + dragbarHeight) - ((2*pagePadding) +menuHeight);
			else
				contentDiv.style.height = clientH - lightboxbar_height - ((2*pagePadding) +menuHeight);
		}

		document.getElementById("lightboxDiv").style.width = clientW - (2*pagePadding);
		document.getElementById("lightboxDragSizeDiv").style.width = clientW - (2*pagePadding);
		document.getElementById("lightboxImagesDiv").style.width = clientW - (2*pagePadding);
	}
	else
		contentDiv.style.height = clientH - ((2*pagePadding) +menuHeight);
	
	// Move footer to bottom of page
	if(document.getElementById('bottomContent'))
	{
		if(bottomContentStartY == 0)
			bottomContentStartY = document.getElementById('bottomContent').offsetTop;
		
		var newX = 0;
		if(document.getElementById('lightboxDiv'))
		{
			if(lightboxbar_isHidden)
				newX = clientH - bottomContentStartY - (lightboxMenuHeight + dragbarHeight) - 108;
			else
				newX = clientH - bottomContentStartY - lightboxbar_height - 108;
		}
		else
			newX = clientH - bottomContentStartY - 108;
		
		if(newX > 0)
			document.getElementById('bottomContent').style.top = newX + "px";
		else
			document.getElementById('bottomContent').style.top = 0 + "px";
	}
	
	if (frontPageBody != null) {
	    contentDiv.style.width = 910;
	} else {
	    contentDiv.style.width = clientW - (2*pagePadding);
	}
		
	// Move popLayer
	if(repositionPopLayer && document.getElementById('popLayer'))
	{
		if(document.getElementById('popLayer').style.display == "block")
			popLayerReposition();
	}
}
// END

// Fire page update on DOM
var domReadyTimer;
var domIsReady = false;
function domReady()
{
	this.n = typeof this.n == 'undefined' ? 0 : this.n + 1;
	
	if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
	{
		clearTimeout(domReadyTimer);
		
		try
		{
			onLoadActions();
		}
		catch(e)
		{
		}
	}
	else
	{
		if(this.n < 500)
			domReadyTimer = setTimeout('domReady()', 250);
		else
			clearTimeout(domReadyTimer);
	}
}
// END

// GLOSSARY
function openGlossary(word) {    
    
    var strAgent=navigator.userAgent;
	
	if (word != null && word != "") word = "#" + word
	
	mywin=window.open("Glossary.aspx" + word,"glossary",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=true,width=500,height=300');
		
    if (strAgent.indexOf('MSIE 3')==-1 && strAgent.length!=0)
    {
        mywin.focus();        
    }
    
    return mywin;
    
}

function openPrint(url, nameWindow, width, height, resizable) {
    var strAgent=navigator.userAgent;
    
    url = getUrlV2("http", "none") + url;

	mywin=window.open(url,nameWindow,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
		
    if (strAgent.indexOf('MSIE 3')==-1 && strAgent.length!=0)
    {
        mywin.focus();        
    }
    
    return mywin;
}

// POPUP
function openPopUp(url, nameWindow, width, height, clean, resizable)
{
    var strAgent=navigator.userAgent;

	if(clean)
		mywin=window.open(url,nameWindow,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=' + resizable + ',width=' + width + ',height=' + height);
	else
		mywin=window.open(url,nameWindow,'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
		
    if (strAgent.indexOf('MSIE 3')==-1 && strAgent.length!=0)
    {
        mywin.focus();        
    }
    
    return mywin;
	
}
// END

// SHOW/HIDE DIV
// Will show a hidden layer and optional hide old layer 
// "divName"			Name of target layer
// "xPos"				The position of the layer from left
// "yPos"				The position of the layer from top
// "closeLowerAccess"	If there should be access to elements below the layer(s)
function divShow(divName, xPos, yPos, closeLowerAccess, darkOutBackground)
{
	// ie hide selectbox hack
	if(document.all)
		hideSelectBoxes();
	
	var element = document.getElementById(divName);
	
	var ilcla = document.getElementById('imageLayerCloseLowerAccess');	
	
	element.style.left = String(xPos).replace(/px/, "") + "px";
	element.style.top = String(yPos).replace(/px/, "") + "px";
	
	element.style.zIndex = zIndexGlobal;
	
	
	zIndexGlobal++;
	
	if(closeLowerAccess)
		document.getElementById('layerCloseLowerAccess').style.display = "block";
	
	if (divName == "popLayer") {
	    document.getElementById('popLayerHeadline').innerHTML = document.getElementById('popLayerHeadline').innerHTML.toUpperCase();
	}
		
	
	if(darkOutBackground)
	{
	    if (divName == "popLayer" && !isOldIE) {
	        document.getElementById('maincontent').style.zIndex = -1;
	    }
	
		//ilcla.src = "gfx/_000000_alpha50.png";
	    /*
		if(document.all)
			ilcla.className = "ieAlfa";
	    */
	}
	else
	{
		ilcla.src = "gfx/_blank.gif";
		ilcla.className = "";
	}
	
	element.style.display = "block";
}

// Will hide a layer
// "divName"			Name of target layer
// "openLowerAccess"	If access to the elements below the layer(s) should be allowed
function divHide(divName, openLowerAccess)
{
	// ie hide selectbox hack
	if(document.all)
		showSelectBoxes();

	if(document.getElementById('layerCloseLowerAccess') && openLowerAccess)
		document.getElementById('layerCloseLowerAccess').style.display = "none";

	if(divName == "popLayer") {
		document.getElementById('popLayerTopSizer').width = 1;
	
		var quickViewImage = document.getElementById("quickViewImage");
		if (quickViewImage != null && quickViewImage.innerHTML != "") 
		    quickViewImage.innerHTML = "";
	}

	document.getElementById(divName).style.display = "none";
	
	if(document.getElementById('popLayer').style.display == "none") {
	    document.getElementById('popLayerDrag').style.display = "block";
	    document.getElementById('popLine').style.backgroundColor = "#cccccc";	    
	    document.getElementById('maincontent').style.zIndex = 1;
		repositionPopLayer = false;
		 
    }
	
	cleanUpMouseEventObject();
}
// END

function popLayerDragStart()
{
	if(mouseEventObject)
	{
		// Subscribe
		mouseEventObject.move = popLayerDragMouseMove;
		mouseEventObject.up = popLayerDragMouseUp;
		mouseEventObject.down = popLayerDragMouseMove;
		mouseEventObject.xOffset = mouseEventObject.x - Number(document.getElementById('popLayer').style.left.replace("px",""));
		mouseEventObject.yOffset = mouseEventObject.y - Number(document.getElementById('popLayer').style.top.replace("px",""));
		document.body.style.cursor = 'move';
	}
}

function popLayerDragMouseMove()
{
    // size of website screen area    
    var framecontentBottom = document.getElementById("framecontentBottom");
    var width = framecontentBottom.clientWidth;
    var height = framecontentBottom.offsetTop + framecontentBottom.clientHeight;

    // dont drag the div to close to the edge.
	if(mouseEventObject.y < (height - 20) && mouseEventObject.x < (width - 20) && mouseEventObject.y > 20 && mouseEventObject.x > 20)
	{
	    var left = mouseEventObject.x - mouseEventObject.xOffset;
	    var top = mouseEventObject.y - mouseEventObject.yOffset;

        var popLayerElement = document.getElementById('popLayer');
		popLayerElement.style.left = (mouseEventObject.x - mouseEventObject.xOffset) + "px";
		popLayerElement.style.top = (mouseEventObject.y - mouseEventObject.yOffset) + "px";
	}
}

function popLayerDragMouseUp()
{
	document.body.style.cursor = 'default';
	cleanUpMouseEventObject();
}


// Reposition popLayer
function popLayerReposition(target, top, left)
{
	var layer = 'popLayer';
	
	if(target)
		layer = target;

	repositionPopLayer = true;
	
	var posTop = rootOffset(document.getElementById(layer + 'PosGfxTopLeft'));
	var posBottom = rootOffset(document.getElementById(layer + 'PosGfxBottomRight'));
	
	document.getElementById(layer + 'TopSizer').width = ((posBottom.left - posTop.left) - 2) + "px";
	
	var fcBottom = document.getElementById("framecontentBottom");
	
	if( typeof(top) == "undefined" && typeof(left) == "undefined" || top == null && left == null) {
	    document.getElementById(layer).style.left = Math.ceil((fcBottom.clientWidth - (posBottom.left - posTop.left))/2) + "px";
	    document.getElementById(layer).style.top = Math.ceil((fcBottom.offsetTop + fcBottom.clientHeight - (posBottom.top - posTop.top))/2) + "px";
	}
	
	
}
// END


// IE hack Hide/showe Select boxes
function hideSelectBoxes()
{
	if(!window.XMLHttpRequest)
	{
		var selectBoxes = document.getElementsByTagName('select');
		for(i=0; i<selectBoxes.length; i++)
		{
			selectBoxes[i].style.display = "none";
		}
	}
}

function showSelectBoxes()
{
	var selectBoxes = document.getElementsByTagName('select');
	for(i=0; i<selectBoxes.length; i++)
	{
		selectBoxes[i].style.display = "block";
	}
}
// END

/*
// Get url from page
function getUrl(includeHttp, includeFilename, includeQureystring)
{
	var pageUrl = window.location.hostname;
	
	if(includeHttp)
		pageUrl = "http://" + pageUrl;
	
	var pathname = window.location.pathname;
    if (pathname.indexOf(".") < 0)
        pathname = "/devfrontend/search.aspx"
	if(includeFilename)
	{	    
		if(includeQureystring)
			pageUrl += pathname + "?" + window.location.search.substring(1).toLowerCase();
		else
	        pageUrl += pathname;
	}
	else
	{
		if(window.location.pathname.length > 0)
			pageUrl += pathname.substring(0, pathname.lastIndexOf("/") + 1);
	}
	
	return pageUrl;
} 
// END
*/

// Get elements offest from top/left
function rootOffset(target)
{
	var pos = {left: 0, top: 0};
	var e = target;

	//while(e.offsetParent)
	while(e)
	{
		pos.left += e.offsetLeft;
		pos.top += e.offsetTop;
		e = e.offsetParent;
	}
	
	return pos;
}
// END

// Change row color
function changeRowColor(target, color)
{
	target.bgColor = color;
}
// END


// Simple test of email adress
function isValidEmail(email)
{
   return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
 
}
// END


// Download images
function downloadImage(imageId)
{
	location.href = "SaveAs.aspx?imageId=" + imageId;
}
// END

// Toogle selection on select box
function toogleCheckbox(id)
{
	d = document.getElementById(id);
	
	if(d.checked)
		d.checked = false;
	else
		d.checked = true;
}
// END

// Return boolean from string
function ReturnTrueFalseAsBoolean(string)
{
	if(string == "true")
		return true;
	else
		return false;
}
// END

// SET AND SELECT OPTIONS IN BOLD SELECT LIST
// "divName"			Name of target layer
// "itemList"			Comma seperated list of elements in the list
// "itemListSelected"	Comma seperated list 0 and 1, where 1 indicate the an item in "itemList" at the same position is selected
// "itemSelected"		The new item you want to select or deselect
// "allowMulit"			If more then one element can be selected in the lisdt
// "styleClass"			Name of style class to be used
function PrefLinksSet(divName, itemList, itemListSelected, itemSelected, allowMulit, styleClass, functionFire)
{
	var listArray = itemList.split(",");
	var listSelectedArray = itemListSelected.split(",");
	var myitemListSelected = "";
	var myInnerHtml = "";
	
	if(functionFire != "" && document.getElementById(divName).innerHTML != "")
	{
		if(allowMulit)
		{
		}
		else
			eval(functionFire + "(listArray[itemSelected]);");
	}
	
	if(allowMulit)
		listSelectedArray[itemSelected] = Math.abs((1 + Number(listSelectedArray[itemSelected])) - 2);
	else
	{
		for(i=0; i<listSelectedArray.length; i++)
			listSelectedArray[i] = 0;
		
		listSelectedArray[itemSelected] = 1;
	}
	
	for(i=0; i<listSelectedArray.length; i++)
	{
		if(i>0)
			myitemListSelected += "," + listSelectedArray[i];
		else
			myitemListSelected += listSelectedArray[i];
	}

	for(i=0; i<listArray.length; i++)
	{
		if(i>0)
			myInnerHtml += " - ";
		
		if(listSelectedArray[i] == 1)
			myInnerHtml += "<a class=\"numberOfImages preferedNumberOfImages\" onclick=\"PrefLinksSet('" + divName + "','" + itemList + "', '" + myitemListSelected + "', " + i + ", " + allowMulit + ",'" + styleClass + "','" + functionFire + "');\" onfocus=\"blur()\" class=\"" + styleClass + "\">" + listArray[i] + "</a>";
		else 
			myInnerHtml += "<a class=\"numberOfImages\" onclick=\"PrefLinksSet('" + divName + "','" + itemList + "', '" + myitemListSelected + "', " + i + ", " + allowMulit + ",'" + styleClass + "','" + functionFire + "');\" onfocus=\"blur()\" class=\"" + styleClass + "\">" + listArray[i] + "</a>";
	}
	
	document.getElementById(divName).innerHTML = myInnerHtml;
}
// END


/* START file information show and hide */
	function show_file_info(element, element_id) {			
		var infoDiv = document.getElementById(element_id);
		var contentDiv = document.getElementById("maincontent");
		var file_info = document.getElementById("file_info");		
		file_info.style.position = "absolute";
		file_info.innerHTML = "<div style='position: absolute; z-index: 501; width: 210px; background: #e6e6e6; padding: 5px; top: " + (element.offsetTop + 160 - contentDiv.scrollTop) + "px; left: " + ( element.offsetLeft - 30 ) + "px'>" + infoDiv.innerHTML + "</div>"; 
		file_info.style.display = "block";
		
	}

	function hide_file_info() {
		var file_info = document.getElementById("file_info");		

		file_info.style.display = "none";
	}


/* END file information show and hide */


// Return the keycode form an event
function returnKeyCode(e)
{
	var code;
	
	if (!e) 
		var e = window.event;
	
	if (e.keyCode)
		code = e.keyCode;
	else if 
		(e.which) code = e.which;

	return code;
}
// END


// Return position for cursor in textinput
function returnCaretPosition (textField)
{
	var caretPos = 0;
	
	if (document.selection)
	{ 
		textField.focus();
		var sel = document.selection.createRange ();
		sel.moveStart ('character', -textField.value.length);
		caretPos = sel.text.length;
	}
	else if (textField.selectionStart || textField.selectionStart == '0')
	{
		caretPos = textField.selectionStart;
	}
	
	return caretPos;
}
// END


// Return image size
function returnImageSizeBasedOnPageHeight(pageHeight)
{
    //console.info(pageHeight);

	var imageSize = 400;

	if(pageHeight > 655 && readCookie("TrunkAccount") != "0")
		imageSize = 450;
		
	if(pageHeight > 705 && readCookie("TrunkAccount") != "0")
		imageSize = 500;

	/*	
	if(pageHeight > 690 && readCookie("TrunkAccount") != "0")
		imageSize = 550;

	if(pageHeight > 740 && readCookie("TrunkAccount") != "0")
		imageSize = 600;
		
	if(pageHeight > 790 && readCookie("TrunkAccount") != "0")
		imageSize = 650;
		
	if(pageHeight > 840 && readCookie("TrunkAccount") != "0")
		imageSize = 700;
	*/	
	return imageSize;
}

// returns random string
function returnRandomString(length, doNumb, doAlpha)
{
	var numb = "1234567890";
	var alpha = "qwertyuioplkjhgfdsazxcvbnm";
	
	var string = "";
	var bla = "";
	var type, rand;
	
	while(string.length < length)
	{
		type = Math.floor(Math.random()*2);
		
		if(type == 0 && doNumb)
		{
			rand = Math.floor(Math.random()*numb.length);
			string += numb.substring(rand, rand + 1);
		}
		
		if(type == 1 && doAlpha)
		{
			rand = Math.floor(Math.random()*alpha.length);
			string += alpha.substring(rand, rand + 1);		
		}
	}
	
	rand = Math.floor(Math.random()*numb.length);
	return string;
}

function setOpacity(opacity, e) {    
    if (e) {
      e = e.style; 
      e.opacity = (opacity / 100);
      e.KhtmlOpacity = (opacity / 100);
      e.MozOpacity = (opacity / 100);
      e.filter = "alpha(opacity=" + opacity + ")";
    }
}
	
function comboUpdateField(selected)
{
    
    var selectObj = document.getElementById(selected.id + "Selected");            
    selectObj.value = selected.options[selected.selectedIndex].innerHTML;

    if (selectObj.id == "searchArtistIdSelected") {
        selectObj.value = selected.options[selected.selectedIndex].innerText;
        selected.options[0].innerHTML = "";
        if (selectObj.value != "ARTISTS") {
            selectObj.style.color = '#333333';
        }
    }

    if(isOldIE6orless) {
        selected.style.visibility="hidden";
    }
}

function comboSelect(element) {
    
    var combo = document.getElementById(element.id.replace(/Selected$/,""));    
    if( combo.style.visibility != "visible" )
    {
        combo.style.visibility = "visible";
        combo.focus();
    }
}

function comboSelectInit(elementId) {
    comboSelectPosition(elementId,false);
}

function comboSelectOnMouseOver(elementId){     
     comboSelectPosition(elementId,true);
}

function comboSelectPosition(elementId, setVisible){
    if(isOldIE)
    {        
        var textbox = document.getElementById(elementId);
    
        var combo = document.getElementById(elementId.replace(/Selected$/,""));
        combo.style.position = "absolute";
        combo.style.left = textbox.offsetLeft + "px";
        combo.style.top = textbox.offsetTop + "px";
        combo.style.marginLeft = "-2px";
        combo.style.marginTop = "-2px";
        if (setVisible) combo.style.visibility = "visible";        
    }
}

function comboSelectOnMouseOut(elementId){
    if(isOldIE6orless)
    {
        var combo = document.getElementById(elementId.replace(/Selected$/,""));
        combo.style.visibility = "hidden";
    }
}


// Start - For Header
function ShowSearchTips()
{
	divHide('popLayer',true);
	
	var popObject = new Object();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	
	popObject.content = '<br /><div style="padding-left: 1px; margin: 0px; width: 680px; height: 579px; background: url(gfx/searchtips_bg.gif); background-color: #ffffff;">';
    popObject.content += '  <div style="height: 107px;">';
    popObject.content += '    <table border="0" cellpadding="0" cellspacing="0">';
    popObject.content += '      <tr>';
    popObject.content += '        <td width="640">';
    popObject.content += '        </td>';
    popObject.content += '        <td>';
    popObject.content += '          ';
    popObject.content += '          <div onclick="divHide(\'popLayer\',true);" style="cursor: pointer; width: 22px; height: 22px; margin-top: 4px;"></div>';
    popObject.content += '        </td>';
    popObject.content += '    </table>';
    popObject.content += '  </div>';
    popObject.content += '  <div style="padding-left: 25px; height: 213px;">';
    popObject.content += '    <table border="0" cellpadding="0" cellspacing="0">';
    popObject.content += '      <tr>';
    popObject.content += '        <td>';
    popObject.content += '          <img class="stips_active" onmouseover="stips_show_hint(this)" id="stips_h_1" src="gfx/searchtips_h_1.jpg" />';
    popObject.content += '        </td>';
    popObject.content += '        <td>';
    popObject.content += '          <img class="stips_inactive" onmouseover="stips_show_hint(this)" id="stips_h_2" src="gfx/searchtips_h_2.jpg" />';
    popObject.content += '        </td>';
    popObject.content += '         <td>';
    popObject.content += '          <img class="stips_inactive" onmouseover="stips_show_hint(this)" id="stips_h_3" src="gfx/searchtips_h_3.jpg" />';
    popObject.content += '        </td>';
    popObject.content += '         <td>';
    popObject.content += '          <img class="stips_inactive" onmouseover="stips_show_hint(this)" id="stips_h_4" src="gfx/searchtips_h_4.jpg" />';
    popObject.content += '        </td>';
    popObject.content += '         <td>';
    popObject.content += '          <img class="stips_inactive" onmouseover="stips_show_hint(this)" id="stips_h_5" src="gfx/searchtips_h_5.jpg" />';
    popObject.content += '        </td>';
    popObject.content += '    </table>';
    popObject.content += '  </div>';
    popObject.content += '  <div id="stips_e_1" style="padding-left: 12px; padding-top: 24px; height: 217px;">';
    popObject.content += '      <img src="gfx/searchtips_e_1.jpg" />';
    popObject.content += '  </div>';
    popObject.content += '  <div id="stips_e_2" style="padding-left: 12px; padding-top: 24px; height: 217px; display: none;">';
    popObject.content += '      <img src="gfx/searchtips_e_2.jpg" />';
    popObject.content += '  </div>';
    popObject.content += '  <div id="stips_e_3" style="padding-left: 12px; padding-top: 24px; height: 217px; display: none;">';
    popObject.content += '      <img src="gfx/searchtips_e_3.jpg" />';
    popObject.content += '  </div>';
    popObject.content += '  <div id="stips_e_4" style="padding-left: 12px; padding-top: 24px; height: 217px; display: none;">';
    popObject.content += '      <img src="gfx/searchtips_e_4.jpg" />';
    popObject.content += '  </div>';
    popObject.content += '  <div id="stips_e_5" style="padding-left: 12px; padding-top: 24px; height: 217px; display: none;">';
    popObject.content += '      <img src="gfx/searchtips_e_5.jpg" />';
    popObject.content += '  </div>';
	
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	document.getElementById("popLayerHeadline").innerHTML = "";
	document.getElementById('popLayerDrag').style.display = "none";	
	document.getElementById('popLine').style.backgroundColor = "#ffffff";	
	
	
	document.getElementById('popLayerTopSizer').width = 1;
	
	//ArtistWebEffects.set_opacity(0,"popLayer");				
	divShow('popLayer', 1, 1, popObject.closeLowerAccess, popObject.darkOutBackground);
	
	popLayerReposition();
	//ArtistWebEffects.set_opacity(100,"popLayer");				
}




// --------
var keywordSearchPending = false;
var keywordSearchResultIndex = -1;
var keywordSearchStart = "";
var keywordSearchLastArrowPress = 0;
var doKeywordSearch = true;

function setupKeywordSuggest()
{
    doKeywordSearch = false;
	d = document.getElementById('keywordTxt');
	d.setAttribute('autocomplete','off');

    //Keyword suggestion disabled
    return false;
	setupSuggestPos();

	if(ReturnTrueFalseAsBoolean(getUserSettingFromCookie("searchbar","useKeywordSuggest")))
	{
		doKeywordSearch = true;		
		d.setAttribute('autocomplete','off');
		document.getElementById('keywordSuggestAnchor').innerHTML = "<img src=\"gfx/st_kws_on.gif\"/>";
	}
	else
	{
		doKeywordSearch = false;
		d.setAttribute('autocomplete','on');
		
		document.getElementById('keywordSuggestAnchor').innerHTML = "<img src=\"gfx/st_kws_off.gif\"/>";
	}
}

function keywordSuggest(e)
{
	document.getElementById('maincontent').style.zIndex = -1;
	//console.info("maincontent zIndex = -1");

	if(!doKeywordSearch)
		return true;

	var date = new Date();
	var d = document.getElementById('keywordTxt');
	var input = d.value;
	var keycode = Number(returnKeyCode(e));
	
	if(!keywordSearchPending && input.length > 0)
	{
		if(keywordSearchResultIndex != -1)
			document.getElementById("keywordSuggestSpan" + keywordSearchResultIndex).style.backgroundColor = "#FFFFFF";
		
		if(keycode == 40 || keycode == 38)
		{
			// Safari fires arrow keys twice hack
			if((date.getTime() - keywordSearchLastArrowPress) < 10)
				return true;
			else
				keywordSearchLastArrowPress = date.getTime();
		
			if(keycode == 38 && (document.getElementById("keywordSuggestSpan" + (keywordSearchResultIndex - 1)) || keywordSearchResultIndex == 0))
				keywordSearchResultIndex -= 1;
			
			if(keycode == 40 && document.getElementById("keywordSuggestSpan" + (keywordSearchResultIndex + 1)))
				keywordSearchResultIndex += 1;
				
			if(keywordSearchResultIndex >= 0)
			{
				document.getElementById("keywordSuggestSpan" + keywordSearchResultIndex).style.backgroundColor = "#EEEEEE";
				d.value = returnStartOfKeywordSuggest(keywordSearchStart);
				d.value += unescape(document.getElementById("keywordSuggestOrgSpan" + keywordSearchResultIndex).innerHTML);
			}
			else
				d.value = keywordSearchStart;
		}
		else
		{
			var keywordsArray = input.split(" ");
			keywordSearchPending = true;
			keywordSearchResultIndex = -1;
			keywordSearchStart = input;
			dataRequest("ajax/KeywordSuggest.aspx?keywords=" + input + "&keyword=" + keywordsArray[keywordsArray.length -1], keywordSuggestAjaxCallback);
		}
	}
	else
	{
		document.getElementById('keywordSuggestDiv').style.display = "none";
	}
}

function returnStartOfKeywordSuggest(keywords)
{
	var keywordsArray = keywords.split(" ");
	var string = "";

	for(i=0; i<(keywordsArray.length - 1); i++)
	{
		string += keywordsArray[i] + " ";
	}
	
	return string;
}

function keywordSuggestAjaxCallback(xmlFeed, status, url)
{
	var d = document.getElementById('keywordSuggestDiv');
	
	// This callbackfunction runs, if you choose something in the keywordsuggestDiv and press enter. Then the new page loads, and the ajax's callback function is called. The keywordSuggestDiv is not necessaryli ready in the dom yet, so we just skip the logic in it.
	if(d != null) {
	    d.style.display = "block";
    	
	    var suggestion = "";
    	
	    if(status == 0)
	    {
		    var xmlDoc = xmlFeed.responseXML;
		    var node = xmlDoc.getElementsByTagName('keywords').item(0);
		    var nodeChild = null;
    		
		    for(var i=0; i<node.childNodes.length; i++)
		    {
			    suggestion += '<div id="keywordSuggestOrgSpan' + i + '" style="display: none;">' + node.childNodes[i].firstChild.nodeValue + '</div><div id="keywordSuggestSpan' + i + '" style="width: ' + (Number(d.style.width.replace("px","")) - 4)  + '; padding: 1 2 1 2 ; cursor:pointer;" onmouseover="this.style.backgroundColor = \'#EEEEEE\'; keywordSearchResultIndex = ' + i + ';" onmouseout="this.style.backgroundColor = \'#FFFFFF\'; keywordSearchResultIndex = -1;">' + unescape(node.childNodes[i].firstChild.nodeValue) + '</div>';
		    }
	    }
    	
	    if(suggestion != "")
	    {
		    d.innerHTML = suggestion;
		    document.getElementById('maincontent').style.zIndex = -1;
	    }
	    else
	    {
		    d.style.display = "none";
		    document.getElementById('maincontent').style.zIndex = 1;
	    }

    }
	keywordSearchPending = false;
}

function hideKeywordSuggestDiv()
{

	document.getElementById('maincontent').style.zIndex = 1;
	//console.info("maincontent zIndex = 1");
     
	if(!doKeywordSearch)
		return true;

	if(keywordSearchResultIndex != -1)
	{
		document.getElementById('keywordTxt').value = returnStartOfKeywordSuggest(keywordSearchStart);
		document.getElementById('keywordTxt').value += document.getElementById("keywordSuggestSpan" + keywordSearchResultIndex).innerHTML;
	}

	var d = document.getElementById('keywordSuggestDiv');
	d.style.display = "none";
}

function setupSuggestPos()
{
    var ieSpecificHeight = 0;

    if(isOldIE)
        ieSpecificHeight = 4;
        
	var pos = rootOffset(document.getElementById('keywordTxt'));
	
	var d = document.getElementById('keywordSuggestDiv');
	d.style.position = "absolute";
	d.style.left = Number(pos.left - 5) + "px";
	d.style.top = Number(pos.top + 18) + "px";
	d.style.width = Number(document.getElementById('keywordTxt').offsetWidth + 1 + ieSpecificHeight) + "px";
	d.style.zIndex = zIndexGlobal;
	zIndexGlobal ++;
}

function ToogleKeywordSuggest()
{
	if(ReturnTrueFalseAsBoolean(getUserSettingFromCookie("user","doKeywordSuggestAlert")))
	{
		//displayAlertLayer("<b>Hint:</b> Keyword suggest<br><br>When Keyword suggest is 'on', the system will  suggest words related to each keystroke of the search term.", 220, 0, "TR", 100, 100, zIndexGlobal);
		//zIndexGlobal ++;
		
		updateUserSettings(true, true, "user_doKeywordSuggestAlert", "false");
	}

	if(ReturnTrueFalseAsBoolean(getUserSettingFromCookie("searchbar","useKeywordSuggest")))
		updateUserSettings(true, true, "searchbar_useKeywordSuggest", "false");
	else
		updateUserSettings(true, true, "searchbar_useKeywordSuggest", "true");

	setupKeywordSuggest();
}


// End - For Header


// Common functions for Search.aspx, Lightbox.aspx, that both uses the RenderImage methods to show an image and its data with links to e.g. "more actions"
function setupMoreActions(imageId, showCreateOrder, fromName, fromEmail, backendUrl)
{
	divHide('popLayer',true);
	
	var popObject = new Object();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
    var artistName = imageArray[imageId].artist;
    artistName = artistName.replace(/\s/g, "+").toUpperCase();	
	
	popObject.content =  '<table width="150" border="0" cellspacing="0" cellpadding="8">';
	popObject.content += '	<tr>';
	popObject.content += '		<td valign="top" align="left"><!--View entire set<br>-->';
	popObject.content += '		<a href="' + getUrlV2("root", "none") + artistName + '/images" onfocus="blur()">View all images by artist</a><br>';
	
	if (imageArray[imageId].storyCollectionId && imageArray[imageId].storyCollectionId != "") 
	{
	    popObject.content += '		<a href="' + getUrlV2("root", "none") + artistName + '/images/story%23+' + imageArray[imageId].storyCollectionId + '" onfocus="blur()">View story</a><br>';
	    // popObject.content += '		<a href="Search.aspx?searchString=story%23%20' + imageArray[imageId].storyCollectionId + '" onfocus="blur()">View story</a><br>';
	} else { 
	
	    popObject.content += '		<br>';
	}
	popObject.content += '		<br>';
	popObject.content += '		<a href="JavaScript:emailImages(\'' + fromName + '\',\'' + fromEmail + '\',' + imageId + ');" onfocus="blur()">Email image</a><br>';
	//popObject.content += '		<a href="JavaScript:downloadImage(' + imageId + ');" onfocus="blur()">Download lo-res</a><br>';
	popObject.content += '		<a href="javascript: void openPrint(\'ImagePopup.aspx?imageId=' + imageId + '\',\'Image\', 1000, 500, true);" onfocus="blur()">Open printable image</a>';
    if(showCreateOrder)
    {
	    popObject.content += '		<br><br>';	
	    popObject.content += '		<a href="Javascript:createOrderByImageIds(\'' + imageId + '\', \'otherUsage\', \''+ backendUrl +'\');" onfocus="blur()">Create order (Other usage)</a><br>';
	    popObject.content += '		<a href="Javascript:createOrderByImageIds(\'' + imageId + '\', \'license\', \''+ backendUrl +'\');" onfocus="blur()">Create order (License)</a><br>';
	}
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	popObject.content += '<table width="150" border="0" cellspacing="0" cellpadding="8">';
	popObject.content += '	<tr>';
	popObject.content += '		<td align="right"><a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()">close</a></td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	document.getElementById("popLayerHeadline").innerHTML = "<b>" + imageArray[imageId].no + "</b>";
	document.getElementById('popLayerTopSizer').width = 1;
	
	var fcBottom = document.getElementById("framecontentBottom");
	
	/*
	document.getElementById(layer).style.left = Math.ceil((fcBottom.clientWidth - (posBottom.left - posTop.left))/2) + "px";
	document.getElementById(layer).style.top = Math.ceil((fcBottom.offsetTop + fcBottom.clientHeight - (posBottom.top - posTop.top))/2) + "px";
	*/
	
	divShow('popLayer', (mouseEventObject.x - 75), (mouseEventObject.y - 50), popObject.closeLowerAccess, popObject.darkOutBackground);
    
}

function createOrderByImageIds(imageIds, wizardName, backendUrl)
{
	if (imageIds != "")
		location.href = backendUrl + "/accounts.aspx?wizardCmd=load&wizardName=" + wizardName + "&imageIds=" + imageIds;
	else
		document.getElementById("popLayerAlert").innerHTML = '<b class="red">No images was selected.</b>';
}

/* search tips */

      function stips_show_hint(el) {
        stips_deactivate("1"); 
        stips_deactivate("2"); 
        stips_deactivate("3"); 
        stips_deactivate("4"); 
        stips_deactivate("5"); 

        var nmbr = el.id.replace("stips_h_","");
        var e = document.getElementById("stips_e_" + nmbr);
        el.className = "stips_active";
        //e.style.display = "block";
        stips_fade_in(e.id);
        
      }

      function stips_deactivate(nmbr) {
        var h = document.getElementById("stips_h_" + nmbr);
        var e = document.getElementById("stips_e_" + nmbr);
        e.style.display = "none";
        h.className = "stips_inactive";
      }


      function stips_fade_in(id) {
        var e = document.getElementById(id);
        ArtistWebEffects.set_opacity(0,id)
        e.style.display = "block";
        var o = 0
        var count = 10;
        while (o < 100 ) {
          o += 10;
          count++;
          setTimeout("ArtistWebEffects.set_opacity(" + o + ",'" + id + "')",10 * count);
        }
                                  
      }




/* Effects helper for artists web */

var ArtistWebEffects = {
  set_opacity: function (opacity, id) {
    var e = document.getElementById(id);
    if (e) {
      e = e.style; 
      e.opacity = (opacity / 100);
      e.KhtmlOpacity = (opacity / 100);
      e.MozOpacity = (opacity / 100);
      e.filter = "alpha(opacity=" + opacity + ")";
    }
  },
 
  hide: function (id) {
    
    var e = document.getElementById(id);
    e.style.display = "none";
  },
 
  fade_out: function (id) {
    var e = document.getElementById(id);
    e.style.display = "block";
    var o = 100
    var count = 1;
    while (o > 0) {
      o -= 1;
      count++;
      setTimeout("ArtistWebEffects.set_opacity(" + o + ",'" + id + "')",10 * count);
    }
   
    setTimeout("ArtistWebEffects.hide('" + id + "')",10 * count);
  },

  fade_in: function (id) {
    var e = document.getElementById(id);
    ArtistWebEffects.set_opacity(0,id)
    e.style.display = "block";
    var o = 0
    var count = 10;
    while (o < 100 ) {
      o += 10;
      count++;
      setTimeout("ArtistWebEffects.set_opacity(" + o + ",'" + id + "')",10 * count);
    }
       
  },

  disappear: function (id, seconds) {
    setTimeout("ArtistWebEffects.fade_out('" + id  + "')", seconds * 1000);
  }

}

function clearInput(e, defaultText) {        
    if (e.value == defaultText && e.style.color == "rgb(166, 166, 166)" || String(e.style.color).toUpperCase() == "#A6A6A6") {        
        e.style.color = "#333333";
        e.value = "";
    }
}
function searchKeyDown(e)
{
    if (e.keyCode == 13)
        chkKeywords();
}

function myEscape(s)
{    
    s = escape(s);
    s = s.replace(/%20/g, "+");
    return s;
}

function artistDropDownChanged(dropDown) 
{
    comboUpdateField(dropDown);
    chkKeywords();
}

function chkKeywords() {
    var e = document.getElementById("keywordTxt");
    if (e.value == "KEYWORD SEARCH   " && e.style.color == "rgb(166, 166, 166)" || String(e.style.color).toUpperCase() == "#A6A6A6") {        
        e.value = "";
    }
    
    
    // TESTING URL SEARCH...
    // ================================================================
    var artistName = "";
    var artistSelect = document.getElementById("searchArtistId");
    if (artistSelect.selectedIndex > 0)
        artistName = artistSelect.options[artistSelect.selectedIndex].text;    
    
    var searchString = e.value;
    searchString = searchString.replace(/^\s+|\s+$/, "");
    
    var action = "";
    if (artistName != "" && searchString != "")
        action = artistName + "/images/" + myEscape(searchString);
    else if (artistName != "")
        action = artistName + "/images";
    else if (searchString != "")
        action = "images/" + myEscape(searchString);
    else
        action = "images";
    action = action.replace(/\s/g, "+");
    
    // var f = document.getElementById("headerSearchForm2");
    // f.action = action;
    
    document.location.href = getUrlV2("root", "none") + action;
    
    /*
    document.getElementById("searchArtistIdSelected").disabled = true;
    document.getElementById("searchArtistId").disabled = true;
    document.getElementById("keywordTxt").disabled = true;
    document.getElementById("searchSubmitImage").disabled = true;
    */
    // ================================================================
    
    /*
    var empF = document.getElementById("employeeFilters");
    if (empF) {        
        var searchArtistId = document.getElementById("searchArtistId");
        var aspnetForm = document.getElementById("aspnetForm");
        document.getElementById("keywordTxtHidden").value = e.value;
        document.getElementById("artistIdHidden").value = searchArtistId.options[searchArtistId.selectedIndex].value;
        
        aspnetForm.submit();
        return false;
    }
    */
    
    // if (ev && ev.preventDefault)
    //    ev.preventDefault();            
    
    // f.submit();
        
    return true;
}

function initInput(e, defaultText, initColor) {
    
    if (e.value == "" || e.value == defaultText) {
        e.style.color = initColor;
        e.value = defaultText;
    }
}

function openMiniHelper(url) {
    return window.open(url,"qwload","location=0,status=0,scrollbars=0,width=1,height=1,top=" + screen.height/2 + ",left=" + screen.width/2);
}

function gotoImageDetails(imageId) 
{
    // document.getElementById("baseTag").href = "";
    
    // var url = getRootUrl() + "Image.aspx?image=" + imageId;
    // url = getRootUrl() + "image.aspx?image=284&accessguid=BC95F6FA-6A20-4922-B75F-1CA3F0FB4542";
    // alert(url);
    // url = "Image.aspx?image=" + imageId;
    
    var url = "Image.aspx?image=" + imageId;
    
    if (document.all)
        url = getUrlV2("root", "none") + "Image.aspx?image=" + imageId;
        
    //alert(url);
    
    document.location.href = url;
}

