function getItemsRelations() {
	var relations = {};
	if (pBlocks && pItems) {
		for (var p in pItems) {
			if (pItems[p].rel && pItems[p].rel.length) {
				for (var j in pItems[p].rel) {
					if (!relations[pItems[p].rel[j].id]) {
						relations[pItems[p].rel[j].id] = [];
					}
					relations[pItems[p].rel[j].id].push({i:pItems[p],v:pItems[p].rel[j].v});
				}
			}
		}
	}
	return relations;
}

function proccessItemRelations(p,rel) {//!!rel - childs array
	for (var i in rel) {
		setItemVisibility(rel[i].i,checkItemRelation(rel[i].i.rel,rel[i].i.flags.PF_REL_OR),true);
	}
}

function getPItemById(id) {
	for (var i in pItems) {
		if (pItems[i].id == id) return pItems[i];
	}
	return false;
}


function checkItemRelation(rel,ORlogic) {//!!rel - parents array
	var pc = {};
	var p,circle,vals,k;
	circle = true;
	for (var i in rel) {

		if (!circle && !ORlogic) return false;
		circle = false;
		pc[rel[i].id] = pc[rel[i].id]||getPItemById(rel[i].id);
		p = pc[rel[i].id];

		if (p && (p.input || p.values)) {
			if (!checkItemRelation(p.rel,p.flags&PF_REL_OR)) continue;
			//if (!p.mode) continue;
			if (p.values != undefined) {
				mode = false;
				if (p.values) {
					if (p.flags&PF_MULTISELECT) {
						for (j in rel[i].v) {
							for (k = 0;k < p.values;k++) {
								if (rel[i].v[j] == p.values[k]) {
									mode = true;break;
								}
							}
						}
					} else {
						for (j in rel[i].v) {
							if (rel[i].v[j] == p.values) {
								mode = true;break;
							}
						}
					}
				}
				if (!mode) continue;
			} else {

				switch (p.type) {
					case PT_CHECKBOX:
						if ((rel[i].v[0]^p.input.checked)) continue;
						break;
					case PT_LIST:
						if (p.flags&PF_SELECT) {
							mode = false;

							for (j in rel[i].v) {
								for (k = 0;k < p.input.options.length;k++) {
									if (p.input.options[k].selected && (rel[i].v[j] == p.input.options[k].value)) {
										mode = true;break;
									}
								}
							}
							if (!mode) continue;
						}
						else
						{
							mode = false;

							var els = p.input.getElementsByTagName('input');
							//for (var )
							for (j in rel[i].v) {
								for (k = 0;k < els.length;k++) {
									if (els[k].checked && (els[k].value == rel[i].v[j]))
									{
										mode = true;break;
									}
								}
							}
							if (!mode) continue;
						}
						break;
				}
			}
		} else {
			if (!ORlogic) return false;
		}
		if (ORlogic) return true;
		circle = true;
	}
	if (!circle) return false;

	return true;
}


function setItemVisibility(p,mode,deep) {
	var tr;
	tr = p.input;
	if (tr) {
		while (tr.parentNode && tr.nodeName.toLowerCase()!='tr') {
			tr = tr.parentNode;
		}
		if (tr.nodeName.toLowerCase() == 'tr') {
			tr.style.display = mode ? '' : 'none';
			p.mode = mode;
		}
	}
}

function checkItemValue(p,value) {
	if (p.mode && p.input) {
		switch (p.type) {
			case PT_CHECKBOX:
				if ((value^p.input.checked)) {
					return false;
				}
				break;
			case PT_LIST:
				if (p.flags&PF_SELECT) {
					mode = false;

					for (k = 0;k < p.input.options.length;k++) {
						if (p.input.options[k].selected && (value == p.input.options[k].value)) {
							mode = true;break;
						}
					}

					if (!mode) return false;
				}
				else
				{
					var els = p.input.getElementsByTagName('input');
					//for (var )

					mode = false;

					for (k = 0;k < els.length;k++) {
						if (els[k].checked && (els[k].value == value))
						{
							mode = true;break;
						}
					}

					if (!mode) return false;
				}
				break;
		}
		return true;
	} else return false;

}

function showItemProperties(mode,set_id,item_id) {//mode = 1 : input , mode = 2 : show values
	var tab = document.getElementById('itemproptab' + item_id);
	clearTable(tab);
	var tbody = document.createElement('tbody');

	if (pBlocks && pItems && (ipValues[item_id] || (mode == 1)))
	{
		var b,p,i,tr,td,el,values,proprows,vismode;
		var relations = getItemsRelations();
		for (b in pBlocks) {
			proprows = new Array();
			values = item_id == -1 ? {} : ipValues[item_id];
			for (p in pItems) {
				if (pItems[p].flags&PF_ACTIVE && ((mode == 1) || values[pItems[p].id])) {
					if (pItems[p].block == pBlocks[b].id) {
						el = mode == 1 ? showPInput(pItems[p],set_id,values[pItems[p].id],relations[pItems[p].id]) : showPValue(pItems[p],values[pItems[p].id],set_id);
						if (el) {
							tr = document.createElement('tr');
							td = document.createElement('td');
							td.className = 'first';
							td.appendChild(document.createTextNode(pItems[p].title[lang_] + ((pItems[p].flags&PF_MANDATORY) && (mode == 1) ? ' *' : '')));
							tr.appendChild(td);
							td = document.createElement('td');
							td.innerHTML = '&nbsp;';
							td.className = 'second';
							tr.appendChild(td);
							td = document.createElement('td');
							td.className = 'third';
							td.appendChild(el);
							tr.appendChild(td);


							if (pItems[p].rel && pItems[p].rel.length) {//related to

								vismode = checkItemRelation(pItems[p].rel,pItems[p].flags&PF_REL_OR);

								if (mode == 1) {
									setItemVisibility(pItems[p],vismode);
									proprows.push(tr);
								} else if (vismode) {
									proprows.push(tr);
								}

							} else {
								pItems[p].mode = true;
								proprows.push(tr);
							}

							if (relations[pItems[p].id]) {

							}

						}
					}
				}
			}
			if (proprows.length > 0) {
				tr = document.createElement('tr');
				td = document.createElement('td');
				td.className = 'first';
				el = document.createElement('b');
				el.appendChild(document.createTextNode(pBlocks[b].title[lang_]));
				td.appendChild(el);
				tr.appendChild(td);
				td = document.createElement('td');
				td.innerHTML = '&nbsp;';
				td.className = 'second';
				tr.appendChild(td);
				td = document.createElement('td');
				td.className = 'third';
				td.innerHTML = '&nbsp;';
				tr.appendChild(td);
				tbody.appendChild(tr);
				for (i = 0; i < proprows.length ; i++) {
					tbody.appendChild(proprows[i]);
				}
			}
		}
		for (var j in relations) {


		}

		tab.appendChild(tbody);
	}
}

function compareItems(item1,item2,set_id) {
	var tab = document.getElementById('comparetab');
	clearTable(tab);
	var tbody = document.createElement('tbody');
	if (pBlocks && pItems && ipValues[item1] && ipValues[item2])
	{

		var b,p,v,i,tr,td,el,el2,values,value2,proprows;
		values = ipValues[item1];
		values2 = ipValues[item2];
		for (b in pBlocks) {
			proprows = new Array();
			for (p in pItems) {
				if (pItems[p].flags&PF_ACTIVE) {
					if (pItems[p].block == pBlocks[b].id) {
						el = values[pItems[p].id] ? showPValue(pItems[p],values[pItems[p].id],set_id) : false;
						el2 = values2[pItems[p].id] ? showPValue(pItems[p],values2[pItems[p].id],set_id) : false;
						if (el || el2) {
							tr = document.createElement('tr');
							td = document.createElement('td');
							td.className = 'first';
							td.appendChild(document.createTextNode(pItems[p].title[lang_]));
							tr.appendChild(td);
							td = document.createElement('td');
							td.innerHTML = '&nbsp;';
							td.className = 'second';
							tr.appendChild(td);
							td = document.createElement('td');
							td.className = 'third';
							if (el) {
								td.appendChild(el);
							} else td.appendChild(document.createTextNode("\u00A0"));
							tr.appendChild(td);

							td = document.createElement('td');
							td.className = 'fourth';
							if (el2) {
								td.appendChild(el2);
							} else td.appendChild(document.createTextNode("\u00A0"));
							tr.appendChild(td);
							proprows.push(tr);
						}
					}
				}
			}
			if (proprows.length > 0) {
				tr = document.createElement('tr');
				td = document.createElement('td');
				td.className = 'first';
				el = document.createElement('b');
				el.appendChild(document.createTextNode(pBlocks[b].title[lang_]));
				td.appendChild(el);
				tr.appendChild(td);
				td = document.createElement('td');
				td.innerHTML = '&nbsp;';
				td.className = 'second';
				tr.appendChild(td);
				td = document.createElement('td');
				td.className = 'third';
				tr.appendChild(td);
				td = document.createElement('td');
				td.className = 'fourth';
				tr.appendChild(td);
				tbody.appendChild(tr);
				for (i = 0; i < proprows.length ; i++) {
					tbody.appendChild(proprows[i]);
				}
			}
		}
		tab.appendChild(tbody);
	}


}


function showPValue(p,value,set_id) {
	var rez = false;
	if ((p.flags&PF_HIDDEN) == 0) {
		p.values = value;
		switch (p.type) {
			case PT_CHECKBOX:
				if (parseInt(value) == 1) rez = document.createTextNode('Да');
				break;
			case PT_LIST:
				if (pSetLists[set_id] && pSetLists[set_id][p.list]) {
					if ((p.flags&PF_MULTISELECT) == 0) {
						value = [value];
					}
					var valueText = '';
					for (var i = 0; i < value.length ; i++) {
						if (pSetLists[set_id][p.list][value[i]]) {
							valueText += valueText == '' ? pSetLists[set_id][p.list][value[i]][lang_] : ' / ' + pSetLists[set_id][p.list][value[i]][lang_];
						}
					}
					if (valueText != '') {
						rez = document.createTextNode(valueText);
					}
				}
				break;
			case PT_DATE:
				break;
			case PT_FILE:
				if (value) {
					rez = document.createElement('a');
					rez.appendChild(document.createTextNode(value));
					rez.href = (pDownloadPath||'?') + '&pid=' + p.id;//hardcore
					//return $path.'file_i'.$item_id.'_'.$this->config['id'].'_'.$pid;
				}
				break;
			case PT_TEXT:
			default:
				rez = document.createTextNode(value);

		}
	}
	return rez;
}

function showPInput(p,set_id,value,rel) {//!!rel - childs array
	var rez = false,el;
	if ((p.flags&PF_NOFORM) == 0) {
		switch (p.type) {
			case PT_CHECKBOX:
				el = document.createElement('input');
				el.className = 'spec';
				el.type = 'checkbox';
				el.name = el.id = 'item_value' + p.id;
				el.value = '1';
				if (rel) {
					el.proccessChilds = function () {
						for (var i in rel) {
							setItemVisibility(rel[i].i,checkItemRelation(rel[i].i.rel,rel[i].i.flags&PF_REL_OR));
							if (rel[i].i.input.proccessChilds) rel[i].i.input.proccessChilds();
						}
					}
					//addEvent(el,'click',proccessItemRelations.bind(null,p,rel));
					addEvent(el,'click',el.proccessChilds.bind(el));
				}

				var def = false;
				if (value) {
					el.checked = true;
					el.defaultChecked = true;
				} else if (p.def && (p.def == 1)) {
					el.checked = def;
					el.defaultChecked = def;
				}
				rez = el;
				break;
			case PT_LIST:
				if (pSetLists[set_id] && pSetLists[set_id][p.list]) {
					var cont,t,n,m,s,c;
					var v = {};
					if ((p.flags&PF_MULTISELECT) == 0) {
						t = 'radio';
						n = 'item_value' + p.id;
						m = false;s = 1;

					} else {
						t = 'checkbox';
						n = 'item_value' + p.id + '[]';
						m = true;s = 5;
					}

					if (value) {
						if (value.length) {
							for (var j in value) {
								v[value[j]] = true;
							}
						} else {
							v[value] = true;
						}
					}

					if (p.flags&PF_SELECT) {
						cont = document.createElement('select');
						cont.name = cont.id = n;
						if (rel) {
							cont.proccessChilds = function () {
								for (var i in rel) {
									setItemVisibility(rel[i].i,checkItemRelation(rel[i].i.rel,rel[i].i.flags&PF_REL_OR));
									if (rel[i].i.input.proccessChilds) rel[i].i.input.proccessChilds();
								}
							}
							addEvent(cont,'change',cont.proccessChilds.bind(cont));
						}
						if (p.act) {
							cont.proccessAction = function () {
								for (var i in p.act) {
									if (this.value == i) {
										switch (p.act[i].type) {
											case PA_LOCATION:
												location.href = p.act[i].param;
												break;
											case PA_SETFIELD:
												break;
										}
										break;
									}
								}
							}
							addEvent(cont,'change',cont.proccessAction.bind(cont));
						}

						if (!m) cont.options[0] = new Option('...','');
					} else {
						cont = document.createElement('div');
					}

					for (var i in pSetLists[set_id][p.list]) {
						if (p.flags&PF_SELECT) {
							cont.options[cont.options.length] = new Option(pSetLists[set_id][p.list][i][lang_],i);
							cont.multiple = m;
							cont.size = s;
							if (v[i]) {
								cont.options[cont.options.length - 1].selected = true;
							} else if (!value && (p.flags&PF_COOKIE)) {
								if ((c = getCookie('pv' + p.id)) && (c == i)) {
									cont.options[cont.options.length - 1].selected = true;
									cont.selectedIndex = cont.options.length - 1;
								}
							}
						} else {
							if (dd.ie) {
								el = document.createElement('<input name="'+n+'"/>');
							} else {
								el = document.createElement('input');
							}
							el.type = t;
							el.value = i;
							el.className = 'spec';
							el.name = el.id =  n;
							//if (!m) addEvent(el,'click',function(){el.checked = true;});
							if (v[i]) {
								el.checked = true;
								el.defaultChecked = true;
							} else if (!value && (p.flags&PF_COOKIE)) {
								if ((c = getCookie('pv' + p.id)) && (c == i)) {
									el.checked = true;
									el.defaultChecked = true;
								}
							}


							if (rel) {
								el.proccessChilds = function () {
									for (var i in rel) {
										setItemVisibility(rel[i].i,checkItemRelation(rel[i].i.rel,rel[i].i.flags&PF_REL_OR));
										if (rel[i].i.input.proccessChilds) rel[i].i.input.proccessChilds();
									}
								}
								addEvent(el,'click',el.proccessChilds.bind(el));
							}
							if (p.act) {
								el.proccessAction = function () {
									for (var i in p.act) {
										if (this.value == i) {
											switch (p.act[i].type) {
												case PA_LOCATION:
													location.href = p.act[i].param;
													break;
												case PA_SETFIELD:
													break;
											}
											break;
										}
									}
								}
								addEvent(el,'click',el.proccessAction.bind(el));
							}


							cont.appendChild(el);
							cont.appendChild(document.createTextNode(pSetLists[set_id][p.list][i][lang_]));
							cont.appendChild(document.createElement('br'));
						}
					}

					rez = cont;

				}
				break;
			case PT_DATE:
				break;
			case PT_FILE:

				rez = document.createElement('input');
				rez.type = 'file';
				rez.name = rez.id = 'item_value' + p.id;
				rez.className = 'spec';


				break;
			case PT_TEXT:

			default:
				if (p.flags&PF_TEXT) {
					rez = document.createElement('textarea');
				} else {
					rez = document.createElement('input');
					rez.type = 'text';
				}

				rez.name = rez.id = 'item_value' + p.id;

				if (value) {
					rez.value = value;
				} else {
					rez.value = p.value ? p.value[lang_] : '';
				}
		}
	}

	/*
	p.setVisible = function (mode) {
		var t = rez;
		while (t.parentNode && t.nodeName.toLowerCase()!='tr') {
			t = t.parentNode;
		}
		if (t.nodeName.toLowerCase == 'tr') {
			t.style.display = mode ? '' : 'none';
		}
	}
	*/

	p.input = rez;


	return rez;

}

function cImageManager(orig,ipath,fmt) {
	this.cont = document.getElementById('item_image');
	this.orig = orig;this.path = ipath;this.fmt = fmt;
	this.origTb = this.cont.src;
	this.timer = null;
	this.popup = orig;

	this.swap = function (image_id) {
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}
		this.cont.src = 'images/px.gif';
		this.cont.style.background = 'url(\''+ this.path + 'tb/' + 'img_' + image_id + '.' + this.fmt + '\') no-repeat center center';
		this.popup = this.path + 'img_' + image_id + '.' + this.fmt;
	}

	this.out = function () {
		if (this.timer) {
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(this.restore.bind(this),'5000');
	}

	this.restore = function () {
		this.cont.src = this.origTb;
		this.cont.style.background = 'none';
		this.popup = this.orig;
		this.timer = null;
	}
}

/* * * * * * * * * */

function cItemManager() {
	this.rescont = null;
	this.items = [];
	this.state = 0;
	this.pager = null;
	this.itemcont = null;
	this.pickButton = null;
	this.dragState = 0;
	this.dragObj = null;
	this.dragSlot = null;
	this.compareSlots = {};
	this.compareSet = 0;
	this.compareCart = null;

	this.requestInfo = function(idList) {
		var params = '';
		if (idList != undefined) {
			for (var i = 0 ; i < idList.length; i++) {
				params += idList[i] + ',';
			}
			params = 'itemlist=' + params;
		} else return;

		var url = base_path + '?view=catalog&mode=jspickup';
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.itemResponse.bind(this),
				onFailure:this.itemResponse.bind(this)
			});
	}



	this.itemResponse = function(t) {
		this.state = 0;
		if (this.pickButton) {
			this.pickButton.className = '';
		}

		try {

			var response = eval('(' + t.responseText + ')');

			this.items = response;
		} catch (e) {

		}

		clearNode(this.rescont);
		this.initPager(this.items.length);

		var br = document.createElement('br');

		br.style.clear = 'both';
		this.rescont.appendChild(br);
		this.itemcont = document.createElement('div');
		this.itemcont.style.clear = 'both';
		this.rescont.appendChild(this.itemcont);
		this.showItems();

	}

	this.showItems = function() {
		clearNode(this.itemcont);

		var div,div2,a,img,span,span2;


		var s = (this.pager.page - 1)*this.pager.rpp;
		var e = this.pager.page*this.pager.rpp - 1;
		e = e > this.items.length ? this.items.length : e;

		for (var i = s; i < e ; i++) {

			div = document.createElement('div');
			a = document.createElement('a');
			a.className = "item1_tit";
			a.appendChild(document.createTextNode(this.items[i].t));
			a.href = link+'/view/' + this.items[i].a +'/';
			div.appendChild(a);
			div2 = document.createElement('div');
			div2.className = 'item_img';
			img = new Image();img.src = img_dir+'/tb/'+this.items[i].id+'.jpg';img.alt = this.items[i].t;
			div2.appendChild(img);
			div.appendChild(div2);


			div2 = document.createElement('div');
			div2.className = 'item1_butt';
			a = document.createElement('a');
			a.href = link+'/view/' + this.items[i].a +'/';
			img = new Image();img.src = 'img/ru/item-detail.gif';img.alt = 'детальней';
			a.appendChild(img);
			div2.appendChild(a);
			div.appendChild(div2);

			this.itemcont.appendChild(div);
		}

	}

	this.initPager = function (total,page,rpp,ppr) {
		if (this.pager && this.pager.container && this.pager.container.parentNode) {
			this.pager.container.parentNode.removeChild(this.pager.container);
		}

		this.pager = new cPager();
		this.pager.init(total,page,rpp,ppr);
		this.pager.setAction(this,this.showItems);

		this.rescont.appendChild(this.pager.container);
	}


	this.pickup = function (frm,obj) {
		if (this.state == 0) {
			this.state = 1;
			if (obj) {
				obj.className = 'disabled';
				this.pickButton = obj;
			}
			var el = frm.getElementsByTagName('input');
			var params = 'js=1';
			var i;
			var std_fields = ['category','price_from','price_to','set_id','cid'];
			for (i in std_fields) {
				if (frm[std_fields[i]]) {
					params += '&'+std_fields[i]+'='+urlencode(frm[std_fields[i]].value);
				}
			}

			for (var i = 0; i < el.length; i++) {
				if (el[i].name) {
					if (el[i].name.match(/^item_value/)) {
						if ((el[i].type == 'checkbox') || (el[i].type == 'radio')) {
							if (el[i].checked) {
								params += '&' + el[i].name + '=' + urlencode(el[i].value);
							}
						} else {
							params += '&' + el[i].name + '=' + urlencode(el[i].value);
						}
					}
				}
			}
			var url = base_path + '?view='+core_page+'&mode=jspickup';

			var myAjax = new Ajax.Request(
				url,
				{
					method: 'post',
					parameters: params,
					onComplete: this.itemResponse.bind(this),
					onFailure:this.itemResponse.bind(this)
				});
		}
	}

	this.initDraggable = function () {
		var i;
		var els = document.getElementsByTagName('div');
		for (i = 0; i< els.length; i++) {
			if (els[i].className && els[i].className == 'draggable')
			{
				addEvent(els[i],'mousedown',this.dragClick.bindAsEventListener(this,els[i]));
			}
		}

		for (i = 0; i < arguments.length; i++) {
			els = document.getElementById(arguments[i]);
			if (els) {
				addEvent(els,'mouseover',this.dragSlotIn.bind(this,els));
				addEvent(els,'mouseout',this.dragSlotOut.bind(this,els));
			}
		}

		addEvent(document,'mousemove',this.dragMove.bindAsEventListener(this));
		addEvent(document,'mouseup',this.dragUp.bindAsEventListener(this));


	}

	this.dragSlotIn = function(obj) {
		if (this.dragState == 1) {
			this.dragSlot = obj;
		}

	}

	this.dragSlotOut = function(obj) {
		if (this.dragState == 1) {
			this.dragSlot = null;
		}
	}

	this.dragClick = function(e,obj) {
		if (this.dragState == 0) {
			this.dragSlot = null;
			this.dragObj = obj.cloneNode(true);
			var inner = this.dragObj.getElementsByTagName('a');
			var elremove = [];
			for (var i = 0;i < inner.length ; i++) {
				if (!inner[i].className || inner[i].className != 'image') {
					elremove.push(inner[i]);
				}
			}
			for (var i = 0;i < elremove.length ; i++) {
				elremove[i].parentNode.removeChild(elremove[i]);
			}


			this.dragObj.className += ' transparent';
			this.dragObj.style.position = 'absolute';
			this.dragObj.style.zIndex = 150;
			var tmp = new dd.evt(e);
			this.dragObj.style.left = (tmp.x + 5) + 'px';
			this.dragObj.style.top = tmp.y + 'px';

			document.body.appendChild(this.dragObj);
			this.dragState = 1;

		}

		if (e && e.preventDefault) e.preventDefault(); // DOM style
		e.returnValue = false;
  		return false; //IE STYLE
	}

	this.dragMove = function(e) {
		if (this.dragState == 1) {
			var tmp = new dd.evt(e);
			this.dragObj.style.left = (tmp.x + 5) + 'px';
			this.dragObj.style.top = tmp.y + 'px';
			if (e.preventDefault) e.preventDefault(); // DOM style
			e.returnValue = false;
  			return false; //IE STYLE
		}
	}

	this.dragUp = function (e) {
		if (this.dragState == 1)
		{
			this.dragState = 0;
			var compupdate = false;
			this.dragObj.parentNode.removeChild(this.dragObj);
			if (this.dragSlot) {
				var rez;
				if (rez = this.dragObj.id.match(/ditem_(\d+)/)) {
					this.compareSlots[this.dragSlot.id] = rez[1];
					compupdate = true;
					clearNode(this.dragSlot);
					this.dragObj.style.position = 'static';
					this.dragObj.className = 'draggable';
					this.dragSlot.appendChild(this.dragObj);
				}
			}

			this.dragObj = null;

			if (compupdate) {
				this.compareItems();
			}
		}
	}


	this.compareItems = function () {
		if (this.compareSet > 0) {


			var items = [];
			for (var i in this.compareSlots) {
				items.push(this.compareSlots[i]);
			}

			if (items.length == 2) {
				compareItems(items[0],items[1],this.compareSet);

			}
		}
	}

	this.showCompare = function () {
		var arrayPageScroll = getPageScroll();
		if (!this.comparecart) {
			this.comparecart = document.getElementById('comparecart');
		}
		this.comparecart.style.top = (arrayPageScroll[1] + 200 + 'px');
		this.comparecart.style.left = '40%';
		this.comparecart.style.display = 'block';
		setTimeout('m_IM.hideCompare()',4000);
	}

	this.hideCompare = function () {
		if (!this.comparecart) {
			this.comparecart = document.getElementById('comparecart');
		}
		this.comparecart.style.display = 'none';
	}
	this.add2compare = function(id,set,page) {
		var url = base_path + '?view='+page+'&mode=compare&item_id='+id+'&set_id='+set;
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: '',
				onComplete: this.compareResponse.bind(this),
				onFailure:this.compareResponse.bind(this)
			});

	}

	this.addmasscompare = function(frm,set,page) {
		els = frm.getElementsByTagName('input');
		var ids = "";
		for (var i = 0; i < els.length ; i ++ )
		{

			if (els[i].checked){
				ids = ids+els[i].value+",";
			}

		}
		var url = base_path + '?view='+page+'&mode=massCompare&items='+ids+'&set_id='+set;

		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: '',
				onComplete: this.compareResponse.bind(this),
				onFailure:this.compareResponse.bind(this)
			});

	}

	this.compareResponse = function(t) {
		try {
			var result = parseInt(t.responseText);
			if (result == 1) {//added
				this.showCompare();
			} else if (result == 2) {//already

			}
		} catch (e) {

		}
	}

	this.removeCompare = function(id,obj) {
		while (obj && obj.className != 'draggable') {
			obj = obj.parentNode;
		}

		if (obj) {
			var url = base_path + '?view=goods&mode=compare&item_id='+id+'&set_id='+this.compareSet+'&remove=1';
			var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: '',
				onComplete: function(){},
				onFailure:function(){}
			});

			obj.parentNode.removeChild(obj);
		}
	}


}

var m_IM = new cItemManager();

/* * * * * * * * * */
/* * * * * * * * * * * * * * * * */
function cPager() {
	this.total = 0;
	this.page = 1;
	this.pages = 0;
	this.rpp = 5;
	this.ppr = 5;
	this.container = null;
	this.prev = null;this.next = null;
	this.pblock = null;this.nblock = null;
	this.pageBlock = null;

	this.prevPage = function () {
		if (this.page > 1) {
			this.selectPage(this.page - 1);
		}
	}

	this.nextPage = function () {
		if (this.page < this.pages) {
			this.selectPage(this.page + 1);
		}
	}

	this.prevBlock = function () {
		var sp = this.ppr*Math.ceil(this.page/this.ppr)-this.ppr + 1;
		if (sp > 1) {
			this.selectPage(this.page - this.ppr);
		}
	}

	this.nextBlock = function () {
		var sp = this.ppr*Math.ceil(this.page/this.ppr)-this.ppr + 1;
		var ep = sp + this.ppr - 1;
		if (ep < this.pages) {
			this.selectPage(ep + 1);
		}
	}

	this.selectPage = function (page,noaction) {
		this.page = page > 0 ? page : 1;
		var pB = this.createPages();
		if (this.pageBlock && this.pageBlock.parentNode) {
			this.pageBlock.parentNode.replaceChild(pB,this.pageBlock);
		}
		this.pageBlock = pB;
		if (this.prev) this.prev.style.display = this.page == 1 ? 'none' : 'inline';
		if (this.next) this.next.style.display = (this.page == this.pages) || (this.pages == 0) ? 'none' : 'inline';

		var sp = this.ppr*Math.ceil(this.page/this.ppr)-this.ppr + 1;

		if (this.pblock) this.pblock.style.display = sp > 1 ? 'inline' : 'none';
		if (this.nblock) this.nblock.style.display = (sp + this.ppr - 1) < this.pages ? 'inline' : 'none';

		if ((noaction != true) && this.action) {
			this.action.f.apply(this.action.o,arguments);
		}
	}

	this.setAction = function(obj,f) {
		this.action = {o:obj,f:f};
	}

	this.setParams = function (total,rpp) {
		this.total = total ? total : 0;
		this.rpp = rpp ? rpp : this.rpp;
		this.pages = Math.ceil(this.total/this.rpp);
		this.selectPage(this.page,true);
	}

	this.init = function (total,page,rpp,ppr) {
		this.total = total ? total : 0;
		this.page = page ? page : 1;
		this.rpp = rpp ? rpp : 20;
		this.ppr = ppr ? ppr : 5;

		if (this.total > this.rpp) {
			var cont,d1,d2,a,img,i;
			this.pages = Math.ceil(this.total/this.rpp);

			cont = document.createElement('div');
			cont.className = 'pager';
			d1 = document.createElement('div');
			d1.className = 'div';
			img = new Image();img.src = 'images/px.gif';img.height = 5;
			d1.appendChild(img);
			cont.appendChild(d1);
			d1 = document.createElement('div');
			d1.className = 'pages';
			/*
			this.prev = document.createElement('a');
			this.prev.href = 'javascript:;';
			addEvent(this.prev,'click',this.prevPage.bind(this));
			img = new Image();img.src = 'img/butt-left_p.gif';img.alt = '<';
			this.prev.appendChild(img);
			d1.appendChild(this.prev);
			if (this.page == 1) this.prev.style.display = 'none';
			*/

			d1.appendChild(document.createTextNode("\u00A0"));



			this.pblock = document.createElement('div');
			this.pblock.className = 'img';
			a = document.createElement('a')
			a.href = 'javascript:;';
			//this.pblock.href = 'javascript:;';
			addEvent(a,'click',this.prevBlock.bind(this));
			img = new Image();img.src = 'img/butt-left_p.gif';img.alt = '<';
			a.appendChild(img);
			this.pblock.appendChild(a);
			d1.appendChild(this.pblock);
			d1.appendChild(document.createTextNode("\u00A0"));

			this.pageBlock = document.createElement('span');//this.createPages();
			d1.appendChild(this.pageBlock);

			this.nblock = document.createElement('div');
			this.nblock.className = 'img';
			a = document.createElement('a');
			a.href = 'javascript:;';
			img = new Image();img.src = 'img/butt-right_p.gif';img.alt = '>';
			a.appendChild(img);
			addEvent(a,'click',this.nextBlock.bind(this));
			this.nblock.appendChild(a);
			d1.appendChild(this.nblock);

			/*
			this.next = document.createElement('a');
			this.next.href = 'javascript:;'
			img = new Image();img.src = 'img/butt-right_p.gif';img.alt = '>';
			this.next.appendChild(img);
			addEvent(this.next,'click',this.nextPage.bind(this));

			d1.appendChild(this.next);
			if (this.page >= this.pages) this.next.style.display = 'none';
			*/

			cont.appendChild(d1);
			this.container = cont;
			this.selectPage(this.page);

		} else {
			this.container = document.createElement('span');
		}
		return this.container;
	}

	this.createPages = function() {
		var sp = this.ppr*Math.ceil(this.page/this.ppr)-this.ppr + 1;
		var ep = sp + this.ppr - 1;
		ep = ep > this.pages ? this.pages : ep;
		var pB = document.createElement('span');
		for (var i = sp ; i <= ep ; i++) {
			a = document.createElement('a');
			if (i == this.page) {
				a.className = 'act';
			}
			a.appendChild(document.createTextNode(i+'.'));
			a.href = 'javascript:;';
			addEvent(a,'click',this.selectPage.bind(this,i));

			pB.appendChild(a);
			if (i == ep) {
				pB.appendChild(document.createTextNode("\u00A0"));
			}
		}
		return pB;
	}
}
/* * * * * * * * * * * * * * * * */
var ipValues = {};
var pSetLists = {};