﻿var xmlHttp;

var timeout;
	var ww=500;
		var hh=500;
		var timeout=0;
	

function fade(idSlike,smjer,trenutno){
	
	if (isNaN(trenutno)) trenutno=1;
	if (smjer==1 && trenutno<1) trenutno+=0.1;
	if (smjer==-1 && trenutno>0.3) trenutno-=0.1;
	document.getElementById(idSlike).style.opacity=trenutno;
	document.getElementById(idSlike).style.filter="alpha(opacity="+Math.floor(trenutno*100)+");";
	
	if ((trenutno>0.3 && smjer==-1)) 
	timeout=setTimeout(function(){fade(idSlike,smjer,trenutno)},70); 

	if ((trenutno<1 && smjer==1)) 
	timeout=setTimeout(function(){fade(idSlike,smjer,trenutno)},70);
}

	
		function fullopacity(ime){
			object=document.getElementById(ime).style;
			object.opacity = (100 / 100); 
			object.MozOpacity = (100 / 100); 
			object.KhtmlOpacity = (100 / 100); 
			object.filter = "alpha(opacity=" + 100 + ")"; 
		}
		
		function lowopacity(ime){
			object=document.getElementById(ime).style;
			object.opacity = (80 / 100); 
			object.MozOpacity = (80 / 100); 
			object.KhtmlOpacity = (80 / 100); 
			object.filter = "alpha(opacity=" + 80 + ")"; 
		}
		function povecaj(el, tp, lp, progress){
				//document.getElementById("countries").innerHTML=;
				if (progress>100) progress=100;
				el.style.marginTop=10+tp-(progress/100)*tp+"px";
				el.style.marginLeft=50+lp-(progress/100)*lp+"px";
				el.style.width=(progress/100)*(ww)+"px";			
				el.style.height=(progress/100)*(hh)+"px"; 
				//document.write("ovod");
				if (Math.abs(progress)<100){
					setTimeout (function(){povecaj(el,tp,lp,progress+6)},33);
				} else el.style.overflow="auto";
			}
			
			function smanji(el, tp, lp, progress){
				//document.getElementById("countries").innerHTML=;
				if (progress>100) progress=100;
				el.style.marginTop=0+(progress/100)*tp;
				el.style.marginLeft=0+(progress/100)*lp;
				el.style.width=((100-progress)/100)*(wn*ww);
				el.style.height=((100-progress)/100)*(hh*hn);
				//document.write("ovod");
				if (Math.abs(progress)<100){
					setTimeout (function(){smanji(el,tp,lp,progress+7)},33);
				} else document.all.center.removeChild(el);
			}
			
			function killChild(id){
				var el=document.getElementById(id);
				var tc=(parseInt(id.substring(8,10))*hh)+hh/2;
				if (id.substring(8,10)=="08") tc=8*hh;
				if (id.substring(8,10)=="09") tc=9*hh;
				lc=(parseInt(id.substring(10,12))*ww)+ww/2;
				if (id.substring(10,12)=="08") lc=8*ww;
				if (id.substring(10,12)=="09") lc=9*ww;
				//el.innerHTML=id+" "+id.substring(8,10)+" "+id.substring(10,12);
				el.style.overflow="hidden";
				smanji(el,tc,lc,0);
				
			}
			

function initGallery(id){
		
	var htmlCode="<a id=\"close\" href=\"javascript:(closeArticle());\"></a><div id=\"articlePicture\" class=\"articlePicture\"></div><div id=\"articleName\"></div><div id=\"articleData\"></div>";
	var opened = document.createElement('div');
	opened.setAttribute("id","articleBig");
	opened.className="articleBig";
	opened.style.width=0;//140;
	opened.style.height=0;//20;
	opened.style.overflow="hidden";//20;
	opened.style.marginTop=260+"px";
	opened.style.marginLeft=300+"px"
	opened.innerHTML=htmlCode;//document.getElementById("hiddenDiv").innerHTML;
				
	document.getElementById("gallery").style.display="none";
				
	document.getElementById("center").appendChild(opened);
				
				//document.write("ovod");

	getArticle(id);
	povecaj(opened,parseInt(opened.style.marginTop),parseInt(opened.style.marginLeft),0);
				
}

function closeArticle(){
	el=document.getElementById("articleBig");
	document.getElementById("center").removeChild(el);
	document.getElementById("gallery").style.display="block";
}

function fade(idSlike,smjer,trenutno){	
	if (isNaN(trenutno)) trenutno=1;
	if (smjer==1 && trenutno<1) trenutno+=0.1;
	if (smjer==-1 && trenutno>0.3) trenutno-=0.1;
	document.getElementById(idSlike).style.opacity=trenutno;
	document.getElementById(idSlike).style.filter="alpha(opacity="+Math.floor(trenutno*100)+");";
	
	if ((trenutno>0.3 && smjer==-1)) 
	timeout=setTimeout(function(){fade(idSlike,smjer,trenutno)},70); 

	if ((trenutno<1 && smjer==1)) 
		timeout=setTimeout(function(){fade(idSlike,smjer,trenutno)},70);
}

function getArticle(articleId){
	clearTimeout(timeout);
	fade('articlePicture',-1,1);
	
	xmlHttp=getXmlHttpObject();
	if (xmlHttp==null){
		document.write("nevalja ti brovser!!!");
	}
	var url=domain+"getArticleData.php";
	url=url+"?id="+articleId;
	url=url+"&cat="+document.getElementById("category").innerHTML;
	url=url+"&subCat="+document.getElementById("subCategory").innerHTML;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=articleOmnited;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	
}




function articleOmnited(){
	if (xmlHttp.readyState==4  || xmlHttp.readyState=="complete" ){
		textIzlaz=xmlHttp.responseText;
		var niz=textIzlaz.split("|");//xmlHttp.responseText.split("|");
		document.getElementById("articlePicture").style.opacity=0.3;
		document.getElementById("articlePicture").style.filter="alpha(opacity=30);";
		document.getElementById("articlePicture").innerHTML=niz[0];
		document.getElementById("articleName").innerHTML=niz[1];
		document.getElementById("articleData").innerHTML=niz[2];
		//document.getElementById("artId").innerHTML=niz[3];
		clearTimeout(timeout);
		fade('articlePicture',1,0.3);
	}
	
}

function otvoriSlicicu(picId){
	clearTimeout(timeout);
	fade('artPicture',-1,1);
	
	xmlHttp=getXmlHttpObject();
	if (xmlHttp==null){
		document.write("nevalja ti brovser!!!");
	}
	var url=domain+"dohvatiSlicicu.php";
	url=url+"?id="+picId;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=slicicaDohvacena;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	
}




function slicicaDohvacena(){
	if (xmlHttp.readyState==4  || xmlHttp.readyState=="complete" ){

		textIzlaz=xmlHttp.responseText;
	//	var niz=textIzlaz.split("|");//xmlHttp.responseText.split("|");
		document.getElementById("artPicture").style.opacity=0.3;
		document.getElementById("artPicture").style.filter="alpha(opacity=30);";
		document.getElementById("artPicture").innerHTML=textIzlaz;

		//document.getElementById("artId").innerHTML=niz[3];
		clearTimeout(timeout);
		fade('artPicture',1,0.3);
	}
	
}


function dodajUKosaricu(articleId){
	
	var artikli=readCookie("artikli");
	if (artikli==null) artikli="";
	if (artikli.indexOf("|"+articleId+"|",0)==-1){
		if (artikli!="") 
			artikli=artikli+articleId+"|";
		else artikli="|"+articleId+"|";
		eraseCookie("artikli");
		createCookie("artikli",artikli,10);
		dohvatiSadrzajKosarice(artikli,"Molimo pričekajte, zahtjev se obrađuje...,");
		
		var brArt=readCookie("brojArtikala");
		if (brArt==null) brArt=0;
		brArt++;
		eraseCookie("brojArtikala");
		createCookie("brojArtikala",brArt,10);
	} else
		alert("Ovaj artikl se već nalazi u košarici!");
}

function izbaciIzKosarice(articleId){
	
	var brArt=readCookie("brojArtikala");
	if (brArt==null) brArt=0;
	brArt--;
	eraseCookie("brojArtikala");
	createCookie("brojArtikala",brArt,10);
	var artikli=readCookie("artikli");
	if (artikli==null) artikli="";
	artikli=artikli.replace("|"+articleId+"|","|");
	artikli=artikli.replace("||","|");
	artikli=artikli.replace("||","|");
	eraseCookie("artikli");
	createCookie("artikli",artikli,10);
	dohvatiSadrzajKosarice(artikli,"Molimo pričekajte, zahtjev se obrađuje...");
}

function isprazniKosaricu(){
	eraseCookie("brojArtikala");
	eraseCookie("artikli");
	dohvatiSadrzajKosarice("","");
}

function osvjeziKosaru(){
	var artikli=readCookie("artikli");
	dohvatiSadrzajKosarice(artikli,"");
}
function dohvatiSadrzajKosarice(artikli,poruka){
	
	if (artikli!="" && artikli!="|" && artikli!=null )
	{
		document.getElementById("sadrzajKosarice").innerHTML="<img src=\""+domain+"pictures/graphics/loading.gif\"><br />"+poruka;
		xmlHttp=getXmlHttpObject();
		if (xmlHttp==null){
			document.write("nevalja ti brovser!!!");
		}
		var url=domain+"getShopCart.php";
		url=url+"?id="+artikli;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=kosaricaDohvacena;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	} else {
		document.getElementById("sadrzajKosarice").innerHTML="Vaša košarica trenutno je prazna!";
		var urlPomoc=domain+aditiv+"kupnja"+sufix;
		document.getElementById("kontrole").innerHTML="<a href=\""+urlPomoc+"\">Kako kupiti?</a>";
	}
	
}




function kosaricaDohvacena(){
	if (xmlHttp.readyState==4  || xmlHttp.readyState=="complete" ){
		result=xmlHttp.responseText;
		result=result.split("|");
		if (result[1]=="ok"){
			document.getElementById("sadrzajKosarice").innerHTML=result[0];
			var kontrole="<a class=\"left\" href=\"javascript:isprazniKosaricu();\" title=\"Isprazni ko&#154;aricu\">Isprazni</a>";
			kontrole=kontrole+"<a class=\"right\"  href=\""+domain+aditiv+"kupi"+sufix+"\" title=\"Isprazni košaricu\">Kupi</a>";
			document.getElementById("kontrole").innerHTML=kontrole;
		}
		else {
			isprazniKosaricu();
			osvjeziKosaru();
		}
	}
	
}


function getXmlHttpObject()
{
var xmlHttp=null;
try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
catch (e)
	 {
	 // Internet Explorer
		 try
			  {
			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
		 catch (e)
			  {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
	 }
return xmlHttp;
}