﻿/* Handle the retrieval of WYSIWYG via AJAX on the competition page tabs */
function Competition_SetupTabs() {
    $("#competitionTabs ul li a").live("click", function(e) {
        e.preventDefault();
    
        var mode = '';
        $(this).parents("ul").find("li").removeClass('selected');
        $(this).parent("li").addClass('selected');
        
        switch($(this).attr("id"))
        {
            case 'aJudging':
              mode = 'Judging';
              break;
            case 'aPrizes':
              mode = 'Prizes';
              break;
            case 'aRules':
              mode = 'Rules';
              break;
            default:
                return;
        }
        
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "?GetContent=true&mode=" + mode,
            success: function(data) {
                if (data.WYSIWYGContent) {
                    $('#competitionWYSIWYG').html(data.WYSIWYGContent);
                    $("#competitionWYSIWYG").scrollTop(0);
                }
            }
        });
        
    });
}

function Competition_SetupEntrySelection() {
   
    $('#ulSelectedEntries li .remove').live("click", function(e) {
        /* Delete the image */
        e.preventDefault(); 
        var clickedLi = $(this).parent();
        var primaryImgLogo = clickedLi.find('img.primary');
        if(primaryImgLogo.length)
        {
            primaryImgLogo.remove();
        }
        clickedLi.empty();
    
        /* Bubble any empty list items to the end of the list */
        var container = $('#ulSelectedEntries');
        var listItems = $('#ulSelectedEntries li');
        listItems.removeClass('first');
        
        listItems.each(function(index, value) {
            var li = $(this);
            if(li.html().trim() == '')
            {
                if(clickedLi.attr("id") == li.attr("id"))
                {
                    li.remove();
                    container.append(li);
                    Competition_StyleListItems();
                    container.find("li:first").append(primaryImgLogo);
                }
            }
        });
    });
    
    $('#ulSelectedEntries li .makePrimary').live("click", function(e) {
        e.preventDefault(); 
        
        var clickedLi = $(this).parent();
        var img = $('#ulSelectedEntries li img.primary');
        var primaryLi = img.parent();
        img.remove();
        clickedLi.append(img);
    });
}

function Competition_StyleListItems()
{
    var listItems = $('#ulSelectedEntries li');
    var intLoop = 0;
    listItems.each(function() {
        var li = $(this);
        if((intLoop==0)||(intLoop/5==1))
        {
            li.addClass('first');
        }
        intLoop++;
    });
}


$(document).ready(function() {
    Competition_SetupTabs();    
    Competition_SetupEntrySelection();
});
