Site = function() { };

Site.images = [];
Site.submenu = 0;
Site.subactive = 0;
Site.banner = 0;
Site.allBanners = 0;
Site.banTimer = null;
Site.shown = '';
Site.tmpShow = null;
Site.tmrShow = null;
Site.preimgNow = false;
Site.preimgTO = null;
Site.pivScrolling = false;

Site.preload = function(src) {
	Site.images[Site.images.length] = new Image();
	Site.images[Site.images.length-1].src = src;
};

Site.menushow = function(object, id) {
	var pos = Site.absolute(object);
	var item = document.getElementById('subgroup' + id);
	item.style.left = pos.x + 'px';
	item.style.display = '';
	Site.submenu = id;
};

Site.menuhide = function(e) {
	if (Site.submenu > 0) {
		if (!e) e = window.event;
		var sm = document.getElementById('subgroup' + Site.submenu);
		var go = false;
		var target = e.relatedTarget ? e.relatedTarget : e.toElement;
		while (target) {
			if (target == sm) {
				go = true;
				break;
			}
			target = target.parentNode;
		}
		
		if (!go) {
			sm.style.display = 'none';
			Site.submenu = 0;
		}
	}
};

Site.openview = function(src) {
	if (src == Site.shown) return;
	if (Site.tmpShow) Site.showview(2);
	var is = document.getElementById('ishow');
	Site.tmpShow = document.createElement('IMG');
	Site.tmpShow.style.position = 'absolute';
	Site.tmpShow.style.top = '0';
	Site.tmpShow.style.left = Math.round(is.parentNode.offsetWidth/2 - is.offsetWidth/2) + 'px';
	Site.tmpShow.style.width = is.offsetWidth;
	Site.tmpShow.style.height = is.offsetHeight;
	Site.tmpShow.style.opacity = '0';
	Site.tmpShow.style.filter = 'alpha(opacity=0)';
	Site.tmpShow.src = src;
	is.parentNode.appendChild(Site.tmpShow);
	Site.shown = src;
	Site.showview(0);
};

Site.showview = function(opacity) {
	if (Site.tmpShow) {
		if (opacity <= 1) {
			Site.tmpShow.style.opacity = opacity;
			Site.tmpShow.style.filter = 'alpha(opacity=' + (opacity*100) + ')';
			Site.tmrShow = window.setTimeout('Site.showview(' + (opacity + 0.1) + ');', 30);
		} else {
			window.clearTimeout(Site.tmrShow);
			var is = document.getElementById('ishow');
			is.src = Site.shown;
			is.parentNode.removeChild(Site.tmpShow);
			Site.tmpShow = null;
		}
	}
};

Site.previewimg = function(id, inv) {
	Site.preimhide();
	Site.preimgNow = [];
	Site.preimgNow['prev'] = document.getElementById('ip' + id);
	Site.preimgNow['cont'] = document.getElementById('ipc' + id);
	Site.preimgNow['link'] = document.getElementById('ipcl' + id);
	Site.preimgNow['anch'] = document.getElementById('ipl' + id);
	Site.preimgNow['prev'].style.display = 'none';
	Site.preimgNow['cont'].style.display = '';
	Site.preimgNow['cont'].style.zIndex = '700';
	Site.preimgNow['cont'].style.width = '124px';
	Site.preimgNow['cont'].style.height = '94px';
	Site.preimgNow['link'].style.width = '120px';
	Site.preimgNow['link'].style.zIndex = '701';
	Site.preimgNow['link'].style.height = '90px';
	if (inv) Site.preimgNow['anch'].className += ' linkInvert';
	Site.preimanime();
};

Site.preimanime = function() {
	if (!Site.preimgNow) return false;
	var delta = 10;
	var width = parseInt(Site.preimgNow['link'].style.width);
	if (width >= 180) return false;
	
	Site.preimgNow['link'].style.width = (width + delta) + 'px';
	Site.preimgNow['link'].style.height = Math.round((width + delta) / 4 * 3) + 'px';
	Site.preimgNow['cont'].style.width = (width + delta + 4) + 'px';
	Site.preimgNow['cont'].style.height = Math.round((width + delta) / 4 * 3 + 4) + 'px';
	
	Site.preimgTO = window.setTimeout('Site.preimanime()', 40);
};

Site.preimhide = function(inv) {
	if (Site.preimgNow) {
		window.clearTimeout(Site.preimgTO);
		Site.preimgNow['prev'].style.display = '';
		Site.preimgNow['cont'].style.display = 'none';
		if (inv) Site.preimgNow['anch'].className = 'txbig clrgreen';
		Site.preimgNow = false;
	}
};

Site.setProduct = function(link, text) {
	var ajx = new Ajax('/kernel/modules/m.setproduct.php', 'POST');
	ajx.post = 'pr=' + text + '&nc=' + Ajax.nocache();
	ajx.include = link.href;
	
	ajx.finish = function() {
		if (this.object.responseText == 'true') window.location.href = this.include;
	};
	ajx.start();
	return false;
};

Site.pivScrollNext = function() {
	var piv = document.getElementById('pageIViewerConteiner');
	if (!piv || Site.pivScrolling) return;
	Site.pivScrolling = true;
	var delta = piv.scrollLeft + piv.offsetWidth;
	if (delta > (piv.scrollWidth - piv.offsetWidth)) delta = piv.scrollWidth - piv.offsetWidth;
	Site.pivScrollSoft(delta);
};

Site.pivScrollPreview = function() {
	var piv = document.getElementById('pageIViewerConteiner');
	if (!piv || Site.pivScrolling) return;
	Site.pivScrolling = true;
	var delta = piv.scrollLeft - piv.offsetWidth;
	if (delta < 0) delta = 0;
	Site.pivScrollSoft(delta);
};

Site.pivScrollSoft = function() {
	var piv = document.getElementById('pageIViewerConteiner');
	if (!piv) return false;
	if (arguments.length == 1) {
		if (navigator.appName.match(/microsoft/i)) document.getElementById('ishow').style.position = '';
		var point, delta, time;
		point = arguments[0] - piv.scrollLeft;
		delta = point >= 0 ? 46 : -46;
		point = Math.abs(point);
		time = Math.round(point / 46) * 10 - 20;
		Site.pivScrollSoft(piv.scrollLeft + delta, 0, time, delta);
	} else {
		piv.scrollLeft = arguments[0];
		if (arguments[1] <= arguments[2]) window.setTimeout('Site.pivScrollSoft(' + (piv.scrollLeft + arguments[3]) + ', ' + (arguments[1] + 10) + ', ' + arguments[2] + ', ' + arguments[3] + ');', 10);
		else {
			var bn = document.getElementById('pageIViewerRight');
			var bp = document.getElementById('pageIViewerLeft');
			bn.style.visibility = 'visible';
			bp.style.visibility = 'visible';
			var maxScroll = piv.scrollWidth - piv.offsetWidth;
			if (piv.scrollLeft == 0) bp.style.visibility = 'hidden';
			if (maxScroll <= piv.scrollLeft) bn.style.visibility = 'hidden';
			Site.pivScrolling = false;
			if (navigator.appName.match(/microsoft/i)) document.getElementById('ishow').style.position = 'absolute';
		}
	}
};

Site.setIngredient = function(id, unset) {
	if (!unset) unset = 0;
	document.getElementById('result').innerHTML = '<div style="padding: 10px;" class="txbig clrdgreen">ئلوْم...</div>';
	var ajx = new Ajax('/kernel/modules/m.ingchooser.php', 'POST');
	ajx.post = 'id=' + id + '&unset=' + unset + '&decode=1&nc=' + Ajax.nocache();
	
	ajx.finish = function() {
		var tx = this.object.responseText;
		if (tx) {
			var ajx = new Ajax('/kernel/modules/m.ingfind.php');
			ajx.parameters = '?decode=1&nc=' + Ajax.nocache();
			ajx.finish = function() {
				var tx = this.object.responseText;
				if (tx) document.getElementById('result').innerHTML = tx;
			};
			ajx.start();
			document.getElementById('chooser').innerHTML = tx;
		}
	};
	ajx.start();
	return false;
}

Site.absolute = function(object, stopObject) {
	var oPos = {x : object.offsetLeft, y : object.offsetTop};
	if (object.offsetParent && (object.offsetParent != stopObject)) {
		var oTemp = Site.absolute(object.offsetParent);
		oPos.x += oTemp.x;
		oPos.y += oTemp.y;
	}
	return oPos;
};
