//order
var t_OB = null;

function orderItem (goods) {
	this.request = [];
	this.goods = goods;
	this.cart = document.getElementById('ngoods');
	this.obox = document.getElementById('orderbox');
	this.ounits = document.getElementById('ordounits');
	this.nextid = 1;
	this.fcart = document.getElementById('floatcart');
	this.cartadded = document.getElementById('cartadded');
	this.item = 0;
	this.amount = document.getElementById('namount');

	this.promptItem = function (id,units,price,ratio,obj) {
		var x,y;
		x = getOffsetLeft(obj);
		y = getOffsetTop(obj);

		this.ounits.innerHTML = units;
		this.obox.style.left = x + 'px';
		this.obox.style.top = (y + obj.offsetHeight + 1) + 'px';
		this.obox.style.display = 'block';
		if (typeof t_OB == "number") {
			clearTimeout(t_OB);
			t_OB = null;
		}

		ratio = parseFloat(ratio);
		if (isNaN(ratio) || (ratio <= 0)) ratio = 1;

		this.item = {id:id , price:price,ratio:ratio};

		//t_OB = setTimeout('IO.hideObox(1)',4000);

		return false;
	}

	this.addItem = function (id,q) {
		if (q == undefined) q = 1;

		this.request.push({oid:this.nextid,id:id,q:q});
		var url = base_path + '?view='+(window.cart_page||'catalog')+'&mode=additem&js=1';

		var params = 'id=' + id + '&q=' + q + '&oid=' + this.nextid;
		this.nextid++;
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.addResponse.bind(this),
				onFailure:this.addResponse.bind(this)
			});
		return false;

	}

	this.addResponse = function (transport) {
		//alert(transport.responseText);
		try {
			var response = eval('(' + transport.responseText + ')');
		} catch (e) {
			this.errorMsg('Ошибка получения данных');
		}
		if (response.result && (parseInt(response.result) != -1)) {
			this.completeRequest(parseInt(response.result),parseInt(response.items),parseFloat(response.amount),response.message);
		}
	}


	this.setCart = function (goods,amount) {
		this.cart.innerHTML = goods;
		this.amount.innerHTML = amount;
	}

	this.completeRequest = function (oid,total,amount) {

		var reqs = [];
		var msg = '';
		for (var i = 0 ; this.request.length;i++) {
			if (parseInt(this.request[i].oid) == oid) {
				this.goods += parseInt(this.request[i].q);
				this.setCart(total,amount);
				this.showFloatCart(this.request[i].q,this.completeRequest.arguments[3]);
			} else {
				reqs.push(this.request[i]);
			}
		}
		this.request = reqs;
	}

	this.showFloatCart = function (qa) {
		var arrayPageScroll = getPageScroll();
		if (this.showFloatCart.arguments[1] == 1){
			el = document.getElementById("additem");
			el.innerHTML = "Извините, Вы уже добавили максимальное количество товаров, для одного заказа";
		}else{
			this.cartadded.innerHTML = qa;
		}

		this.fcart.style.top = (arrayPageScroll[1] + 200 + 'px');
		this.fcart.style.left = '40%';
		this.fcart.style.display = 'block';

		setTimeout('IO.hideFloat()',4000);

	}

	this.hideFloat = function () {

		this.fcart.style.display = 'none';
	}

	this.showObox = function () {
		if (typeof t_OB == "number") {

			clearTimeout(t_OB);
			t_OB = null;
		}
		this.obox.style.display = 'block';
	}

	this.hideObox = function (timeout) {
		if (timeout == undefined) {
			if (typeof t_OB == "number") {
			    clearTimeout(t_OB);
			}
			t_OB = setTimeout('IO.hideObox(1)',20000);
		} else {
			this.obox.style.display = 'none';
			t_OB = null;
		}
	}

	this.errorMsg = function(msg) {
		alert(msg);
	}


	this.cleanCart = function() {
		var url = base_path + '?view='+(window.cart_page||'catalog')+'&mode=cleancart';
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: '',
				onComplete: function() {},
				onFailure:function() {}
			});
		this.setCart(0,0);
	}

}

var currItems = [];
var pt,pf;
function currCount(value,koef) {
	koef = parseFloat(koef);
	value = parseFloat(value);
	if (!isNaN(koef) && !isNaN(value)) {
		return Math.round(value*koef);
	}
	return value;
}

function o_reqCurrency(curr) {
	if (currencyList[curr]) {
		var url = base_path + '?view=goods&mode=currency&c='+curr;
		var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: '',
			onComplete: o_responseCurrency,
			onFailure:o_responseCurrency
		});

		var i;
		if (currItems.length == 0) {
			var items = document.getElementsByTagName('span');
			var item = {};
			for (i = 0; i < items.length ;i++) {
				if (items[i].className) {
					item = {};
					if (items[i].className == 'spprice') {
						item.type = 1;
						item.orig = parseInt(items[i].innerHTML);
						item.orig = isNaN(item.orig) ? 0 : item.orig;
						item.obj = items[i];
						currItems.push(item);

					} else if (items[i].className == 'spcurrency') {
						item.type = 2;
						item.orig = 0;
						item.obj = items[i];
						currItems.push(item);
					}
				}
			}
		}

		currCurr = curr;
		curr = currencyList[curr];
		var koef = curr.k / currencyList[initCurr].k;


		for (i = 0; i < currItems.length ; i++) {
			if (currItems[i].type == 1) {
				currItems[i].obj.innerHTML = currCount(currItems[i].orig,koef);
			} else {
				currItems[i].obj.innerHTML = curr.t;
			}
		}
		frm = document.forms.pricefilter;
		if (frm){
			if (!pf && !pt){
				pf = parseInt(frm.pf.value);
				pt = parseInt(frm.pt.value);
			}
			if (isNaN(pf)){
				frm.pf.value = "";
			} else
			{
				frm.pf.value = currCount(pf,koef);
			}
			
			if (isNaN(pt)){
				frm.pt.value = "";
			} else
			{
				frm.pt.value = currCount(pt,koef);
			}
		}
	}
}

function o_responseCurrency(t) {
	try {
		if (parseInt(t.responseText) == 1) {

		}
	} catch (e) {}
}