var eImgNum = 0;
var eImgLoad = 0;
var eImgArr = new Array();
var eImgPreload = new Array();
var eImgVisible = new Array();

var eWnd = {on:0,x:0,y:0};

var eCurrentControl = 0;
var eCurrentElement = 0;
var eCurrentWndMode = 0;

var eOldElementCount = 0;

function wpizza_priceRecalc(price, k){
	price = price * k;
	price = Math.round(price);
	modP = price % 5;
	if (modP==1 || modP==2){
		price = price - modP;
		return price;
	}
	if (modP==3 || modP==4){
		price = price + (5 - modP);
		return price;
	}else{
	return price;
	}
}

function wpizza_resize(size){
	for (var i in cje.wp.elm) {
		n = cje.wp.elm[i].count;
		if (n) {
		cje.wp.pizzaPrice -= n*cje.wp.elm[i].price;
		cje.fullPrice -= n*cje.wp.elm[i].price;
		cje.wp.elm[i].count = 0;
		}
		cje.wp.elm[i].price = wpizza_priceRecalc(cje.wp.elm[i].priceInit,cje.wp.sizes[size][cje.wp.elm[i].gid]);
		cje.wp.elm[i].mass = wpizza_priceRecalc(cje.wp.elm[i].massInit,cje.wp.sizes[size][cje.wp.elm[i].gid]);
		//window.alert(i+' изначально '+ cje.wp.elm[i].priceInit +', стало '+cje.wp.elm[i].price+', товар: '+cje.wp.elm[i].name);
		$('#elP'+i).html(cje.wp.elm[i].price);
		if (n) {
		wpizza_processElement(i,n);
		}
	}
}

function wpizza_initialize() {
	if (cje.wp) {
		cje.wp.loadComplete = false;
		cje.wp.lockConstructor = false;

		cje.wp.grp = new Array();
		cje.wp.compG = new Array();
		cje.wp.pizzaContent = new Array();
		cje.wp.pizzaHistory = new Array();
		cje.wp.pizzaHistoryNum = 0;
		cje.wp.compGnum = 0;
		cje.wp.pizzaPrice = 0;
//		cje.wp.fullPrice = cje.wp.oldPrice || 0;
		cje.fullPrice = cje.oldPrice || 0;
		cje.wp.needBeProcessed = new Array();

		for (var i in cje.wp.elm) {
			cje.wp.grp[cje.wp.elm[i].gid] = 0;

			// create array for compositor - there be HTML codes of items
			if (!cje.wp.elm[i].radio && !cje.wp.compG[cje.wp.elm[i].gid]) cje.wp.compG[cje.wp.elm[i].gid] = {num:0,val: new Array()};

			if (cje.wp.elm[i].count > 0) {
				var n = cje.wp.elm[i].count;
				cje.wp.elm[i].count = 0;
				var nowI = i;
				var nowN = n;
				if (!cje.wp.loadPizza)
					wpizza_processElement(nowI,nowN);
			}
		}

		cje.wp.loadComplete = true;
		for (var i in cje.wp.loadPizza) {
			$('#elR'+cje.wp.loadPizza[i]).attr('checked','checked');
	//window.alert('load pizza: '+(cje.wp.loadPizza[i])+'('+(cje.wp.elm[cje.wp.loadPizza[i]].count+1)+')');
			wpizza_processElement(cje.wp.loadPizza[i],cje.wp.elm[cje.wp.loadPizza[i]].count+1);
		}
	}

	if (cje.wpb && cje.wpb.needProceed) {
		for (var i in cje.wpb.needProceed)
			if (cje.wpb.items[i]) {
	//			window.alert('need proceed block: id #'+i+'; count = '+cje.wpb.needProceed[i]);
				wpizza_processBlockElement(i,cje.wpb.needProceed[i]);
				cje.wpb.items[i].count = cje.wpb.needProceed[i];
			}
	}

}

function imgLoadComplete() {
	if (cje.wp)
		for (var i in cje.wp.needBeProcessed)
			wpizza_processLayer(i,cje.wp.needBeProcessed[i]);
}

function wpizza_processCompositorGroup(g) {
	var out = '';
	for (var i in cje.wp.compG[g].val)
		out += cje.wp.compG[g].val[i];

	return out;
}
function wpizza_processElement(eid,count) {
	if (!cje.wp || !cje.wp.elm[eid]) return;

	// calculate price change
	cje.wp.pizzaPrice += (count-cje.wp.elm[eid].count)*cje.wp.elm[eid].price;
	cje.fullPrice += (count-cje.wp.elm[eid].count)*cje.wp.elm[eid].price;

	// add to content
	cje.wp.pizzaContent[eid] = count;

	// add to history
	var n = 0;
	for (var i=0; i<cje.wp.pizzaHistory.length; i++) {
		if (cje.wp.pizzaHistory[i] == eid) {
			n++;
			if (n>count) {
				cje.wp.pizzaHistory.splice(i,1);
				i--;
			}
		}
	}

	if (n<count)
		for (var i=n; i<count; i++)
			cje.wp.pizzaHistory[cje.wp.pizzaHistory.length] = eid;

	if (cje.wp.elm[eid].radio) {
		if (count) {
			$('#comp_e'+cje.wp.elm[eid].gid).html(cje.wp.elm[eid].name);
			document.getElementById('comp_g'+cje.wp.elm[eid].gid).style.display = 'block';
		} else {
			document.getElementById('comp_g'+cje.wp.elm[eid].gid).style.display = 'none';
		}
	} else {
		if (count) {
			var n = ((cje.wp.elm[eid].cID != undefined) && (cje.wp.compG[cje.wp.elm[eid].gid].val[cje.wp.elm[eid].cID] != '')) ? cje.wp.elm[eid].cID : cje.wp.compG[cje.wp.elm[eid].gid].num ;
			var ev = (cje.wp.elm[eid].parts>1) ? 'wpizza_elementClick(this,document.getElementById(\'elR'+eid+'\'),'+eid+',2,1);' : '';

			cje.wp.compG[cje.wp.elm[eid].gid].val[n] =
				'<div class="add_block"><a href="#"onclick="'+ev+'return false;" title=""><span>'
				+cje.wp.elm[eid].name
				+'</span></a>';

			if (cje.wp.elm[eid].mass > 0)
					cje.wp.compG[cje.wp.elm[eid].gid].val[n] +=	' (' + (count*cje.wp.elm[eid].mass) + ' g.)'

			cje.wp.compG[cje.wp.elm[eid].gid].val[n] +=
				'<a href="#" class="add_block_del" onclick="document.getElementById(\'elR'+eid+'\').checked=false; wpizza_elementClick(this,document.getElementById(\'elR'+eid+'\'),'+eid+',2,2); return false;"></a><br/><span class="price">'
				+(count*cje.wp.elm[eid].price)
				+'</span></div>';

			cje.wp.elm[eid].cID = n;

			cje.wp.compG[cje.wp.elm[eid].gid].num++;

			$('#comp_e'+cje.wp.elm[eid].gid).html(wpizza_processCompositorGroup(cje.wp.elm[eid].gid));
			$('#comp_g'+cje.wp.elm[eid].gid).show();
			//document.getElementById('comp_g'+cje.wp.elm[eid].gid).style.display = 'block';
		} else {
			cje.wp.compG[cje.wp.elm[eid].gid].val[cje.wp.elm[eid].cID] = '';

			var s = wpizza_processCompositorGroup(cje.wp.elm[eid].gid);
			if (s == '')
				$('#comp_g'+cje.wp.elm[eid].gid).hide();
				//document.getElementById('comp_g'+cje.wp.elm[eid].gid).style.display = 'none';
			else
				$('#comp_e'+cje.wp.elm[eid].gid).html(s);
		}
	}

	$('#wpizza_currentPrice').html(cje.wp.pizzaPrice);
	$('#wpizza_fullPrice').html(cje.fullPrice);

	if (cje.wp.loadComplete)
		wpizza_processLayer(eid,count);
	else
		cje.wp.needBeProcessed[eid] = count;
}
function wpizza_processBlockElement(eid,count) {
	if (count>0) {
		$("#wpbCount_"+eid).html(count+" шт.").show();
		$("#wpbDel_"+eid).show();
	} else {
		$("#wpbCount_"+eid).hide();
		$("#wpbDel_"+eid).hide();
	}
	//window.alert("count: "+cje.wpb.items[eid].count+" "+count);
	cje.fullPrice += (count-cje.wpb.items[eid].count)*cje.wpb.items[eid].price;
	cje.wpb.items[eid].count = count;
	$('#wpizza_fullPrice').html(cje.fullPrice);
}
function wpizza_processLayer(eid,count) {
	//window.alert('process layer');
	for (var i = 1; i <= cje.wp.elm[eid].parts; i++)
		if (cje.wp.eimg[eid][i]) {
			//window.alert('process layer: eid='+eid+'; i='+i+'; count='+count);
			if (i<=count && cje.wp.eimg[eid][i].zid>0) {
				// do nothing
			} else if (i>count && cje.wp.eimg[eid][i].zid>0) {
				// reduce element amount

				// some manipulations for cheese_down:
				if (cje.wp.eimgZcd[eid]) {
					for (var n in eImgVisible)
						if (eImgVisible[n] == eid+'x'+i)
							eImgVisible.splice(n,1);
					if (!eImgVisible.length) {
						wpizza_processElement(0,0);
					}
				}
/*
	mammont's shit
				if (!cje.wp.elm[eid].radio) {
					for (var n in eImgVisible)
						if (eImgVisible[n] == eid+'x'+i)
							eImgVisible.splice(n,1);
					if (!eImgVisible.length) {
						$('#cheese_down').hide();
					}
				}
*/

				if (cje.wp.eimg[eid][i].visible) {
					$('#wpl_'+eid+'_'+i).hide();
					cje.wp.eimg[eid][i].visible = 0;
				}
				if (!cje.wp.eimg[eid][i].loaded || cje.wp.eimg[eid][i].loaded<2) {
					for (var n in eImgPreload)
						if (eImgPreload[n] == eid+'x'+i)
							eImgPreload.splice(n,1);
					if (!eImgPreload.length)
						$('#wpl_loader').hide();
					cje.wp.eimg[eid][i].loaded = 0;
				}

				cje.wp.grp[cje.wp.elm[eid].gid]--;

				// reduce zindex of all elements in our z-indexMIN group
				var ourZID = cje.wp.eimg[eid][i].zid;
				for (var x1 in cje.wp.eimg)
					for (var x2 in cje.wp.eimg[x1])
						// if it's our zId group, and it's zId exceed zId of our element
						if (cje.wp.eimgZ[x1]==cje.wp.eimgZ[eid] && cje.wp.eimg[x1][x2].zid>ourZID) {
							// ok, lets decrease zId amount
							cje.wp.eimg[x1][x2].zid -- ;
							$('#wpl_'+x1+'_'+x2).css({'z-index':cje.wp.eimg[x1][x2].zid});
						}

				// set hiding image zIndex to zero
				cje.wp.eimg[eid][i].zid = 0;
			} else if (i<=count && cje.wp.eimg[eid][i].zid==0) {
				// increase element amount

				var maxZID = cje.wp.eimgZ[eid]-1;
				for (var x1 in cje.wp.eimg)
					for (var x2 in cje.wp.eimg[x1])
						// if it's our zId group, and it has zid (e.g. it already draw and take this zid)
						if (cje.wp.eimgZ[x1]==cje.wp.eimgZ[eid] && cje.wp.eimg[x1][x2].zid) {
							if (cje.wp.eimg[x1][x2].zid > maxZID) maxZID = cje.wp.eimg[x1][x2].zid;
						}
				// ok, we have max zId in maxZID variable
				cje.wp.eimg[eid][i].zid = maxZID+1;	// max zId, and little more -)
//window.alert('addLayer: '+eid+'; count='+count+'; num='+i+'; zid = '+cje.wp.eimg[eid][i].zid);


				// some manipulations for cheese_down:
				if (cje.wp.eimgZcd[eid]) {
					if (!cje.wp.elm[eid].radio) {
						eImgVisible[eImgVisible.length] = eid+'x'+i;
						//$('#cheese_down').show();
						wpizza_processElement(0,1);
					}
				}

				//window.alert('here we add new layer');
				if (!cje.wp.eimg[eid][i].visible) {
					cje.wp.eimg[eid][i].visible = 1;
					$('#wpl_'+eid+'_'+i).show();
				}
				// check - may be we must add new pic?
				if (!cje.wp.eimg[eid][i].loaded) {
					$('#wpl_loader').show();
					cje.wp.eimg[eid][i].loaded = 1;
					var elCB = eimgCreateLoadCallback(eid,i);
					eImgPreload[eImgPreload.length] = eid+'x'+i;
					$('<img />')
					    .load(elCB)
					    .error(function () {
					    		//window.alert('load error');
					    		var i1 = eid;
					    		var i2 = i;
						    	$(this).attr('src',cje.wp.eimg[i1][i2].src);
					    	})
					    .attr('src', cje.wp.eimg[eid][i].src)
				    ;
				}

				$('#wpl_'+eid+'_'+i).css({'z-index':cje.wp.eimg[eid][i].zid});

				cje.wp.grp[cje.wp.elm[eid].gid]++;
			} else if (i>count && cje.wp.eimg[eid][i].zid==0) {
				// do nothing
			}
		}
	cje.wp.elm[eid].count = count;
}

function eimgCreateLoadCallback(i,j) {
	return function() {
		for (var n in eImgPreload)
			if (eImgPreload[n] == i+'x'+j)
				eImgPreload.splice(n,1);
		if (!eImgPreload.length)
			$('#wpl_loader').hide();

		var img = '';
		img += $("#wpizzaImage").html();
		img += '<img id="wpl_'+i+'_'+j+'" src="'+cje.wp.eimg[i][j].src+'" class="wpizza_layer iePNG" style="display: none;" />';
		img += $("#wpizzaImage").html(img);

		if (cje.wp.eimg[i][j].visible) {
			$('#wpl_'+i+'_'+j).css({'z-index':cje.wp.eimg[i][j].zid});
			$('#wpl_'+i+'_'+j).show();//.ifixpng();
/*
			if (document.getElementById('wpl_'+i+'_'+j)) {
				document.getElementById('wpl_'+i+'_'+j).style.zIndex = cje.wp.eimg[i][j].zid;
			}
*/
			cje.wp.grp[cje.wp.elm[i].gid]++;
		}

		cje.wp.eimg[i][j].loaded = 2;
	}
}

$(document).ready(function() {
	cje.wpizzaWndName = '';

/*
	var cheese_down = '';

	cheese_down += $("#wpizzaImage").html();
	cheese_down += '<img id="cheese_down" src="'+cje.base_url+'/cheese_down.gif" class="wpizza_layer" style="display: none; z-index:'+cje.wp.zCheeseDown+';" />';
	cheese_down += $("#wpizzaImage").html(cheese_down);
*/

	$("#eimgLoadProgressbar").hide();
	$("#preloadImgDIV").hide();

	$("<img>").attr("src", cje.base_url+'/themes/wpizza2_0/win/button_close_on.gif');

	$('#wpizza_wnd_content_close_img').hover(
		function() { $(this).attr("src",cje.base_url+'/themes/wpizza2_0/win/button_close_on.gif'); },
		function() { $(this).attr("src",cje.base_url+'/themes/wpizza2_0/win/button_close.gif'); }
	);

	wpizza_initialize();

	imgLoadComplete();

	$("#eimgLoadProgressbar").progressbar({
		value: 0
	});

/*

	eImgNum = 0;
	for (var i in cje.wp.eimg)
		for (var j in cje.wp.eimg[i])
			eImgNum++;

	eImgLoad = 0;
	for (var i in cje.wp.eimg)
		for (var j in cje.wp.eimg[i])
			$('<img />')
			    .attr('src', cje.wp.eimg[i][j].src)
			    .load(eimgProcessLoad);

*/


	//var elW = $('#wpizza_wnd').width();
	//var elH = $('#wpizza_wnd').height();
	var elW = 429;
	var elH = 226;
	var wW = $(window).width();
	var wH = $(window).height();

	eWnd.x = (wW-elW)/2;
	eWnd.y = (wH-elH)/2;
//	window.alert('eWnd.x: '+eWnd.x+'; wW: '+wW+'; elW: '+elW);


	$("#wpizza_wnd").draggable({
		handle: '#wpizza_wnd_title_line',
		start: function() {
			;
		},
		drag: function() {
			;
		},
		stop: function() {
			var offs = $("#wpizza_wnd").offset();
			eWnd.x = offs.left - $(window).scrollLeft();
			eWnd.y = offs.top - $(window).scrollTop();
		}
	});

	if ($.browser.msie && $.browser.version == 6) {
		imgPizzaH = $("#wpizza_makepizza_constructorBlock_pizzaImageBlock").css({left:"-525px"});
	}

	$(window).bind("scroll", function(e){
		var imgContainerH = $("#wpizza_makepizza_constructorBlock").height();
		var imgPizzaH = $("#wpizza_makepizza_constructorBlock_pizzaImageBlock").height();
//		window.alert("imhCH = "+imgContainerH+"; imgPH = "+imgPizzaH);
		var scrH = $(window).scrollTop()-$("#wpizza_makepizza_constructorBlock").offset().top;
		if (scrH<0) scrH=0;
		if (scrH+imgPizzaH>imgContainerH)
			scrH = imgContainerH - imgPizzaH;
		$("#wpizza_makepizza_constructorBlock_pizzaImageBlock").animate({'top':scrH}, { queue: false, duration:"normal" } );

		if (eWnd.on) {
			$("#wpizza_wnd").draggable('disable');
			$('#wpizza_wnd').animate( {'left':(eWnd.x+$(window).scrollLeft()),'top':(eWnd.y+$(window).scrollTop())}, { queue: false, duration:"normal" } );
			$("#wpizza_wnd").draggable('enable');
		}
	});

/*
	$("#wpizza_lock").css({display:"block"});
	$("#wpizza_lock").addClass("wpizza_hidden");
	$("#wpizza_wnd").css({display:"block"});
	$("#wpizza_wnd").addClass("wpizza_hidden");
*/
});
function eimgProcessLoad() {
	eImgLoad++;
	$("#preloadImgDIV").html('Image preload: '+eImgLoad+'/'+eImgNum);
	$('#eimgLoadProgressbar').progressbar('option', 'value', 100*eImgLoad/eImgNum);
	if (eImgLoad >= eImgNum)
		imgLoadComplete();
}

function mousePageXY(e) {
	var x = 0, y = 0;
	if (!e) e = window.event;
	if (e.pageX || e.pageY) {
		x = e.pageX;
		y = e.pageY;
	} else if (e.clientX || e.clientY) {
		x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
		y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
	}

	return {"x":x, "y":y};
}


function wpizza_elementClick(el,elCheck,elm,type,mode) {
	if (cje.wp.lockConstructor) return;
//window.alert('element click (elm='+elm+'; type='+type+'; mode='+mode+') [eCount = '+cje.wp.elm[elm].count+']');
	// type = 1 - radio
	// type = 2 - checkbox

	// mode = 0 - std. mode, click on checkbox/radio
	// mode = 1 - window mode, click on element's name
	// mode = 2 - delete mode, delete element

	eCurrentElement = elm;

/*
		CALCULATE PRICE
*/
	switch (type) {
		case 1:

			break;
		case 2:

			break;
	}

//window.alert(mode);

	switch (mode) {
		case 0:
			eCurrentControl = el;
			break;
		default:
			eCurrentControl = document.getElementById('elR'+elm);
	}

	if (mode==1 && type==2) {
		// oh, it's chekbox and window mode! lets open our window
		wpizza_windowCountOpen(el,elm,'');
//window.alert('return 1');
		return;
	}

	if ((mode==2 && type==2) || (cje.wp.elm[elm].count>0 && type==2 && mode==0)) {
		// yeah, its checkbox in delete mode! (or we decide so)
		wpizza_processElement(elm,0);
		cje.wp.elm[elm].count = 0;
//window.alert('return 2');
		return;
	}
//	window.alert('pause');

/*
		AT FIRST, CHECK - MAY BE IT IS RADIO?
*/
	if (type==1) {
		for (var i in cje.wp.elm)
			if (cje.wp.elm[i].gid == cje.wp.elm[elm].gid) {
				// lets off it;
				wpizza_processElement(i,0);
				cje.wp.elm[i].count = 0;
				if (i!=elm) document.getElementById('elR'+i).checked = false;
			}
		if (cje.wp.elm[elm].parts==1) {
			wpizza_processElement(elm,1);
			cje.wp.elm[elm].count = 1;
			elCheck.checked = true;
		} else {
			wpizza_windowCountOpen(el,elm,'');
		}
		return;
	} else if (type==2) {
//window.alert('umm, it\'s std callback');
//		window.alert('type = '+type+'; mode = '+mode+'; eCount = '+cje.wp.elm[elm].count+'; parts = '+cje.wp.elm[elm].parts);
		if (cje.wp.elm[elm].parts==1) {
/*
			if (cje.wp.elm[elm].count==0) {
				cje.wp.elm[elm].count=1;
				eCurrentControl.checked = 1;
				setTimeout('eCurrentControl.checked=true;',1000);
				el.checked=true;
			} else {
				cje.wp.elm[elm].count=0;
				el.checked=false;
			}
*/
			var c = (elCheck.checked) ? 1 : 0;
			wpizza_processElement(elm,c);
			cje.wp.elm[elm].count = c;
		} else {
			wpizza_windowCountOpen(el,elm,'');
		}
	}
}
function wpizza_windowCountOpen(el,elm,mode) {
// mode - 1 if it's std pizza, 2 for blocks
/*
		FILL UP WINDOW WITH OUR INFO
*/

	$('#wpizza_wnd_body_count').show();

	eCurrentWndMode = mode;
	if (mode=='') {
		cje.wpizzaWndName = 'wnd_count';
		eOldElementCount = cje.wp.elm[elm].count;
		eCurrentElement = elm;
		// set 1 if it's off
		if (cje.wp.elm[elm].count<1) {
			wpizza_processElement(elm,1);
			cje.wp.elm[elm].count=1;
		}
		$('#wpizza_wnd_title').html(cje.wp.elm[elm].name);
		$('#wpizza_wnd_description').html(cje.wp.elm[elm].desc);
		$('#wpizza_wnd_weight').html(cje.wp.elm[elm].mass+' гр.');
		$('#wpizza_wnd_count').html(cje.wp.elm[elm].count);
		$('#wpizza_wnd_body_count .wpizza_wnd_logo').attr("src",cje.wp.elm[elm].logo);
	} else if (mode=='block') {
		cje.wpizzaWndName = 'wnd_block';
		eCurrentElement = elm;
		eOldElementCount = cje.wpb.items[elm].count;
		// set 1 if it's off
		if (cje.wpb.items[elm].count<1) {
			wpizza_processBlockElement(elm,1);
			cje.wpb.items[elm].count=1;
		}

		$('#wpizza_wnd_title').html(cje.wpb.items[elm].name);
		$('#wpizza_wnd_description').html(cje.wpb.items[elm].desc);
		$('#wpizza_wnd_weight').html(cje.wpb.items[elm].mass);
		$('#wpizza_wnd_count').html(cje.wpb.items[elm].count);
		$('#wpizza_wnd_body_count .wpizza_wnd_logo').attr("src",cje.wpb.items[elm].logo);
	}

	wpizza_wndShow();
}

function wpizza_elementCounter(op) {
	if (eCurrentWndMode == '') {
		var c = -1;
		switch (op) {
			case 1:
				if (cje.wp.elm[eCurrentElement].count>0) c = cje.wp.elm[eCurrentElement].count-1;
				break;
			case 2:
				if (cje.wp.elm[eCurrentElement].count<cje.wp.elm[eCurrentElement].parts) c = cje.wp.elm[eCurrentElement].count+1;
				break;
		}
		if (c == -1) return;
		$('#wpizza_wnd_count').html(c);
		wpizza_processElement(eCurrentElement,c);
		cje.wp.elm[eCurrentElement].count = c;
	} else {
		var c = -1;
		switch (op) {
			case 1:
				if (cje.wpb.items[eCurrentElement].count>0)
					c = cje.wpb.items[eCurrentElement].count-1;
				break;
			case 2:
				if (cje.wpb.items[eCurrentElement].count<cje.wpb.items[eCurrentElement].parts)
					c = cje.wpb.items[eCurrentElement].count+1;
				break;
		}
		if (c == -1) return;
		$('#wpizza_wnd_count').html(c);
		wpizza_processBlockElement(eCurrentElement,c);
		cje.wpb.items[eCurrentElement].count = c;
	}
}


function wpizza_elementAddButton() {
/*
		CHECK - IF IT'S NOT YET SELECTED, SELECT IT!
*/
/*
	if (eCurrentWndMode=='') {
		if (cje.wp.elm[eCurrentElement].count<1) {
			wpizza_processElement(eCurrentElement,1);
			cje.wp.elm[eCurrentElement].count = 1;
		}
	} else {
		if (cje.wpb.items[eCurrentWndMode][eCurrentElement].count<1) {
			wpizza_processBlockElement(eCurrentWndMode,eCurrentElement,1);
			cje.wpb.items[eCurrentWndMode][eCurrentElement].count = 1;
		}
	}
*/

	//eOldElementCount = cje.wp.elm[eCurrentElement].count;

	//closeWpizzaWindow();
}

function wpizza_serialize() {
	var s;

	s = '';
	for (var i=0; i<cje.wp.pizzaHistory.length; i++)
		s += (s=='') ? cje.wp.pizzaHistory[i] : ';'+cje.wp.pizzaHistory[i];
//		window.alert('pH['+i+'] = '+cje.wp.pizzaHistory[i]);
	$("#elOrder").val(s);


	s = '';
	s += 'size,'+$('[name=size]:checked').val();
	for (var i in cje.wp.pizzaContent)
		s += (s=='') ? i+','+cje.wp.pizzaContent[i] : ';'+i+','+cje.wp.pizzaContent[i];
//		window.alert('pC['+i+'] = '+cje.wp.pizzaContent[i]);
	$("#elCount").val(s);
	//window.alert(s);
	$("#elPizzaName").val($("#pizzaName").val());
}

function wpizza_submit_blocks() {
	if (cje.wpb) {
		s = '';
		for (var i in cje.wpb.items)
			if (cje.wpb.items[i].count)
				s += (s=='') ? i+','+cje.wpb.items[i].count : ';'+i+','+cje.wpb.items[i].count;
		$("#elBlock").val(s);
	}
}

function wpizza_lockConstructor() {
	cje.wp.lockConstructor = true;

	for (var i in cje.wp.elm)
		if (document.getElementById("elR"+i))
			document.getElementById("elR"+i).disabled = true;

	$("#wpizza_addToOrder").addClass("add_button_link_off").removeClass("add_button_link_on");
	$("#wpizza_makeOneMore").addClass("add_button_link_on").removeClass("add_button_link_off");
	$("#constructor_controlElements").fadeTo("slow", 0.25);
}

function wpizza_makeOneMore() {
	if (!cje.wp.lockConstructor) return;
	document.location = cje.base_url+'/makepizza';
	//document.getElementById("pageMode").value = "just_add";
	//wpizza_makeOrder();
}
function wpizza_goToOrderList() {
	if (!cje.wp.lockConstructor) {
/*		if (confirm('Добавить в корзину текущую пиццу?')) {*/
/* HERE WE SHOW A WND 'wnd_addpizza_yesno' */
		cje.wpizzaWndName = 'wnd_addpizza_yesno';
		$('#wpizza_wnd_body_yesno').show();

		$('#wpizza_wnd_title').html("Добавить текущую пиццу в корзину?");
		$('#wpizza_wnd_body_yesno .wpizza_wnd_logo').attr("src",cje.base_url+'/themes/wpizza2_0/win/wnd_imgBasket.jpg');

		wpizza_wndShow();
	} else {
		document.getElementById("pageMode").value = "just_order";//"add_and_order";
		wpizza_makeOrder();
	}
}
function wpizza_makeOrder() {
	wpizza_serialize();
//	window.alert('now submitting form...'+document.getElementById("pageMode").value);
	document.getElementById("wpizza_constructor_page").submit();
}

function wpizza_addToMenu() {
	document.getElementById("pageMode").value = "add_to_menu";
	wpizza_makeOrder();
}

//////////////////////////////////////////////////////////////////////////////////////////

/*

	NEXT FOLLOW SOME API FOR WND LIGHTBOXES

*/

function wpizza_wndShow(speed) {
/*
		CALCULATE WINDOW POSITION
*/

	wX = $(window).scrollLeft() + eWnd.x;
	wY = $(window).scrollTop() + eWnd.y;

	$('#wpizza_wnd').css({'left':wX,'top':wY,'position':'absolute'});

	eWnd.on = 1;

$("#wpizza_wnd").draggable('disable');
	$('#wpizza_wnd').toggleClass('wpizza_hidden');
$("#wpizza_wnd").draggable('enable');

	var spd;
	switch (speed) {
		case 'fast':
			spd = 0;
			break;
		default:
			spd = "slow";
	}

	$("#wpizza_lock").height($(document).height());
//	if($.browser.msie) {
//		$('#wpizza_lock').height($(document).height()).css({'background-color':'#000'}).toggleClass('wpizza_hidden');
//	} else {
		$('#wpizza_lock').height($(document).height()).fadeTo(0,0).toggleClass('wpizza_hidden').fadeTo(spd, 0.35);
//	}
}

function closeWpizzaWindow() {
	switch (cje.wpizzaWndName) {
		case 'wnd_count':
			wpizza_processElement(eCurrentElement,eOldElementCount);
			cje.wp.elm[eCurrentElement].count = eOldElementCount;

			eCurrentControl.checked = (cje.wp.elm[eCurrentElement].count > 0);

			eWnd.on = 0;
			break;
		case 'wnd_block':
			wpizza_writeBlockToDB(eCurrentElement,eOldElementCount);
			eWnd.on = 0;
			break;
		case 'wnd_somethingelse':
			break;
	}
	$('#wpizza_wnd_content > .wpizza_wnd_body').hide();

	$('#wpizza_lock').toggleClass('wpizza_hidden');
	$('#wpizza_wnd').toggleClass('wpizza_hidden');
}

function wpizza_writeBlockToDB(bID,count) {
	// at first we set count of this blocks on this page
	wpizza_processBlockElement(bID,count);

	// then we set its in our DB - through ajax request
	sa = $.ajax({
		type: "POST",
		url: cje.base_url+"/ajax/submitBlocks",
		data: "elBlock="+bID+","+count,
		cache: false,
		async:false,
		error: function () { window.alert("Error occured when sending block elements. Check your connection to the Internet."); }//wpizza_blocks_ajaxSend
	});//.responseText;
}

function wpizza_wndButton_add() {
	switch (cje.wpizzaWndName) {
		case 'wnd_count':
//			eOldElementCount = cje.wp.elm[eCurrentElement].count;

			eOldElementCount = cje.wp.elm[eCurrentElement].count;

//	$('#wpizza_lock').toggleClass('wpizza_hidden');
//	$('#wpizza_countBlock').toggleClass('wpizza_hidden');

			break;
		case 'wnd_block':
			eOldElementCount = cje.wpb.items[eCurrentElement].count;
	}
	closeWpizzaWindow();
}
function wpizza_wndButton_yes() {
	switch (cje.wpizzaWndName) {
		case 'wnd_addpizza_yesno':
			document.getElementById("pageMode").value = "add_and_order";
			wpizza_makeOrder();
			//wpizza_addPizzaToOrder();
			break;
	}
	closeWpizzaWindow();
}
function wpizza_wndButton_no() {
	switch (cje.wpizzaWndName) {
		case 'wnd_addpizza_yesno':
			if (!cje.wp.orderFull) {
/*				window.alert('Извините, но в заказе должна быть хотябы одна пицца!');*/
				/*  */
				closeWpizzaWindow();

				cje.wpizzaWndName = 'wnd_zeroofpizzas';
				$('#wpizza_wnd_body_msg').show();

				$('#wpizza_wnd_title').html("Вы еще не собрали ни одной пиццы!");
				$('#wpizza_wnd_body_yesno .wpizza_wnd_logo').attr("src",cje.base_url+'/themes/wpizza2_0/win/wnd_imgPlate.jpg');

				wpizza_wndShow('fast');

				return;
			}
			document.getElementById("pageMode").value = "just_order";
			wpizza_makeOrder();
			break;
	}
	closeWpizzaWindow();
}
function wpizza_wndButton_ok() {
	switch (cje.wpizzaWndName) {
		case 'wnd_zeroofpizzas':
			;
			break;
	}
	closeWpizzaWindow();
}

function wpizza_addPizzaToOrder() {
	wpizza_serialize();

	$.ajax({
		type: "POST",
		url: cje.base_url+"/ajax/submitPizza",
		data: "elOrder="+$("#elOrder").val()+"&elCount="+$("#elCount").val()+"&elName="+$("#elPizzaName").val(),
		cache: false,
		async: false,
		error: function () { window.alert("Error occured when sending block elements. Check your connection to the Internet."); }//wpizza_blocks_ajaxSend
	});

	wpizza_lockConstructor();
}