var gClose = null;

function InitPage()
{
	LoadView();
	InitTooltips();
	InitCustomerMenu();
}

function InitCustomerMenu()
{
	var menus = $('content').select('div.topbar ul.customerfunctions li');

	for(var i=0;i<menus.length;i++){
		if(menus[i].parentNode.parentNode.parentNode==$('content')){
			menus[i].onmouseover = menus[i].onmousemove = function()
			{
				var ul = this.getElementsByTagName('ul');
				
				if(gClose && gClose.element==ul[0]){
					gClose.cancel();
				}
				
				if(!Element.visible(ul[0])){
					Effect.Appear(ul[0],{ duration: 0.3 });
				}
			}
			
			menus[i].onmouseout = function()
			{
				var ul = this.getElementsByTagName('ul');
				gClose = Effect.Fade(ul[0],{ duration: 0.1, delay: 0.2 });
			}
		}
	}
}

function InitTooltips()
{
	var input = $('content').select('.siteform input');
	for(var i=0;i<input.length;i++){
		input[i].onfocus = function(){
			if(this.type=='text' || this.type=='password'){
				Element.addClassName(this.parentNode,'active');
				
				if(Element.hasClassName(this,'shortpre')){
					this.nextSibling.nextSibling.nextSibling.nextSibling.style.display = 'block';
				}else{				
					this.nextSibling.nextSibling.style.display = 'block';
				}
			}
		}
		
		input[i].onblur = function(){
			if(this.type=='text' || this.type=='password'){
				Element.removeClassName(this.parentNode,'active');
				if(!Element.hasClassName(this.parentNode,'error')){
					if(Element.hasClassName(this,'shortpre')){
						this.nextSibling.nextSibling.nextSibling.nextSibling.style.display = 'none';
					}else{
						this.nextSibling.nextSibling.style.display = 'none';
					}
				}
			}
		}
	}
	
	var select = $('content').select('.siteform select');
	for(i=0;i<select.length;i++){
		select[i].onfocus = function(){
			Element.addClassName(this.parentNode,'active');
			if(Element.hasClassName(this,'short')){
				this.nextSibling.nextSibling.nextSibling.nextSibling.style.display = 'block';
			}else{
				this.nextSibling.nextSibling.style.display = 'block';
			}
		}
		
		select[i].onblur = function(){
			Element.removeClassName(this.parentNode,'active');
			if(!Element.hasClassName(this.parentNode,'error')){
				if(Element.hasClassName(this,'short')){
					this.nextSibling.nextSibling.nextSibling.nextSibling.style.display = 'none';
				}else{
					this.nextSibling.nextSibling.style.display = 'none';
				}
			}
		}
	}
}

function TargetBlank (url) {
	var newwindow	= window.open(url, "newwindow"+String(Math.round(Math.random()*10)),"menubar=yes,location=yes,status=yes,scrollbars=yes,toolbar=yes,resizable=yes");
	newwindow.focus();
	return false;
}

function LoadView()
{
	var view = ReadCookie('view');
	if(view){
		SetActiveView(view);
		
		if($(view)){
			$(view).parentNode.className = 'active';
		}
	}else{
		SetActiveView('Galerieansicht');
		
		if($('tab')){
			var li = $('tab').select('li');
			for(var i=0;i<li.length;i++){
				li[i].className = '';
			}
			li[0].className = 'active';
		}
	}
}

function ToggleView(Link,View)
{
	SetActiveView(View);
	
	var li = $('tab').select('li');
	for(var i=0;i<li.length;i++){
		li[i].className = '';
	}
	Link.parentNode.className = 'active';
	Link.blur();
	
	return false;
}

function ToggleLogin(IsClient)
{
	if(IsClient.checked){
		$('fs-login').style.display = 'block';
		$('fs-data').style.display = 'none';
		$('fs-delivery').style.display = 'none';
		$('fs-pass').style.display = 'none';
		$('fs-remarks').style.display = 'none';
	}else{
		$('fs-login').style.display = 'none';
		$('fs-data').style.display = 'block';
		if($('f-delivery').checked){
			$('fs-delivery').style.display = 'block';
		}else{
			$('fs-delivery').style.display = 'none';
		}
		$('fs-pass').style.display = 'block';
		$('fs-remarks').style.display = 'block';
	}
}

function TogglePayment(Payment)
{
	switch(Payment.value){
	case 'bank':
		$('fs-bank').style.display = 'block';
		$('fs-creditcard').style.display = 'none';
		break;
	case 'creditcard':
		$('fs-bank').style.display = 'none';
		$('fs-creditcard').style.display = 'block';
		break;
	default:
		$('fs-bank').style.display = 'none';
		$('fs-creditcard').style.display = 'none';
		break;
	}
}

function SetActiveView(View)
{
	for(var i=0; (a=document.getElementsByTagName("link")[i]); i++){
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")){
			if(a.getAttribute("title")==View){
				a.disabled = true;
				a.disabled = false;
				CreateCookie('view',View,365);
			}else{
				a.disabled = true;
			}
		}
	}
}

function ReInit(Num)
{
	$('tab-'+Num).style.display = 'block';
	new Interface.Slidebox('scrollable-wrapper-'+Num,'scrollable-'+Num,'slot', 'scroller-'+Num, { left: 'medialeft', right: 'mediaright', track: 'track', handle: 'handle', speed: 10, elementmargin: 0 });

	return false;
}

function changeImage(Tag) {
	Tag = urldecode(Tag);
	var tmp = $$('#active-image img');
	tmp[0].replace(Tag);
	return false;
}

function urldecode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'

    var histogram = {}, histogram_r = {}, code = 0, str_tmp = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urlencode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}

/**
* Creates a cookie
*
* @param	string		name of the cookie
* @param	string		value
* @param	integer		number of days till expires
*/
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=/";
}

/**
* read a cookie by name
*
* @param	string		name of cookie
* @return	string		get value of cookie
*/
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 '';
}

/**
* erase a cookie by name
*
* @param	string		name of cookie
*/
function EraseCookie(name) {
	CreateCookie(name,"",-1);
}
