﻿// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page 
// to access data from any server in the web, which is really useful. 
// Unfortunately, the data is returned in the form of a script. That script 
// can deliver the data, but it runs with the same authority as scripts on 
// the base page, so it is able to steal cookies or misuse the authorization 
// of the user with the server. A rogue script can do destructive things to 
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//
// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl;
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");

    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
};
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);
};
// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
};
//function addScript() {
    //var obj = new JSONscriptRequest('http://vp129.alertir.com/index.php?p=share&s=json&afw_callback=Stock'); //production
    //obj.buildScriptTag(); // Build the script tag      
    //obj.addScriptTag(); // Execute (add) the script tag
//}

function addScript() 
{
    $.ajax({
        type: "get",
        dataType: "json",
        url: '/pages/getStockInfo.ashx',
        async: false,
        success: function (ret) {
            if (ret != null) {
                Stock(ret);
            }
        }
    });

}

function MM_jumpMenu(targ, selObj, restore) { //v3.0 
    eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
    if (restore) selObj.selectedIndex = 0;
}

function Stock(data) {
    var text = '';
    var bAlert = false;
    if (data == null || data.data[0] == null) {
        if (bAlert) {
            alert('No stock info was found!');
        }
        else {
            //do nothing
        }
    }
    else {
        var d = data.data[0];
        if (bAlert) {
            text = 'Latest: ' + d.lastprice + '\n';
            text += '%: ' + d.diffpercent + '\n';
            text += '+/-: ' + d.diff + '\n';
            text += 'Buy: ' + d.buy + '\n';
            text += 'Sell: ' + d.sell + '\n';
            alert(text);
        }
        else {
            $("#banner_stockinfo #LatestPrice span").empty().append(d.lastprice);
            $("#banner_stockinfo #LatestDiffInPercent span").empty().append(d.diffpercent);
            $("#banner_stockinfo #LatestDiff span").empty().append(d.diff);
            $("#banner_stockinfo #BuyPrice span").empty().append(d.buy);
            $("#banner_stockinfo #SellPrice span").empty().append(d.sell);
        }
    }
}

$(document).ready(function () {
    if ($("#banner_stockinfo:visible").size() > 0) {
        addScript();
    }
});


/*
* runOnLoad.js: portable registration for onload event handlers.
* 
* This module defines a single runOnLoad() function for portably registering
* functions that can be safely invoked only when the document is fully loaded
* and the DOM is available.
*
* Functions registered with runOnLoad() will not be passed any arguments when
* invoked. They will not be invoked as a method of any meaningful object, and
* the this keyword should not be used.  Functions registered with runOnLoad()
* will be invoked in the order in which they were registered.  There is no
* way to deregister a function once it has been passed to runOnLoad().
*
* In old browsers that do not support addEventListener() or attachEvent(),
* this function relies on the DOM Level 0 window.onload property and will not
* work correctly when used in documents that set the onload attribute
* of their <body> or <frameset> tags.
*/
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function () {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for (var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch (e) { /* An exception in one function shouldn't stop the rest */ }
    }

    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;

runOnLoad(function () { //fix for FireFox; when <img-tags doesn't have width and height set the "$(document).ready(function(){" will run before the images are loaded	
    if ($("div.epieditmode:visible").size() > 0) {
        setTimeout('fixSecoHtml()', 1000);
    }
    else {
        fixSecoHtml();
    }
});

function fixSecoHtml() {
    $("#main_content img").each(function () {
        //alert($(this).parent("a").attr("class"))
        if (!$(this).parent("a").attr("class")) {
            $(this).parent("a").attr("class", "seco_clean");
        }
    });

    //-------------------------------------------------------------------------------------
    //fix main content - change class depending on if there is a left navigation
    //-------------------------------------------------------------------------------------
    if ($("#sub_navigation").size() < 1) {
        $("#main_content").attr("class", "seco_nonav");
    }

    if ($("#content_top #breadcrumb:visible").size() < 1) {
        $("#content_top #print").css("float", "right");
    }

    //-------------------------------------------------------------------------------------
    //fix content columns - change class depending on how many columns there is in content
    //-------------------------------------------------------------------------------------
    var iNrOfColumns = $("#content .seco_column:visible").size();
    var iCnt = 0;

    $("#content .seco_column:visible").each(function () {
        iCnt++;
        $(this).attr("id", "column_" + iNrOfColumns + "_" + iCnt);
    });

    if (iNrOfColumns < 1) {
        $("#content #ad_info").css("margin-bottom", "1px");
    }

    jQuery.fn.fixColumnHeight = function () {
        var iColumnMaxHeight = 0;
        if ($("#content .seco_column:visible").size() > 1) {	//more than one column
            $("#content .seco_column:visible").each(function () {
                if ($(this).height() > iColumnMaxHeight) {
                    iColumnMaxHeight = $(this).height();
                }
            });

            //fix content column heights (for vertical borders)
            if (iColumnMaxHeight > 0) {
                $("#content .seco_column").css("height", iColumnMaxHeight);
            }
        }
    }; //--------------------------------------------------------------------------------------
    //var isIE = (navigator.appVersion.indexOf("MSIE") > -1 ? true : false);
    //var isIELowerThan7 = false;
    //var iBrowserVersion = parseFloat(navigator.appVersion.split('MSIE')[1]);

    //fix sub menu height - for background to reach the footer
    jQuery.fn.fixSubNav = function () {
        if ($("#page_footer").offset() && $("#page_content").offset()) {
            $("#sub_navigation").css("height", $("#page_footer").offset().top - $("#page_content").offset().top);
        }
    }; //fix sub menu - take away first border top for sub menu
    $("#sub_navigation ul ul li:first").css("border-top", "none");

    //fix footer
    $("#page_footer #footer_items div:first").attr("class", "seco_first");
    $("#page_footer #footer_items div:last").attr("class", "seco_last");

    /* -------------- START: Info form  -------------------------- */
    $("div.seco_info:visible").each(function () {
        var iBoxWidth = $(this).width();

        if ($(this).children(".seco_content").children(".seco_item").size() > 0) {
            var iActualBoxItemWidth = iBoxWidth - parseInt($(this).children(".seco_content").children(".seco_item").css("padding-left").replace("px", "")) - parseInt($(this).children(".seco_content").children(".seco_item").css("padding-right").replace("px", ""));

            //take away last padding
            $(this).children(".seco_content").children(".seco_item").children("div:last-child").each(function () {
                $(this).css("padding-right", 0);
            });

            //get max columns and max row padding
            var iMaxCols = 0;
            var iMaxHorizontalRowPadding = 0;
            $(this).children(".seco_content").children(".seco_item").each(function () {
                if ($(this).children("div").size() > iMaxCols) {
                    iMaxCols = $(this).children("div").size();
                    var tmpRowPadding = 0;
                    $(this).children("div").each(function () {
                        tmpRowPadding += parseInt($(this).css("padding-left").replace("px", "")) + parseInt($(this).css("padding-right").replace("px", ""));
                    });
                    if (tmpRowPadding > iMaxHorizontalRowPadding) {
                        iMaxHorizontalRowPadding = tmpRowPadding;
                    }
                }
            });

            iActualBoxItemWidth -= iMaxHorizontalRowPadding;
            var iAverageColWidth = Math.floor(iActualBoxItemWidth / iMaxCols);

            //init
            var i = 0;
            var aColMaxWidth = new Array(iMaxCols);
            for (i = 0; i < aColMaxWidth.length; i++) {
                aColMaxWidth[i] = 0;
            }

            //get max width for each column
            $(this).children(".seco_content").children(".seco_item").each(function () {
                i = 0;
                $(this).children("div").each(function () {
                    if ($(this).width() > aColMaxWidth[i]) {
                        aColMaxWidth[i] = $(this).width();
                    }
                    i++;
                });
            });

            //calculatate column width
            var iCntOverAverage = 0;
            var iColWidthSpace = 0;
            var iTotMaxWidth = 0;
            for (i = 0; i < aColMaxWidth.length; i++) {
                iTotMaxWidth += aColMaxWidth[i];
                if (aColMaxWidth[i] > iAverageColWidth) {
                    iCntOverAverage++;
                }
                else {
                    iColWidthSpace += iAverageColWidth - aColMaxWidth[i];
                }
            }

            if (iCntOverAverage > 0 && iTotMaxWidth > iActualBoxItemWidth) {
                for (i = 0; i < aColMaxWidth.length - 1; i++) {
                    if (aColMaxWidth[i] > iAverageColWidth) {
                        aColMaxWidth[i] = iAverageColWidth + Math.floor(iColWidthSpace / iCntOverAverage);
                    }
                }
            }

            var iAllButLastColWidth = 0; //list width - width of column 2 to last  
            for (i = 0; i < aColMaxWidth.length - 1; i++) {
                iAllButLastColWidth += aColMaxWidth[i];
            }
            aColMaxWidth[aColMaxWidth.length - 1] = iActualBoxItemWidth - iAllButLastColWidth;

            //set column width
            $(this).children(".seco_content").children(".seco_item").each(function () {
                i = 0;
                $(this).children("div").each(function () {
                    $(this).css("width", aColMaxWidth[i]);
                    i++;
                });
            });
        }
    });

    /* -------------- END: Info form -------------------------- */

    /* -------------- START: Box control -------------------------- */
    $("div.seco_boxinfo:visible, div.seco_boxerror:visible").each(function () {
        var iBoxWidth = $(this).width();
        var iActualBoxItemWidth = iBoxWidth - 2 - parseInt($(this).children(".seco_item").css("padding-left").replace("px", "")) - parseInt($(this).children(".seco_item").css("padding-right").replace("px", ""));
        var iBoxHeaderWidth = iBoxWidth - $(this).children(".seco_header").children(".seco_left").width() - $(this).children(".seco_header").children(".seco_right").width();
        $(this).children(".seco_header").children(".seco_center").css("width", iBoxHeaderWidth);
        $(this).children(".seco_item").css("width", iActualBoxItemWidth);
        $(this).children(".seco_footer").children(".seco_center").css("width", iBoxHeaderWidth);



        //get max columns and max row padding
        var iMaxCols = 0;
        var iMaxHorizontalRowPadding = 0;
        $(this).children(".seco_item").each(function () {
            if ($(this).children("div").size() > iMaxCols) {
                iMaxCols = $(this).children("div").size();
                var tmpRowHorizontalPadding = 0;
                var tmpRowVerticalPadding = 0;
                $(this).children("div").each(function () {
                    tmpRowHorizontalPadding += parseInt($(this).css("padding-left").replace("px", "")) + parseInt($(this).css("padding-right").replace("px", ""));
                });
                if (tmpRowHorizontalPadding > iMaxHorizontalRowPadding) {
                    iMaxHorizontalRowPadding = tmpRowHorizontalPadding;
                }
            }
        });
        iActualBoxItemWidth -= iMaxHorizontalRowPadding;

        var iAverageColWidth = Math.floor(iActualBoxItemWidth / iMaxCols);

        //init
        var i = 0;
        var aColMaxWidth = new Array(iMaxCols);
        for (i = 0; i < aColMaxWidth.length; i++) {
            aColMaxWidth[i] = 0;
        }


        //get max width for each column
        $(this).children(".seco_item").each(function () {
            i = 0;
            $(this).children("div").each(function () {
                if ($(this).width() > aColMaxWidth[i]) {
                    aColMaxWidth[i] = $(this).width();
                }
                i++;
            });
        });

        //calculatate column width
        var iCntOverAverage = 0;
        var iColWidthSpace = 0;
        var iTotMaxWidth = 0;
        for (i = 0; i < aColMaxWidth.length; i++) {
            iTotMaxWidth += aColMaxWidth[i];
            if (aColMaxWidth[i] > iAverageColWidth) {
                iCntOverAverage++;
            }
            else {
                iColWidthSpace += iAverageColWidth - aColMaxWidth[i];
            }
        }

        if (iCntOverAverage > 0 && iTotMaxWidth > iActualBoxItemWidth) {
            for (i = 0; i < aColMaxWidth.length - 1; i++) {
                if (aColMaxWidth[i] > iAverageColWidth) {
                    aColMaxWidth[i] = iAverageColWidth + Math.floor(iColWidthSpace / iCntOverAverage);
                }
            }
        }

        var iAllButLastColWidth = 0; //list width - width of column 2 to last  
        for (i = 0; i < aColMaxWidth.length - 1; i++) {
            iAllButLastColWidth += aColMaxWidth[i];
        }
        aColMaxWidth[aColMaxWidth.length - 1] = iActualBoxItemWidth - iAllButLastColWidth;

        //set column width
        $(this).children(".seco_item").each(function () {
            i = 0;
            $(this).children("div").each(function () {
                $(this).css("width", aColMaxWidth[i]);
                i++;
            });
        });
    });


    /* -------------- END: Box control -------------------------- */

    /* -------------- START: List control -------------------------- */
    jQuery.fn.setListColumnWidth = function (type) {
        var iListWidth, iNrOfColumns, iHorizontalColPadding, iActualListWidth, iAverageColWidth;

        if (type == "title") {
            iListWidth = $(".seco_list").width();
            iNrOfColumns = $(".seco_list .seco_title div").size() - $(".seco_list .seco_title .seco_left").size() - $(".seco_list .seco_title .seco_right").size();
            iHorizontalColPadding = parseInt($(".seco_list .seco_title .seco_left").next("div").css("padding-left").replace("px", "")) + parseInt($(".seco_list .seco_title .seco_left").next("div").css("padding-right").replace("px", ""));
            iActualListWidth = iListWidth - (iNrOfColumns * iHorizontalColPadding) - $(".seco_list .seco_title .seco_left").width() - $(".seco_list .seco_title .seco_right").width();
            iAverageColWidth = Math.floor(iActualListWidth / iNrOfColumns);
        }
        else {
            iListWidth = $(".seco_list").width();
            iNrOfColumns = $(".seco_list .seco_header .seco_col").size();
            if (iNrOfColumns > 0) {
                iHorizontalColPadding = parseInt($(".seco_list .seco_col:first").css("padding-left").replace("px", "")) + parseInt($(".seco_list .seco_col:first").css("padding-right").replace("px", ""));
                iActualListWidth = iListWidth - (iNrOfColumns * iHorizontalColPadding);
                iAverageColWidth = Math.floor(iActualListWidth / iNrOfColumns);
            }
        }

        //init
        var i = 0;
        var aColMaxWidth = new Array(iNrOfColumns);

        //get max width for each column
        if (type == "title") {
            $(".seco_list .seco_title").children("div").each(function () {
                var strClass = $(this).attr("class");
                if (strClass != "seco_left" && strClass != "seco_right") {
                    aColMaxWidth[i] = $(this).width();
                    i++;
                }
            });
        }
        else {
            for (i = 0; i < aColMaxWidth.length; i++) {
                aColMaxWidth[i] = 0;
            }
            $(".seco_list .seco_header, .seco_list .seco_item").each(function () {
                i = 0;
                $(this).children(".seco_col").each(function () {
                    if ($(this).width() > aColMaxWidth[i]) {
                        aColMaxWidth[i] = $(this).width();
                    }
                    i++;
                });
            });
        }

        //calculatate column width
        var iCntOverAverage = 0;
        var iColWidthSpace = 0;
        for (i = 0; i < aColMaxWidth.length; i++) {
            if (aColMaxWidth[i] > iAverageColWidth) {
                iCntOverAverage++;
            }
            else {
                iColWidthSpace += iAverageColWidth - aColMaxWidth[i];
            }
        }
        if (iCntOverAverage > 0) {
            for (i = 1; i < aColMaxWidth.length; i++) {
                if (aColMaxWidth[i] > iAverageColWidth) {
                    aColMaxWidth[i] = iAverageColWidth + Math.floor(iColWidthSpace / iCntOverAverage);
                }
            }
        }

        var iAllButFirstColWidth = 0; //list width - width of column 2 to last  
        for (i = 1; i < aColMaxWidth.length; i++) {
            iAllButFirstColWidth += aColMaxWidth[i];
        }
        aColMaxWidth[0] = iActualListWidth - iAllButFirstColWidth;

        //set column width
        if (type == "title") {
            i = 0;
            $(".seco_list .seco_title").children("div").each(function () {
                var strClass = $(this).attr("class");
                if (strClass != "seco_left" && strClass != "seco_right") {
                    $(this).css("width", aColMaxWidth[i]);
                    i++;
                }
            });
        }
        else {
            $(".seco_list .seco_header, .seco_list .seco_item").each(function () {
                i = 0;
                $(this).children(".seco_col").each(function () {
                    $(this).css("width", aColMaxWidth[i]);
                    i++;
                });
            });
        }
    };
    if ($("div.seco_list").size() > 0) {
        $(this).setListColumnWidth('title');
        $(this).setListColumnWidth('rows');
    }
    /* -------------- END: List control -------------------------- */

    //Allways last and in this order
    $(this).fixColumnHeight();
    $(this).fixSubNav();
}		




