jQuery(document).ready(function(){
    ContestSchedule.init();
	Top5.init();
})

ContestSchedule = {
	init: function() {
		jQuery('#artist_search').submit(ContestSchedule.submit_form);
		jQuery('#artist_search a.search').click(ContestSchedule.submit_form);
		jQuery('#artist_search_q').blur(ContestSchedule.bring_back_overlay)
		                    .click(ContestSchedule.focus_on_search_box);
	},
	submit_form: function() {
	    var q = jQuery('#artist_search_q').val();
	    var round_tables = jQuery('table.schedule');
        var no_results_div = jQuery('#contest_schedule #no_results').removeClass("error");
        no_results_div.html('');
	    // undo any added classes from prior searches
	    round_tables.show();
	    jQuery("tr.highlight").removeClass("highlight");
	    if (!q || q === "" || q === "Find Artist") return false;
	    // hide all round tables
	    round_tables.hide();
	    // select those that match the text, show them, highlight artist row
	    jQuery('td a.name:contains("' + q + '")').each(function(){
	       jQuery(this).parents('table.schedule').show();
	       jQuery(this).parents('tr').addClass("highlight");
	    });
	    if (jQuery('table.schedule:visible').size() == 0) {
	        // there are no results so say something nice
	        no_results_div.html("There are no artists matching the text you entered: " + q + "!").addClass("error");
	    }
		return false;
	},
	focus_on_search_box: function(){
	    var val = jQuery(this).val();
	    if (val === "Find Artist") jQuery(this).val('').removeClass("gray");
	},
	bring_back_overlay: function() {
	    var val = jQuery(this).val();
		if (val == "" || !val) {
			jQuery(this).val("Find Artist").addClass("gray");
		}
	},
    jump_to_round: function(obj){
        var select = jQuery(obj);
        var anchor = jQuery("a[name='round" + select.val() + "']")
        window.scrollTo(0, anchor.get(0).offsetTop);
    }
}

Top5 = {
    init: function(){
        Top5.panel = jQuery('#top5_panel');
        if (logged_in) {
            if (Top5.panel.size() > 0) {
                Top5.list = jQuery('#top5_panel ul');
                Top5.currentItem = null;
                jQuery('a.top5').click(Top5.showForm);
                jQuery('#top5_panel td a').click(Top5.addToList);
                jQuery('#top5_panel ul li').live('click',Top5.selectItem);
                jQuery('#top5_up').click(Top5.moveUp);
                jQuery('#top5_down').click(Top5.moveDown);
                jQuery('#top5_remove').click(Top5.remove);
                jQuery('#top5_panel a.close').click(Top5.hideForm);
                jQuery('#top5_clear').click(Top5.reset);
            } else {
                jQuery('a.top5').click(Top5.showPopup);
            }
        } else {
            jQuery('a.top5').click(function(){
                jQuery(this).parents("li.entry").overlay();
                return false;
            })
        }
    },
    showPopup: function(){
        if (Top5.popup) {
            Top5.popup.show();
        } else if (typeof(rankings) != "undefined") {
            Top5.popup = jQuery("<div id='top5_popup'></div>");
            var list = "", count = 1;
            jQuery(rankings).each(function(){
               list += "<li><span>" + count++ + "</span>" + this + "</li>" 
            });
            Top5.popup
                .append("<a href='#' class='close' onclick='return Top5.hidePopup()'>close</a>")
                .append("<h2>My Top 5</h2><ul>" + list + "</ul>")
                .appendTo(document.body);
        } else {
            Top5.popup = jQuery("<div id='top5_popup'></div>");
            Top5.popup
                .append("<a href='#' class='close' onclick='return Top5.hidePopup()'>close</a>")
                .append("<h2>Top 5</h2><p>We are sorry, but we cannot accept any more Top 5 votes for this round from this computer.<br /><br />This is to protect the integrity of the voting process.</p>")
                .appendTo(document.body);
        }
        return false;
    },
    hidePopup: function(){
        Top5.popup.hide();
        return false;  
    },
    showForm: function(){
        Top5.panel.slideDown("fast");
		if (document.body.scrollTop > 500)
        	scrollTo(0,300);
        return false;
    },
    hideForm : function(){
        Top5.reset();
        Top5.panel.slideUp("fast");
        return false;
    },
    deselect: function(){
        jQuery('li.selected', Top5.list).removeClass('selected');
    },
    select: function(link){
        Top5.deselect();
        link.addClass("selected");
        Top5.currentItem = link;
    },
    getListCount: function(){
      return jQuery("li", Top5.list).size();  
    },
    addToList: function(){
        var link = jQuery(this);
        // check if already in list (has class "ranked")
        if (link.hasClass("ranked")) return false;
        // check that we don't already have 5 bands
        if (jQuery('li', Top5.list).size() == 5) return false;
        // adds to list
        Top5.deselect();
        Top5.currentItem = jQuery("<li id='ranked_" + this.id + "' class='selected'>" + link.text() + "</li>");
        Top5.list.append(Top5.currentItem);
        Top5.updateFormFields();
        
        // adds class "ranked"
        link.addClass("ranked");
        return false;
    },
    selectItem: function(){
        var link = jQuery(this);
        Top5.select(link);
    },
    moveUp : function(){
        if (!Top5.currentItem) {
            return false;
        }
        if (Top5.currentItem.prev("li").size() > 0) {
            Top5.currentItem.prev("li").before(Top5.currentItem);
            Top5.updateFormFields();
        }
        return false;
    },
    moveDown : function(){
        if (!Top5.currentItem) {
            return false;
        }
        if (Top5.currentItem.next("li").size() > 0) {
            Top5.currentItem.next("li").after(Top5.currentItem);
            Top5.updateFormFields();
        }
        return false;
    },
    remove : function(){
        if (!Top5.currentItem) {
            return false;
        }
        var id = Top5.currentItem.attr("id").replace(/ranked_/,'');
        jQuery('#' + id).removeClass("ranked");
        Top5.currentItem.remove();
        Top5.currentItem = null;
        Top5.updateFormFields();
        return false;
    },
    updateFormFields : function(){
        Top5.adjustListBackground();
        var fields = jQuery('#top5_fields');
        jQuery('#top5_error').hide();
        fields.html('');
        var params = [], count = 1;
        jQuery('li', Top5.list).each(function(){
            fields.append('<input type="hidden" name="entries[' + count++ + ']" value="' + this.id.replace('ranked_contestant_', '') + '" />');
        });  
    },
    submit : function() {
        // make sure there are 5 artists ranked
        if (jQuery('li', Top5.list).size() != 5) {
            jQuery('#top5_error').show();
            return false;
        } else {
            Top5.updateFormFields();
            return confirm("Are you sure? All votes are final.");
        }
    },
    reset : function() {
        Top5.currentItem = null;
        jQuery('li', Top5.list).remove();
        jQuery('#top5_panel table a.ranked').removeClass("ranked");
        Top5.adjustListBackground();
        return false;
    },
    adjustListBackground : function(){
        Top5.list.css("background-position", "30px " + (Top5.getListCount() * 30) + "px" );
    }
}
