// GLOBALS
var nNavigationTimeout;
var animationTimeout;
//CONSTANTS
var TIMEOUT=1000;

//CONSTANTS
//GLOBALS


function findPosX(obj){
	var curleft = 0;
    if(obj.offsetParent)
        while(1){
          curleft += obj.offsetLeft;
          if(!obj.offsetParent) break;
          obj = obj.offsetParent;
        }
    else if(obj.x) curleft += obj.x;
    return curleft;
}

function findPosY(obj){
    var curtop = 0;
    if(obj.offsetParent)
        while(1){
          curtop += obj.offsetTop;
          if(!obj.offsetParent)break;
          obj = obj.offsetParent;
        }
    else if(obj.y) curtop += obj.y;
    return curtop;
}

  
function getAJAX(){
   var xmlHttp=(typeof(XMLHttpRequest)!="undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
   xmlHttp=(typeof(xmlHttp)=="undefined")? new ActiveXObject("Microsoft.XMLHTTP") : xmlHttp;
   return xmlHttp;
}


function SEOName(bid,name,mode){
	var _iso88592 =new Array("õ", "Õ", "û", "Û", "ö", "Ö","ü","Ü","á","Á","é","É","í","Í","ó","Ó","ú","Ú","/","|"," ","_","&",",",")","(",".","+","'","`","!","---","--","ä","?");
	var _iso88591 =new Array("&otilde;", "&Otilde;", "&ucirc;", "&Ucirc;", "&ouml;", "&Ouml;","&uuml;","&Uuml;","&aacute;","&Aacute;","&eacute;","&Eacute;","&iacute;","&Iacute;","&oacute;","&Oacute;","&uacute;","&Uacute;","/","|"," ","-","&",",",")","(",".","+","'","","!","---","--","ä","?");
	var _Unix =new Array("o", "o", "u", "U", "o", "O","u","U","a","A","e","E","i","I","o","O","u","U","","","-","-","","","","","","","","-","","-","-","a","");
	var _url="";
		if(name!=""){
	/*var _name=str_replace($_iso88592,$_Unix,$name);
	$_name=str_replace($_iso88591,$_Unix,$_name);
	$_url.="".$bid.".".$_name.".".$mode.".html";*/
			var _name=name;
			var i=0;
			while(_iso88592[i]){
				_name=_name.replace(_iso88592[i],_Unix[i]);
				i++;	
			}
			i=0;
			while(_iso88591[i]){
				_name=_name.replace(_iso88591[i],_Unix[i]);
				i++;	
			}
			_url+=""+bid+"."+_name+"."+mode+".html";
		}

//$_url.="?IMRSID=$sid";
	return _url;
}


var buildMenuFromString=function(sMenu){

	var aMenu=sMenu.split("|");
	var oNavigationBody=document.getElementById("navigation_body");
	var oItem,aMenuItem;
	var i=0;
	while(oNavigationBody.childNodes[0])oNavigationBody.removeChild(oNavigationBody.childNodes[0]);
	while(aMenu[i]){
		aMenuItem=aMenu[i].split("@");
		oItem=document.createElement("a");
		oItem.appendChild(document.createTextNode(aMenuItem[0].substring(0)));
		oItem.setAttribute("href",aMenuItem[1]<=1?SEOName(aMenuItem[2],aMenuItem[0],"kategoria"):"javascript:return false;");
		oItem.setAttribute("name",aMenuItem[0]);
		if(aMenuItem[2]==""){
			oItem.onclick=function(){showNavigation();}
			if(oItem.addEventListener)oItem.addEventListener("click",showNavigation,true);
			if (oItem.captureEvents) oItem.captureEvents(Event.CLICK);		
		}
		oNavigationBody.appendChild(oItem);
		i++;
	}
	var fixFloat=document.createElement("div");
	fixFloat.setAttribute("class","clear");
	fixFloat.innerHTML="&nbsp;";
	oNavigationBody.appendChild(fixFloat);

}

var hideNavigation=function(e){
	nNavigationTimeout=setTimeout("document.getElementById('navigation_panel').style.display='none'",TIMEOUT);
}


var easeLinear=function(obj,deltaX,deltaY,toX,toY){
	
	if(Math.round(findPosX(obj))!=Math.round(toX) || Math.round(findPosY(obj))!=Math.round(toY)){
		if(Math.round(findPosX(obj))!=Math.round(toX)){
			obj.style.left=Number(obj.style.left.substr(0,obj.style.left.indexOf('px')))+deltaX+"px";
		}
		if(Math.round(findPosY(obj))!=Math.round(toY)){
			obj.style.top=Number(obj.style.top.substr(0,obj.style.top.indexOf('px')))+deltaY+"px";
		}
		animationTimeout=setTimeout(function(){easeLinear(obj,deltaX,deltaY,toX,toY);},1000);
		//document.getElementById("debug").innerHTML=obj.style.left+" "+toX+"<br>"+obj.style.top+" "+toY;
	}else{
		//document.getElementById("debug").innerHTML+="x <br>";
		//clearInterval(animationTimeout);
	}

}

var startTween=function(obj,time,fineness,toX,toY){

	var deltaT=time/fineness;
	
	var fromX=findPosX(obj);
	var fromY=findPosY(obj);
	var deltaX=(toX-fromX)/fineness;
	var deltaY=(toY-fromY)/fineness;
	animationTimeout=setTimeout(function(){easeLinear(obj,deltaX,deltaY,toX,toY);},1000);
}

var showNavigation=function(e){
	clearTimeout(nNavigationTimeout);
	//target
	var e=(e==undefined)?window.event:e;
	var target=(e.target!=undefined)?e.target:e.srcElement;
	var sTitle=target.innerHTML;
	var sName=target.name;
	var iMenuItemY=findPosY(target);
	var iMenuItemX=findPosX(target);
	//navibox
	var oNavigation=document.getElementById("navigation_panel");
	oNavigation.style.position="absolute";
	var oNavigationTitle=document.getElementById("navigation_title").firstChild;
	var oNavigationBody=document.getElementById("navigation_body");

	if(target.parentNode.id!="navigation_body" && target.parentNode.parentNode.id!="navigation_title"){
		//oNavigationTitle.innerHTML=sTitle;

		while(oNavigationTitle.childNodes[0])oNavigationTitle.removeChild(oNavigationTitle.childNodes[0]);
		//if(oNavigation.style.display!="none"){
		/*	oNavigation.style.zIndex=100;
			document.getElementById("debug").innerHTML+="<br>"+(iMenuItemX+220)+" "+iMenuItemY+" <br> "+findPosX(oNavigation)+" "+findPosY(oNavigation);
			startTween(oNavigation,10,10,(iMenuItemX+220),iMenuItemY);*/
			
			//var times=5;
		//	animationTimeout=setTimeout(function(){alert(self); times--; animationTimeout=setTimeout(function(){this.toSource();},100);},100);
											
		//}else{
			oNavigation.style.top=iMenuItemY+"px";
			oNavigation.style.left=(iMenuItemX+220)+"px";
		//}
		var	oItem=document.createElement("a");
		oItem.appendChild(document.createTextNode(sTitle));
		oItem.setAttribute("href","javascript:return false;");
		oItem.setAttribute("name",sTitle);

		oItem.onclick=function(){showNavigation();}
		if(oItem.addEventListener)oItem.addEventListener("click",showNavigation,true);

		if (oItem.captureEvents) oItem.captureEvents(Event.CLICK);		
		oNavigationTitle.appendChild(oItem);

	}else if(target.parentNode.id=="navigation_body"){
		oNavigationTitle.appendChild(document.createTextNode(" > "));
		var	oItem=document.createElement("a");
		oItem.appendChild(document.createTextNode(sTitle));
		oItem.setAttribute("href","javascript:return false;");
		oItem.setAttribute("name",target.name);
		oItem.onclick=function(){showNavigation();}
		if(oItem.addEventListener)oItem.addEventListener("click",showNavigation,true);

		if (oItem.captureEvents) oItem.captureEvents(Event.CLICK);		
		oNavigationTitle.appendChild(oItem);
		
	}else{
		while(target.nextSibling)target.parentNode.removeChild(target.nextSibling);
	}
	oNavigation.style.display="block";

	http=getAJAX();
	http.onreadystatechange=function(){
		if(http.readyState==4){
			if(http.responseText.length>0){
				buildMenuFromString(http.responseText);
			}
		}

	}
	var	param="needle="+sName;
	//+getFormParams()+"&from="+(Number(from)+10);
	//alert(encodeURI(sName));
	http.open("POST","c/t/js/ajax/getSubMenu.php",true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8;");
	http.setRequestHeader("Content-length", param.length);
	http.setRequestHeader("Connection", "close");
	http.send(param);
}

function showPicture(src,w,h){

					var bigPictureContainer=document.getElementById('big_product_picture');
					bigPictureContainer.src=src;
					bigPictureContainer.style.width=w+"px";
					bigPictureContainer.style.height=h+"px";
				}

function nLoad(e){
	var bigPictureContainer=document.getElementById('big_product_picture');
	var smallPicturesContainer=document.getElementById('small_images');
	if(bigPictureContainer && smallPicturesContainer){
		var smallPictures=smallPicturesContainer.childNodes;
		bigPictureContainer.style.cursor='pointer';
		bigPictureContainer.onclick=function(e){
			var e=(e==undefined)?window.event:e;
			var target=(e.target!=undefined)?e.target:e.srcElement;
			var testImage=new Image();
			testImage.src=target.src;
			window.open(target.src, 'wndPicture','status=0, toolbar=0, location=0, menubar=0, directories=0,resizable=0,scrollbars=0,width='+(testImage.width+20)+', height='+(testImage.height+20)+'');
			testImage=NULL;
		}
		
	}
	return false;
}


function showTellafriend(){
	var tafBg=document.getElementById('taf');
	var tafBody=document.getElementById('taf_body');
	tafBg.parentNode.removeChild(tafBg);
	document.body.appendChild(tafBg);
	
	tafBg.style.position='absolute';
	tafBg.style.top='0px';
	tafBg.style.left='0px';
	tafBg.style.backgroundImage='url(c/t/js/opacity.gif)';
	tafBg.style.width='100%';
	tafBg.style.height=document.getElementById('full_page').offsetHeight+'px';
	tafBg.style.display='block';
	tafBg.style.textAlign='center';
	tafBg.style.zIndex='500';
	
	tafBody.style.textAlign='left';
	tafBody.style.display='block';
	tafBody.style.backgroundColor='#ffffff';
	tafBody.style.margin='0 auto';
	tafBody.style.marginTop=Math.round((document.body.offsetHeight-tafBody.offsetHeight)/2)+'px';
	

}

function closeTellafriend(){
var tafBg=document.getElementById('taf');
tafBg.style.display='none';

}
