var tstart=null;
var tstop=null;
var pending=false;

function wczytaj(www,target,mode) {
	if (www.href) www= www.href;
	if (mode=="sklep"){
		$.get(www,function(data) { parseResultSklep(data);});
		//mint.Request(www,null,function() { parseResultSklep(this.responseText,target,www);},function() {alert("Wystąpił błąd. Bedą max "+this.retryNum+" ponowienia ");} )
	} else { 
		$.get(www,function(data) { parseResult(data);});
		//mint.Request(www,null,function() { parseResult(this.responseText,target,www);},function() {alert("Wystąpił błąd. Bedą max "+this.retryNum+" ponowienia ");} )
	}

}

function parseResult(text, target, www) {
	text = trim(text); // obcinamy smieci

	if (text=="1" || text==1 || text=="ok") { 
		
	} else if (text=="refresh") { //odswierzamy strone
		window.location.reload();
	} else if (text.substr(0,4)=="goto") { 
		did = text.substr(5,text.length-6); 
		refreshLayer(did);
	} else { //pokazujemy komunikat
		e.pokaz_komunikat (text);
	}
	
	
	
}

function parseResultSklep(text,target,www) {
	text = trim(text); // obcinamy smieci

	if (text=="1" || text==1 || text=="ok") { 
		
	} else if (text=="refresh") { //odswierzamy strone
		window.location.reload();
	} else if (text.indexOf("|")!=-1) { 
		 tmp=text.split("|");
		 kwota=tmp[1];
		 ilosc=tmp[0];
		 //alert(kwota+","+ilosc);
		$dialog.dialog('open');

	} else { //pokazujemy komunikat
		//e.pokaz_komunikat (text);
		$dialog_komunikat.dialog('open');
	}
	 pending=false;
}


function parseResultSklepOLD(text, target, www) {
	tstop=new Date();
	text = trim(text); //obcinamy zbedne smieci z poczatku i konca
	if (target==-1) {
		tab = text.split(":");
		getID("cart_top1").innerHTML = tab[0];
		getID("cart_top2").innerHTML = tab[1];
	} else {
		if (getID("msg"+target)) {
		  s=getID("msg"+target).innerHTML;
		  var r = s.match(/[\d]+/g);
		  //t = parseInt();
  //alert(t+" "+getID("msg"+target).innerHTML);
		  //t=t+getID("ilosc"+target).value;

		t=(r[0]*1)+(getID("ilosc"+target).value*1);
		  getID("msg"+target).innerHTML = mesg+". "+zdanie+t;	
		}
		getID("ilosc"+target).value="";
		sklep.update();
	    pending=false;
	}
}



function sklep() {
	this.mode = "ajax"; //ajax / normal
	
	this.action = function(link,idp) {
		if (this.mode=="normal") {
			link+="&mode=cart";
			window.location = link;
		}
		if (this.mode=="ajax") {
			//alert(link);
			wczytaj(link,idp,"sklep");
		}
	}
	
	this.add = function(idp,ilosc) {
		if (pending) return;
		if (!ilosc) ilosc=1;
		
		pending=true;
		link = "/cart/cart_add.php?idp="+idp+"&ilosc="+ilosc;
		//if (this.mode=="normal") link+="&mode=ref";
		this.action(link,idp);
		
	}
	
	this.update = function() {
		link="/cart/cart_info.php";
		this.action(link,-1);
	}
	this.reset = function() {
		link = "/cart/cart_reset.php";
		this.mode="normal";
		this.action(link,idp);
	}
	
	this.remove = function(idp,ilosc) {
		link="/cart/cart_remove.php?idp="+idp+"&ilosc="+ilosc;
		this.mode="normal";
		this.action(link,idp);
	}
	this.del = function(idp) {
		link="/cart/cart_delete.php?idp="+idp;
		this.mode="normal";
		this.action(link,idp);
	}
	
	this.change = function(idp,ilosc){
		link="/cart/cart_change.php?idp="+idp+"&ilosc="+ilosc;
		this.mode="normal";
		this.action(link,idp);
	}

}
