// Generic get object by id
function getObject(elmID) {
	if (document.getElementById) { elmID = document.getElementById(elmID); }
	else if (document.all) { elmID = document.all[elmID]; }
	else if (document.layers) { elmID = this._getLayer(elmID); }
	else if (document.forms) {
		if(document.forms[elmID]) { elmID = document.forms[elmID]; }
		else {
			for(var i=0; i<document.forms.length; i++) {
				if(document.forms[i][elmID]) {
					elmID = document.forms[i][elmID];
					break;
				}
			}
		}
	} else { elmID = null; }
	return elmID;
}

// Managing multiple object event
function addEvent(obj, evType, fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else {
		var originalEvent = obj["on" + evType];
		if (typeof obj["on" + evType] != "function") {
			obj["on" + evType] = fn;
		} else {
			obj["on" + evType] = function() {
				originalEvent();
				fn();
			}
		}
	}
}

// Image rollover and rollout functions
function imgOut(image) {
	ext = image.src.substr(image.src.lastIndexOf('.') + 1);
	image.src = image.src.replace(new RegExp('ov.' + ext + '$'), 'on.' + ext);
}

function imgOver(image) {
	ext = image.src.substr(image.src.lastIndexOf('.') + 1);
	image.src = image.src.replace(new RegExp('on.' + ext + '$'), 'ov.' + ext);
}

// Image changer
function changeImages() {
	if (document.images) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = eval(changeImages.arguments[i+1]).src;
		}
	}
}

// Generic image popup window
function imagePopUp(imageURL,imageTitle){
	var defaultWidth  = 400;
	var defaultHeight = 500;
	var autoClose = false;
	var imgWin = openPopUpCenter("about:blank", defaultWidth, defaultHeight, "scrollbars=no");
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(400,500);');
		writeln('width=400-(document.body.clientWidth-document.images[0].width);');
		writeln('height=500-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);');
		writeln('window.moveTo(screen.width/2-width/2, screen.height/2-height/2);}');
		writeln('if (isNN){');       
		writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;');
		writeln('window.moveTo(screen.width/2-window.innerWidth/2, screen.height/2-window.innerHeight/2);}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');
		writeln('</sc'+'ript>');
		if (!autoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
		close();
	}
}

function openPopUpCenter(pageUrl, width, height, feature) {
	openPopUpCenter(pageUrl, "Popup", width, height, feature);
}

function openPopUpCenter(pageUrl, windowName, width, height, feature) {
	feature += ",width=" + width + ",innerWidth=" + width;
	feature += ",height=" + height + ",innerHeight=" + height;
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		feature += ",left=" + xc + ",screenX=" + xc;
		feature += ",top=" + yc + ",screenY=" + yc;
	}
	return window.open(pageUrl, windowName, feature);
}

function centerWindow() {
	var ns = (navigator.appName == "Netscape") ? true : false;
	iWidth = (ns) ? window.innerWidth : document.body.clientWidth;
	iHeight = (ns) ? window.innerHeight : document.body.clientHeight;
	window.moveTo(screen.width/2-iWidth/2, screen.height/2-iHeight/2);
	self.focus();
}

function getWindowSize() {
	var windowSize = new Object();
	windowSize.width = 0;
	windowSize.height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowSize.width = window.innerWidth;
		windowSize.height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		windowSize.width = document.documentElement.clientWidth;
		windowSize.height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		windowSize.width = document.body.clientWidth;
		windowSize.height = document.body.clientHeight;
	}
	return windowSize;
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function getObjectPosition(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		while (1) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			if (!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
		curtop += obj.y;
	}
	return {x:curleft, y:curtop};
}

function getObjectDimension(obj) {
	return { width:obj.offsetWidth, height:obj.offsetHeight }
}

// Hint popup
function prepareHints() {
	var windowSize = getWindowSize();
	var hasHint = false;
	
	var triggerList = [
		{tagName:"input", displayEvent:"focus", hideEvent:"blur"},
		{tagName:"select", displayEvent:"focus", hideEvent:"blur"},
		{tagName:"textarea", displayEvent:"focus", hideEvent:"blur"},
		{tagName:"img", displayEvent:"mouseover", hideEvent:"mouseout"},
		{tagName:"a", displayEvent:"mouseover", hideEvent:"mouseout"}
	];
	
	for(var i = 0; i < triggerList.length; i++) {
		var trigger = triggerList[i];
		var triggerElements = document.getElementsByTagName(trigger.tagName);
		for (var j = 0; j < triggerElements.length; j++){
			var triggerObj = triggerElements[j];
			var hintElements = triggerObj.parentNode.getElementsByTagName("span");
			for (var k = 0; k < hintElements.length; k++) {
				if (hintElements[k].className == "hint") {
					var hintObj = hintElements[k];
					// Add pointer
					var pointer = document.createElement("span");
					pointer.className = "hint_pointer";
					pointer.innerHTML = " ";
					hintObj.appendChild(pointer);
					
					// Check the position of the hint, make sure it's not over the window size, otherwise set to show the hint on the top
					hintObj.style.display = "inline";
					var hintPosition = getObjectPosition(hintObj);
					var hintDimension = getObjectDimension(hintObj);
					hintObj.style.display = "none";
					if (hintPosition.x + hintDimension.width > windowSize.width) {
						hintObj.className = "hint_offset";
						hintObj.style.marginTop = -(hintDimension.height + 4) + "px";
						hintObj.style.marginLeft = -(hintDimension.width + 35) + "px";
						pointer.style.top = hintDimension.height - 2 + "px";
						pointer.style.left = hintDimension.width + "px";
					}
	
					// Add the event
					triggerObj.hintObj = hintObj;
					addEvent(triggerObj, trigger.displayEvent, function(eventObj) {
						var thisObj = eventObj["srcElement"] ? eventObj["srcElement"] : eventObj["target"];
						thisObj.hintObj.style.display = "inline";
						
						// Fix for hover overlapping in IE 6 using i-frame
						var iframe = getObject("hint_mask");
						iframe.style.display = "block";
						iframe.style.width = thisObj.hintObj.offsetWidth;
						iframe.style.height = thisObj.hintObj.offsetHeight;
						iframe.style.left = thisObj.hintObj.offsetLeft;
						iframe.style.top = thisObj.hintObj.offsetTop;

					}, false);
					addEvent(triggerObj, trigger.hideEvent, function(eventObj) {
						var thisObj = eventObj["srcElement"] ? eventObj["srcElement"] : eventObj["target"];
						thisObj.hintObj.style.display = "none";
						
						var iframe = getObject("hint_mask");
						iframe.style.display = "none";
						
					}, false);
					
					hasHint = true;
				}
			}
		}
	}
	
	if (hasHint) {
		// Fix for hover overlapping in IE 6 using i-frame
		var hintMask = document.createElement("iframe");
		hintMask.id = "hint_mask";
		hintMask.scrolling = "no";
		hintMask.frameborder = "0";
		hintMask.width = "0";
		hintMask.height = "0";
		document.body.appendChild(hintMask);
	}
}
addEvent(window, "load", prepareHints, false);

function toggleDisplay(showids, hideids, sender) {
	if (showids != null) {
		showids = showids.split(",");
		for (var i = 0; i < showids.length; i++)
		{
			id = showids[i];
			element = document.getElementById(id);
			if (element != null) {
				if (element.style.display != "block") {
					element.style.display = "block";
				} else {
					element.style.display = "none";
				}
			}
		}
	}
	if (hideids != null) {
		hideids = hideids.split(",");
		for (var i = 0; i < showids.length; i++)
		{
			id = hideids[i];
			element = document.getElementById(id);
			if (element != null) {
				element.style.display = "none";
			}
		}
	}
	if (sender != null) {
		if (endWith(sender.className, "_on")) {
			sender.className = sender.className.replace(/_on$/, "_off");
		} else if (endWith(sender.className, "_off")) {
			sender.className = sender.className.replace(/_off$/, "_on");
		}
	}
}

function startWith(strCheck, pattern) {
	return strCheck.indexOf(pattern) === 0;
}

function endWith(strCheck, pattern) {
	var d = strCheck.length - pattern.length;
	return d >= 0 && strCheck.lastIndexOf(pattern) === d;
}

function getInternetExplorerVersion() {
	var rv = -1;
	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;
}

var stillHide = false;
var lastProductId = 0;
var lastObjRel = null;

function fShowDiv(objectId, objRel, offsetX, offsetY) {
	var obj = getObject(objectId);
	obj.style.display = 'block';
	
	var relPos = 	getObjectPosition(objRel);
	var relDim = 	getObjectDimension(objRel);
	obj.style.left = relPos.x+(offsetX?offsetX:0)+"px";
	obj.style.top = relPos.y+(offsetY?offsetY:0)+"px";
	
	//AssignPosition(obj, 0);
	return false;
}

var fHideDivTimeout = 0;
function fHideDiv(objectId, delay, e) {
	if (delay && delay > 0) {
		fHideDivTimeout = setTimeout('fHideDiv("' + objectId + '")', delay);
		return false;
	}
	if (!e) var e = window.event;
	if (e) {
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		while (reltg.tagName != 'BODY') {
			if (reltg.id == objectId) { return; }
			reltg = reltg.parentNode;
		}
	}
	
	var obj = document.getElementById(objectId);
	obj.style.display = 'none';
	return false;
}

function fCancelHideDiv() {
	clearTimeout(fHideDivTimeout);
	fHideDivTimeout = 0;
}

function fShow(productId, objRel) {
	stillHide	= true;
	if(lastProductId > 0 && lastProductId != productId)
		fCheckHide(lastProductId);
		
	var obj = document.getElementById("popUp" + productId);
	obj.style.display = 'block';
	
	//AssignPosition(obj, productId, objRel);
	var relPos = 	getObjectPosition(objRel);
	var relDim = 	getObjectDimension(objRel);
	obj.style.left = (relDim.width-(relDim.width/2))+"px";
	obj.style.top = (relDim.height-(relDim.height/2)-15)+"px";
	var windowSize = getWindowSize();
	var objPos = getObjectPosition(obj);
	var objDim = getObjectDimension(obj);
	if (objPos.x + objDim.width > windowSize.width) {
		obj.style.left = -(relDim.width+(relDim.width/2))+"px";
	}
	if (objPos.y + objDim.height > windowSize.height + f_scrollTop()) {
		obj.style.top = -((objPos.y + objDim.height) - (windowSize.height + f_scrollTop()) - 20)+"px";
	}
	
	if (lastObjRel) lastObjRel.parentNode.parentNode.style.zIndex = 900;
	objRel.parentNode.parentNode.style.zIndex = 1000;
	lastObjRel = objRel;
	
	if (getInternetExplorerVersion() >= 6 && getInternetExplorerVersion() < 8)
		getObject("footer").style.zIndex = -1;
	
	lastProductId = productId;
	return false;
}

function fHide(productId) {
	stillHide = true;
	setTimeout('fCheckHide('+ productId +')', 1000);
}

function fCheckHide(productId)
{
	if(stillHide) {
		document.getElementById("popUp" + productId).style.display = 'none';
	}
	
	if (getInternetExplorerVersion() >= 6 && getInternetExplorerVersion() < 8)
		getObject("footer").style.zIndex = 10;
}

var cX = 0; var cY = 0; var rX = 0; var rY = 0;
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }

function AssignPosition(d, productId, objRel) {
	if(self.pageYOffset) {
		rX = self.pageXOffset;
		rY = self.pageYOffset;
	} else if(document.documentElement && document.documentElement.scrollTop) {
		rX = document.documentElement.scrollLeft;
		rY = document.documentElement.scrollTop;
	} else if(document.body) {
		rX = document.body.scrollLeft;
		rY = document.body.scrollTop;
	}
	if(document.all) {
		//cX += rX; 
		//cY += rY;
	}
		
	if(productId >= 9)
		cY = cY - 270;
	//else
		//cY = cY - 100;
	
	if(productId == 4 || productId == 8 || productId == 12)
		cX = cX - 280;

	if(productId == 0)
	{
		//cY = cY + 100;
		//cX = cX + 10;
	}
		
	d.style.left = (cX+10) + "px";
	d.style.top = (cY+10) + "px";
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    }
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = getObject(id);
    object.style.opacity = (opacity / 100);
    object.style.MozOpacity = (opacity / 100); 
    object.style.KhtmlOpacity = (opacity / 100); 
    object.style.filter = "alpha(opacity=" + opacity + ")"; 
    if (opacity == 100) {
			object.style.filter = "";
    }
} 

function shiftOpacity(id, millisec) { 
    //if an element is invisible, make it visible, else make it ivisible 
    if(getObject(id).style.opacity == 0) { 
        opacity(id, 0, 100, millisec); 
    } else { 
        opacity(id, 100, 0, millisec); 
    } 
} 

/*Browser Detect*/

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
