var timeout;

function showlayer(idname) {
	clearTimeout(timeout);
	var lager = document.getElementById(idname);
	lager.style.visibility = "visible";
}

/*function showlayerstoptime(idname) {
	var lager = document.getElementById(idname);
	lager.style.visibility = "visible";
	clearTimeout(timeout);
}
*/
function hidelayer(idname) {
	var lager = document.getElementById(idname);
    lager.style.visibility = "hidden";
}
function hidelayerwhithtimeout(idname) {
    var lager = document.getElementById(idname);
	timeout = window.setTimeout("document.getElementById('"+idname+"').style.visibility = 'hidden';", 500);
}



/* menu */

/*function closeMen(e) {
	var l=document.getElementById("products_dropdown");
	if (navigator.userAgent.indexOf("MSIE")!=-1) {
		if (!contains(this,window.event.toElement)) hidelayerwhithtimeout("products_dropdown");
	} else {
		if (!e || !contains(this,e.relatedTarget)) hidelayerwhithtimeout("products_dropdown");
	}
}
*/
// Return true if node a contains node b.
function contains(a, b) {
	//b.tagName.toUpperCase()!="INPUT" && 
  while (b.parentNode) {
	b = b.parentNode;
  	if (b==a) return true;
  }
//  if (b.tagName.toUpperCase()!="INPUT") return true;
//  else 
	return false;
}

var animobj=new Array();
function init() {
	// menu
	if (document.getElementById("products_dropdown")) {
		document.getElementById("products_link").onmouseover=new Function("showlayer('products_dropdown')");
		document.getElementById("products_link").onmouseout=new Function("hidelayerwhithtimeout('products_dropdown')");
		document.getElementById("products_dropdown").onmouseover=new Function("showlayer('products_dropdown')");
		document.getElementById("products_dropdown").onmouseout=new Function("hidelayer('products_dropdown')");
	}
	if (document.getElementById("login")) {
		animobj[0]=new Anim(0,document.getElementById("login"));
// Får ändra detta till klick - men då måste ju formulärets klickande stoppa... alt att man bara kan klicka på login-texten...
//		document.getElementById("login").onmouseover=loginDown;
//		document.getElementById("login").onmouseout=loginUp;
		var lbox=document.getElementById("login");
		lbox.style.right="-144px";
		lbox.style.top="-72px";
		lbox.onclick=loginDown;

		document.getElementById("clickarea").onclick=loginUp;
		
		if (document.getElementById("passwd")) {
			document.getElementById("passwd").setAttribute("placeholder", "password");
			document.getElementById("username").setAttribute("placeholder", "username/email");
			activatePlaceholders();
		}
	}
	
}	

window.onload=init;


/* anim start */
function slideDown() {
	if (this.count<this.curve.length) {
		this.obj.style.top=this.curve[this.count]+"px";
		this.obj.style.right=this.curve[this.count]*2+"px";
		this.count++;
		this.timer=setTimeout("animobj["+this.no+"].slideDown()",30);
	} else {
		this.count=0;
		this.timer=false;
	}
}
function slideUp() {
	if (this.count<this.curve.length) {
		this.obj.style.top=(this.curve[this.curve.length-1-this.count])+"px";
		this.obj.style.right=(this.curve[this.curve.length-1-this.count])*2+"px";
		this.count++;
		this.timer=setTimeout("animobj["+this.no+"].slideUp()",30);
	} else {
		this.count=0;
		this.timer=false;
	}
}

function slideLeft() {
	if (this.countX<this.curve.length) {
		this.obj.style.right=this.curve[this.count]+"px";
		this.countX++;
		this.timerX=setTimeout("animobj["+this.no+"].slideDown()",30);
	} else {
		this.countX=0;
		this.timerX=false;
	}
}
function slideRight() {
	if (this.count<this.curve.length) {
		this.obj.style.right=(this.curve[this.curve.length-1-this.count])+"px";
		this.count++;
		this.timer=setTimeout("animobj["+this.no+"].slideUp()",30);
	} else {
		this.count=0;
		this.timer=false;
	}
}


function Anim(no,obj) {
	this.no=no;
	this.obj=obj;
	this.count=0;
	this.countX=0;

	this.f=this.obj.offsetHeight;
	this.curve=new Array()
	for (var i=.23;i<1.6;i+=.2) {
		this.curve[this.curve.length]=Math.round(Math.sin(i)*this.f - this.f);
	}
	var lastval=this.curve[this.curve.length-1];
	if (lastval) { // adjust to zero last
		for(var j=0;j<this.curve.length;j++) {
			this.curve[j]=this.curve[j]-lastval;
		}
	}
	this.slideDown=slideDown;	
	this.slideUp=slideUp;	
	this.timer=0;	
	this.timerX=0;	
}

var loginActive=false;
function loginDown(e) {
	if (!animobj[0].timer && loginActive==false) {
		animobj[0].slideDown();
		loginActive=true;
	}
}
function loginUp(e) {
	if (!animobj[0].timer && loginActive) {
/*		if (navigator.userAgent.indexOf("MSIE")!=-1) {
			if (!contains(this,window.event.toElement)) {		
*/				loginActive=false;
				animobj[0].slideUp();
//				animobj[0].slideLeft();
/*				
			}
		} else {
			if (!e || !contains(this,e.relatedTarget)) { 
				loginActive=false;
				animobj[0].timer=setTimeout("animobj["+0+"].slideUp()",500); 
			}
		}*/
	if (!e) e=window.event;	
    if (e.stopPropagation) {
      e.stopPropagation();
    } else {
      e.cancelBubble = true;
    }		
	}
}

function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase(); 
if (detect.indexOf("safari") > 0) return false;
var inputs = document.getElementsByTagName("input");
for (var i=0;i<inputs.length;i++) {
  if (inputs[i].getAttribute("type") == "text" || inputs[i].getAttribute("type") == "password") {
   if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
    inputs[i].value = inputs[i].getAttribute("placeholder");
    inputs[i].onfocus = function() {
     if (this.value == this.getAttribute("placeholder")) {
      this.value = "";
     }
     return false;
    }
    inputs[i].onblur = function() {
     if (this.value.length < 1) {
      this.value = this.getAttribute("placeholder");
     }
    }
   }
  }
}
}
