if ( ! jQuery) {
    var $ = jQuery.noConflict();
} else {
    var $ = jQuery;
}

var countMySites = 0;

$d = function() { console.log.apply( console, arguments ) };
//$d = function() { alert( arguments ) };

$(document).ready(function() {
    
    $("#jsTable-1 td[name]").mouseover(function(){
        $(this).css("border", "7px solid #FFC000");
    });
    
    $("#jsTable-1 td[name]").mouseout(function(){
        $(this).css("border", "7px solid white");
    });
    
    $("#jsTable-1 td[name]").each(function(){
        $currentLink = $(this);
        $currentLink.qtip({
            content: {
            url: URL_TO_SOURCE + "tooltip.php",
            data: "img=" + escape($currentLink.attr("name")),
            method: "get"
            },
            position: {
              corner: {
                 target: 'topRight',
                 tooltip: 'topLeft'
              }
            },
            show: {
                delay: 0,
                effect: {
                    type: "fade",
                    length: 500
                    }
                },
            hide: {
                delay: 0,
                effect: {
                    type: "fade",
                    length: 1000
                    }
                },
            style: {
                width: 650,
                height: 450,
                padding: 5,
                textAlign: "center",
                border: {
                width: 1,
                radius: 7,
                color: "#FFC000"
                },
                tip: "topLeft"
            }
            });
    });

    staticBarTop = $("#staticBar").height();
    $("#staticBar").css("display", "block");

    $("#pagerOption option:contains('Favorite')").attr("selected","selected");
    $(".cssPager:eq(1)").css("fontWeight", "bold");

    if ( ! ($.browser.msie && $.browser.version.substr(0,1) < 7))
    {
        $('.cssSelectorTd').corner();
        $('.cssButtonSave').corner();
    }

    if ($("#jsTable-1 tr").length) {
        // avoid error
        jQuery.tableDnD = $.tableDnD;
        $("#jsTable-1").tableDnD({
            onDragClass: "myDragClass"
        });

    }

    function get_holdSitesArray()
    {
        var holdSites = new Array();

        if ( ! $.cookie("holdSites"))
        {
            return holdSites;
        }

        if ($.cookie("holdSites").indexOf(",") >= 0)
        {
            holdSites = $.cookie("holdSites").split(",");
        }
        else
        {
            if ($.cookie("holdSites").length)
                holdSites[0] = $.cookie("holdSites");
        }

        return holdSites;
    }

    $(".selectBookmark").click(function() {
        var holdSites = get_holdSitesArray();
        var siteId  = $(this).closest("tr").attr("id");
        var index   = $.inArray(siteId, holdSites);
        if(index == -1)
        {
            holdSites.push(siteId);
            countMySites--;
            $(this).addClass("cssButtonAccepted");
            $(this).removeClass("cssButtonAdd");
            $(this).attr("src", URL_TO_SOURCE + "png/accepted.png").attr("title", "Favorite website").css("width", "48px");
        }
        else
        {
            holdSites.splice(index,1);
            countMySites++
            $(this).removeClass("cssButtonAccepted");
            $(this).addClass("cssButtonAdd");
            $(this).attr("src", URL_TO_SOURCE + "png/add.png").attr("title", "Add to my favorites").css("width", "24px");
        }

        $.cookie("holdSites", holdSites ? holdSites.join(",") : "", { expires: 365 });
        staticBarTop = $("#staticBar").height();
    });

    $(".cssSelector2").click(function() {
        if ($("#jsSelectorTd1").is(":visible")) {
            $("#jsSelectorTd1").parent().hide();
        }
        else
        {
            $("#jsSelectorTd1").parent().show();
        }

        staticBarTop = $("#staticBar").height();
    });

    $("select[name='pagerOption']").click(function(){
       if ($(this).val() == 2)
       {
            var holdSites = get_holdSitesArray();

            if ( ! holdSites.length)
            {
                alert("You must select some website first!")
                return;
            }

            $("html, body").animate({
                scrollTop: $("#jsTable-1").offset().top
            }, 500);

            $("#jsSelectorTd2").animate({
                opacity: "hide"
            });

            var mytable = document.getElementById("jsTable-1");
            var myrows = mytable.rows.length;

            for (i = 0; i < myrows; i++)
            {
                $(mytable.rows[i]).animate({
                    opacity: "hide"
                });
            }

            for (i = 0; i < holdSites.length; i++)
            {
                var myrow = document.getElementById(holdSites[i]);
                $(myrow).animate({
                    opacity: "show"
                });
            }
       }
       else if ($(this).val() == 1)
       {
            $("#jsSelectorTd2").animate({
                opacity: "show"
            });

            $(".cssPager:eq(1)").click();
       }
       else
       {
            $("html, body").animate({
                scrollTop: $("#jsTable-1").offset().top
            }, 500);

            $("#jsSelectorTd2").animate({
                opacity: "hide"
            });

            $("#jsTable-1 tr").each(function(){
                $(this).animate({
                    opacity: "show"
                });
            });
       }

        $.cookie("pagerOption", $(this).val(), { expires: 365 });
    });

    var pageActual = 1;
    var mytable = document.getElementById("jsTable-1");
    $(".cssPager").click(function(){
        page = $(this).text();

        if (page == "Previous")
            page = pageActual > 1 ? pageActual - 1 : 1;
        else if (page == "Next")
            page = pageActual < countSites ? pageActual + 1 : countSites;

        page = parseInt(page);
        pageActual = page;

        $("html, body").animate({
            scrollTop: $("#jsTable-1").offset().top
        }, 500);

        $("#jsTable-1 tr").each(function(){
            $(this).animate({
                opacity: "hide"
            });
        });

        for (i = page * 25 - 25; i < page * 25; i++)
        {
            $(mytable.rows[i]).animate({
                opacity: "show"
            });
        }

        $(".cssPager").each(function(){
            $(this).css("fontWeight", "normal");
            if (page == $(this).html())
                $(this).css("fontWeight", "bold");
        });
    });
})
