/* (c) Jean Luc Biellmann - Groupe Ressources - 2010  */

var _ShowRoom = {
	img : null,
	draging : false,
	notwinsmess : 'Vous avez déjà ajouter cet objet à votre sélection !',
	drag : function (e) {
		if (!_ShowRoom.draging && e && Event.element(e) && Event.element(e).hasClassName('signal') && !Event.element(e).descendantOf('selection')) {
			_ShowRoom.img = Event.element(e).cloneNode(false).removeClassName('signal').removeClassName('signalover').removeClassName('shadow');
			document.body.appendChild(_ShowRoom.img);
			_ShowRoom.img.absolutize();
			_ShowRoom.img.style.left = parseInt(Event.pointerX(e)+10-_ShowRoom.img.offsetWidth/2)+'px';
			_ShowRoom.img.style.top = parseInt(Event.pointerY(e)+20)+'px';
			_ShowRoom.draging = true;
			Event.stop(e);
		}
	},
	move : function (e) {
		if (e==null)
			e=window.event;
		if (_ShowRoom.draging) {
			_ShowRoom.img.style.left = parseInt(Event.pointerX(e)+10-_ShowRoom.img.offsetWidth/2)+'px';
			_ShowRoom.img.style.top = parseInt(Event.pointerY(e)+20)+'px';
			Event.stop(e);
		}
	},
	drop : function (e) {
		if (_ShowRoom.draging && e && Event.element(e) && _ShowRoom.img) {
			if  (Event.element(e)==$('selection') || Event.element(e).descendantOf('selection')) {
				if (_ShowRoom.notwins(_ShowRoom.img.alt)) {
					_ShowRoom.add(_ShowRoom.img);
				} else {
					Element.remove(_ShowRoom.img);
					alert(_ShowRoom.notwinsmess);
				}
			} else {
				Element.remove(_ShowRoom.img);
			}
			_ShowRoom.draging = false;
			Event.stop(e);
		}
	},
	// control if ref/src isn't already in the target slot
	notwins : function (alt) {
		var notwins = true;
		$$('#selection img.slot').each(function (img) {
			if (img.alt==alt)
				notwins = false;
		});
		return notwins;
	},
	num : function () {
		var i=1;
		$$('div.objectselectednum').each(function (obj) {
			obj.update(i++);
		});
	},
	add : function (img) {
		img.addClassName('slot');
		// IE7 bug : img.relativize
		img.style.position='relative';
		img.style.left='0';
		img.style.top='0';
		var psrc = img.src.replace(/.*\//,'').replace(/.jpg$/,'');
		var div = E('div').addClassName('objectselected');
		A($('selection'),div);
		var a = E('a',{'href':'index.php?p=Meubles&ref='+img.alt+'&psrc='+psrc});
		A(a,img);
		A(div,a);
		var num = E('div').addClassName('objectselectednum');
		A(div,num);
		var iconclose = E('a',{'title':'Effacer l\'objet'}).addClassName('iconclose');
		A(div,iconclose);
		_ShowRoom.num();
		new Ajax.Request('index.php?m=uuid&c=UUIDSel&f=jsonAdd&ref='+img.alt+'&psrc='+psrc, {
			method: 'post',
			asynchronous: false
		});
		img.style.left='0';
		img.style.top='0';
	},
	rem : function (div) {
		var img = div.firstDescendant().firstDescendant();
		var psrc = img.src.replace(/.*\//,'').replace(/.jpg$/,'');
		Element.remove(img);
		new Ajax.Request('index.php?m=uuid&c=UUIDSel&f=jsonDel&ref='+img.alt+'&psrc='+psrc, {
			method: 'post',
			asynchronous: false
		});
		Element.remove(div);
		_ShowRoom.num();
	},
	del : function (iconclose) {
		if (iconclose && $(iconclose.parentNode).hasClassName('objectselected'))
			_ShowRoom.rem(iconclose.parentNode);
	},
	delAll : function () {
		if (confirm('Effacer toute la sélection ?')) {
			$$('#selection div.objectselected').invoke('remove');
			new Ajax.Request('index.php?m=uuid&c=UUIDSel&f=jsonDelAll', {
				method: 'post',
				asynchronous: false
			});
		}
	},
	dblclick : function (e) {
		if (!_ShowRoom.draging && e && Event.element(e) && Event.element(e).hasClassName('signal') && !Event.element(e).descendantOf('selection')) {
			if (_ShowRoom.notwins(Event.element(e).alt)) {
				_ShowRoom.img = Event.element(e).cloneNode(false).removeClassName('signal').removeClassName('signalover').removeClassName('shadow');
				_ShowRoom.add(_ShowRoom.img);
			} else {
				alert(_ShowRoom.notwinsmess);
			}
		}
	},
	manual : function () {
		if (!_ShowRoom.draging && $('objpicts') && $('objpicts').select('img') && $('objpicts').select('img').length) {
			_ShowRoom.img = $('objpicts').select('img')[0];
			if (_ShowRoom.notwins(_ShowRoom.img.alt)) {
				var img = _ShowRoom.img.cloneNode(false).removeClassName('signal').removeClassName('signalover').removeClassName('shadow');
				_ShowRoom.add(img);
			} else {
				alert(_ShowRoom.notwinsmess);
			}
		}
	},
	contact : function () {
		if (!_ShowRoom.draging && $('objpicts') && $('objpicts').select('img') && $('objpicts').select('img').length) {
			_ShowRoom.img = $('objpicts').select('img')[0];
			if (_ShowRoom.notwins(_ShowRoom.img.alt)) {
				var img = _ShowRoom.img.cloneNode(false).removeClassName('signal').removeClassName('signalover').removeClassName('shadow');
				_ShowRoom.add(img);
			}
			document.location='index.php?m=contact&c=Contact&f=edit';
		}
	},
	dynamic : function (e) {
		if (!_ShowRoom.draging && e && Event.element(e) && Event.element(e).hasClassName('signalover')) {
			var psrc = Event.element(e);
			var ptgt = $('mainpict');
			if (ptgt.src!=psrc.src.replace(/thumbs\//,'')) {
				ptgt.src = psrc.src.replace(/thumbs\//,'');
				ptgt.alt = psrc.alt;
				ptgt.title = psrc.title;
			}
		}
	},
	// retrieve articles tree for admin side (TblPromos)
	getDBLight : function (select_id,input_id) {
		var _JSON = new JSON();
		$(select_id).stopObserving('change');
		_JSON.onSuccess = function (json) {
			var option1,option2,option3,wcn,wsn,ref;
			var value = $F(input_id);
			$(select_id).update('');
			for (wcn in json.refs) {
				option1 = E('option',{value:String('index.php?p=Meubles&wcn='+wcn).escapeHTML()});
				A($(select_id),option1);
				option1.update(json.cats[wcn]['cn']);
				option1.selected = (option1.value.unescapeHTML()==value ? true : false);
				for (wsn in json.refs[wcn]) {
					option2 = E('option',{value:String('index.php?p=Meubles&wcn='+wcn+'&wsn='+wsn).escapeHTML()});
					A($(select_id),option2);
					option2.update('&nbsp;&nbsp;'+json.subcats[wcn][wsn]['sn']);
					option2.selected = (option2.value.unescapeHTML()==value ? true : false);
					for (ref in json.refs[wcn][wsn]) {
						option3 = E('option',{value:String('index.php?p=Meubles&wcn='+wcn+'&wsn='+wsn+'&ref='+ref).escapeHTML()});
						A($(select_id),option3);
						option3.update('&nbsp;&nbsp;&nbsp;&nbsp;'+json.refs[wcn][wsn][ref]['title']);
						option3.selected = (option3.value.unescapeHTML()==value ? true : false);
					}
				}
			};
			$(select_id).observe('change',function () {
				var i = $(select_id).options.selectedIndex;
				if (i!=-1)
					$(input_id).value = $(select_id).options[i].value.unescapeHTML(); 
			});
		};
		_JSON.send('m=showroom&c=ShowRoom&f=jsonGetDBLight');
	}
};


