﻿function JS_Generic()
{
}

JS_Generic.prototype =
{
    __OBJECT_ID: "__JS_JOB_PANEL_",
    FONT: "",
    FONT_SIZE: "",
    HEADER_COLOR: "",
    TEXT_COLOR: "",
    LOCATION_COLOR: "",
    PUBLISHER_ID: 0,
    QUERY: "",
    AFFILIATE_ID: 0,
    JOB_COUNT: 5,
    ORIENTATION: 3,
    JOB_LIST: null,
    LINK_STYLE: 1,

    Setup: function() {
        // Save variables
        this.FONT_SIZE = typeof (_JS_FONT_SIZE) == "undefined" ? "" : _JS_FONT_SIZE;
        this.FONT = typeof (_JS_FONT) == "undefined" ? "" : _JS_FONT;
        this.HEADER_COLOR = typeof (_JS_HEADER_COLOR) == "undefined" ? "" : _JS_HEADER_COLOR;
        this.TEXT_COLOR = typeof (_JS_TEXT_COLOR) == "undefined" ? "" : _JS_TEXT_COLOR;
        if (typeof (_JS_LOCATION_COLOR) == "undefined" && typeof (_JS_COMPANY_COLOR) != "undefined")
            _JS_LOCATION_COLOR = _JS_COMPANY_COLOR; // handle legacy name for this param
        this.LOCATION_COLOR = typeof (_JS_LOCATION_COLOR) == "undefined" ? "#006600" : _JS_LOCATION_COLOR;
        this.AFFILIATE_ID = typeof (_JS_AFF_ID) == "undefined" ? 0 : _JS_AFF_ID;
        this.JOB_COUNT = typeof (_JS_JOB_COUNT) == "undefined" ? -1 : _JS_JOB_COUNT;
        this.ORIENTATION = typeof (_JS_AD_ORIENTATION) == "undefined" ? 3 : _JS_AD_ORIENTATION;
        this.JOB_LIST = (typeof (_JS_JOB_LIST) != "undefined" && _JS_JOB_LIST && _JS_JOB_LIST.length > 0 ? _JS_JOB_LIST : null);
        this.LINK_STYLE = (typeof (_JS_LINK_STYLE) != "undefined" && _JS_LINK_STYLE == 0 ? 0 : 1);

        if (this.JOB_COUNT == -1) {
            switch (this.ORIENTATION) {
                case 1: this.JOB_COUNT = 4; break;  // vertical
                case 2: this.JOB_COUNT = 2; break;  // horizontal
                case 3: this.JOB_COUNT = 1; break;  // inline
            }
        }

        // Find available ad ID
        var id = 1;
        while (document.getElementById(this.__OBJECT_ID + id) != null) id++;

        // Create ad panel
        this.__OBJECT_ID = this.__OBJECT_ID + id;
        var fontSizeText = this.FONT_SIZE == "" ? "inherit" : this.FONT_SIZE;
        var fontFamilyText = this.FONT == "" ? "inherit" : this.FONT;
        var panelHtml = "<div id=\"" + this.__OBJECT_ID + "\" style=\"font-family:" + fontFamilyText + ";font-size:" + fontSizeText + "px;text-align:left; padding-bottom:10px;\"><i>Loading... Please wait...</i></div>";
        document.write(panelHtml);

        var panel = document.getElementById(this.__OBJECT_ID);

        var jobList = "";
        if (this.JOB_LIST && this.JOB_LIST.length > 0) {
            for (var i = 0; i < this.JOB_LIST.length; i++) {
                jobList += "," + this.JOB_LIST[i];
            }
        }

        panel.__JS_OBJECT = this;
        panel.__JS_OBJECT.__ApiCall("http://www.jobsyndicate.com/JServ/API/Jobs/List.aspx?a=" + this.AFFILIATE_ID + "&c=" + this.JOB_COUNT + (jobList.length > 0 ? "&jl=" + jobList : "") + "&callback=document.getElementById('" + this.__OBJECT_ID + "').__JS_OBJECT.CreateAd");
    },

    CreateAd: function(data) {
        var panel = document.getElementById(this.__OBJECT_ID);

        panel.innerHTML = "";

        var lineHeight = this.FONT_SIZE == "" ? "" :
             "line-height:" + Math.round(this.FONT_SIZE + this.FONT_SIZE / 2) + "px;";
        var headerColor = this.HEADER_COLOR == "" ? "" :
             "color:" + this.HEADER_COLOR + ";";
        var locationColor = this.LOCATION_COLOR == "" ? "" :
             "color:" + this.LOCATION_COLOR + ";";
        var textColor = this.TEXT_COLOR == "" ? "" :
             "color:" + this.TEXT_COLOR + ";";

        if (data && data.length > 0) {
            if (this.ORIENTATION == 1) {    // vertical
                var onclickScript = function(ad, jobid) { return "this.parentNode.__JS_OBJECT.__ShowJobDetail('" + jobid + "'," + ad.AFFILIATE_ID + "," + ad.LINK_STYLE + ")"; };
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    html += "<div style=\"padding-bottom:20px;cursor:pointer;\" "
                                    + " onclick=\"" + onclickScript(this, data[i].id) + "\">"
                                    + "<a href=\"javascript:void('Click to see job description')\" style=\"display:block; text-decoration:none; font-weight:bold;" + headerColor + lineHeight + "padding-bottom:3px;\">" + data[i].title + "</a>"
                                    + "<div style=\"" + textColor + lineHeight + "\">" + data[i].description + "</div>"
                                    + "<div style=\"" + locationColor + "\">" + data[i].location + "</div>"
                               + "</div>";
                }
                html += "<div style=\"padding-bottom:10px;\"><a href=\"http://www.jobsyndicate.com/JServ/Page/List.aspx?aid=" + encodeURI(this.AFFILIATE_ID) + "&f=" + encodeURI(this.FONT) + "&fs=" + encodeURI(this.FONT_SIZE) + "\" onclick=\"this.target='_blank';\">more jobs...</a></div>";
                panel.innerHTML = html;
            }
            else if (this.ORIENTATION == 2)    // horizontal
            {
                var onclickScript = function(ad, jobid) { return "this.parentNode.parentNode.parentNode.parentNode.parentNode.__JS_OBJECT.__ShowJobDetail('" + jobid + "'," + ad.AFFILIATE_ID + "," + ad.LINK_STYLE + ")"; };
                var html = "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%;\"><tbody><tr>";
                for (var i = 0; i < data.length; i++) {
                    html += "<td style=\"width:" + Math.round(100 / data.length) + "%;vertical-align:top;\"><div style=\"padding-bottom:20px;padding-right:" + (i < data.length - 1 ? "10" : "0") + "px;cursor:pointer;\" "
                                    + " onclick=\"" + onclickScript(this, data[i].id) + "\">"
                                    + "<a href=\"javascript:void('Click to see job description')\" style=\"display:block; text-decoration:none; font-weight:bold;" + headerColor + lineHeight + "padding-bottom:3px;\">" + data[i].title + "</a>"
                                    + "<div style=\"" + textColor + lineHeight + "\">" + data[i].description + "</div>"
                                    + "<div style=\"" + locationColor + "\">" + data[i].location + "</div>"
                               + "</div>";
                }
                html += "</tr></tbody></table>";
                html += "<div style=\"padding-bottom:10px;\"><a href=\"http://www.jobsyndicate.com/JServ/Page/List.aspx?aid=" + encodeURI(this.AFFILIATE_ID) + "&f=" + encodeURI(this.FONT) + "&fs=" + encodeURI(this.FONT_SIZE) + "\" onclick=\"this.target='_blank';\">more jobs...</a></div>";
                panel.innerHTML = html;
            }
            else // inline text
            {
                var onclickScript = function(ad, jobid) { return "this.parentNode.__JS_OBJECT.__ShowJobDetail('" + jobid + "'," + ad.AFFILIATE_ID + "," + ad.LINK_STYLE + ")"; };
                var html = "";
                for (var i = 0; i < data.length; i++) {   
                    html += "<span onclick=\"" + onclickScript(this, data[i].id) + "\">"
                                + "<a href=\"javascript:void('Click to see job description')\" style=\"padding-bottom:20px;cursor:pointer;text-decoration:none;outline-width:0px;" + headerColor + "\">"
                                + "<span style=\"font-weight:bold;\">" + data[i].company + "</span>"
                                + " is looking for a <span style=\"font-weight:bold;\">" + data[i].title + "</span>"
                                + " in <span style=\"font-weight:bold;white-space:nowrap;\">" + data[i].location + "</span>"
                           + "</a></span>";
                }
                html += "<span style=\"white-space:nowrap;\"></span> - <a style=\"text-decoration:none;" + headerColor + "\" href=\"http://www.jobsyndicate.com/JServ/Page/List.aspx?aid=" + encodeURI(this.AFFILIATE_ID) + "&f=" + encodeURI(this.FONT) + "&fs=" + encodeURI(this.FONT_SIZE) + "\" onclick=\"this.target='_blank';\">more jobs</a></span>";
                panel.innerHTML = html;
            }

        }
    },

    __ApiCall: function(url) {
        var header = document.getElementsByTagName("head")[0];
        var scriptObj = document.createElement("script");

        // Add script object attributes
        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("charset", "utf-8");
        scriptObj.setAttribute("src", url);

        header.appendChild(scriptObj);
    },

    __ShowJobDetail: function(jobId, affId, overlay) {
        if (overlay == 1) {
            if (document.getElementById("__JS_INFO_OVERLAY")) document.body.removeChild(document.getElementById("__JS_INFO_OVERLAY"));

            var overlay = document.createElement("div");

            overlay.id = "__JS_INFO_OVERLAY";
            overlay.style.width = "100%";
            overlay.style.position = "absolute";
            overlay.style.left = "0px";
            overlay.style.top = "0px";
            overlay.style.zIndex = 10000;
            overlay.style.backgroundImage = "url(http://www.jobsyndicate.com/JServ/Images/OverlayBackground.png)";
            overlay.onclick = function() {
                if (document.getElementById("__JS_INFO_OVERLAY")) document.body.removeChild(document.getElementById("__JS_INFO_OVERLAY"));
            }

            var content = overlay.appendChild(document.createElement("div")).appendChild(document.createElement("div"));

            content.style.position = "relative";
            content.style.top = (this.__GetScrollY() + 20) + "px";
            content.style.minWidth = "600px";
            content.style.maxWidth = "930px";
            if (this.FONT != "")
                content.style.fontFamily = this.FONT;
            if (this.FONT_SIZE != "")
                content.style.fontSize = this.FONT_SIZE != "" ? this.FONT_SIZE : "100%";
            content.style.margin = "0px auto";

            content.parentNode.style.padding = "0px 80px";

            document.body.appendChild(overlay);

            // Fetch job detail from server
            var frameHolder = content.appendChild(document.createElement("div"));
            var frame = document.createElement("iframe");

            frameHolder.style.backgroundColor = "#3d3d3d";
            frameHolder.onclick = function(evt) {
                if (!evt) var evt = window.event;

                if (window.event)
                    window.event.cancelBubble = true;
                else if (evt.stopPropagation)
                    evt.stopPropagation();
            }

            var closeLink = frameHolder.appendChild(document.createElement("div")).appendChild(document.createElement("a"));
            closeLink.parentNode.style.textAlign = "right";

            closeLink.style.fontSize = "13px";
            closeLink.style.fontFamily = "Arial";
            closeLink.style.color = "#fff";
            closeLink.href = "javascript:;";
            closeLink.innerHTML = "return to site <span style=\"color:#f90;\">X</span>";
            closeLink.style.fontWeight = "bold";
            closeLink.style.textDecoration = "none";
            closeLink.parentNode.style.padding = "5px 15px 0px 0px";

            closeLink.onclick = function() {
                if (document.getElementById("__JS_INFO_OVERLAY")) document.body.removeChild(document.getElementById("__JS_INFO_OVERLAY"));
            }

            frame.id = "JS_FRAME";
            frame.frameBorder = "0";
            frame.src = "http://www.jobsyndicate.com/JServ/Page/Detail.aspx?jid=" + jobId + "&aid=" + affId + "&f=" + this.FONT + "&fs=" + this.FONT_SIZE;
            frame.style.width = "100%";
            frame.style.height = "780px";
            frame.style.borderWidth = "0px";
            frame.onload = function() {
                //this.style.height = "px";
            }

            frameHolder.appendChild(frame);

            // Extend overlay height
            var height = (document.body.offsetHeight > document.documentElement.clientHeight ? document.body.offsetHeight : document.documentElement.clientHeight);
            overlay.style.height = height + "px";
        }
        else {
            window.open("http://www.jobsyndicate.com/JServ/Page/Detail.aspx?jid=" + jobId + "&aid=" + affId + "&f=" + this.FONT + "&fs=" + this.FONT_SIZE, "_blank");
        }
    },

    __GetScrollY: function() {
        var y = 0;

        if (typeof (window.pageYOffset) == "number") {
            //Netscape compliant
            y = window.pageYOffset;
        }
        else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            //DOM compliant
            y = document.body.scrollTop;
        }
        else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
            //IE6 standards compliant mode
            y = document.documentElement.scrollTop;
        }

        return y;
    }
}

new JS_Generic().Setup();
