﻿var JobViewer = new __JobViewer();

function __JobViewer() {}

(function()
{

    var LOADING_PREVIEW = false;
    
    __JobViewer.prototype.ShowAjaxPage = function()
    {
        var index = document.location.href.indexOf("#");
        
        if(index > 0)
        {
            var info = document.location.href.substring(index+1);
            var infoArr = info.split('/');
            
            this.RefreshList(infoArr[2], unescape(infoArr[1]));
        }
    }

    __JobViewer.prototype.RefreshList = function(page, search)
    {
        GetJobList(page, search);
    }

    __JobViewer.prototype.AssignHoverEffect = function()
    {
        var jobTable = document.getElementById("jobTable");
        
        if(!jobTable) return;

        var rows = jobTable.rows;
        
        for(var i=0; i<rows.length; i++)
        {
            if(rows[i].cells[0].nodeName.toLowerCase() == "td")
            {
                rows[i].onmouseover = function()
                {
                    for(var j=0; j<rows.length; j++)
                    {
                        if(this != rows[j])
                        {
                            removeHover(rows[j]);
                        }
                    }

                    this.className = "BlueHover";

                    var spans = $$(this, "span");
                    JobViewer.Preview(this, spans[0].innerHTML, spans[1].innerHTML);

                    this.onclick = function()
                    {
                        window.location.href = this.cells[2].getElementsByTagName("a")[0].href;
                    }
                }
            }
        }

        function removeHover(row)
        {
            row.className = "";
        }
    }

    __JobViewer.prototype.ClosePreview = function()
    {
        if(CURRENT_PREVIEW) CURRENT_PREVIEW.style.display = "none";
        
        CURRENT_PREVIEW = $("welcomePanel");
        $("welcomePanel").style.display = "";
        $("closeLink").style.display = "none";
    }

    __JobViewer.prototype.Preview = function(dataRow, branchId, jobId)
    {
        if(LOADING_PREVIEW || !PAGE_LOADED) return;
        
        // Hide current preview
        if(CURRENT_PREVIEW) CURRENT_PREVIEW.style.display = "none";
        
        // Show cached preview if exist
        if(dataRow.__preview)
        {
            $("closeLink").style.display = "";
            dataRow.__preview.style.display = "";
            CURRENT_PREVIEW = dataRow.__preview;
            return;
        }
        
        // Fetch preview
        var callback =
        {   
            success: function(o)
            {
                var xmlDoc = o.responseXML;
                
                var previewPanel = document.createElement("div");
                var industryList = $$(xmlDoc, "industry");
                
                var industry = "";
                
                for(var i=0; i<industryList.length; i++)
                {
                    industry += ", " + industryList[i].firstChild.nodeValue;
                }
                
                if(industry.length > 0) industry = industry.trim().substring(2);
                
                var previewHtml =
                "<div style=\"background-image:url(" + $$(xmlDoc, "logo")[0].firstChild.nodeValue + ");background-position:center top;background-repeat:no-repeat;height:80px;border-bottom:dotted #ccc 1px;\"></div>" +
                "<table cellspacing=\"0\" cellpadding=\"0\" style=\"vertical-align:top;font-size:95%;\">" +
                "<tr><td style=\"padding:5px 20px 5px 0px;vertical-align:top;\">Funding</td><td style=\"padding:5px 0px 5px 0px;color:#dc861c;\">" + $$(xmlDoc, "funding")[0].firstChild.nodeValue + "</td></tr>" +
                "<tr><td style=\"padding:5px 20px 5px 0px;vertical-align:top;\">Employees</td><td style=\"padding:5px 0px 5px 0px;color:#dc861c;\">" + ($$(xmlDoc, "employees")[0].firstChild.nodeValue == "0" ? "Shhhh..." : $$(xmlDoc, "employees")[0].firstChild.nodeValue) + "</td></tr>" +
                "<tr><td style=\"padding:5px 20px 5px 0px;vertical-align:top;\">Salary</td><td style=\"padding:5px 0px 5px 0px;color:#dc861c;\">" + $$(xmlDoc, "salary")[0].firstChild.nodeValue + "</td></tr>" +
                "<tr><td style=\"padding:5px 20px 5px 0px;vertical-align:top;\">Fulltime</td><td style=\"padding:5px 0px 5px 0px;color:#dc861c;\">" + ($$(xmlDoc, "jobType")[0].firstChild.nodeValue == "Full Time" ? "Yes" : "No") + "</td></tr>" +
                "<tr><td style=\"padding:5px 20px 5px 0px;vertical-align:top;\">Telecommute</td><td style=\"padding:5px 0px 5px 0px;color:#dc861c;\">" + $$(xmlDoc, "telecommute")[0].firstChild.nodeValue + "</td></tr>" +
                (industry.length > 0 ? "<tr><td style=\"padding:5px 20px 5px 0px;vertical-align:top;\">Industry</td><td style=\"padding:5px 0px 5px 0px;color:#dc861c;\">" + industry + "</td></tr>" : "") +
                "</table>" +
                "<div style=\"padding:10px 0px;border-bottom:dotted #ccc 1px;color:#c15b02;font-weight:bold;\">" + $$(xmlDoc, "title")[0].firstChild.nodeValue + "</div>" +
                "<div style=\"padding:10px 0px;margin-bottom:10px;border-bottom:solid #ccc 1px;font-weight:bold;\">Overview</div>" +
                "<div style=\"line-height:1.6em;\">" + ($$(xmlDoc, "information")[0].firstChild ? $$(xmlDoc, "information")[0].firstChild.nodeValue : "") + "</div>" +
                "</div>";
                
                previewPanel.innerHTML = previewHtml;
                
                $("welcomePanel").style.display = "none";

                $("previewPanel").appendChild(previewPanel);
                
                CURRENT_PREVIEW = dataRow.__preview = previewPanel;
                $("loadingIndicator").style.visibility = "hidden";
                LOADING_PREVIEW = false;
                $("closeLink").style.display = "";
            }
        }
        
        LOADING_PREVIEW = true;
        $("loadingIndicator").style.visibility = "";
        YAHOO.util.Connect.asyncRequest("GET", "Ajax/GetJobPreview.aspx?bid=" + branchId + "&jid=" + jobId, callback, "");
    }

    function GetStaticUrl()
    {
        var staticUrl = window.location.href;
        
        if(staticUrl.indexOf("p=") > 0)
        {
            staticUrl = staticUrl.replace(new RegExp("p=[^&]*", "ig"), "p={0}");
        }
        else
        {
            staticUrl = (staticUrl.indexOf('?') > 0 ? staticUrl + "&p={0}" : staticUrl + "?p={0}");
        }
        
        return Daedalus.Utility.UrlEncode(staticUrl);
    }

    function GetJobList(pageNumber, search)
    {
        var jobTable = document.getElementById("jobTable");
        search = search.trim();
        
        // Make HTTP request
        var callback =
        {
            success: function(o)
            {
                var xmlDoc = o.responseXML;
                var nodes = $$(xmlDoc, "job");
                var pagination = $$(xmlDoc, "pagination")[0];
                
                if(search && search.length > 0) $("rssLink").href = "Rss/Search.aspx?s=" + search.urlEncode();

                // Delete old data
                var childLen = jobTable.rows.length;
                for(var i=0; i<childLen; i++) jobTable.deleteRow(0);
                
                // Create pagination
                $("jobPaginationPanel").innerHTML = pagination.firstChild.nodeValue;
                
                for(var i=0; i<nodes.length; i++)
                {
                    var panelRow = jobTable.insertRow(i);

                    for(var j=0; j<5; j++) panelRow.insertCell(j);

                    var jobId = $$(xmlDoc, "jobId")[i].firstChild.nodeValue;
                    var branchId = $$(xmlDoc, "branchId")[i].firstChild.nodeValue;
                    var title = $$(xmlDoc, "title")[i].firstChild.nodeValue;
                    var companyName = $$(xmlDoc, "companyName")[i].firstChild.nodeValue;
                    var companyId = $$(xmlDoc, "companyId")[i].firstChild.nodeValue;
                    var address = $$(xmlDoc, "address")[i].firstChild.nodeValue;
                    var photoCount = parseInt($$(xmlDoc, "photoCount")[i].firstChild.nodeValue);

                    var companyLink = "Companies/" + companyName.replace(new RegExp("[\\W_1234567890]+", "ig"), "_") + "_" + companyId + ".aspx";

                    panelRow.cells[0].innerHTML = "<div style=\"width:45px;height:30px;background-image:url(API/Image/Resize.aspx?id=" + companyId + "&amp;w=45&amp;h=30&amp;t=logo);background-position:center center;background-repeat:no-repeat;\">&nbsp;</div>";
                    panelRow.cells[1].innerHTML = "<div style=\"width:100px;height:30px;line-height:30px;overflow:hidden;white-space:normal;\"><a href=\"" + companyLink + "\" style=\"color:#333;\">" + companyName + "</a></div>";
                    panelRow.cells[2].innerHTML = "<div style=\"height:30px;line-height:30px;overflow:hidden;font-weight:bold;white-space:normal;\"><a href=\"Jobs/" + title.replace(new RegExp("[\\W_1234567890]+", "ig"), "_") + "_" + branchId + "_" + jobId + ".aspx\">" + title + "</a><span style=\"display:none;\">" + branchId + "</span><span style=\"display:none;\">" + jobId + "</span></div>";
                    panelRow.cells[3].innerHTML = "<a href=\"" + companyLink + "\"><img src=\"" + (photoCount > 0 ? "App_Themes/Default/Images/HomePage/HasPic.gif" : "App_Themes/Default/Images/HomePage/NoPic.gif") + "\" alt=\"\" title=\"" + (photoCount > 0 ? "This company has photos" : "") + "\" /></a>";
                    panelRow.cells[4].innerHTML = "<div style=\"width:90px;height:30px;line-height:30px;overflow:hidden;white-space:normal;\"><a href=\"javascript:;\" onclick=\"$('searchText').value='';$('searchLocation').value=this.innerHTML;performSearch(event);return false;\" style=\"color:#555;\">" + address.trim() + "</a></div>";

                    panelRow.cells[0].style.borderBottom = panelRow.cells[1].style.borderBottom = panelRow.cells[2].style.borderBottom = panelRow.cells[3].style.borderBottom = panelRow.cells[4].style.borderBottom = "dotted #ddd 1px";
                    panelRow.cells[0].style.width = "60px";
                    panelRow.cells[0].style.padding = "0px";
                    panelRow.cells[1].style.width = "100px";
                    panelRow.cells[1].style.padding = panelRow.cells[2].style.padding = panelRow.cells[4].style.padding = "0px 15px";
                    //panelRow.cells[2].style.width = "60%";
                    
                    panelRow.cells[0].style.cursor = panelRow.cells[1].style.cursor = panelRow.cells[2].style.cursor = panelRow.cells[3].style.cursor = "pointer";
                }

                JobViewer.AssignHoverEffect();

                // Clear preview panel
                var previewPanel = $("previewPanel");
                var welcomePanel = previewPanel.removeChild($("welcomePanel"));
                
                // Create caption for search
                var caption = $("caption");
                if(search && search.length > 0)
                {
                    var link = $("advanceSearchLink");
                    if(!caption.__defaultText) caption.__defaultText = caption.innerHTML;
                    caption.innerHTML = "Found <span style=\"font-size:18px;color:#e66d00;font-style:italic;font-family:Georgia;\">" + xmlDoc.documentElement.getAttribute("count") + "</span> results for " + (link.innerHTML == "close" ? "your search!" : "<span style=\"font-size:18px;color:#e66d00;font-style:italic;font-family:Georgia;\">" + Daedalus.Utility.HtmlEncode(search) + "</span>");
                }
                else
                {
                    if(caption.__defaultText) caption.innerHTML = caption.__defaultText;
                }
                
                previewPanel.innerHTML = "";
                previewPanel.appendChild(welcomePanel);
                
                welcomePanel.style.display = "";
                CURRENT_PREVIEW = welcomePanel;
                $("closeLink").style.display = "none";

                Daedalus.Ajax.HideLoadingPanel();
            }
        }

        document.location.href = "#/" + (search ? search.urlEncode() : "") + "/" + pageNumber;
        YAHOO.util.Connect.asyncRequest("GET", "Ajax/GetJobList.aspx?p=" + pageNumber + (search ? "&s=" + search.urlEncode() : ""), callback, "");
    }

})();