﻿function assignHoverEffects(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 = "WidgetHover";
            }

            rows[i].onclick = function()
            {
                top.location.href = this.cells[0].getElementsByTagName("a")[0].href;
            }
        }
    }

    function removeHover(row)
    {
        row.className = "";
    }
}

function cancelEvent(evt)
{
    if (!evt) var evt = window.event;

    if (window.event)
        window.event.cancelBubble = true;
    else if (evt.stopPropagation)
        evt.stopPropagation();
}

function RedditJobs_Init()
{
    assignHoverEffects($("jobTable"));
}

var LOADING = false;
var PAGE = 1;
var LAST_PAGE = false;

function nextPage()
{
    if(LOADING || LAST_PAGE) return;
    PAGE++;
    
    var callback =
    {   
        success: function(o)
        {
            var html = o.responseText;

            if(html.trim().length == 0)
            {
                LOADING = false;
                PAGE--;
                LAST_PAGE = true;
                return;
            }

            var holder = $("jobTableHolder");
            var div = document.createElement("div");
            
            holder.style.height = holder.offsetHeight + "px";
            
            div.innerHTML = html;
            assignHoverEffects($$(div, "table")[0]);

            holder.appendChild(div);
            
            var attributes =
            {
                top: { to: -(holder.offsetHeight) }
            };
            var anim = new YAHOO.util.Anim(holder, attributes, 0.6, YAHOO.util.Easing.backOut);
            anim.onComplete.subscribe(resetJobTable);
            anim.animate();
            
            function resetJobTable()
            {
                var tables = $$(holder, "table");
                holder.removeChild(tables[0].parentNode);
                holder.style.top = "0px";
                LOADING = false;
            }
        }
    }

    LOADING = true;
    YAHOO.util.Connect.asyncRequest("POST", "Ajax/List.aspx", callback, "p=" + PAGE);
}

function previousPage()
{
    if(LOADING) return;
    if(PAGE > 1)
        PAGE--;
    else
        return;
    
    LAST_PAGE = false;
    
    var callback =
    {
        success: function(o)
        {
            var html = o.responseText;
            
            if(html.trim().length == 0)
            {
                LOADING = false;
                PAGE++;
                return;
            }
            
            var holder = $("jobTableHolder");
            var div = document.createElement("div");
            
            holder.style.height = holder.offsetHeight + "px";
            
            div.innerHTML = html;
            assignHoverEffects($$(div, "table")[0]);

            holder.style.top = "-" + holder.style.height;
            holder.insertBefore(div, holder.childNodes[0]);

            var attributes =
            {
                top: { to: 0 }
            };
            var anim = new YAHOO.util.Anim(holder, attributes, 0.6, YAHOO.util.Easing.backOut);
            anim.onComplete.subscribe(resetJobTable);
            anim.animate();
            
            function resetJobTable()
            {
                var tables = $$(holder, "table");
                holder.removeChild(tables[1].parentNode);
                holder.style.top = "0px";
                LOADING = false;
            }
        }
    }

    LOADING = true;
    YAHOO.util.Connect.asyncRequest("POST", "Ajax/List.aspx", callback, "p=" + PAGE);
}