toBuy = new Array();  // что покупается
Count = new Array();  //
bSumm = new Array();  // кол-во одного наименования
iPrice = new Array(); // цена за ед товара
var tPrice = 0; //сумма заказа

function buy(item){

var tb = false;
var itemName = $("#"+item).parent().parent().find('h3').text();
for(i=0;i<toBuy.length;i++){
if(toBuy[i]==item){
tb = true;
break;
}}
if(!tb){
toBuy[toBuy.length] = item;
Count[Count.length] = 1;}
else{
Count[i]++;
}
if(!bSumm[item] && bSumm[item]!=0){
bSumm[item]=0;

document.getElementById("selected_items").innerHTML += "<p class='sel_items' id='s"+item+"'><b>"+itemName+"</b><br>Количество: "+bSumm[item]+", <a style=\"cursor:pointer;\" onclick=\"unBuy('"+item+"')\">Убрать</a></p>";
}
bSumm[item]++;
document.getElementById("s"+item).innerHTML = "<b>"+itemName+"</b><br>Количество: "+bSumm[item]+", <a style=\"cursor:pointer;\" onclick=\"unBuy('"+item+"')\">Убрать</a>";
document.getElementById("s"+item).style.display="";

iPrice[item] = Number($("#"+item).parent().parent().find('span.price').text());

setCookie(item, bSumm[item] +"|"+ itemName +"|"+ iPrice[item] );
 //сумма заказа
 tPrice += iPrice[item];
 $("#tPrice").text(tPrice);
}

function unBuy(item){
bSumm[item]--;

if(bSumm[item]==0){
  document.getElementById("s"+item).style.display = "none";
  setCookie(item, 1, "Mon, 01-Jan-2001 00:00:00 GMT");
  }else{
  document.getElementById("s"+item).innerHTML = "<b>"+ $("#s"+item).find('b').text() +"</b><br>Количество: "+bSumm[item]+", <a style=\"cursor:pointer;\" onclick=\"unBuy('"+item+"')\">Убрать</a>";
  setCookie(item, bSumm[item]+"|"+$("#s"+item).find('b').text()+"|"+ iPrice[item]);
}

tPrice -= iPrice[item];
$("#tPrice").text(tPrice);

for(i=0;i<toBuy.length;i++){
if(toBuy[i]==item){
Count[i]--;
break;
}}}

function doBuy(){
if(Count.length>0){
for(i=0;i<toBuy.length;i++){
child = document.createElement("input");
child.type = "hidden";
child.name = "buyItem"+toBuy[i];
child.value = Count[i];
document.forms.dobuy.appendChild(child);
}
document.forms.dobuy.submit();
}else{
alert("Вы ничего не выбрали!");
}}

function page(url){
document.forms.other_page.action=url;
for(i=0;i<toBuy.length;i++){
child = document.createElement("input");
child.type = "hidden";
child.name = "buyItem"+toBuy[i];
child.value = Count[i];
document.forms.other_page.appendChild(child);
}
document.forms.other_page.submit();
}



function Hi()
{
	var h=(new Date()).getHours();
	if (h > 22 || h < 7) document.write("Доброй ночи");
	if (h > 6 && h < 12) document.write("Доброе утро");
	if (h > 11 && h < 18) document.write("Добрый день");
	if (h > 17 && h < 23) document. write("Добрый вечер");
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function showBasket(){
	if (document.cookie.length > 0){

		var arr_cookie = document.cookie.split(';');
		for(var i=0; i<arr_cookie.length; i++){
			var cookie = arr_cookie[i].split('=');
			var cookie_val = unescape(cookie[1]).split('|');

			var item = cookie[0].replace(' ', '');
			var name = cookie_val[1];
			var kol = cookie_val[0];
			var price = Number(cookie_val[2]);

			document.getElementById("selected_items").innerHTML += "<p class='sel_items' id='s"+item+"'><b>"+name+"</b><br>Количество: "+kol+", <a style=\"cursor:pointer;\" onclick=\"unBuy('"+item+"')\">Убрать</a></p>";

	        toBuy[i] = item;
		    Count[i] = kol;
			bSumm[item] = kol;
			iPrice[item] = price;
			tPrice += price*kol;

		}
		$("#tPrice").text(tPrice);
    }
}

function showOrder(){
	if (document.cookie.length > 0){
        var total = 0;
        tPrice = 0;
		var arr_cookie = document.cookie.split(';');
		for(var i=0; i<arr_cookie.length; i++){
			var cookie = arr_cookie[i].split('=');
			var cookie_val = unescape(cookie[1]).split('|');

			var item = cookie[0].replace(' ', '');
			var name = cookie_val[1];
			var kol = Number(cookie_val[0]);
			var price = Number(cookie_val[2]);

			$('<tr><td><input type="hidden" name="items[]" value="'+name+'|'+price+'|'+kol+'">'+ (i+1) +'.</td><td>'+ name +'</td><td align="left">'+ price +' руб.</td><td align="center">'+ kol +'</td></tr>').appendTo('#list');

			toBuy[i] = item;
		    Count[i] = kol;
			bSumm[item] = kol;
			iPrice[item] = price;
			tPrice += price*kol;
			total += kol;
		}
		$('<tr class="head"><td colspan="2" class="right">Итого:</td><td>'+ tPrice +' руб.</td><td>'+ total +'</td></tr>').appendTo('#list');

    }
}

function doPic(imgName) 
{
	myWin=window.open(imgName, "wind1", "width=1024,height=683,resizable=no,scrollbars=yes,menubar=no");
}

function doPics(imgName) 
{
	myWin=window.open(imgName, "wind1", "width=600,height=849,resizable=no,scrollbars=yes,menubar=no");
}

function ImageExpander(oThumb, sImgSrc)
{
        // изображение ногтя большого пальца руки магазина и переписывает своего onclick укладчика.
        this.oThumb = oThumb;
        this.oThumb.expander = this;
        this.oThumb.onclick = function() { this.expander.expand(); }
        
        // сделайте запись оригинального размера
        this.smallWidth = oThumb.offsetWidth;
        this.smallHeight = oThumb.offsetHeight; 
 
        this.bExpand = true;
        this.bTicks = false;
        
        // сам организованный список
        if ( !window.aImageExpanders )
        {
                window.aImageExpanders = new Array();
        }
        window.aImageExpanders.push(this);
 
        // создайте полное размерное изображение.
        this.oImg = new Image();
        this.oImg.expander = this;
        this.oImg.onload = function(){this.expander.onload();}
        this.oImg.src = sImgSrc;
}
 
ImageExpander.prototype.onload = function()
{
        this.oDiv = document.createElement("div");
        document.body.appendChild(this.oDiv);
        this.oDiv.appendChild(this.oImg);
        this.oDiv.style.position = "absolute";
        this.oDiv.expander = this;
        this.oDiv.onclick = function() {this.expander.toggle();};
        this.oImg.title = "Щелкните, чтобы уменьшить.";
        this.bigWidth = this.oImg.width;
        this.bigHeight = this.oImg.height;
        
        if ( this.bExpand )
        {
                this.expand();
        }
        else
        {
                this.oDiv.style.visibility = "hidden";
                this.oImg.style.visibility = "hidden";
        }
}
ImageExpander.prototype.toggle = function()
{
        this.bExpand = !this.bExpand;
        if ( this.bExpand )
        {
                for ( var i in window.aImageExpanders )
                        if ( window.aImageExpanders[i] !== this )
                                window.aImageExpanders[i].reduce();
        }
}
ImageExpander.prototype.expand = function()
{
        // руководство набора расширения.
        this.bExpand = true;
 
        // заставьте все другие изображения уменьшать
        for ( var i in window.aImageExpanders )
                if ( window.aImageExpanders[i] !== this )
                        window.aImageExpanders[i].reduce();
 
        // if not loaded, don't continue just yet
        if ( !this.oDiv ) return;
        
        // hide the thumbnail
        this.oThumb.style.visibility = "hidden";
        
        // calculate initial dimensions
        this.x = this.oThumb.offsetLeft;
        this.y = this.oThumb.offsetTop;
        this.w = this.oThumb.clientWidth;
        this.h = this.oThumb.clientHeight;
        
        this.oDiv.style.left = this.x + "px";
        this.oDiv.style.top = this.y + "px";
        this.oImg.style.width = this.w + "px";
        this.oImg.style.height = this.h + "px";
        this.oDiv.style.visibility = "visible";
        this.oImg.style.visibility = "visible";
        
        // start the animation engine.
        if ( !this.bTicks )
        {
                this.bTicks = true;
                var pThis = this;
                window.setTimeout(function(){pThis.tick();},0);        
        }
}
ImageExpander.prototype.reduce = function()
{
        // set direction of expansion.
        this.bExpand = false;
}
ImageExpander.prototype.tick = function()
{
        // calculate screen dimensions
        var cw = document.body.clientWidth;
        var ch = document.body.clientHeight;
        var cx = document.body.scrollLeft + cw / 2;
        var cy = document.body.scrollTop + ch / 2;
 
        // calculate target
        var tw,th,tx,ty;
        if ( this.bExpand )
        {
                tw = this.bigWidth;
                th = this.bigHeight;
                if ( tw > cw )
                {
                        th *= cw / tw;
                        tw = cw;
                }       
                if ( th > ch )
                {
                        tw *= ch / th;
                        th = ch;
                }
                tx = cx - tw / 2;
                ty = cy - th / 2; 
        }
        else
        {
                tw = this.smallWidth;
                th = this.smallHeight;
                tx = this.oThumb.offsetLeft;
                ty = this.oThumb.offsetTop;
        }       
        // move 5% closer to target
        var nHit = 0;
        var fMove = function(n,tn) 
        {
                var dn = tn - n;
                if ( Math.abs(dn) < 3 )
                {
                        nHit++;
                        return tn;
                }
                else
                {
                        return n + dn / 5;
                }
        }
        this.x = fMove(this.x, tx);
        this.y = fMove(this.y, ty);
        this.w = fMove(this.w, tw);
        this.h = fMove(this.h, th);
        
        this.oDiv.style.left = this.x + "px";
        this.oDiv.style.top = this.y + "px";
        this.oImg.style.width = this.w + "px";
        this.oImg.style.height = this.h + "px";
 
        // if reducing and size/position is a match, stop the tick      
        if ( !this.bExpand && (nHit == 4) )
        {
                this.oImg.style.visibility = "hidden";
                this.oDiv.style.visibility = "hidden";
                this.oThumb.style.visibility = "visible";
 
                this.bTicks = false;
        }
        
        if ( this.bTicks )
        {
                var pThis = this;
                window.setTimeout(function(){pThis.tick();},0);
        }
}

