/*
WebLex.js
Ultralingua WebLex

Created by Ben Kazez on 8/15/08.
Copyright 2008 Ultralingua. All rights reserved.
*/

// Working around issues with getting this dynamically.
var WLPopUpWidth = 325;
var WLSrcLang = '';
var WLDstLang = '';
var WLClient = '';

/* Shortcut to save bandwidth. */
function WLEL(id)
{
	return document.getElementById(id);
}

function _W(e)
{
	if (!e) var e = window.event;	

    searchStr = WLGetEventTarget(e).innerHTML;
	WLPopUpShow(e, searchStr);
}

function WLPopUpShow(e, searchStr)
{
    if (WLEL("WLPopUp").style.display == "block"){
        var content = "<html><body>Please click the red exit button above to close the WebLex popup window before starting another search.</body></html>" 
        var iframe = WLEL("WLPopUpContent");
        var doc = iframe.document;
        if(iframe.contentDocument)
            doc = iframe.contentDocument; // For NS6
        else if(iframe.contentWindow)
            doc = iframe.contentWindow.document; // For IE5.5 and IE6
        // Put the content in the iframe
        doc.open();
        doc.writeln(content);
        doc.close();
        return true;
    }

    mousePos = WLGetMousePosition(e);

	// Calculate popup position: horizontally centered, vertically lowered.
	var x, y;
	x = Math.max(mousePos.x - (WLPopUpWidth / 2), 10);
	y = mousePos.y + 20;
	WLEL("WLPopUp").style.left = x + "px";
	WLEL("WLPopUp").style.top = y + "px";

    var url = 'http://'+window.location.hostname+'/onlinedictionary/weblex/WLController.py'
    //window.location.protocol + "//" + window.location.host + window.location.pathname;
    url += "?client=" + encodeURIComponent(WLGetURLParam("client"))
         + "&src=" + encodeURIComponent(WLGetURLParam("src"))
         + "&dst=" + encodeURIComponent(WLGetURLParam("dst"))
         + "&q=" + encodeURIComponent(searchStr);


	WLEL("WLPopUpContent").src = url;
	if (!is_ie6()){
        WLEL("WLOverlay").style.display = "block";
    }
	WLEL("WLPopUp").style.display = "block";
}
function is_ie6()
{
    var agt = navigator.userAgent.toLowerCase();
    if (agt.indexOf("msie 6.") != -1){
        return true;
    }
    return false;
}
function getPlatform()
{
    var platform = navigator.platform.toLowerCase();
    if (platform.indexOf("win") != -1){
        return 'win';
    }
    else if (platform.indexOf("mac") != -1){
        return 'mac';
    }
    else {    
        return null;
    }
}
function setPopupClosePosition(platform)
{
    if (platform != null) {
        var popup = document.getElementById("WLPopUp");
        if (platform == 'mac'){
            WLEL("WLPopUpCloseButton").style.cssFloat = "left";
            if (WLEL("WLAccountFrame")) {
                WLEL("WLAccountCloseButton").style.cssFloat = "left";
            }
        } 
    }
}
function WLPopUpHide(elem)
{
	WLEL("WLOverlay").style.display = "none";
    WLEL("WLPopUp").style.display = "none";
    WLEL("WLPopUpContent").src = "http://"+window.location.hostname+"/onlinedictionary/weblex/templates/default/loader.html";
}

// Source: http://www.quirksmode.org/js/events_properties.html
function WLGetMousePosition(e)
{
	var posx = 0;
	var posy = 0;
    var cursory = e.screenY;
    var screeny = screen.height;
    var popupheight = 195;
    var offset = 20;
    var padding = 50;

	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	if (cursory + popupheight + offset + padding > screeny) {
        posy = posy - popupheight - (2*offset);
    }

	return {'x': posx, 'y': posy};
}

// Source: http://www.quirksmode.org/js/events_properties.html
function WLGetEventTarget(e)
{
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
    return targ;
}

// Source: http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
function WLGetURLParam(strParamName){
  var strReturn = "";
    if (strParamName == "src")
    {
        if (WLSrcLang != "")
        {
            return WLSrcLang;
        }
    }
    if (strParamName == "dst")
    {
        if (WLDstLang != "")
        {
            return WLDstLang;
        }
    }
    if (strParamName == "client")
    {
        if (WLClient != "")
            return WLClient;
    }    
    var strHref = window.location.href;
    if ( strHref.indexOf("?") > -1 ){
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
            if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    if (strReturn == "" && strParamName == "client"){
        var client_pattern = /http:\/\/.+\.(.+)\.(?:com|edu|gov)/
        strReturn = strHref.match(client_pattern)[1]
        if (strReturn == null){
            strReturn = ""
        }
    }
    return unescape(strReturn);
}

function WLSetGlobals(caller)
{
    var langs = caller.value.split(",");
    var loc = langs[0];
    var oldsrc = WLSrcLang;
    var olddst = WLDstLang;
    WLSrcLang = langs[1];
    WLDstLang = langs[2];
    set_WLCookie("WebLex-SrcLang", WLSrcLang);
    set_WLCookie("WebLex-DstLang", WLDstLang);
    if (loc == "header")
    {
        var id = "popup,"+langs[1]+","+langs[2];
        WLEL(id).selected = true;
    }
    else
    {
        var id = "header,"+langs[1]+","+langs[2];
        WLEL(id).selected = true;
    }
    var PopUp = WLEL("WLPopUp");
    if (PopUp.style.display != "none")
    {
        WLRefreshPopUp(langs[1], langs[2], oldsrc, olddst);
    }

}
function WLRefreshPopUp(srcLang, dstLang, oldsrc, olddst)
{
    var popupContent = WLEL("WLPopUpContent");
    var url = popupContent.src;
    url = url.replace("src="+oldsrc, "src="+srcLang);
    url = url.replace("dst="+olddst, "dst="+dstLang);
    popupContent.src = url;
}
function set_WLCookie(name, value)
{
    var exp = new Date();
    exp.setTime(exp.getTime() + (1000*60*60*24*14));

    document.cookie = name + "=" + escape(value) + "; path=/; expires=" + exp.toGMTString();
}
function WLSetGlobalsFromURL()
{
    WLSrcLang = WLGetURLParam('src');
    WLDstLang = WLGetURLParam('dst');
    var id = "";
    if (WLSrcLang != "" && WLDstLang != ""){
        id = WLSrcLang+','+WLDstLang;
    }
    else {
        id = 'english,spanish';
        WLSrcLang = 'english';
        WLDstLang = 'spanish';
    }
    WLEL("header,"+id).selected = true;
    WLEL("popup,"+id).selected = true;
}

