﻿$(function () {
    $("html body form#aspnetForm div#wrapper div#top_menu ul.tabs li a:last").attr("target", "_Blank");
    $("ul.tabs > li:last-child").addClass("markedtab");
});

function cancelEvent(e) {
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    } else if (typeof e.cancelBubble != "undefined") {
        e.returnValue = false;
        e.cancelBubble = true;
    }
    return false;
}

function transferBetween(list, select, insertValue, extractIdentifier) {
    var ul = $(list);
    var sel = $(select);
    ul.find("a").live("click", function () {
        var id = extractIdentifier(this.parentNode);
        var name = $(this).text();
        if (id == null || id == "" || name == null || name == "") {
            return false;
        }
        insertValue(sel, id, name);
        return false;
    });
}

function addOverlay(identifier, spinnerPath) {
    if (spinnerPath == "undefined") {
        spinnerPath = "App_Themes/Default/img/Spinner1.gif";
    }
    var el = $(identifier);
    var overlayPanel = $("#overlayPanel");
    if (overlayPanel.length == 0) {
        overlayPanel = $("<div id='overlayPanel'></div>").appendTo("body");
        overlayPanel.css({ position: "absolute", width: el.width() + 3, height: el.height() + 3, top: el.offset().top, left: el.offset().left, opacity: 0.5, backgroundColor: "#FFF" });
        //var spinner = $("<img src='" + spinnerPath + "' />").appendTo(overlayPanel);
        //spinner.css({ top: "50%", left: "50%", position: "absolute", marginLeft: "-14px", marginTop: "-14px" });
    }
    overlayPanel.show();
}

function hideOverlay() {
    $("#overlayPanel").hide();
}

function hightLightSearchText(container, word) {
    container.html(container.html().replace(eval("/" + word + "/gi"), function (w) { return "<span class=\"searchword\">" + w + "</span>"; }));
}

var MsseUtil = {
    scrollTo: function (selector, speed) {
        if (speed == "undefined") {
            speed = 0;
        }
        if (isNaN(selector)) {
            var targetOffset = $(selector).offset().top;
            $('html,body').animate({ scrollTop: targetOffset }, speed);
        } else {
            $('html,body').animate({ scrollTop: selector }, speed);
        }
    },
    cache: {},
    tmpl: function (str, data) {
        // Simple JavaScript Templating
        // John Resig - http://ejohn.org/ - MIT Licensed
        var fn = !/\W/.test(str) ?
          this.cache[str] = this.cache[str] ||
          this.tmpl(document.getElementById(str).innerHTML) :
          new Function("obj",
            "var p=[],print=function(){p.push.apply(p,arguments);};" +
            "with(obj){p.push('" +
            str
              .replace(/[\r\t\n]/g, " ")
              .split("<?").join("\t")
              .replace(/((^|\?>)[^\t]*)'/g, "$1\r")
              .replace(/\t=(.*?)\?>/g, "',$1,'")
              .split("\t").join("');")
              .split("?>").join("p.push('")
              .split("\r").join("\\'")
          + "');}return p.join('');");
        return data ? fn(data) : fn;
    },
    currentPath: "",
    doSearch: function (search, company, category, program, resultsIdentifier, callback) {
        $(resultsIdentifier).show(200);
        addOverlay(resultsIdentifier);
        $.getJSON(this.currentPath + "/Handlers/FundSearchHandler.ashx", { s: search, co: company, ca: category, ul: program, n: "200" }, function (data) {
            $(resultsIdentifier + " ul").empty();
            if (data == null || data.length == 0) {
                $("<li><a href='#'>Inga fonder hittades.</a></li>").appendTo(resultsIdentifier + " ul");
            } else {
                for (var i = 0; i < data.length; i++) {
                    $("<li><input type='hidden' value='" + data[i].p + data[i].i + "' /><a href='#'>" + data[i].f + "</a></li>").appendTo(resultsIdentifier + " ul");
                }
            }
            hideOverlay();
            if (callback) {
                callback(data);
            }
        });
    }

    //    doSearchAdvChart: function (search, company, category, program, resultsIdentifier, callback) {
    //        $(resultsIdentifier).show(200);
    //        addOverlay(resultsIdentifier);
    //        $.getJSON(this.currentPath + "/Handlers/FundSearchHandler.ashx", { s: search, co: company, ca: category, ul: program }, function (data) {
    //            $(resultsIdentifier + " ul").empty();
    //            if (data == null || data.length == 0) {
    //                $("<li><a href='#'>Inga fonder hittades.</a></li>").appendTo(resultsIdentifier + " ul");
    //            } else {
    //                var url = window.location.href;
    //                var i = url.indexOf("?compareids");
    //                for (var i = 0; i < data.length; i++) {
    //                    if (i = -1) {
    //                        $("<li><input type='hidden' value='" + data[i].p + data[i].i + "' /><a href='" + url + "?compareids=" + data[i].i + "|'>" + data[i].f + "</a></li>").appendTo(resultsIdentifier + " ul");
    //                    }
    //                    else {
    //                        $("<li><input type='hidden' value='" + data[i].p + data[i].i + "' /><a href='" + url + data[i].i + "|'>" + data[i].f + "</a></li>").appendTo(resultsIdentifier + " ul");
    //                    }
    //                }
    //            }
    //            hideOverlay();
    //            if (callback) {
    //                callback(data);
    //            }
    //        });
    //    }

};
