﻿

var midreshet = {
    widgetName: "midreshet",
    jQueyUrl: "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js",
    cssUrl: "css/widget.css?ver=" + new Date(),
    winContentUrl: "widget.aspx",
    timer: null,
    widgetBase: null,

    //INSTALL WIDGET
    //*******************************************************
    Init: function() {
        this.widgetBase = document.getElementById(this.widgetName + "_src").getAttribute("src").replace("js/widget.js", "");
        this.AddEvent(window, "load", function() { midreshet.Install() });

        document.write("<div class='" + this.widgetName + "_widget_holder' style='height:187px;'></div>");
    },
    Install: function() {
        if (!this.IsJQueryExist()) {
            this.ImportScript(this.jQueyUrl, "myJQuery");
        }

        var cssUrl = this.widgetBase + this.cssUrl;
        this.ImportStyle(cssUrl, "myGimaoCss");
        this.timer = setInterval(this.widgetName + ".CheckInstallStatus()", 2000);

    },
    IsJQueryExist: function() {
        return (typeof (jQuery) == "function");
    },
    ImportScript: function(url, id) {
        var item = document.createElement("script");
        item.src = url;
        item.type = "text/javascript";
        item.defer = true;
        item.id = id;
        var head = document.getElementsByTagName("head").item(0);
        if (head)
            head.appendChild(item);
        else
            document.body.appendChild(item);
    },
    ImportStyle: function(url, id) {

        var item = document.createElement("link");
        item.href = url;
        item.type = "text/css";
        item.rel = "stylesheet";
        item.id = id;
        var head = document.getElementsByTagName("head").item(0);
        if (head)
            head.appendChild(item);
        else
            document.body.appendChild(item);
    },
    AddEvent: function(obj, evt, func) {
        if (document.addEventListener)
            obj.addEventListener(evt, func, false);
        else if (document.attachEvent)
            obj.attachEvent("on" + evt, func);
        else
            eval(obj + ".on" + evt + "=" + func);
    },
    CheckInstallStatus: function() {
        if (this.IsJQueryExist()) {
            clearInterval(this.timer);
            this.OnDocumentReady();
        }
    },

    //WIDGET METHODS
    //*******************************************************
    OnDocumentReady: function() {

        this.AddCode();

    },
    AddCode: function() {

        var holder = $("." + this.widgetName + "_widget_holder");
        holder.html("<iframe class='" + this.widgetName + "_window_frame' src='about:blank' frameborder='0' scrolling='no'>");

        var win = $("." + this.widgetName + "_window_frame");
        if (win && win.length > 0) {
            win.css("opacity", 0).css("width", holder.width() + "px").css("height", holder.height() + "px").show();

            win.unbind().load(function() {
                win.animate({ opacity: 1 }, 500, null);
            });
            
            this.FrameSetUrl();
        }
    },
    FrameSetUrl: function() {
        var url = midreshet.widgetBase + midreshet.winContentUrl;
        var win = $("." + this.widgetName + "_window_frame");
        win.attr("src", "").attr("src", url);
    }
}

midreshet.Init();

 
