//
// Table sort-saver
//
function sort_saver()
{
    var sortSess = new Array();

    $("#abroad-holidays > thead > tr > th").each(function(index, th)
    {
        if($(th).hasClass("headerSortDown"))
        {
            sortSess["column"] = index;
            sortSess["sortType"] = 0;
        }
        if($(th).hasClass("headerSortUp"))
        {
            sortSess["column"] = index;
            sortSess["sortType"] = 1;
        }
    });
    var serialized = "column:"+sortSess["column"]+",sortType:"+sortSess["sortType"];
    setCookie("sort-hash", serialized,expiryday);
}

//
// Zebralizer
//
function unZebralize()
{
    $("#abroad-holidays > tbody > tr:not(:hidden)").each(function(index, element)
    {
        $(element).removeClass("colored");
    });
}

function zebralize(index,element)
{
    index%2 ? $(element).addClass("colored") : false;
}

function zebralize_Abroad_Holidays()
{
    unZebralize();
    $("#abroad-holidays > tbody > tr:not(:hidden)").each(zebralize);
}

//
// Table Filters
//
function filter_abroad_holidays_threaded()
{
    setTimeout("filter_abroad_holidays()", 15);
}

//
// IMPERIAL BOOST
//

//
// Clean emphasized
// Resets all highlighted tds
//
function clear_emphasized()
{
    $(".emphasized").each(function(i,elem)
    {
        $(elem).next().removeClass("hidden");
        $(elem).remove();
    });
}

//
// Filters table with entered key
//
function filter_abroad_holidays(evt)
{
    setTimeout("clear_emphasized();", 10);
    $("#nothing_found").addClass("hidden");

    // Flush previous filters
    // init vars
    var specials = [
        '/', '.', '*', '+', '?', '|',
        '(', ')', '[', ']', '{', '}', '\\'
      ];

    sRE = new RegExp(
        '(\\' + specials.join('|\\') + ')', 'g'
    );     

    var global_found = false;
    var rawKey = $("#search-form > input[name=search]").val().toLowerCase();
    if(rawKey.replace(/^\s+|\s+$/g, "") == "")
            return -1;
    rawKey = rawKey.replace(sRE, '\\$1');
    var search_phrase = '('+rawKey+')';
    var search = new RegExp(search_phrase, "gi");

    

    //
    if(search_phrase.length == 0)
    {
        $("#abroad-holidays > tbody > tr").each(function(index, tbody)
        {
            $(tbody).removeClass("hidden");
        });
        zebralize_Abroad_Holidays();
    }
    else
    {
        var rows = $("#abroad-holidays > tbody > tr");
        var portion = {
            start: 0,
            end: 10
        };

        for(var i = 0; i < ((rows.length / 10)+1); i++ )
        {
            var found = false;

            $(rows).slice(portion.start, portion.end).each(function(i, tr)
            {
                setTimeout(function()
                {
                    $(tr).find("th > a, td > span").each(function(i, o)
                    {
                        var td_contents = $(o).text();
                        if(search.test(td_contents))
                        {
                            found = true;
                            global_found = true;
                        }
                    });

                    found ? $(tr).removeClass("hidden") : $(tr).addClass("hidden");
                    global_found ? $("#nothing_found").addClass("hidden") : $("#nothing_found").removeClass("hidden");
                    found = false;
                }, 5);

                // highlight
                setTimeout(function()
                {
                    $(tr).find("th > a, td > span").each(function(i, o)
                    {
                        var td_contents = $(o).text();
                        if(search.test(td_contents))
                        {
                                $(o).clone().prependTo($(o).parent()).addClass("emphasized").html((td_contents).replace(search ,"<strong>\$1</strong>"));
                            $(o).addClass("hidden");
                        }
                    });
                }, 55);
            });
            portion.start += 10;
            portion.end += 10;
        }
        setTimeout("zebralize_Abroad_Holidays()", 10);
    }
}

//
// Dom:loaded
//
$(document).ready(function()
{
    // zebralize
    zebralize_Abroad_Holidays();

    // init search input listener
    $("#search-form > input[name=search]").bind("keyup", function()
    {
        setTimeout(function()
        {
            filter_abroad_holidays_threaded();
        },3);
    });

    // read previously searched phrase from cookies
    if(null != getCookie("excel-search"))
    {
        $("#search-form > input[name=search]").val(decodeURIComponent(getCookie("excel-search")));
        filter_abroad_holidays_threaded();
        
        zebralize_Abroad_Holidays();
    }
    
    // store searched phrase
    $(window).unload(function()
    {
        setCookie("excel-search", encodeURIComponent($("#search-form > input[name=search]").val()), expiryday);
        sort_saver();
    });

    // Rezebralize after sorting is finished
    $(document).bind("sortEnd", zebralize_Abroad_Holidays);
});
