var IE = document.all?true:false;
var IEver = 0;

if (IE) {
    IEver = getIEVer();
}

function getIEVer() {
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
	}
	return rv;
}

function findPosIE(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


var visibleSubmenu=0;
var keepVisible=false;
var hideIt=0;

function showLevel(refObj, id) {
	hideIt=0;
	hideCurrentLevel(0);
	elem=document.getElementById('menu'+id);
	if (elem) {
		x=refObj.offsetLeft;
		y=refObj.offsetTop;
		if (IE && IEver<8) {
			IEpos=findPosIE(refObj);
			x=IEpos[0];
			y=IEpos[1];
			if (IEver<7) {
				x -= 150;
			}
		}
		elem.style.display="block";
		elem.style.position="absolute";
		elem.style.top=(y+40)+"px";
		elem.style.left=(x-10)+"px";
		elem.onmouseover = function() {keepVisible=true;}
		elem.onmouseout = function() {keepVisible=false;hideLevel(id);}
		visibleSubmenu=id;
	}
}

function hideLevel(id) {
	hideIt=id;
	setTimeout("hideCurrentLevel("+id+")",1000);
}

function hideCurrentLevel(id) {
	if (hideIt!=id) {
		return true;
	}
	if ( visibleSubmenu>0 && !keepVisible) {
		document.getElementById('menu'+visibleSubmenu).style.display="none";
	}
}


// handling country select boxes on 'Order' page of basket
$(document).ready(function() {
	
	 var delivAdd = $('#country').attr('rel');
	 $("#country option[text=" + delivAdd + "]").attr('selected', 'selected');
	 $("#delivery_country_code").val(delivAdd);
	 
	 var billingAdd = $('#country_ro').attr('rel');	 
	 $("#country_ro option[rel=" + billingAdd + "]").attr('selected', 'selected');
	 $("#country_ro_code").val(billingAdd);
	 
	 $("#country_ro").change(function() {
			var cn_iso_3 = $("option:selected", this).attr('rel');
			$("#country_ro_code").val(cn_iso_3);
			//alert(cn_iso_3);
	 });
	 
	 $("#country").change(function() {
			var deliv_country_name = $("option:selected", this).text();		
			$("#delivery_country_code").val(deliv_country_name);
			//alert(deliv_country_name);
	 });
	
});





