var selection = {};

var lottery = {
    init: function(){	 

        $('#formEvents').submit(function(){

            $length = 0;
            for(var k in selection){
                $length ++;
                break;
            }

            if( $length == 0 ){
                alert("Please select at least one performance for the Free Night Lottery");
                return false;
            }

            return true;
        });

        //manage entry tab hide/show    
        $('div.widget-tab #button').toggle(function(event){
            $('div.widget-tab').each(function(){
                $(this).animate(
                {
                    right: -190,
                    opacity: .4
                },
                1000,
                '',        
                function(){
                    $('div.widget-tab #button').html("&laquo;&laquo;");
                });

            });
        },
        function(event){
            $('div.widget-tab').each(function(){
                $(this).animate(
                {
                    right: 0,
                    opacity: 1
                },
                1000,
                '',        
                function(){
                    $('div.widget-tab #button').html("&raquo;&raquo;");
                }
                );
            });
        });        

        $('#main').prepend('<div id="reason"></div>');

        lottery.rebuild_behavior();
    },

    rebuild_behavior: function(){

        $('#reason:visible').hide();

        //update all checkboxes with data from variable 'selection'       
        $('div.checkbox').removeClass('checked');
        for (var k in selection) {
            $('input.rating[name='+k+'][value='+selection[k].rank+']').attr('checked','checked');
        }

        $('input.rating[type=radio]').click(function(e){

            var name=$(e.target).attr('name');
            var re=new RegExp("[0-9]+\-[0-9]+\-[0-9]+", "g");
            key = re.exec(name);

            $('#reason:visible').hide();

            var pos = $(this).parent().parent().position();
            var left = $(this).width();
            var key = key[0].split('-');
            
            var value = $(e.target).val();

            for (var k in selection) {
                var re=new RegExp("[0-9]+\-[0-9]+\-[0-9]+", "g");
                selection_key = re.exec(k);
                var index = selection_key[0].split('-');

                var title = selection[k].title;
                var datetime = selection[k].date;
                if (k != name) {
                    if (key[0] == index[0])                 
                        var reason = 'of the same show';
                    else if (key[2] == index[2]) 
                        var reason = 'at the same time';
                    else if (selection[k].rank == value) 
                        var reason = 'with the same rating';
                }

                if (key[0] == index[0] || key[2] == index[2] || selection[k].rank == value) {                                    

                    if (k != name) {
                            $('#reason').html('We have de-selected your entry for <b>'+title+'</b> at <b>'+datetime+'</b> because you selected another performance '+reason+' as this entry.');
                            $('#reason').css({'top': pos.top + 0, 'left': $(this).parent().position().left+$(this).parent().innerWidth()});   
                            $('#reason').slideDown();
                    }

                    delete selection[k];
                    $('input#'+k).val('');
                    $('input.rating[name='+k+']').removeClass('checked').attr('checked',false);
                }

            }

            $ret = check_radio($(this));
            if($ret == true)
            {
                delete selection[name];
            }
            else
            {
                $(this).addClass('checked');
                //index = name.split("-");
                var title = $('h4[id=title-rating-'+key[0]+'] > a > span:eq(0)').text();
                var datetime = $('#date-rating-'+key[0]+'-'+ key[1]+'-'+ key[2]).text();
                var fnot = $('#hidden-rating-'+key[0]+'-'+ key[2]).val();
                selection[name] = {
                    "title": title,
                    "date": datetime,
                    "rank": value,
                    "fnot": fnot
                };              
            }

            //update all checkboxes with data from variable 'selection' after selection was changed
            $('input.rating[type=radio]').attr('checked',false).removeClass('checked');
            for (var k in selection) {
                $('input.rating[name='+k+'][value='+selection[k].rank+']').attr('checked',true).addClass('checked');
            }
            //apdate entry tab on page load after selection was changed
            update_entry_tab();
        });
    }
};

/**
 * function used to de-select a radio button
 */
function check_radio(elem)
{
    $radio_status_elem = document.getElementById('radio_status');
    $val = elem.attr('name')+"_"+elem.attr('value');
    $status = $radio_status_elem.value;

    if($val == $status)
    {
        elem.attr('checked', false);
        $radio_status_elem.value = '';
        return true;
    }
    else
    {
        $radio_status_elem.value = $val;
    }

    return false;
}

/**
* Updating entry tab with selected entries
*/
function update_entry_tab(){
    $('div.widget-tab li').html('<span>&lt;<i>please select an entry</i>&gt;</span>').unbind('click').unbind('mouseover').unbind('mouseout');

    var $fields = $('#formEvents input[type=hidden]');
    var $index = 0;

    $fields.val('');
    $fields.attr('name', '');

    for (var k in selection) {
        var title = selection[k].title;        
        var datetime = selection[k].date;
        //alert(k+" "+selection[k].rank);
        $('div.widget-tab #'+selection[k].rank).html("<a href='#"+k+"'><b>"+title+'</b><br>'+datetime+'</a>');
        $('div.widget-tab #'+selection[k].rank).bind('mouseover',function(event){
            this.style.textDecoration='underline';
            this.style.cursor='pointer';
        }).bind('mouseout',function(event){
            this.style.textDecoration='none';
            this.style.cursor='';
        });
        

        // update hidden fields
        $fields.eq($index).attr('name', "rating[" + selection[k].fnot + "]");
        $fields.eq($index).val(selection[k].rank);

        $index ++;

    }
}

$(document).ready(function() {
    lottery.init();
});

