var IMAGEPATH = "http" + (window.location.protocol.indexOf('https:')==0?'s':'') + "://tag1.virtual-identity.com/stoerer/resources";
//var IMAGEPATH = "http://tag1.virtual-identity.com/stoerer/resources";
//var IMAGEPATH = "./resources";

/**************************************************/
/*START: toggle clip function*/
var iMINWIDTH = 30;
var iMAXWIDTH = 340;
var iSTEP = 10;
var iDELAY_TIMEOUT = 1000;

//save timeout-id
var delayTimeout;

function togglestoerer(side)
{
	clipElement = document.getElementById("vi-stoerer");
	clickLayer = document.getElementById("vi-clicklayer");
	if (typeof(nextAction) == 'undefined') {
		nextAction = "min";
	}
	if (checkCookie()) {
		var state = readCookie();
		if (state == 'hide') nextAction = "max";
		if (state == 'show') nextAction = "min";
	}
	if (nextAction == "min") {
		toggleClipTimed(iMAXWIDTH, "min", side);
		nextAction = "max";
		writeCookie("hide");
	} else {
		toggleClipTimed(iMINWIDTH, "max", side);
		nextAction = "min";
		writeCookie("show");
	}
}

function toggleClipTimed(w, action, side)
{
	//clear timeout, ie6 needed it
	window.clearTimeout(delayTimeout);

	if (side == "left") clipElement.style.left = (w - iMAXWIDTH) + "px";
	if (side == "right") clipElement.style.width = w + "px";
	clipElement = document.getElementById("vi-stoerer");
	clickLayer = document.getElementById("vi-clicklayer");
	clipElement.style.visibility="visible";
	clickLayer.style.display="block";
	if (action=="min") {
		w-=iSTEP;
		if(w >= iMINWIDTH) window.setTimeout("toggleClipTimed("+w+", 'min', '"+side+"')", 10);
		if(w == iMINWIDTH) {
			if (side == "left") clickLayer.style.backgroundImage = "url("+IMAGEPATH+"/arrow-right.gif)";
			if (side == "right") clickLayer.style.backgroundImage = "url("+IMAGEPATH+"/arrow-left.gif)";
			clickLayer.style.backgroundPosition="13px 81px";
		}
	} else {
		w+=iSTEP;
		if(w <= iMAXWIDTH) window.setTimeout("toggleClipTimed("+w+", 'max', '"+side+"')", 10);
		if(w == iMAXWIDTH) {
			clickLayer.style.backgroundImage = "url("+IMAGEPATH+"/close.gif)";
			clickLayer.style.backgroundPosition = "5px 75px";
		}
	}
}
/*END: toggle clip function*/
/**************************************************/

function setState(state, side)
{
	clipElement = document.getElementById("vi-stoerer");
	clickLayer = document.getElementById("vi-clicklayer");
	// use visibility=hidden to make sifr-replacement possible (in conjunction with delay)
	clipElement.style.visibility="hidden";
	
	clickLayer.style.display="none";
	clickLayer.style.backgroundImage = "url("+IMAGEPATH+"/close.gif)";
	clickLayer.style.backgroundPosition = "5px 75px";
	
	if (side == "left") {
		if (state == "hide") {
			clipElement.style.left = (iMINWIDTH - iMAXWIDTH) + "px";
			clickLayer.style.backgroundImage = "url("+IMAGEPATH+"/arrow-right.gif)";
			clickLayer.style.backgroundPosition="13px 81px";
			clipElement.style.visibility="visible";
			clickLayer.style.display="block";
		}
		else {
			delayTimeout = window.setTimeout(function() { toggleClipTimed(iMINWIDTH, "max", side) }, iDELAY_TIMEOUT);
		}
	}
	if (side == "right") {
		if (state == "hide") {
			clipElement.style.width = iMINWIDTH + "px";
			clickLayer.style.backgroundImage = "url("+IMAGEPATH+"/arrow-left.gif)";
			clickLayer.style.backgroundPosition="13px 81px";
			clipElement.style.visibility="visible";
			clickLayer.style.display="block";
		}
		else {
			delayTimeout = window.setTimeout(function() { toggleClipTimed(iMINWIDTH, "max", side) }, iDELAY_TIMEOUT);
		}
	}
}

function checkCookie()
{
	var c = document.cookie;
	var state = c && c.search('clip=') != -1;
	var side = "left";
	if(!state) {
		var clipElement  = document.getElementById("vi-stoerer");
		if (clipElement && clipElement.className.indexOf('right')>-1) {
			side = "right"
		}
		setState("show", side);
	}
	return state;
}

function readCookie()
{
	var c = document.cookie;
	var posName = c.indexOf('clip=');
	var posValueStart = c.indexOf("=", posName)+1;
	var posValueEnd = c.indexOf(";", posName);
	if (posValueEnd == -1) posValueEnd = c.length;
	
	var value = c.substring(posValueStart, posValueEnd);
	return unescape(value);
}

function writeCookie(state)
{
	var d = new Date();
	d = new Date(d.getTime() +1000*60*60*24);
	document.cookie = 'clip='+state+'; expires='+d.toGMTString()+';path=/;';
}
