﻿function mouseOver(e) {
	if (e.className.indexOf('-over') < 0) {
		e.oldClassName = e.className;
		e.className += '-over';
	}
}

function mouseOut(e) {
	if (e.oldClassName) {
		e.className = e.oldClassName;
	}
}

function mouseClick(e) {
	var a = getChild(e,'a');
	if (a && a.href != '') document.location = a.href;	
}

function goBack() {
	document.location += (document.location.toString().indexOf('?') < 0) ?  '?back=1' : '&back=1';
}

function gotoPage(page) {
	document.location = page;
}

function constructURL() {
	// First argument is page, next parameters are pairs name value
	if (!(arguments.length % 2)) return; // use odd number of arguments
	
	var url = arguments[0];
	if (arguments[0].indexOf('?') < 0) url+="?";
	else url+="&";
	
	var i;
	for (i=1; i<arguments.length; i+=2) {
		url += arguments[i]+"="+escape(arguments[i+1]);
		if (i<arguments.length-2) url += "&";
	}
	return url;
}

function order(e) {
	clickTimeOutID = setTimeout(function() {
		if (clickTimeOutID >=0 ) { // firefox compatibility
			if (!e) return;
			var inputs = e.getElementsByTagName('input');
			if (!inputs.length) return;
			inputs[0].value = (inputs[0].value == "1" || inputs[0].value == "") ? "0" : "1";
			inputs[0].form.submit();
		}
	}, 600,e);
}

function clearOrder(e) {
	clearTimeout(clickTimeOutID);
	clickTimeOutID = -1;  // firefox compatibility
	if (!e) return;
	var inputs = e.getElementsByTagName('input');
	if (!inputs.length) return;
	if (inputs[0].value == "") return;
	inputs[0].value = "";
	inputs[0].form.submit();
}



function listPage(page) {
	var start = document.getElementById('list_start');
	if (!start) return;
	start.value = page;
	start.form.submit();
}

function onEnterSubmit(element,e) {
	var keynum;
	var keychar;

	if (window.event) {// IE
		keynum = e.keyCode;
	}
	else if (e.which) {// Netscape/Firefox/Opera
		keynum = e.which;
	} else {
		return;
	}
	keychar = String.fromCharCode(keynum);
	if (keychar == '\r') { // Enter key
		element.form.submit(); 
	}
}

function getChild(e,tagName) {
	var children = e.getElementsByTagName(tagName);
	return (children.length) ? children[0] : false; 
}

function getDefElementDisplay(e) {
	if (e) {
		switch(e.tagName.toUpperCase()) {
			case 'DIV':
				return 'block';
			break;
			case 'TR' :
				return (document.all) ? 'block' : 'table-row';
			break;
			case 'TD' :
				return (document.all) ? 'block' : 'table-cell';
			break;			
		}
	}
}

function showLayer(id) {
	var obj = document.getElementById(id);
	if (obj) {
		obj.style.display = getDefElementDisplay(obj);
		obj.style.visibility = 'visible';
	}
}

function hideLayer(id) {
	var obj = document.getElementById(id);
	if (obj) {
		obj.style.display = 'none';
		obj.style.visibility = 'hidden';
	}
}

function toggleLayerClass(layerID,class1,class2) {
	var obj = document.getElementById(layerID);
	if (obj) obj.className = (obj.className == class1) ? class2 : class1;
}

function toggleLayersClass(layerID,haystack,class1,class2) {
	var layers = layerID.split(',');
	var i,obj;
	for (i=0; i<haystack.length; i++) {
		obj = document.getElementById(haystack[i]);
		if (obj) obj.className = (in_array(obj.id,layers)) ? class1 : class2;
	}
}

function toggleLayersStyle(layerID,haystack,styleName,style1,style2) {
	var layers = layerID.split(',');
	var i,obj,styleValue;
	for (i=0; i<haystack.length; i++) {
		obj = document.getElementById(haystack[i]);
		if (obj) {
			styleValue = (in_array(obj.id,layers)) ? style1 : style2;
			if (styleName == 'alpha') {
				setAlpha(obj,styleValue);
			}
			else obj.style[styleName] = styleValue;
		}
	}
}

function in_array(needle,haystack) {
	for (var i in haystack) {
		if (haystack[i] == needle) return true;
	}
	return false;
}

function setAttribute(obj,attrName,attrValue) {
	if (!obj) return;
	if (document.all) obj[attrName] = attrValue;
	obj.setAttribute(attrName,attrValue);
}

function _getAttribute(obj,attrName) {
	if (!obj) return false;
	return (document.all) ? obj[attrName] : obj.getAttribute(attrName);
}

function getAlpha(obj) {
	if (!obj) return -1;
		var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		var re=/(alpha)(.{1})(opacity=)(\d{1,})(.{1})/;
		var result = re.exec(obj.style.filter);
		return (result != null)  ? parseFloat(result[4]) : 100;
	} else {
		if (obj.style.MozOpacity != undefined) return (obj.style.opacity == '') ? 100 : parseFloat(obj.style.opacity)*100;
		if (obj.style.MozOpacity != undefined) return (obj.style.MozOpacity == '') ? 100 : parseFloat(obj.style.MozOpacity)*100;
		if (obj.style.KhtmlOpacity != undefined) return (obj.style.KhtmlOpacity == '') ? 100 : parseFloat(obj.style.KhtmlOpacity)*100;
	}
	return -1;
}
 
function setAlpha(obj,alpha) {
	if (!obj) return;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		obj.style.filter = "alpha(opacity="+alpha+")";
	} else {
		obj.style.opacity = alpha*0.01;
		obj.style.MozOpacity = alpha*0.01;
		obj.style.KhtmlOpacity = alpha*0.01;
	}
}

//products list
function sliderChange(percent,slider) {
	var price = Math.round(document.minPrice+(document.maxPrice-document.minPrice)*percent);
	if (slider == 'from') {
		var min_price = document.getElementById('filter_min_price');
		if (min_price) min_price.value = price;
	}
	if (slider == 'to') {
		var max_price = document.getElementById('filter_max_price');
		if (max_price) max_price.value = price;
	}
}


window.onbeforeunload = function () {
	// This fucntion does nothing.  It won't spawn a confirmation dialog   // But it will ensure that the page is not cached by the browser.
}

$('document').ready(function(){
    $(function() {
        $('#lightbox a').lightBox();
    });
});
