/***********************************************
* Some functionality for art-switch.com
***********************************************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/***********************************************
*   Set the window name to ArtSwitchMain
***********************************************/
function setName(){
	window.name="ArtSwitchMain"
}

/***********************************************
*   Opens a page as fullscreen
*   @string page Url of the page
***********************************************/
function openFullscreen(page) {
	var yes = 'yes';
	var no = 'no';
	var fullscreen = yes; // IE / Opera
	var menubar = no; // The File, Edit, View Menus
	var scrollbars = no; // Horizontal and vertical scrollbars
	var locationbar = no; // The location box with the site URL
	var directories = no; // the "What's New", "What Cool" links
	var resizable = yes; // Can the window be resized?
	var statusbar = no; // Status bar (with "Document: Done")
	var toolbar = no; // Back, Forward, Home, Stop toolbar
	if (navigator.appName == "Microsoft Internet Explorer"){ // better be ie6 at least
		windowprops = "width=" + (screen.width-10) + ",height=" + (screen.height-30) + ",top=0,left=0";
	} else { // i.e. if Firefox
		windowprops = "width=" + (screen.width) + ",height=" + (screen.height) + ",top=0,left=0";
	}
	windowprops += (",fullscreen=" + fullscreen +
	",menubars=" + menubar +
	",scrollbars=" + scrollbars +
	",location=" + locationbar +
	",directories=" + directories +
	",resizable=" + resizable +
	",status=" + statusbar +
	",toolbar=" + toolbar);
	window.open(page, 'fullPopup', windowprops);
}

/***********************************************
*   Toggles the display of a div element
*   @string which The id of the div element
*   @string hasOpenCloseIndicator Optional paramenter. If set the script sets the which+1 and which+2 elements accordingly
*   @string reverseme Optional paramenter. If set the script will change this element accordingly (reverse)
***********************************************/
function toggleDivDisplay(which, hasOpenCloseIndicator, reverseme) {
	if (document.getElementById(which).style.display!='none') {
		document.getElementById(which).style.display='none';
		if (hasOpenCloseIndicator) {
			document.getElementById(which+'1').style.display='none';
			document.getElementById(which+'2').style.display='inline';
		}
		if (reverseme) {
    		document.getElementById(reverseme).style.display='block';
		}
	} else {
		document.getElementById(which).style.display='block';
		if (hasOpenCloseIndicator) {
			document.getElementById(which+'1').style.display='inline';
			document.getElementById(which+'2').style.display='none';
		}
		if (reverseme) {
    		document.getElementById(reverseme).style.display='none';
		}
	}
}

/***********************************************
*   Choose which div to view on the artwork page
*   @string which The name of the element to view
***********************************************/
function viewdivArtwork(which){
	document.getElementById('mainfacts').style.display='none';
	document.getElementById("mainfactslinks").className="likebtnstyle";
	document.getElementById('mainartistcomment').style.display='none';
	document.getElementById("mainartistcommentlinks").className="likebtnstyle";
	document.getElementById('mainpubliccomments').style.display='none';
	document.getElementById("mainpubliccommentslinks").className="likebtnstyle";
	document.getElementById('mainartist').style.display='none';
	document.getElementById("mainartistlinks").className="likebtnstyle";
	document.getElementById('mainshortlist').style.display='none';
	document.getElementById("mainshortlistlinks").className="likebtnstyle";
	
	document.getElementById(which).style.display='block';
	document.getElementById(which+"links").className="likebtnstyleR";
}

/***********************************************
*   Handles the left right key functions
*   @event evt The Event
***********************************************/
function handleKeysLeftRight(evt) {
    if (keysactive) {
        var evt = (evt) ? evt : ((window.event) ? event : null);
        var keycode = evt.keyCode;
        if (keycode == 37) {
        	//LEFT
        	leftclick();
        }
        if (keycode == 39) {
        	//RIGHT
        	rightclick();
        }
    }
}

/***********************************************
*   "Log in using OpenID"
***********************************************/
openid_clicked = false;
function openidclick() {
	if (openid_clicked == true) {
		openid_clicked = false;
		document.getElementById("openid_whatlogin").value='login';
		document.getElementById("openid_loginlab").firstChild.nodeValue="USERNAME:";
		document.getElementById("openid_passwordall").style.visibility='visible';
		document.getElementById("openid_passwordbtn").style.visibility='visible';
		document.getElementById("openid_login").style.width='95px';
	} else {
		openid_clicked = true;
		document.getElementById("openid_whatlogin").value='openid';
		document.getElementById("openid_loginlab").firstChild.nodeValue="OPEN ID:";
		document.getElementById("openid_passwordall").style.visibility='hidden';
		document.getElementById("openid_passwordbtn").style.visibility='hidden';
		document.getElementById("openid_login").style.width='180px';
	}
}

/***********************************************
*	FULLSCREEN -----------------------------------------------------------------
***********************************************/

	/*
	* Split up the quere list at the end of e.g. an url
	*/
	function querylist(querystring) {
		if(querystring == '') return;
		var valuestring = unescape(querystring);
		valuestring = valuestring.slice(1);
		var pairs = valuestring.split("&");
		for (var i=0; i < pairs.length; i++) {
			var name = pairs[i].substring(0, pairs[i].indexOf("="));
			var value = pairs[i].substring(pairs[i].indexOf("=")+1, pairs[i].length);
			this[name] = value;
		}
	}
		
	/*
	*	Handle the key press events
	*/
	function fullscreenHandleKeys(evt) {
		var evt = (evt) ? evt : ((window.event) ? event : null);
		var keycode = evt.keyCode;
		
		//alert(keycode);
		
		if ( (keycode == 27) || (keycode == 13) ) {
			//ESC ENTER
			fullscreenCloseme();
		}
		if (keycode == 37) {
			//LEFT
			fullscreenPrevClick();
		}
		if (keycode == 39) {
			//RIGHT
			fullscreenNextClick();
		}
		if ( (keycode == 32) || (keycode == 83) ) {
			//SPACE	S
			fullscreenSpaceClick();
		}
		if (keycode == 66) {
			//B
			fullscreenBgBlack();
		}
		if (keycode == 87) {
			//W
			fullscreenBgWhite();
		}
		if (keycode == 83) {
			//S
			fullscreenShortlist();
		}
	}

	/*
	*	Sets BG color to black
	*/
	function fullscreenBgBlack() {
		colorbg = 1;
		try {
			document.getElementById('allpage').style.backgroundColor='#000000';
			document.getElementById('infotxt').style.backgroundColor='#060606';
			document.getElementById('menubar').style.backgroundColor='#060606';
			document.getElementById('menubartop').style.backgroundColor='#060606';
		} catch (e) {
			window.setTimeout("fullscreenBgBlack();", 500);
		}
	}
	
	/*
	*	Sets BG color to white
	*/
	function fullscreenBgWhite() {
		colorbg = 0;
		try {
			document.getElementById('allpage').style.backgroundColor='#FFFFFF';
			document.getElementById('infotxt').style.backgroundColor='#F6F6F6';
			document.getElementById('menubar').style.backgroundColor='#F6F6F6';
			document.getElementById('menubartop').style.backgroundColor='#F6F6F6';
		} catch (e) {
			window.setTimeout("fullscreenBgWhite();", 500);
		}
	}
	
	/*
	*	Shows the info text and the menubar and sets the lastmove date
	*/
	function fullscreenShowText() {
		if (thequerylist['slide']!='yes') {
			try {
				document.getElementById('infotxt').style.visibility='visible';
				document.getElementById('menubar').style.visibility='visible';
				document.getElementById('menubartop').style.visibility='visible';
			} catch (e) {
				window.setTimeout("fullscreenShowText();", 500);
			}
			now = new Date();
			lastmove = now.getTime();
		} else {
			try {
				document.getElementById('infotxt').style.visibility='visible';
				document.getElementById('menubar').style.visibility='visible';
				document.getElementById('menubartop').style.visibility='visible';
			} catch (e) {
				window.setTimeout("fullscreenShowText();", 500);
			}
		}
	}
	
	/*
	*	Hides the info text and the menubar
	*/
	function fullscreenHideText() {
		try {
			document.getElementById('infotxt').style.visibility='hidden';
			document.getElementById('menubar').style.visibility='hidden';
			document.getElementById('menubartop').style.visibility='hidden';
		} catch (e) {
			window.setTimeout("fullscreenHideText();", 500);
		}
	}
	
	/*
	*	Is called regulary to check if the lastmove was more then 2.5 seconds before.
	*	If yes - Call fullscreenHideText()
	*/
	function fullscreenChecklastmove() {
		now = new Date();
		if (thequerylist['new']=='yes') {
			waittime = 12500;
		} else {
			waittime = 3500;
		}
		if ( (lastmove+waittime) < now.getTime() ) {
			fullscreenHideText();
		}
	}
	
	/*
	*	Starts the regular check fullscreenChecklastmove() 
	*/
	function fullscreenChecklastmoveStart() {
		if (thequerylist['slide']=='yes') {
			fullscreenHideText();
			timerfullscreenChecklastmove = window.setInterval("fullscreenNextClick();", 3500);
		} else {
    		timerfullscreenChecklastmove = window.setInterval("fullscreenChecklastmove()", 500);
		}
	}
	
	/*
	*	Stops the regular check fullscreenChecklastmove()
	*/
	function fullscreenChecklastmoveStop() {
		if (timerfullscreenChecklastmove) { 
			window.clearInterval(timerfullscreenChecklastmove);
		}
		fullscreenShowText();
	}

	/*
	*	Set the artwork size and position the menu
	*/
	function fullscreenArtworksize() {
		//Get the sizes
		if (navigator.appName == "Microsoft Internet Explorer"){ // better be ie6 at least
			wwidth = allpage.clientWidth;
			wheight = allpage.clientHeight;
		} else { // i.e. if Firefox
			wwidth = window.innerWidth;
			wheight = window.innerHeight;
		}
		
		//Get the ratio
		awwidthratio = wwidth/awwidth;
		awheightratio = wheight/awheight;
		
		//Get smaller ratio
		if (awwidthratio>awheightratio) {
			nwawheight=((awheight*awheightratio)-10)+'px';
			nwawwidth=((awwidth*awheightratio)-20)+'px';
		} else {
			nwawwidth=((awwidth*awwidthratio)-20)+'px';
			nwawheight=((awheight*awwidthratio)-10)+'px';
		}
		
		try {
			document.getElementById('artImage').style.height = nwawheight;
			document.getElementById('artImage').style.width = nwawwidth;
		} catch (e) {
			window.setTimeout("fullscreenArtworksize();", 500);
		}
		
		fullscreenMenubarpos();
	}
	
	/*
	*	Set the menu bar position
	*/
	function fullscreenMenubarpos() {
		//Get the sizes
		if (navigator.appName == "Microsoft Internet Explorer"){ // better be ie6 at least
			wwidth = allpage.clientWidth;
			wheight = allpage.clientHeight;
		} else { // i.e. if Firefox
			wwidth = window.innerWidth;
			wheight = window.innerHeight;
		}
		
		document.getElementById('menubar').style.top = (wheight-30)+'px';
		if (thequerylist['slide']!='yes') {
			document.getElementById('menubar').style.left = (wwidth-300)+'px';
		} else {
			document.getElementById('menubar').style.left = (wwidth-165)+'px';
		}
		
		document.getElementById('menubartop').style.left = (wwidth-155)+'px';
	}
	
	function fullscreenOnLoadScript() {
		/*
		*	Sets the artwork size and moves the menu and sets the event handler for resize
		*/
		fullscreenArtworksize();
		window.onresize = fullscreenArtworksize;
		
		/*
		*	Either prepares next click for slideshow, or start check for last move
		*/
		fullscreenChecklastmoveStart();
		
		/*
		*	Checks the background color
		*/
		if (thequerylist['colorbg']==1) {
			fullscreenBgBlack();
		} else {
			fullscreenBgWhite();
		}
	
	}

/***********************************************
*	SIGNUP AND PAYMENT ---------------------------------------------------------
***********************************************/

/*
*	Math Round on 2 digits
*/
function roundoff(n) {
	return Math.round(n*100) / 100;
}

/*
*	Math Round on 2 digits by a already by 100 multiplied value
*/
function roundoff100(n) {
	return Math.round(n) / 100;
}

/*
*	Find the radio value
*/
function radioValue(element) {
    for (var i=0; i<element.length; i++) {
    	if (element[i].checked) return element[i].value;
    }
    return false;
}

/*
*	Select a dropdown option with value
*/
function selectSelectValue(element, value) {
	for (var i=0; i<element.options.length; i++) {
		if (element.options[i].value == value) 
			element.options[i].selected = true;		
		else
			element.options[i].selected = false;	
	}
}
