﻿// JScript File
var wloaded = false
window.onresize = function() {
    setFooter();
    if (wloaded) {
        //   window.location.reload();
    }
}


window.onload = function() {

    setFooter();
    CallFirstTip();
}

var tipTarget;
var tipDir;
var operation;
var posX;
var posY;
var diObj;
var isOpen;

function LoadPageTips() {
    if (CheckForTips() && GetCookie('dontShowCookies') == null) {
        var title = GetTipTitle();
        var content = GetTipContent(GetCookie('seenTips'));
        document.getElementById("tpTitle").innerHTML = title;
        document.getElementById("tpContent").innerHTML = content;
    }

}
function CallFirstTip() {
    if (CheckForTips() && GetCookie('dontShowCookies') == null) {
        tipTarget = GetTipTargetID(GetCookie('seenTips'));
        tipDir = GetTipDirection(GetCookie('seenTips'));
        operation = GetTipOperation(GetCookie('seenTips'));
        posX = GetTipPositionX(GetCookie('seenTips'));
        posY = GetTipPositionY(GetCookie('seenTips'));
        showTip(tipTarget, true)
    }
}

function hideGrid() {
    showElement('show_results');
    // document.getElementById('gridMin').style.position = "relative";
    document.getElementById('gridMin').style.top = '-1000px';
    hideElement('hide_results');
}
function showGrid() {
    try { pageTracker._trackEvent('Content Page', 'show grid', document.getElementById("itemID").value) } catch (e) { }
    showElement('hide_results');
    document.getElementById('gridMin').style.position = "absolute";
    // document.getElementById('gridMin').style.top = '0px';
    for (var i = 1; i < 12; i++) {
        nextheight = (11 - i) * (120 / 11)
        nexty = -4 / (12 - i) - nextheight + 28

        //document.getElementById('gridMin').style.top="20px"
        document.getElementById('gridMin').style.left = "0px"
        moveAction = "document.getElementById('gridMin').style.top='" + nexty + "px';document.getElementById('gridMin').style.clip='rect(" + nextheight + "px 840px 120px 0px)'"

        window.setTimeout(moveAction, i * 25)
    }
    hideElement('show_results');
}

function openElaborationPoint(pointID) {
    if (getFlashObject("subject_interface")) {
        try { pageTracker._trackEvent('Content Page', 'open elaboration point', document.getElementById("itemID").value + "/" + pointID); } catch (e) { }
        getFlashObject("subject_interface").selectPoint(String(pointID));
    }
}
function getFlashObject(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? document.getElementById(movieName) : document[movieName];

}
function setFontSize(size, lineHeight) {
    try { pageTracker._trackEvent('Content Page', 'set font size', document.getElementById("itemID").value) } catch (e) { }
    document.getElementById("subject_text_box").style.fontSize = size
    document.getElementById("subject_text_box").style.lineHeight = lineHeight
}
function hideElement(element_id) {
    if (document.getElementById && document.getElementById(element_id) && document.getElementById(element_id).style) {
        document.getElementById(element_id).style.visibility = "hidden";
        document.getElementById(element_id).style.display = "none";
    }
}
function showElement(element_id) {
    if (document.getElementById && document.getElementById(element_id) && document.getElementById(element_id).style) {
        document.getElementById(element_id).style.visibility = "visible";
        document.getElementById(element_id).style.display = "";
    }
}
function showGraph(graphid) {
    try { pageTracker._trackEvent('Content Page', 'show graph', document.getElementById("itemID").value + "/" + graphid); } catch (e) { }
    showElement("graph" + graphid);
    var el = document.getElementById("subject_text_box")
    try {
        scrollElement(el, document.getElementById("graph" + graphid).offsetTop - 40)
    } catch (e) {
    }
    hideElement("graphlink" + graphid);
    void (0);
}
function hideGraph(graphid) {
    try { pageTracker._trackEvent('Content Page', 'hide graph', document.getElementById("itemID").value + "/" + graphid) } catch (e) { }
    hideElement("graph" + graphid);
    showElement("graphlink" + graphid);
    void (0);
}
function toggleGraph(graphid) {
    if (document.getElementById(element_id).style.display == "") {
        hideGraph(graphid)
    } else {
        showGraph(graphid)
    }
}
function gotoHeader(headerid) {
    var el = document.getElementById("subject_text_box")
     scrollElement(el,0)
    scrollElement(el, document.getElementById(headerid).offsetTop - 10)

}
function scrollElement(element, scroll) {
    var ns4 = (document.layers) ? true : false;
    if (ns4) {
        curScroll = element.pageYOffset;
    } else {
        curScroll = (element.scrollTop);
    }
    document.getElementById(element.id).scrollTop = scroll
    /*if (scroll>curScroll){
    for (var i=curScroll;i<=scroll;i=i+(scroll-curScroll)/30){
    scrollAction=i
    scrollAction="document.getElementById('" + element.id + "').scrollTop =" +scrollAction 
    window.setTimeout(scrollAction,i*1)
    }
    }else{
    for (var i=curScroll;i>=scroll;i=i-(curScroll-scroll)/30){
    scrollAction=i
    scrollAction="document.getElementById('" + element.id + "').scrollTop =" +scrollAction 
    window.setTimeout(scrollAction,(30-i)*1)
    }
    }*/
}
var elabPointXML
function showFullscreen(pointXML, tab, imageIndex) {
    // window.scrollTo(0,0)
    try { pageTracker._trackEvent('Content Page', 'show fullscreen', document.getElementById("itemID").value + "/elaboration_point_images_mc/" + imageIndex) } catch (e) { }
    elabPointXML = pointXML
    document.getElementById("fullscreen").style.top = '0px';

    document.getElementById("fullscreen").style.height = getWindowHeight() + "px";
    // scaleTo("fullscreen",100,100,0,100,"%",500) 
    //getFlashObject("elaborationPoint").setElaboration(elabPointXML);
    // alert(getFlashObject('elaborationPoint').setFullScreenEP);
    if (getFlashObject('elaborationPoint').setFullScreenEP == undefined) {
        setTimeout("alert('notfound')", 1000)
    } else {
        getFlashObject('elaborationPoint').setFullScreenEP(elabPointXML, tab, imageIndex);
    }
}
function getElaboration() {

    //getFlashObject("elaborationPoint").setElaboration(elabPointXML);
}
function hideFullscreen() {
    document.getElementById("fullscreen").style.top = '-2000px'
}
function scaleTo(objName, startW, endW, startH, endH, unit, time) {
    var fps = 10
    var eventGap = 1000 / 25
    var obj = document.getElementById(objName);
    obj.style.width = String(startW) + unit;
    obj.style.height = String(startH) + unit;
    var t = 0
    var targetW = startW
    var targetH = startH
    var scaleGapW = (endW - startW) / (eventGap)
    var scaleGapH = (endH - startH) / (eventGap)
    for (var i = 1; i <= eventGap; i++) {
        t = t + time / eventGap
        targetW += scaleGapW
        targetH += scaleGapH

        var action = "document.getElementById('" + objName + "').style.width='" + targetW + unit + "'";
        action += ";document.getElementById('" + objName + "').style.height='" + targetH + unit + "'";
        window.setTimeout(action, t);
    }
    window.setTimeout("showElement(getFlashObject('elaborationPoint').id);getElaboration();", time)
}
function getWindowHeight() {
    var windowHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}
function setFooter() {

    if (document.getElementById) {
    if (!document.getElementById('footer')) return
        var windowHeight = getWindowHeight();

        if (windowHeight > 0) {
            var contentHeight = document.getElementById('content').offsetHeight;

            var footerElement = document.getElementById('footer');
            var footerHeight = footerElement.offsetHeight;
            if (windowHeight - (contentHeight + footerHeight) >= 0) {

                footerElement.style.position = 'relative';
                footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
            }
            else {

                footerElement.style.position = 'relative';

            }
        }
    }
}

function checkEmailValid() {
    if (!addCommentFrame) {
        addCommentFrame = document.getElementById('addCommentFrame')
    }
    var testMail = new String(addCommentFrame.document.getElementById('writerEmailBox').value)
    testMail = testMail.toLowerCase()
    var regE = new RegExp("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$")
    if (testMail.search(regE) != -1) {
        addCommentFrame.document.getElementById('updateMe').disabled = false
        addCommentFrame.document.getElementById('showMe').disabled = false
    } else {
        addCommentFrame.document.getElementById('updateMe').disabled = true
        addCommentFrame.document.getElementById('showMe').disabled = true
    }
}

function reloadPage() {

    // var btnID = document.getElementById("btnID").value;
    //document.getElementById(btnID).click();
    //fetchResultPage(currentPageField, submitBtn + i.ToString() + "); "" > "
    // window.location.reload();
    hideComment()
    var rfrshbtnid = document.getElementById("refreshBtn").value
    document.getElementById(rfrshbtnid).click()
    // window.location = window.location;

}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    alert('Query Variable ' + variable + ' not found');
}

function gotoForum() {
    window.location = "piForum.aspx"
}

function hideComment() {

    hideElement('addRatingFrame')
}

function printPage() {
    try { pageTracker._trackEvent('Content Page', 'open elaboration point', document.getElementById("itemID").value); } catch (e) { }
    document.getElementById("print_text").innerHTML = "<h1>" + document.getElementById("subjectTitle").innerHTML + "</h1>"
    if (document.getElementById("authors") != null) {
        document.getElementById("print_text").innerHTML += "<span style='font-size:12px;text-align:center;display:block;margin:auto;'><strong>מחברי הערך:</strong> " + document.getElementById("authors").innerHTML + "</span><br/><br/>";
    }
    var pageHtml = new String()
    var pageHtml = document.getElementById("subject_text_box").innerHTML;
    pageHtml = regXreplace(pageHtml, "none", "block");
    pageHtml = regXreplace(pageHtml, "hidden", "visible");
    pageHtml = regXreplace(pageHtml, "id=[\"\'].*?[\"\']", "");
    pageHtml = regXreplace(pageHtml, "id=graph", "id=fraph");
    document.getElementById("print_text").innerHTML += pageHtml;
    window.print();

}
function regXreplace(theString, searchString, replaceString) {
    var re = new RegExp(searchString, "g");
    re.ignoreCase = true;
    var mystring = new String(theString)
    results = mystring.replace(re, replaceString)
    return results
}

function ElaborationTip() {
    var tipElement = document.getElementById(tipTarget);
    if (tipElement != null) {
        if (document.getElementById("oeltip")) {

            posX = document.getElementById("oeltip").offsetLeft
            posY = document.getElementById("oeltip").offsetTop
            document.getElementById("oeltip").style.left = posX + "px"
            document.getElementById("oeltip").style.top = posY + "px"
            //alert(posX + " " + document.getElementById("oeltip").style.left)
            //var objDiv = document.getElementById("subject_text_box");
            var objDiv = document.getElementById(tipTarget);
            scrollElement(objDiv, posY - 300)

            // tipDir: 0=top-left 1=top-right 2=bottom-right 3=bottom-left
            //set x position

            //posX=0
            // posX = Math.max(posX, 218)
            // posX = Math.min(posX, 400)
            if (posY < 200) {
                if (posX > 200) {
                    tipDir = 2
                } else {
                    tipDir = 3
                }
            } else {
                if (posX > 200) {
                    tipDir = 0
                } else {
                    tipDir = 1
                }
            }

            return true
        }
    }
    return false;
}

function GraphTip() {
    var tmpArr = GetGraphLinkIDs();

    if (tmpArr != null) {
        var tipElement = document.getElementById(tmpArr);
        if (tipElement != null) {
            if (getFlashObject("subject_interface")) {
                try {
                    getFlashObject("subject_interface").openForm("graphs");
                }
                catch (Error) {
                    setTimeout("GetGraphLinkIDs()", 1500)
                }
            }
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

function ThumbsTip() {
    var tipElement = document.getElementById(tipTarget);
    if (tipElement != null) {
        var elab = document.getElementById("elabtip")
        if (elab) {
            try {
                elab.getAttribute('href')
                elab = elab.getAttribute('href');
                elab = elab.replace("javascript:", "")
                elab = elab.replace(";void(0);", "")
                eval(elab)
            }
            catch (Error) {
                setTimeout("ThumbsTip()", 1500);
            }
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

function ContentTableTip() {

    var tipElement = document.getElementById(tipTarget);

    if (tipElement != null) {

        if (getFlashObject("subject_interface")) {
            try {
                getFlashObject("subject_interface").openForm("toc");
            }
            catch (Error) {
                setTimeout('ContentTableTip()', 2000);
            }
        }
        else {
            
            setTimeout('ContentTableTip()', 2000);
        }
        return true;
    }
    else {
        return false;
    }
}

function SearchResultsTip() {
    var tipElement = document.getElementById("show_results");
    if (tipElement != null) {
        showGrid();
        return true;
    }
    else {
        return false;
    }
}

var curTipTarget;
function showTip(tipTarget, flag) {
    try {
        var bool = eval(operation)
    }
    catch (Error) {
        nextTip();
        return;
    }
    if (flag == true) {
        if (bool == false) {
            nextTip();
            return;
        }
    }
    if (!document.getElementById(tipTarget)) {
        nextTip();
        return;
    }
    isOpen = true;
    dir = "rtl";
    tipName = "_name" + tipTarget;
    var tipHTML = document.getElementById("tipRef").innerHTML;
    var randomnumber = Math.floor(Math.random() * 987)
    var tipID = tipTarget + "_tip" + randomnumber.toString();
    tipHTML = regXreplace(tipHTML, "tipID", tipID);
    //document.getElementById(tipTarget).parentNode.innerHTML += tipHTML;
    // tipDir: 0=top-left 1=top-right 2=bottom-right 3=bottom-left
    var tipObjTarget = document.getElementById(tipTarget);
    var tipNode = document.createElement('div');
    tipNode.innerHTML = tipHTML

    curTipTarget = tipNode;
    document.getElementById(tipTarget).innerHTML += ""
    document.getElementById(tipTarget).appendChild(tipNode)



    //tipNode.style.top=document.getElementById(tipTarget).offsetTop + "px"
    //tipNode.style.left=document.getElementById(tipTarget).offsetLeft + "px"
    posX = parseInt(posX)
    posY = parseInt(posY)

    tipNode.style.top = posY + "px"
    tipNode.style.left = posX + "px"
    tipNode.style.position = "absolute"

    var jg = new jsGraphics(tipID);
    var tipObj = document.getElementById(tipID);

    var arrowArrayX
    var arrowArrayY
    var ntipDir = parseInt(tipDir)
    //alert(ntipDir)
    switch (ntipDir) {
        case 0:
            var tipObjY = -tipObj.offsetHeight - 22;
            var tipObjX = -tipObj.offsetWidth + 20; //tipObjTarget.offsetLeft;
            tipObj.style.top = tipObjY + "px";
            tipObj.style.left = tipObjX + "px";
            var tipObjHeight = tipObj.offsetHeight;
            arrowArrayX = new Array(146, 150, 124)
            arrowArrayY = new Array(tipObjHeight - 2, tipObjHeight + 22, tipObjHeight - 2)
            break;
        case 1:
            var tipObjY = -tipObj.offsetHeight - 22;
            var tipObjX = 20; //tipObjTarget.offsetLeft;
            tipObj.style.top = tipObjY + "px";
            tipObj.style.left = tipObjX + "px";
            var tipObjHeight = tipObj.offsetHeight;
            arrowArrayX = new Array(26, 22, 48)
            arrowArrayY = new Array(tipObjHeight - 2, tipObjHeight + 22, tipObjHeight - 2)
            break;
        case 2:
            var tipObjY = 50;
            var tipObjX = -tipObj.offsetWidth + 20; //tipObjTarget.offsetLeft;
            tipObj.style.top = tipObjY + "px";
            tipObj.style.left = tipObjX + "px";
            var tipObjHeight = tipObj.offsetHeight;
            arrowArrayX = new Array(146, 150, 124)
            arrowArrayY = new Array(-1, -23, -1)
            break;
        case 3:
            var tipObjY = 50;
            var tipObjX = 20; //tipObjTarget.offsetLeft;
            tipObj.style.top = tipObjY + "px";
            tipObj.style.left = tipObjX + "px";
            var tipObjHeight = tipObj.offsetHeight;
            arrowArrayX = new Array(26, 22, 48)
            arrowArrayY = new Array(-1, -23, -1)
            break;
    }
    jg.setColor("#FBF0B2");
    jg.fillPolygon(arrowArrayX, arrowArrayY);

    jg.paint();

    jg.setColor("#755F48");
    jg.drawPolyline(arrowArrayX, arrowArrayY);
    jg.paint();
}

function nextTip() {
    var lastTip = parseInt(GetCookie('seenTips'))
    if (lastTip < diObj.length - 1) {
        lastTip = parseInt(GetCookie('seenTips'))
        lastTip = lastTip + 1
        setCookie('seenTips', lastTip, 5000, document);
    }
    else {
        lastTip = 0
        setCookie('seenTips', lastTip, 5000, document);
    }
    LoadPageTips()
    CallFirstTip();

    //tipTarget + "_tip"
    //alert(diObj[lastTip].title + "\r\n" + diObj[lastTip].content + "\r\n" + diObj[lastTip].operation)
    //document.getElementById("tpTitle").innerText = GetTipTitle()
    //document.getElementById("tpContent").innerText = GetTipContent(GetCookie('seenTips'));

}

function GetTipTitle() {
    var lastTip
    if (GetCookie('seenTips') != null) {
        if (parseInt(GetCookie('seenTips')) < diObj.length) {
            lastTip = parseInt(GetCookie('seenTips'))
            //lastTip = lastTip+1
            //setCookie('seenTips',lastTip,5000, document);
        }
        else {
            lastTip = 0
            setCookie('seenTips', lastTip, 5000, document);
        }
    }
    else {
        lastTip = 0
        setCookie('seenTips', lastTip, 5000, document);
    }
    var title = diObj[lastTip].title;
    return title
}

function GetTipContent(index) {
    return diObj[index].content
}
function GetTipDirection(index) {
    return diObj[index].tipDirection
}
function GetTipPositionX(index) {
    var totalX = diObj[index].posX

    return totalX
}
function GetTipPositionY(index) {
    var totalY = diObj[index].posY

    return totalY
}
function GetTipTargetID(index) {
    var targetID = diObj[index].targetID;
    return targetID;
}
function GetTipOperation(index) {
    var tipOperation = diObj[index].operation;
    return tipOperation;
}

function closeTip(tipID) {
    isOpen = false;
    var lastTip = parseInt(GetCookie('seenTips'))
    lastTip = lastTip + 1
    setCookie('seenTips', lastTip, 5000, document);

    if (document.getElementById("dontShow_" + tipID).checked) {
        setCookie("dontShowCookies", "1", 5000, document);
    }
    curTipTarget.parentNode.removeChild(curTipTarget);
}

function GetCookie(sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }

    // a cookie with the requested name does not exist
    return null;
}

function setCookie(NameOfCookie, value, expiredays, doc) {
    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    doc.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function getElementsByPrefix(inPrefix, inRoot) {
    var elem_array = new Array;

    if (typeof inRoot.firstChild != 'undefined') {
        var elem = inRoot.firstChild;
        while (elem != null) {
            if (typeof elem.firstChild != 'undefined') {
                elem_array = elem_array.concat(getElementsByPrefix(inPrefix, elem));
            }
            if (typeof elem.id != 'undefined') {
                var reg = new RegExp('^' + inPrefix + '.*');
                if (elem.id.match(reg)) {
                    elem_array.push(elem);
                }
            }
            elem = elem.nextSibling;
        }
    }
    return elem_array;
}

function CheckElaboration() {
    var elabLink = document.getElementById('elabTip');
    if (elabLink)
        return true
    else
        return false;
}

function GetGraphLinkIDs() {
    var elemArr = getElementsByPrefix('graphlink', document.documentElement);
    if (elemArr.length > 0) {
        return elemArr[0].id;
    }
    else {
        return null
    }
}

function CheckForTips() {
    if (diObj != null) {
        if (diObj.length > 0) {
            return true;
        }
        else {
            return false;
        }
    }
}


function ShowNextTip() {
    LoadPageTips();
    CallFirstTip();
}

function getAbsolutePos(obj) {
    totalX = obj.offsetLeft
    totalY = obj.offsetTop
    while (obj.tagName != "BODY") {
        obj = obj.offsetParent
        totalX += obj.offsetLeft
        totalY += obj.offsetTop

    }
    return ({ x: totalX, y: totalY })
}

function CallTips() {
    if (isOpen) {
        return;
    }
    else {
        if (GetCookie('dontShowCookies') != null) {
            document.cookie = "dontShowCookies=GONEcbEndCookie; expires=Monday, 19-Aug-1996 05:00:00 GMT";
        }
        setCookie('seenTips', 0, 5000, document);
        LoadPageTips();
        CallFirstTip();
    }
}

// focus field handler
var nav = (window.Event && window.captureEvents) ? true : false;

if (nav) {
    window.captureEvents(Event.KEYDOWN);
    window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
    document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) {
    if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false; }
    return true;
}

function MicrosoftEventHandler_KeyDown() {
    if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')
       
        return false;
    return true;
}

function searchFocus(field) {
    var initValue = document.getElementById("enterSearch").value
    if (field.value == initValue) {
        field.value = ""

    }
}

function searchLostFocus(field) {
    var initValue = document.getElementById("enterSearch").value
    if (field.value == "") {
        field.value = initValue

    }
}
function handleKeyPress(e) {
    var key = e.keyCode || e.which;
    if (key == 13) {
       
       // document.getElementById(document.getElementById("btnID").value).click();
    }
}