$(function () {

    //Creates cookie for tablesorter widget
    $.tablesorter.addWidget({
        // give the widget an id
        id: "sortPersist",
        // format is called when the on init and when a sorting has finished
        format: function(table) {

            // Cookie info
            var cookieName = 'EVENTS_SORT';
            var cookie = $.cookie(cookieName);
            var options = {
                path: '/'
            };

            var data = {};
            var sortList = table.config.sortList;
            var tableId = $(table).attr('id');
            var cookieExists = (typeof(cookie) != "undefined" && cookie != null);

            // If the existing sortList isn't empty, set it into the cookie and get out
            if (sortList.length > 0) {
                if (cookieExists) {
                    data = $.evalJSON(cookie);
                }
                data[tableId] = sortList;
                $.cookie(cookieName, $.toJSON(data), options);
            }

            // Otherwise...
            else {
                if (cookieExists) {

                    // Get the cookie data
                    var data = $.evalJSON($.cookie(cookieName));

                    // If it exists
                    if (typeof(data[tableId]) != "undefined" && data[tableId] != null) {

                        // Get the list
                        sortList = data[tableId];

                        // And finally, if the list is NOT empty, trigger the sort with the new list
                        if (sortList.length > 0) {
                            $(table).trigger("sorton", [sortList]);
                        }
                    }
                }
            }
        }
    });


    $(".events-list").tablesorter({
        widthFixed: false,
        widgets: ['zebra', 'sortPersist']
        });

    //Adds the sort button div to the top/th of the table for the table sorter table
    $(".events-list th").each(function(){
        var that = this;
        $(that).prepend("<div class='sortBtn' >&nbsp;</div>");
        if($(that).hasClass("short-header")){
            if($.browser.msie){
                $(that).addClass("short-headerIE");
            }
        } else if($(that).hasClass("medium-header")){
            if($.browser.msie){
                $(that).addClass("medium-headerIE");
            }
        }
    });

    //Hover list of events
    $('.calendar-dayHasEvents').each(function () {
        // options
        var distance = -15;
        var time = 250;
        var hideDelay = 450;
        var showDelay = 300;

        var hideDelayTimer = null;
        var showDelayTimer = null;

        // tracker
        var beingShown = false;
        var shown = false;
        var waitingToBeShown = false;

        var trigger = $(this);
        var popup = $('.hoverEvent', this).css('opacity', 0);

        if($.browser.msie){
            $(popup).addClass('hoverEventIEHack');
        }

        // set the mouseover and mouseout on both element
        $([trigger.get(0), popup.get(0)]).mouseover(function (e) {
            // stops the hide event if we move from the trigger to the popup element
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            // don't trigger the animation again if we're being shown, or already visible
            if (beingShown || shown || waitingToBeShown) {
                return;
            } else {
                waitingToBeShown = true;

                showDelayTimer = setTimeout(function (){
                    waitingToBeShown = false;
                    beingShown = true;

                    // reset position of popup box

                    //Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
                    //$(popup).css('left', e.pageX + 10);
                    $(popup).css('display', 'block') // brings the popup back in to view

                    // (we're using chaining on the popup) now animate it's opacity and position
                    .animate({
                        bottom: '+=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        // once the animation is complete, set the tracker variables
                        beingShown = false;
                        shown = true;
                    });

                }, showDelay);
            }
        }).mouseout(function () {
            // reset the timer if we get fired again - avoids double animations
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            //resets the hoverIntent
            if(showDelayTimer) clearTimeout(showDelayTimer);
            waitingToBeShown = false;

            // store the timer so that it can be cleared in the mouseover if required
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                popup.animate({
                    bottom: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    // once the animate is complete, set the tracker variables
                    shown = false;
                    // hide the popup entirely after the effect (opacity alone doesn't do the job)
                    popup.css('display', 'none');
                });
            }, hideDelay);
        });
    });
    
    //Capture button on click
    $("#btnShowAll").click(function(){
    	$("#searchString").val("");
		$("#searchForm").submit();
		return false;	
    });
});
