var __browser__;

function shoppingCart()
{
	//Elements
	this.sCartContainer=null;
	this.sCartDraggable=null;
	//Functions
	this.init = init;
	this.elementGet = elementGet;
	this.attributeGet = attributeGet;
	this.attributeSet = attributeSet;
	this.action = action;
	this.show = sc_show;
	this.hide = sc_hide;
	this.scroll = sc_scroll;
	this.cancel = sc_cancel;
	this.write = sc_write;
	this.addToCart = addToCart;
	this.submitUpdate = submitUpdate;
	this.getTovById = getTovById;
	this.formatNum = formatNum;
	//variables
	this.mode = "undef";
	this.submitCallback="/Ebasket/submit_basket.php";
	this.submitControl=null;
	this.browser = "IE";
	if(navigator.userAgent.indexOf('Opera') != -1){
		this.browser = "OP";
	}else if(navigator.userAgent.indexOf("Mozilla/5") != -1 || navigator.userAgent.indexOf("Gecko") != -1){
		this.browser = "MZ";
	}else if(!document.all){
		this.browser = "UNK";
	}
	if(this.browser == "UNK") this.mode = "simple";
	__browser__ = this.browser;	// used by global functions
	this.state = "wait";
	this.X=0;
	this.Y=0;
	this.Xoff=0;
	this.Yoff=0;
	this.last_scroll=0;
	this.tovars = new Array();
	this.a_param;
	//Actions
	this.init();
}

function init()
{
	if(!this.sCartDraggable){
		this.sCartDraggable = this.elementGet("sDraggable");
	}
	if(!this.sCartContainer){
		this.sCartContainer = this.elementGet("sContainer");
	}
	if(this.mode == "undef" && this.sCartDraggable && this.sCartContainer)
	{
		this.mode = "fly";
	}
	if(this.mode == "fly")
	{
		if(this.browser=="IE" || this.browser=="OP"){
			document.body.attachEvent("onmousemove",b_call);
			document.body.attachEvent("onmousedown",b_call);
			document.body.attachEvent("onmouseup",b_call);
			document.body.attachEvent("onclick",b_call);
			window.attachEvent("onscroll",b_call);
		}else{
			window.addEventListener("mousemove",b_call,true);
			window.addEventListener("mousedown",b_call,true);
			window.addEventListener("mouseup",b_call,true);
			window.addEventListener("click",b_call,true);
			this.sCartContainer.style.position='fixed';
		}
		this.initHTML = this.sCartContainer.innerHTML;
	}
	this.write();
}

function action(evnt)
{
	if(this.mode != "fly") return;
	if(this.browser == "IE" || this.browser == "OP"){
		evnt = window.event;
		var src = evnt.srcElement;
	}else if(this.browser == "MZ")	var src = evnt.target;
	 var act;
	if(evnt.type=="mousemove")act="move";
	if(evnt.type=="mousedown")act ="mdown";
	if(evnt.type=="mouseup")act ="mup";
	if(evnt.type=="scroll")act="scroll";
	if(evnt.type=="click")act="click";
	if(!act){ this.cancel(evnt); return;}
	var scart = this.sCartContainer;
	if(act=="mdown")
	{
		var e = src;
		while(e && e != this.sCartDraggable) e = e.parentNode;
		if(!e)return;
		if(this.browser == "IE" || this.browser == "OP")
		{
			this.X = scart.offsetLeft + scart.clientLeft;
			this.Y = scart.offsetTop +  scart.clientTop;
			this.Xoff = evnt.clientX - this.X;
			this.Yoff = evnt.clientY - this.Y;
		}else{
			this.Xoff = evnt.layerX;
			this.Yoff = evnt.layerY;
		}
		this.state = "drag";
	}else if(act=="move" && this.state == "drag"){
		scart.style.left = evnt.clientX - this.Xoff;
		scart.style.top = evnt.clientY - this.Yoff;
		this.X = evnt.clientX - this.Xoff;
		this.Y = evnt.clientY - this.Yoff;
		if(this.X < 0)this.X = scart.style.left = 0;
		if(this.Y < 0)this.Y = 	scart.style.top = 0;
		this.cancel();
	}else	if(act == "mup") {
		this.state = "wait";
	}else if(act=="scroll"){
		this.scroll();
	}else if(act=="click"){
		var e = src;
		while(e && this.attributeGet(e,"id") != "sClose") e = e.parentNode;
		if(e) this.hide();
	}
}

function show_select_boxes(show)
{
	if (__browser__ == "IE") {
		var sels = document.getElementsByTagName("SELECT");
		for (var i = 0; i < sels.length; i++) {
			sels[i].style.display = show ? "" : "none";
		}
	}
}

function sc_show(x,y)
{
	if(this.mode != "fly") return;
	this.sCartContainer.style.left = x + (this.browser == "MZ" ?  0 : document.body.scrollLeft);
	this.sCartContainer.style.top = y + (this.browser == "MZ" ?  0 : document.body.scrollTop);
	show_select_boxes(false);
}

function sc_hide(x,y)
{
	if(this.mode != "fly") return;
	this.sCartContainer.style.left = -1000;
	this.sCartContainer.style.top = -1000;
	show_select_boxes(true);
}

function sc_scroll(){
	var top=this.sCartContainer.style.top;
	top=top.replace('px','');
	top=top*1;

	top+=document.body.scrollTop-this.last_scroll;
	this.last_scroll=document.body.scrollTop;
	this.sCartContainer.style.top=top+'px';
}

function sc_write(error)
{
	if (!this.initHTML) return;
	var str = this.initHTML;
	if(!error) error="";
	var t_num=0; var n_num=0; var s_usd=0;
	var s_rub=0; var s_euro=0;
	for(var i=0; i< this.tovars.length; i++)
	{
		var t = this.tovars[i];
//		alert(t.num);
		t_num += t.num*1; n_num++;
		switch(t.currency)
		{
			case "USD":
				s_usd += t.num*t.price;
				break;
			case "EUR":
				s_euro += t.num*t.price;
				break;
			case "RUB":
				s_rub += t.num*t.price;
				break;
			default:
				s_rub += t.num*t.price;
		}
	}
	str = str.replace('_TOVARNUM_',t_num);
	str = str.replace('_NAMENUM_',n_num);
	str = str.replace('_SUMMUSD_',s_usd);
	str = str.replace('_SUMMEUR_',s_euro);
	str = str.replace('_SUMMRUB_',s_rub);
	str = str.replace('_SUBMITERROR_',error);
	this.sCartContainer.innerHTML = str;
	this.sCartDraggable = this.elementGet("sDraggable");
	this.sCartContainer = this.elementGet("sContainer");
}

function getTovById(id)
{
	for(var i=0; i< this.tovars.length; i++)
	{
		if(this.tovars[i].id == id) { return this.tovars[i];}
	}
}

function submitUpdate(new_id)
{
	/*if(new_id){
		var l_tov;
		for(i=0,j=0; i< this.tovars.length; i++,j++){
			var t = this.tovars[i];
			if(t.id==new_id){
				j--; l_tov = t;
			}else{
				this.tovars[j] = t;
			}
		}
		this.tovars[this.tovars.length-1] = l_tov;
	}*/
	this.write((this.lastSubmitStatus == "error" ? this.lastSubmitStatusMess : ""));	
}

function addToCart(tovar_id,tovar_num,inc,type,x_pos,y_pos)
{
	if(!inc) inc=0;
	if(this.browser=="UNK") return true;
	if(!type) type="basket";
	var url = this.submitCallback + "?tovar_id="+tovar_id+"&tovar_num="+tovar_num+"&incrim="+inc+"&type="+type+"&s_time="+(new Date()).getTime()+"&s_rand="+Math.random()+"&a_param="+this.a_param;
//	alert(url);
	if(!this.submitControl){
		this.submitControl = document.createElement("DIV");
		document.documentElement.appendChild(this.submitControl);
//		document.body.appendChild(this.submitControl);
	}
	this.submitControl.innerHTML = "<iframe style='position:absolute; left:-1000px; top:-1000px;' src='"+url+"'></iframe>";
	if(this.mode=="fly" && x_pos && y_pos){ 
		var fx = this.X - (this.browser == "MZ" ? 0 : document.body.scrollLeft);
		var fy = this.Y - (this.browser == "MZ" ? 0 : document.body.scrollTop);
		this.show(( fx > 0 && fx  < document.body.clientWidth-20 ? fx : x_pos),( fy > 0 && fy < document.body.clientHeight-20 ? fy : y_pos));
	}
	return false;
}

/*Multi browser support functions*/

function elementGet(id,pObj)
{
	if(!pObj) pObj=document;
	if(this.browser == "UNK") return pObj;
	if(this.browser == "IE") return pObj.all[id];
	return document.getElementById(id);
}


function attributeGet(elem,a_name)
{
	if(this.browser=="IE") return elem[a_name];
	if(!elem.getAttribute) return "";
	return elem.getAttribute(a_name);
}

function attributeSet(elem,a_name,a_value)
{
	if(this.browser=="IE") elem[a_name] = a_value;
	elem.setAttribute(a_name,a_value);
	return elem;
}



function sc_cancel()
{
	if(this.browser=="IE" || this.browser=="OP")
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}else if(this.browser=="MZ"){
		var e=arguments[0];
		if(e)e.preventDefault();
	}
}

function b_call()
{
	if(sCart) sCart.action(arguments[0]);
}

function formatNum(value){
	return Math.round(value*100)/100;
}
/*Tovar*/

function tovar(id,num,name,currency,price,sklad,mu,type)
{
	this.id = id;
	this.num = num;
	this.name = name;
	this.currency = currency;
	this.price = price;
	this.mu = mu;
	this.sklad = sklad;
	this.type = type;
}

var sCart = new shoppingCart();
