var http_request = false;
var scrolling = false;

function getSearchResults(){
	if (!scrolling){
        new  SmoothScroll({  duration:500  }, window); //700
        
        var fx = new Fx.Morph($("content"), {duration:150, link:'cancel'});
	    fx.start({
	    	'opacity':0.3
	    })
        
	    
	    scrolling = true;
	}
    
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
		    Custom.destroy();
            getSearchResultsDelayed.delay(50);
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}

function expandArtikliKodDobavljaca(){
    var fx = new Fx.Tween($("prosireniSearchDobavljaciWrap"), {duration:1500, link:'cancel'});
    fx.start(
        "height",$("prosireniSearchDobavljaci").getSize().y
    )
}

function expandArtikliNaWebu(){
    var fx = new Fx.Tween($("prosireniSearchWrap"), {duration:1500, link:'cancel'});
    fx.start(
        "height",$("prosireniSearch").getSize().y
    )
}


function getPollHome(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
            $("d-poll").innerHTML = http_request.responseText;
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}



function getSearchResultsDelayed() {
/*
	if($("dodatniProizvodiProizvodjaci")){
		$("dodatniProizvodiProizvodjaci").innerHTML = "Pronađeni proizvodi dobavljivi su u roku dan, dva:";
	}
	*/
	/*
	if($("dodatniProizvodi")){
		$("dodatniProizvodi").innerHTML = "Pronađeni proizvodi dobavljivi su u roku dan, dva";
	}*/
/*
    if (http_request.responseText.indexOf("prosireniSearchDobavljaciEnd")>0){
        //$("prosireniSearchDobavljaci").innerHTML = http_request.responseText;
        
        $("prosireniSearch").innerHTML += http_request.responseText;
        
        $$("p.dodatniProizvodi").each(function(el, indx){
            if (indx==1) {
                el.innerHTML = "Pronađeni proizvodi dobavljivi su u roku dan, dva:";
            }
        })
        
        
        var fx = new Fx.Tween($("prosireniSearchWrap"), {duration:1500, link:'cancel'});
        fx.start(
            "height",$("prosireniSearch").getSize().y
        )
        
    }
    else if (http_request.responseText.indexOf("prosireniSearchDobavljaciWrap")>0 ){
        $$("p.dodatniProizvodi").each(function(el, indx){
            if (indx==0) el.innerHTML = "Pronađeni proizvodi na web shopu:";
        })
        
        $("prosireniSearch").innerHTML = http_request.responseText;
        var fx = new Fx.Tween($("prosireniSearchWrap"), {duration:1500, link:'cancel'});
        fx.start(
            "height" , $("prosireniSearch").getSize().y
           );
        
    }
    else{
        
        $("content").innerHTML = http_request.responseText;
        
         
    }
    */
    
    $("content").innerHTML = http_request.responseText;
    productZoom();
    
    var fxC = new Fx.Morph($("content"), {duration:150, link:'cancel'});
    fxC.start({
        'opacity':1
    }) 

    Custom.init();

    killThemAll();
    showHide();
        
        
}

var fxBasket = [];

function ajaxBasket(artikl_id){
	if (artikl_id==0) return;
    artiklId= artikl_id;
    
    /*
    if(!fxBasket[artiklId]) {
		fxBasket[artiklId] = new Fx.Morph($("dot"+artiklId), {duration:550, link:'cancel'});

	}
	fxBasket[artiklId].start({
		'opacity':0
	})
   */

    Ajax(prefix + "/AjaxHandler.aspx?pid=" + artiklId + "&action=addToBasket", "", "addToBasket", "");
}

function addToBasket(){

	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
			
			if (artiklId==0) return;
			
		    var arr = http_request.responseText.split("###");
            $("basket-main").innerHTML = arr[0];
            
            prikazikosariculayer(arr[0]);
            
            /*
            if ($("basket"+artiklId)) $("basket"+artiklId).innerHTML = arr[1];
            $("dot"+artiklId).setStyle("opacity" , 0);
            $("dot"+artiklId).setStyle("display" , "block");
			
			fxBasket[artiklId].start({'opacity':1})

			if(Browser.Engine.name!="trident")   {
                $("dot"+artiklId).pulse(1);
            	$("dot"+artiklId).rotate(360);
			}
			*/

		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}

function ajaxReturn(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
            $("mjesta").innerHTML = http_request.responseText;
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}

function renderMjesta(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
            $("mjesta").innerHTML = http_request.responseText;
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}

function getChatStatus(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
            $("tip-chat-txt").innerHTML = http_request.responseText;
		} else {
			//	alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}


function getRataValue(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
			
			var arr = http_request.responseText.split("###");
            $("okvirni_iznos").innerHTML = arr[0];
            $("cijenadivid").innerHTML = arr[1];
            $("x_amount").value = arr[2];
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}

function getArticleComments(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
            $("ajax-comments").innerHTML = http_request.responseText;
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}

function getVoteResult(){
	if (http_request.readyState == 4) {

		if (http_request.status == 200) {
            var arr = http_request.responseText.split("###");
            $("star-wrapper").innerHTML = arr[0];
            $("stars").innerHTML = arr[1];
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}


function getNewComment(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
		    var arr = http_request.responseText.split("###");
            $("reply-to-" + arr[0]).innerHTML = arr[1];
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}


function getBrandNewComment(){
	if (http_request.readyState == 4) {
        
		if (http_request.status == 200) {
		    var arr = http_request.responseText.split("###");
            $("new-comment").innerHTML = arr[1];
		} else {
			alert('There was a problem with the request (' + http_request.status + ').');
		}
	}
}






var artiklId = 0;


function Ajax(url, metoda, funkcija, formData){
	
	http_request = false;
	scrolling = false;
	

	if ((funkcija != "getBrandNewComment") && (funkcija != "getNewComment") && (funkcija != "getArticleComments") && (funkcija != "getVoteResult") && (funkcija != "getRataValue")  && (funkcija != "renderColon") && (funkcija != "renderMjesta") && (funkcija != "getChatStatus")) {
    	historyURL = "#" + funkcija + "&" + url;
    	window.location.hash = funkcija + "&" + url;
    	historyURL = window.location.hash;
	}
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			alert("Msxml2.XMLHTTP"+e.description);
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {"Microsoft.XMLHTTP"+errLog(e.description);}
		}
	}
	
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = eval(funkcija);
	if(metoda!=""){
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http_request.send(formData);
	}else{
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}

// unmodified function here 
function Ajax2(url, metoda, funkcija, formData){
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType){
		    http_request.overrideMimeType('text/xml');
		// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			errLog("Msxml2.XMLHTTP"+e.description);
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {"Microsoft.XMLHTTP"+errLog(e.description);}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = eval(funkcija);
	if(metoda!=""){
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http_request.send(formData);
	}else{
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}


// Element.Transformations
(function(){var a={rotate:/rotate\((\-?[\d\.]+)deg\)/i,scale:/scale\((\-?[\d\.]+)(?:, ?(\-?[\d\.]+))?\)/i,scaleX:/scaleX\((\-?[\d\.]+)\)/i,scaleY:/scaleY\((\-?[\d\.]+)\)/i,skew:/skew\((\-?[\d\.]+)deg(?:, ?(\-?[\d\.]+)deg)?\)/i,skewX:/skewX\((\-?[\d\.]+)deg\)/i,skewY:/skewY\((\-?[\d\.]+)deg\)/i};Element.implement({getTransformation:function(d){var e=(Browser.Engine.webkit)?this.getStyle("WebkitTransform"):this.getStyle("MozTransform"),c=null,b=null;b=a[d].exec(e);switch(d){case"rotate":c=(b)?b[1].toFloat():0;break;case"scale":c=(b)?(b[2]?[b[1].toFloat(),b[2].toFloat()]:b[1].toFloat()):1;break;case"scaleX":case"scaleY":c=(b)?b[1].toFloat():1;break;case"skew":c=(b)?(b[2]?[b[1].toFloat(),b[2].toFloat()]:b[1].toFloat()):0;break;case"skewX":case"skewY":c=(b)?b[1].toFloat():0;break;default:c=e}return c},setTransformation:function(e,d){var c=(Browser.Engine.webkit)?"WebkitTransform":"MozTransform",f=this.getStyle(c),b="";if(f=="none"){f=""}switch(e){case"rotate":b=" rotate("+d+"deg)";break;case"scale":b=($type(d)=="array")?" scale("+d[0]+", "+d[1]+")":" scale("+d+")";break;case"scaleX":b=" scaleX("+d+")";break;case"scaleY":b=" scaleY("+d+")";break;case"skew":b=($type(d)=="array")?" skew("+d[0]+"deg, "+d[1]+"deg)":" skew("+d+"deg)";break;case"skewX":b=" skewX("+d+"deg)";break;case"skewY":b=" skewY("+d+"deg)";break}if(this.hasTransformation(e)){this.setStyle(c,f.replace(a[e],b.trim()))}else{this.setStyle(c,(f+b).clean())}return this},hasTransformation:function(c){var b=(Browser.Engine.webkit)?this.getStyle("WebkitTransform"):this.getStyle("MozTransform");return b.test(a[c])}})})();
// Fx.Rotate
Fx.Rotate=new Class({Extends:Fx,options:{},initialize:function(b,a){this.element=this.subject=document.id(b);this.parent(a)},set:function(a){this.element.setTransformation("rotate",a);return this},start:function(b,a){if(arguments.length==1){a=b;b=this.element.getTransformation("rotate")}return this.parent(b,a)},reset:function(a){a=$defined(a)?a:true;return(a)?this.start(0):this.set(0)},rotate:function(c,b){var a=this.element.getTransformation("rotate");b=b||true;switch($type(c)){case"number":a=c;break;case"string":switch(c){case"down":a=(a%180===0)?((a%360===0)?a+180:a):(a/360).floor()*360+180;break;case"up":console.log((a/180).floor()*180);a=(a%360===0)?a:(a/360).floor()*360;break;case"cw":a=(a%90===0)?a+90:(a/90).ceil()*90;break;case"ccw":a=(a%90===0)?a-90:(a/90).floor()*90;break}break;default:a=(a%90===0)?a+90:(a/90).ceil()*90}this.fireEvent("rotate",a);return(b)?this.start(a):this.set(a)},rotateBy:function(c,b){var a=this.element.getTransformation("rotate");return this.rotate(a+c,b)},spin:function(a){return this.toggleSpin(a)},changeOption:function(b,a){this["old"+b]=this.options[b];this.options[b]=a},resetOption:function(a){if(this["old"+a]){this.options[a]=this["old"+a];this["old"+a]=null}},toggleSpin:function(a){return(this.spinning)?this.stopSpin():this.startSpin(a)},startSpin:function(a){if(a){this.changeOption("duration",a)}this.changeOption("transition",Fx.Transitions.linear);this.cycle();this.spinning=true;this.fireEvent("spinStart");return this},stopSpin:function(a){this.cancel();this.resetOption("duration");this.resetOption("transition");this.spinning=false;this.fireEvent("spinStop");return this},cycle:function(){this.start(0,360).chain(function(){this.cycle()}.bind(this))}});Element.Properties.rotate={set:function(b){var a=this.retrieve("rotate");if(a){a.cancel()}return this.eliminate("rotate").store("rotate:options",$extend({link:"cancel"},b))},get:function(a){if(a||!this.retrieve("rotate")){if(a||!this.retrieve("rotate:options")){this.set("rotate",a)}this.store("rotate",new Fx.Rotate(this,this.retrieve("rotate:options")))}return this.retrieve("rotate")}};Element.implement({rotate:function(b,a){this.get("rotate").rotate(b,a);return this},spin:function(a){this.get("rotate").spin(a);return this}});
// Fx.Scale
Fx.Scale=new Class({Extends:Fx,options:{},initialize:function(b,a){this.element=this.subject=document.id(b);this.parent(a)},set:function(a){this.element.setTransformation("scale",a);return this},compute:function(c,b,a){return[0,1].map(function(d){return Fx.compute(c[d],b[d],a)})},start:function(b,a){if(arguments.length==1){a=($type(b)=="array")?b:[b,b];b=this.element.getTransformation("scale");if($type(b)!="array"){b=[b,b]}}return this.parent(b,a)},reset:function(a){a=$defined(a)?a:true;return(a)?this.start(1):this.set(1)},scale:function(b,a){a=$defined(a)?a:true;return(a)?this.start(b):this.set(b)},scaleBy:function(c,a){var d=this.element.getTransformation("scale"),b=($type(d)=="array")?[d[0]+c,d[1]+c]:d+c;return this.scale(b,a)},flip:function(c,a){var d=this.element.getTransformation("scale"),b=null;switch(c){case"horizontal":b=($type(d)=="array")?[-d[0],d[1]]:[-d,d];break;case"vertical":b=($type(d)=="array")?[d[0],-d[1]]:[d,-d];break;default:b=($type(d)=="array")?[-d[0],-d[1]]:[-d,-d]}return this.scale(b,a)},pulse:function(b,c,d){var a=this;if(c){this.changeOption("duration",c)}if(d){this.changeOption("transition",d)}this.scaleBy(b).chain(function(){a.scaleBy(-b).chain(function(){a.resetOption("duration");a.resetOption("transition")})})},changeOption:function(b,a){this["old"+b]=this.options[b];this.options[b]=a},resetOption:function(a){if(this["old"+a]){this.options[a]=this["old"+a];this["old"+a]=null}},pulsate:function(a,b,c){return this.togglePulsate(a,b,c)},togglePulsate:function(a,b,c){return(this.pulsing)?this.stopPulsate():this.startPulsate(a,b,c)},startPulsate:function(a,b,c){if(b){this.changeOption("duration",b)}if(c){this.changeOption("transition",c)}this.firstCycle=true;this.cycle(a);this.pulsing=true;this.fireEvent("pulsateStart")},stopPulsate:function(){this.cancel();this.resetOption("duration");this.resetOption("transition");this.pulsing=false;this.fireEvent("pulsateStop")},cycle:function(a){this.scaleBy(this.firstCycle?a:a*2).chain(function(){this.firstCycle=false;this.cycle(-a)}.bind(this))}});Element.Properties.scale={set:function(b){var a=this.retrieve("scale");if(a){a.cancel()}return this.eliminate("scale").store("scale:options",$extend({link:"cancel"},b))},get:function(a){if(a||!this.retrieve("scale")){if(a||!this.retrieve("scale:options")){this.set("scale",a)}this.store("scale",new Fx.Scale(this,this.retrieve("scale:options")))}return this.retrieve("scale")}};Element.implement({scale:function(b,a){this.get("scale").scale(b,a);return this},scaleBy:function(b,a){this.get("scale").scaleBy(b,a);return this},flip:function(a){this.get("scale").flip(a);return this},pulse:function(a,b,c){this.get("scale").pulse(a,b,c);return this},pulsate:function(a,b,c){this.get("scale").pulsate(a,b,c);return this}});
// Fx.Skew

