﻿toggleNews = function (prefix, instrumentId)
{
    var openBoxIdThatIsBeingHidden;

    // Hide any open news boxes with the same name
    $("div[id^=" + prefix + "]").each(function ()
    {
        var $this = $(this);
        if ($this.is(":visible")) {
            openBoxIdThatIsBeingHidden = this.id;
            $this.fadeOut();
        }
    });

    $("div[id^=ptfLine_]").each(function ()
    {
        var $this = $(this);
        if ($this.is(":visible")) {
            openBoxIdThatIsBeingHidden = this.id;
            $this.fadeOut();
        }
    });

    $("div[id^=ptfLineShort_]").each(function ()
    {
        var $this = $(this);
        if ($this.is(":visible")) {
            openBoxIdThatIsBeingHidden = this.id;
            $this.fadeOut();
        }
    });

    var id = prefix + instrumentId;
    if (id != openBoxIdThatIsBeingHidden) { // if news icon is clicked again to close box don't open it again...
        var $newsDiv = $('#' + id);

        if (prefix == 'ptfWatchlistLine_') {
            $newsDiv.css("background-color", "black").css("color", "white").css("left", ("-" + $newsDiv.width()).toString() + "px");
        }
        else {
            $newsDiv.css("left", ($newsDiv.width() - 10).toString() + "px").css("top", ($newsDiv.height() -50).toString() + "px").css("background-color", "black").css("color", "white");
        }

        EI.DelayLoad.loadScriptFile("/JS/jQuery/Plugins/jquery-corner/jquery.corner.js", function ()
        {
            $newsDiv.fadeIn().corner();
        });
    }
}

function Point(x, y)
{
    this.x = x;
    this.y = y;
}

function getStyle(elm) 
{
    if (elm.currentStyle) return elm.currentStyle;
    if (window.getComputedStyle) return window.getComputedStyle(elm, null);
    return elm.style; // not able to get actual style - use inline style instead
}
        
function findPos(obj) 
{
    //if (!obj.offsetParent || (getStyle(obj).position == "relative")) return new Point(0, 0);
    var p = findPos(obj.offsetParent);
    return new Point(obj.offsetLeft + p.x, obj.offsetTop + p.y);
}
        
        
/* ptfLinePop, ptfTimeout and ptfLineUnpop is used by the portfolio/*.aspx pages for the news popup-boxes and edit-boxes */
function ptfLinePop(elmid, sender, poprightside) {
    
    if (poprightside == undefined) poprightside = false;

    var elm = document.getElementById(elmid);
    elm.style.visibility = "visible";
    
    if (sender)
    {   
        elm.style.position = "absolute";
        var pos = findPos(sender);
        if (poprightside) 
        {
            elm.style.left = (pos.x) + "px";    
        }
        else 
        {
            elm.style.left = (pos.x - elm.offsetWidth + sender.offsetWidth) + "px";
        }
        elm.style.top = pos.y + "px";
    } 
}

var ptfTimeOut = null;

function ptfLineUnpop(elmid, evt, fromclick) 
{
    var elm = document.getElementById(elmid);
    var evtElm = evt.toElement ? evt.toElement : evt.relatedTarget;

    if (fromclick) 
    {
        elm.style.visibility = "hidden";
    }
    else if (evtElm != undefined && elm != undefined && !elm.contains(evtElm)) 
    {
        ptfTimeOut = setTimeout("if (document.getElementById('" + elm.id + "')) document.getElementById('" + elm.id + "').style.visibility='hidden'", 0);
        elm.onmouseover = function() { if (ptfTimeOut != null) { clearTimeout(ptfTimeOut); ptfTimeOut = null; } }
    }
    
}
