Reg = /:\/\/(.+\.)?(\w+\.\w+)\/?/i;
if (a = Reg.exec(location.href))
  var Domain = a[2];

function setCookie (name, value, time) {
  $.cookie(name,value,{domain:'.'+Domain,expires:1,path:'/'});
}
function getCookie (name) {
  return $.cookie(name);
}

setCookie ("testCookie", "ok");

if (getCookie("testCookie"))
	var cookieOk = true;
else {
	var cookieOk = false;
}

var Products = new Array ();
// общая стоимость
var ProductsCost = 0;
// общий объем
var ProductsValue = 0;
// кол-во единиц
var ProductsCount = 0;
// кол-во позиций
var ProductsPos = 0;

function Product (id, cost, value, count) {
	this.id = id;
	this.cost = cost;
	this.value = value;
	this.count = count;
}

/*
function save () {
	var pr = '';
	var str = '';
	if (Products.length) {
		for (i=0;i<Products.length;i++) {
		
			P = Products[i];
			if (P.count) {
				str = str+pr+P.id+','+P.cost+','+P.value+','+P.count;
				pr = '$';
			}
		}
		setCookie ("order", str, 12);
	} else {
		setCookie ("order", "no", 12);
	}
}
*/
function oadd (type, id, price, value, count) {
return true;

	if (!cookieOk)
		return true;

	product = getProduct (id, price, value);

	if (count == 'no')
		product.count =0
	else
		product.count = product.count+count;
	countProducts ();
	showInfo ();
	save ();
	return false;
}

function basketClean () {
return true;
	if (!Products.length) {
		alert ("В корзине пусто");
		return false;
	}

        if (!window.confirm ("Вы уверены, что хотите очистить корзину?"))
        	return false;

	if (!cookieOk)
		return true;
		

	Products = new Array ();
	countProducts ();
	showInfo ();
	save ();
	return false;
}
function basketInit (str) {
return false;
//        var str = getCookie ("order");
        if (!str)
        	return false;
        prodAr = str.split("#");
	if (prodAr.length) {
		str = prodAr[0];
		basketType = prodAr[1];
	}

        prodAr = str.split("$");

	if (prodAr.length) {
		Products = new Array ();
	        for (i=0;i<prodAr.length;i++) {
			props=prodAr[i].split(",");
			if (props.length == 4) {
				Products[i] = new Product (parseInt(props[0]), props[1], props[2], parseInt(props[3]));
			}
	        }
	}
	countProducts ()
	showInfo();
}


function countProducts () {
        ProductsCost = 0;
        ProductsValue = 0;
	ProductsCount = 0;
	ProductsPos = 0;
	for (i=0;i<Products.length;i++) {
	        if (Products[i].count) {
		        ProductsCost = ProductsCost + Products[i].cost * Products[i].count;
		        ProductsValue = ProductsValue + Products[i].value * Products[i].count;
		        ProductsCount = ProductsCount + Products[i].count;
		        ProductsPos ++;
	        }
	}

}
function showInfo () {
	oi = document.getElementById ('orderInfo');
	if (oi) {

	        if (ProductsCount) {
		        if (ProductsPos%10==1)
		        	Count = ProductsPos+" позиция";
			else if (ProductsPos%10<5)
		        	Count = ProductsPos+" позиции";
			else
		        	Count = ProductsPos+" позиций";

	        	Count = Count+", "+ProductsCount+" ед.";
	
			var Text = "В корзине:<br/> "+Count+" <br/> на сумму "+ProductsCost+" руб. ("+ProductsValue+" бал.)";
		} else
			var Text = "В корзине ничего нет";

		oi.innerHTML = Text;
	}
	om = $('#orderMessage');
	if (om) {
		om.css({'left':($(window).width()/2-om.width()/2), 'top':($(window).height()/2-om.height()/2)});
		om.html(Text);
		om.fadeIn('slow');
		window.setTimeout ("hideMessage()", 3000);
	}

}
function hideMessage () {
	$('#orderMessage').fadeOut('slow');
}

function getProduct (id, cost, value) {
	for (i=0;i<Products.length;i++) {
		if (Products[i].id == id) {

		        if (Products[i].cost != cost)
				Products[i].cost = cost;

		        if (Products[i].value != value)
				Products[i].value = value;

			return Products[i];
		}
	}
	Products[i] = new Product (id, cost, value, 0);
	return Products[i];
}

$(document).ready(function(){
  var str = getCookie ("order");
  basketInit(str);
  var a = $('body').append(
    '<div id="orderMessage" style="display:none;background:#FFF;z-index:999;position:absolute;top:-1000px;left:-1000px; width:400px; padding:20px;height:100px;border:solid 2px red;"></div>'
  );
//$('#orderInfo').append('1234');
//document.write('<div id="orderMessage" style="background:#FFF;z-index:999;position:absolute;top:10px;left:10px; width:200px; height:200px;border:solid 2px red;">1234</div>');
//alert(a);

});
(function ($) {
	jQuery.fn.basket = function (options) {

		var settings = {
			// user editable settings
			client_type	: 'guest',


		};


		var Products = {};
		/* 
			set up any user passed variables
		***************************************/
		if (options) {
			jQuery.extend(settings, options);
		}


		/***************************************
			begin
		***************************************/
		return this.each(function () {
//alert(sys);
//			$this = $(this);
//alert(settings);
//			var o = settings;
		})
	}
})(jQuery);


