// Generic
function getInnerHeight() {
	var height=0;
	if (self.innerHeight) { // all except Explorer
		height = self.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			height = document.documentElement.clientHeight;
		} else {
			if (document.body) { // other Explorers
				height = document.body.clientHeight;
			}
		}
	}
	return height;
}

function getInnerWidth() {
	var width=0;
	if (self.innerWidth) { // all except Explorer
		width = self.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientWidth) { // Explorer 6 Strict Mode
			width = document.documentElement.clientWidth;
		} else {
			if (document.body) { // other Explorers
				width = document.body.clientWidth;
			}
		}
	}
	return width;
}

function getOffsetX() {
	var offsetX =0;
	if (self.pageXOffset) { // all except Explorer
		offsetX = self.pageXOffset;
	} else {
		if (document.documentElement && document.documentElement.scrollLeft) { // Explorer 6 Strict
			offsetX = document.documentElement.scrollLeft;
		} else {
			if (document.body) { // all other Explorers
				offsetX = document.body.scrollLeft;
			}
		}
	}
	return offsetX;
}

function getOffsetY() {
	var offsetY =0;
	if (self.pageYOffset) { // all except Explorer
		offsetY = self.pageYOffset;
	} else {
		if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
			offsetY = document.documentElement.scrollTop;
		} else {
			if (document.body) { // all other Explorers
				offsetY = document.body.scrollTop;
			}
		}
	}
	return offsetY;
}

// Persos
function colorPop (pUrl) {
	// Creating popup
	if ($('colorPopup') == null) {
		var colorPopup = new Element('div', {'id' : 'colorPopup'});
		colorPopup.inject(document.body, 'top');
	} else {
		colorPopup = $('colorPopup');
		if (colorPopup.style.display == 'none') colorPopup.style.display = 'block';
	}
	
	colorPopup.style.left = ((getInnerWidth()-colorPopup.offsetWidth)/2 + getOffsetX())+"px";
	colorPopup.style.top = (((getInnerHeight()-colorPopup.offsetHeight)/2) + getOffsetY())+"px";
	
	// Creating overlay
	if ($('overlay') == null) {
		var overlay = new Element('div', {'id' : 'overlay', 'onclick' : 'this.style.display = \'none\';$(\'colorPopup\').style.display = \'none\';'});
		overlay.inject(document.body, 'top');
	} else {
		overlay = $('overlay');
		if (overlay.style.display == 'none') overlay.style.display = 'block';
	}
	
	// Now the popup's created, the html content is loaded by an ajax request
	var req = new Request.HTML({
		url : pUrl,
		onSuccess : function(html) {
			colorPopup.set('text', '');
			colorPopup.adopt(html);
		}
	});
	req.send();
}

// sets/updates perso values
function setPerso(vue, emp, perso, colors) {
	// First the hidden inputs are updated
	$('places_' + vue + '_' + emp).value = perso;
	$('nb_c_' + vue + '_' + emp).value = colors;
	var i, t, n, s, v;
	t = vues[vue];
	n = t.length;
	s = '';
	for (i = 0; i < n; ++i) {
		v = $('places_' + vue + '_' + t[i]['id']).value;
		s += t[i]['id'] + '_' + v + '-';
	}
	// reloading view picture
	$('view_' + vue).src = siteUrl + 'scripts/item-view.php?view=' + vue + '&place=' + s.substr(0, s.length - 1);
	
	// Update popup URL
	$('area_' + emp).href = $('popupUrl').value + '?lang=' + $('currlang').value + '&place=' + emp + '&view=' + vue + '&perso=' + perso + '&nbc=' + colors;
	// Then closing popup
	$('overlay').style.display = 'none';
	$('colorPopup').style.display = 'none';
}

//Thumbs in item page
window.addEvent('domready', function () {
	if ($('main_pic') != null) {
		var startImage = $('main_pic').src;
		var thumbs = $$('#thumbs img');
	
		thumbs.each(function(element) {
			element.addEvents({
				'mouseenter': function() { $('main_pic').src = element.src; },
				'mouseleave': function() { $('main_pic').src = startImage; }
			});
		});
	}
});
