﻿
var crappyBrowser = navigator.appName.indexOf("Microsoft")>=0;

var init=null;
var pageScrolled=null;
var adjustLayout=null;

window.onload = function(){
    if (adjustLayout)
        adjustLayout();
    if (init)
        init();
}

window.onscroll = pageScrolled;
window.onresize = adjustLayout;

function language(code)
{
	var url = document.location.href;
	if (url.indexOf("?")>0)
		url=url.substring(0,url.indexOf("?"));
	if (url.lastIndexOf("/")==url.length-1)
		url+="index.jsp";	
	document.location.href=url+"?lang="+code;
}
function setLanguage(id)
{
    var url = "www.netbriller.com";
    if (id=="en")
        url="uk.netbriller.com";
    else if (id=="de")
        url="de.netbriller.com";
    else if (id=="sv")
        url="sv.netbriller.com";
    else if (id=="no")
        url="no.netbriller.com";
    document.changeLanguage.action="http://"+url;
    document.changeLanguage.lang.value=id;
    document.changeLanguage.submit();
}
function setCurrency(id)
{
    var url = location.href;
    if (url.indexOf("?")>0)
        url = url.substring(0,url.indexOf("?"));
    document.location.href = url +"?currency=" +id;
}
function feedback()
{
    document.getElementById("feedbackButtonDiv").innerHTML="<input type=\"button\" onclick=\"leaveFeedback();\" value=\"Send Feedback\" />";
    if (!crappyBrowser){
        showGreyPanel();
        showCentered("feedbackPanel", true);
    }else{
        showCentered("feedbackPanel");
    }
}
function showGreyPanel()
{
//alert("crappy browser="+crappyBrowser+" from "+navigator.appName);
    if (crappyBrowser)
        return;
    xResizeTo("greyPanel", xClientWidth(), xClientHeight());
    if (crappyBrowser)
        xMove("greyPanel", xScrollLeft(), xScrollTop());
    xShow("greyPanel");
}
function hideGreyPanel()
{
    xHide("greyPanel");
}
function pageScrolled()
{
    if (xGetElementById("feedbackPanel").style.visibility=="visible"){
        feedback();
    }
}
function leaveFeedback()
{
    document.getElementById("feedbackButtonDiv").innerHTML="<img src=\"images/loading.gif\" alt=\"Loading...\" />";
    makeRequest("http://www.netbriller.com/servlet/LeaveFeedback",
                "feedbackEmail="+escape(document.getElementById("feedbackEmail").value)
                +"&feedback="+escape(document.getElementById("feedback").value)
                +"&pageURL="+escape(pageURL));
}
function makeRequest(url, formData)
{
    var httpRequest;
    var method="GET";
    if (formData==null)
        formData="";
    if (formData!="")
        method="POST";

    if (window.XMLHttpRequest){               // Firefox, Safari and other Proper Browsers
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType)
            httpRequest.overrideMimeType('text/xml');
    } 
    else if (window.ActiveXObject){           // infernal excrement and other shite
        try{
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(x){
            try{
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(x){}
        }
    }
    if (!httpRequest) {
        alert("Sorry, the feedback system uses AJAX which your browser doesn't support.");
        return false;
    }
    httpRequest.onreadystatechange = function(){ alertContents(httpRequest); };
    httpRequest.open(method, url, true);    
    if (method=="POST"){
        httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        httpRequest.setRequestHeader("Content-length", formData.length);
        httpRequest.setRequestHeader("Connection", "close");
    }           
    httpRequest.send(formData);
    return true;
}
function alertContents(httpRequest)
{
    if (httpRequest.readyState == 4){
        if (httpRequest.status == 200)
            document.getElementById("feedbackButtonDiv").innerHTML="Thank you for the feedback.";
    
        else
            document.getElementById("feedbackButtonDiv").innerHTML="Sorry, there was a problem with the request.";
        setTimeout("closeFeedback();",1600);
    }
}
function closeFeedback()
{
    document.getElementById("feedbackPanel").style.visibility="hidden";
    hideGreyPanel();
}
function getCookieVal(offset)
{
	  var endstr = document.cookie.indexOf (";", offset);
	  if (endstr == -1)
		endstr = document.cookie.length;
	  return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name)
{
	  var arg = name + "=";
	  var alen = arg.length;
	  var clen = document.cookie.length;
	  var i = 0;
	  while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		  return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	  }
	  return null;
}
function adjustLayout()
{
    var cHeight = xHeight("content");
    var lHeight = xHeight("mainMenu");
    var rHeight = xHeight("subColumn");
    var bodyHeight = xHeight("content");
    var maxHeight = Math.max(cHeight, lHeight, rHeight)+100; // f' knows why it needs this +15, margin thing perhaps??

//  xHeight("content", maxHeight);
//  xHeight("left", maxHeight);
//  xHeight("right", maxHeight);

    xMoveTo("debug",0,maxHeight);
    xShow("debug");
}
function checkFeedbackPos()
{
    if (xGetElementById("greyPanel").style.visible=="visible")
        showCentered("greyPanel");
    if (xGetElementById("feedbackPanel").style.visible=="visible")
        showCentered("feedbackPanel");
}
function infoPopUp(e, id)
{
/*
    if (event)
        e=event;
    xGetElementById("footerInfo").innerHTML += "<p>pop "+id+"</p>";

    var x = event.clientX + document.body.scrollLeft;
    var y = event.clientY + document.body.scrollTop + 35;
    var popup = xGetElementById(id);

    x -= popup.width/2;
    if (x<0)
        x=0;
    else if (x+popup.width>xGetElementById('container').width)
        x = xGetElementById('container').width - popup.width;
    xMoveTo(id,x,y);
*/
    xShow(id);
}
function createPopUpPanels()
{
    var tags = document.getElementsByTagName("div");
    xGetElementById("footerInfo").innerHTML += "<p>CREATE POPUPS</p>";
	for (var i = 0; i < tags.length; i++) {
        if (!tags[i].getAttribute("id"))
            continue;
        if (tags[i].getAttribute("id").search(/(.*)InfoPopPanel/) != -1) {
			tags[i].onmouseover = "xShow('" +tags[i].getAttribute("id") +"')";
			tags[i].onmouseout = "xHide('" +tags[i].getAttribute("id") +"')";
		}else if (tags[i].getAttribute("id").search(/(.*)InfoPop/) != -1) {
			tags[i].onmouseover = "infoPopUp('guaranteeInfoPop')";
			tags[i].onmouseout = "xHide('" +tags[i].getAttribute("id") +"Panel')";
            xGetElementById("footerInfo").innerHTML += "<p>infoPopUp "+tags[i].getAttribute("id")+"</p>";
		}
	}
}



var currentTip=null;
var nextTip=null;
var showTimer=null;
var hideTimer=null;
var triggerEvent;
var tipParam;
var nextParam;

function startTipTimer(id, e, p){
debug("check show timer");
    if (id==nextTip && showTimer!=null)
        return;
    if (currentTip==id && hideTimer!=null){
        clearTimeout(hideTimer);
        hideTimer=null;
        return;
    }
    if (showTimer!=null && nextTip!=id)
        clearTimeout(showTimer);
    nextTip=id;
    nextParam = p;
    triggerEvent = new xEvent(e);
    showTimer = setTimeout("initTip()",500);
    document.onmousemove = mouseMoved;
    if (document.captureEvents) document.captureEvents(Event.MOVE);
debug("start show timer");
}
function initTip(){
debug("show tip "+nextTip+" @ "+(triggerEvent.pageX - xWidth(nextTip)-3)+","+(triggerEvent.pageY+3));
    xGetElementById(nextTip).style.display="block";
    if (currentTip){
        xHide(currentTip);
        if (hideTimer){
            clearTimeout(hideTimer);
            hideTimer=null;
        }
        currentTip=null;
    }
    xMoveTo(nextTip,-1000,0);
    xShow(nextTip);
    tipParam=nextParam;
    var tx = triggerEvent.pageX - xWidth(nextTip)-3;
    var ty = triggerEvent.pageY+3;
    if (tipParam){
        if (tipParam.indexOf("quote")>=0){
            tx += 40;
            ty -= xHeight(nextTip)+6;
        }
        if (tipParam.indexOf("right")>=0){
            tx = triggerEvent.pageX + 5;
        }
    }
    xMoveTo(nextTip, tx, ty);
    currentTip = nextTip;
    nextTip=null;
    clearTimeout(showTimer);
    showTimer = null;
}
function showTip(id){
    if (currentTip != null && currentTip != id){
        xHide(currentTip);
    }
    xGetElementById(id).style.display="block";
    var tx = triggerEvent.pageX - xWidth(id)-3;
    var ty = triggerEvent.pageY+3;
    xMoveTo(id,-1000,0);
    xShow(id);
    if (tipParam){
        if (tipParam.indexOf("quote")>=0){
            tx += 40;
            ty -= xHeight(id)+6;
        }
        if (tipParam.indexOf("right")>=0){
            tx = triggerEvent.pageX + 5;
        }
    }
    xMoveTo(id, tx, ty);
    currentTip = id;
    xGetElementById(id).onmouseover=cancelHideTimer;
    xGetElementById(id).onmouseout=startHideTimer;
}
function cancelHideTimer(id){
window.status="cancel hide timer";
    clearTimeout(hideTimer);
    hideTimer=null;
}
function startHideTimer(){
    if (showTimer){
        clearTimeout(showTimer);
        showTimer = null;
        nextTip=null;
    }
    if (currentTip==null)
        return;
    if (hideTimer!=null)
        return;
    hideTimer = setTimeout("hideTip()",150);
}
function hideTip(){
    xHide(currentTip);
    clearTimeout(hideTimer);
    hideTimer=null;
/*
    if (showTimer){
        clearTimeout(showTimer);
        showTimer=null;
        nextTip=null;
    }
*/
    currentTip=null;
}
function mouseMoved(e){
    if (currentTip==null && nextTip==null)
        return;
    triggerEvent = new xEvent(e);
    if (currentTip==null)
        return;
//    window.status = "M @ "+triggerEvent.pageX+", "+triggerEvent.pageY;
    showTip(currentTip);
}
