// hostedModeJSPProcessingHack();

var mediaUploadStatuses = new Array();
window.mouseCoords = {posX: 0, posY: 0};

//function hostedModeJSPProcessingHack() {
//    // this is hack for JSP processing in hosted mode - we should reload page
//    var location = document.location.href;
//    var isShellMode = location.toLowerCase().indexOf("/com.panraven") != -1;
//    if(isShellMode && !getSessionCookie("JSPOK")) {
//        // Simple reload does not work for hosted mode for some reason
//        document.write('<META HTTP-EQUIV="refresh" CONTENT="0; URL=' + location + '">');
//    }
//}

function gwtUploadCallback(mediaId, mediaStatus) {
    mediaUploadStatuses[mediaId] = mediaStatus;
}

function getMediaStatus(mediaId) {
    return parseInt(mediaUploadStatuses[mediaId]);
}

/**
 * Extracted form gwt nocache private code
 * Should be updated if we use newer version of gwt and it supports more browsers
 */
function isBrowserSupported() {
    var ua = navigator.userAgent.toLowerCase();
    var supported = ['opera', 'webkit', 'msie', 'gecko']; //not sure that any version of gecko is supported. It is unclear from gwt code
    for (var i=0; i<supported.length; i++) {
        if (ua.indexOf(supported[i]) != -1) {
            return true;
        }
    }
/*    if (ua.indexOf('gecko') != -1) {
        var result = /rv:([0-9]+)\.([0-9]+)/.exec(ua);
        if (result && result.length == 3) {
            if (makeVersion(result) >= 1008)
                return true;
        }
        return true;
    }*/
    return false;
}

function onPageLoad() {
    window.ua = new UserAgent();
    /*attach mouse tracker listener*/
    if (window.ua.isIE) {
        getDocumentBody().attachEvent("onmousemove", trackMouse);
    } else {
        getDocumentBody().addEventListener("mousemove", trackMouse, true);
    }
    if (!isBrowserSupported()) {
        window.open("/error/ErrorPage.epage?sp=SUNSUPPORTED_BROWSER", "_parent", "");
    }
}

var trackMouse = function(e) {
    if (!window.mouseCoords) {
        return;
    }
    e = window.event ? window.event : e;
    if (e.pageX && e.pageY) {
        window.mouseCoords.posX = e.pageX;
        window.mouseCoords.posY = e.pageY;
    } else {
        window.mouseCoords.posX = e.clientX;
        window.mouseCoords.posY = e.clientY;
        if (window.ua.isIE) {
            window.mouseCoords.posX += (document.documentElement && document.documentElement.scrollLeft) ?
                                       document.documentElement.scrollLeft : getDocumentBody().scrollLeft;
            window.mouseCoords.posY += (document.documentElement && document.documentElement.scrollTop) ?
                                       document.documentElement.scrollTop : getDocumentBody().scrollTop;
        }
    }
    window.mouseCoords.clientX = e.clientX;
    window.mouseCoords.clientY = e.clientY;
};

function UserAgent() {
    var agt = navigator.userAgent.toLowerCase();
    this.isIE = (!window.opera && agt.indexOf('msie') != -1);
    this.isIE7 = (agt.toLowerCase().indexOf('msie 7') != -1);
    this.isIE8 = (agt.toLowerCase().indexOf('msie 8') != -1);
    this.isIE6minus = (!window.opera && agt.indexOf('msie') != -1) && !this.isIE7 && !this.isIE8;
    this.isMac = agt.indexOf("mac") != -1;
    this.isLinux = agt.indexOf("x11") != -1;
    this.isSafari = this.isMac && agt.indexOf("safari") != -1;
    this.isFireFox = agt.indexOf("firefox") != -1;
    this.isFireFox2 = agt.indexOf("firefox/2") != -1;
    this.isOpera = agt.indexOf("opera") != -1;
};


document.isFlashPlayerInstalled = function() {
    var flashInstalled = false;
    var flashVersion = 0;
    if (navigator.plugins && navigator.plugins.length) {
        var x = navigator.plugins["Shockwave Flash"];
        if (x) {
            flashInstalled = true;
            if (x.description) {
                flashVersion = x.description.charAt(x.description.indexOf('.') - 1);
            }
        }
        if (navigator.plugins["Shockwave Flash 2.0"]) {
            flashInstalled = true;
            flashVersion = 2;
        }

    } else if (navigator.mimeTypes && navigator.mimeTypes.length) {
        var x = navigator.mimeTypes["application/x-shockwave-flash"];
        if (x && x.enabledPlugin) {
            flashInstalled = true;
        }
    } else {
        //detect flash in IE
        try {
            var x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
            flashInstalled = true;
            var version = x.GetVariable("$version");
            version = version.substring(version.indexOf("WIN ") + 4); //todo: do not forget to add checking for MAC

            version = version.substring(0, version.indexOf(','));
            flashVersion = parseInt(version);
        } catch (e) {
        }
    }
    return {flashInstalled : flashInstalled, flashVersion : flashVersion};
}

window.flashPlayerInstalled = document.isFlashPlayerInstalled();
window.isFlashInstalled = flashPlayerInstalled.flashInstalled && parseInt(flashPlayerInstalled.flashVersion) >= 9;

function open_faq_window(name) {
    window.open(name, "panraven_faq", "height=600, width=800, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no");
}

function getDocumentBody() {
    /*
        var els = document.getElementsByTagName("body");
        return (els.length > 0) ? els[0] : undefined;
    */
    return document.body;
}


function getStackTrace(func) {
    var st = "";
    if (func) {
       st += func.toString().substring(0, func.toString().indexOf('{')).replace("function ", '') + "\r\n";
       var addition = "Unable to get further stacktrace";
       try {
           addition = getStackTrace(func.caller);
       } catch (e) {}
       st += addition;
    }
    return st;
}

function getNativeStackTrace() {
    return getStackTrace(getNativeStackTrace).replace("getNativeStackTrace() \r\n", '');
}

function setSessionCookie(name, value) {
    value = escape(value);
    document.cookie = name + '=' + value + ';' + 'path=/';
}

function getSessionCookie(name) {
    var value = document.cookie;
    var begin = value.lastIndexOf(name+'=')
    if (begin == -1) {
        return null;
    }
    begin += name.length + 1;
    var end = value.indexOf(';', begin);
    if (end == -1) {
        end = value.length;
    }
    return unescape(value.substring(begin, end));
}

if (window.location != getSessionCookie('currPageURL')) {
    var wl = ""+window.location;
    if (wl.indexOf(";maximized") == -1 && wl.indexOf("_info.htm") == -1) {
        setSessionCookie('prevPageURL', getSessionCookie('currPageURL'))
        setSessionCookie('currPageURL', window.location);
    }
}
