var se = ""

// Event.observe(window, 'load', function(){
//     $$('form').each(function(form) {
//         form.observe('submit', function() {
//             form.getInputs('submit').each(function(submit) {
//                 if (!submit.readAttribute("no_disabled")){
//                     submit.disable();
//                 }
//             });
//         });
//     }); 
// }); 

function enable_comment(id){
    comment=$(id);
    comment.enable();
}

function change_field(field_id, controller_name){
    url = "/" + controller_name + "/change_field"
    url += '?field_id='+field_id
    refresh_div("field_field", url)
}

function refresh_players_list(div, url, serial)
{
    refresh_div(div, url+"?"+serial);
}

function update_players_for_team(div_players, div_hidden, action, team_id, controller, form){
    disable_all_links(true);
    new Ajax.Updater(div_players, "/"+ controller + "/"+action+"/?team_id="+team_id+"&partial=contacts/players_list",{
        asynchronous:false,
        evalScripts:true,
        parameters: Form.serialize(form)
    });
    disable_all_links(true);
    new Ajax.Updater(div_hidden, "/"+ controller + "/" +action+"/?team_id="+team_id+"&partial=contacts/hidden_players",{
        asynchronous:false,
        evalScripts:true,
        parameters: Form.serialize(form)
    });
    disable_all_links(false);
}


function update_players(div_players, div_hidden, action, new_id, controller, form, picture_id){
    disable_all_links(true);
    new Ajax.Updater(div_players, "/"+ controller + "/"+action+"/?contact_id="+new_id+"&partial=contacts/players_list",{
        asynchronous:false,
        evalScripts:true,
        parameters:Form.serialize(form)
    });
    disable_all_links(true);
    new Ajax.Updater(div_hidden, "/"+ controller + "/" +action+"/?contact_id="+new_id+"&partial=contacts/hidden_players",{
        asynchronous:false,
        evalScripts:true,
        parameters:Form.serialize(form)
    });
    disable_all_links(false);
}

function new_mail_player(div_players, div_hidden, mail, form, id){
    disable_all_links(true);
    if (id){        
        new Ajax.Updater(div_players, "/matches/add_mail_player/"+id+"?partial=contacts/players_list&"+mail,{
            asynchronous:false,
            evalScripts:true,
            parameters:Form.serialize(form)
        });
        new Ajax.Updater(div_hidden, "/matches/add_mail_player/"+id+"?partial=contacts/hidden_players&"+mail,{
            asynchronous:false,
            evalScripts:true,
            parameters:Form.serialize(form)
        });
    }
    else{
        new Ajax.Updater(div_players, "/matches/add_mail_player/?partial=contacts/players_list&"+mail,{
            asynchronous:false,
            evalScripts:true,
            parameters:Form.serialize(form)
        });
        new Ajax.Updater(div_hidden, "/matches/add_mail_player/?partial=contacts/hidden_players&"+mail,{
            asynchronous:false,
            evalScripts:true,
            parameters:Form.serialize(form)
        });
    }
    disable_all_links(false);
}

function disable_all_links(disable){    
    e = $('spinner');
    if (disable){        
        e.setStyle({
            display: 'block'
        });
    }else{
        e.setStyle({
            display: 'none'
        });
       /* e.focus();*/
    }
    
    $$('.play_link').each(function(element){
        if (disable){
            element.setStyle({
                display: 'none'
            });
        }else{
            element.setStyle({
                display: 'block'
            });
        }
    });
}

function refresh_div(div, url){
    new Ajax.Updater(div, url, {
        asynchronous:true,
        evalScripts:true
    })
}

var Invitations = {
    check_or_uncheck: function(){
        $$('.check_mail').each(function(element){
            element.checked = $('enable').checked;
        });
        Invitations.check_and_add_emails();
    },
    check_and_add_emails: function(){
        $('email_to').setValue("");
        
        $$('.check_mail').each(function(element){
            if (element.checked){
                if ($('email_to').getValue() != ""){
                    $('email_to').setValue($('email_to').getValue() + ",");
                }
                $('email_to').setValue($('email_to').getValue() + element.getValue());
            }
        });
    },
    init: function(){
        $$('.check_mail').each(function(element){
            Event.observe(element, 'click', Invitations.check_and_add_emails.bindAsEventListener());  
        });
        Event.observe('enable', 'click', Invitations.check_or_uncheck.bindAsEventListener())
       
        Invitations.check_and_add_emails();
    }
}

var Teams = {
    showActions: function(event) {
        var element = event.element();
        var dl = null;
        
        if(element.match("li"))
            dl = element;
        else {
            dl = element.up("#contacts li");
        }
                
        var options = dl.down("div.options_container");
        if(options){
            dl.addClassName("active");
            options.show(); 
        }
    },        
    hideActions: function(event) {
        var element = event.element();
        var dl = null;
        
        if(element.match("li")){
            dl = element;
        }else{
            dl = element.up("#contacts li")
        }
                
        dl.removeClassName("active");
        var options = dl.down("div.options_container");
        options.hide();
    },   
    init: function(){
        $$('#contacts li').invoke("observe", 'mouseover', Teams.showActions.bindAsEventListener());
        $$('#contacts li').invoke("observe", 'mouseout', Teams.hideActions.bindAsEventListener());
    }
}

var Contacts = {
    showActions: function(event) {
        var element = event.element();
        var dl = null;
        
        if(element.match("li"))
            dl = element;
        else {
            dl = element.up("#contacts li");
        }
                
        var options = dl.down("div.options_container");
        if(options){
            dl.addClassName("active");
            options.show(); 
        }
    },        
    hideActions: function(event) {
        var element = event.element();
        var dl = null;
        
        if(element.match("li")){
            dl = element;
        }else{
            dl = element.up("#contacts li")
        }
                
        dl.removeClassName("active");
        var options = dl.down("div.options_container");
        options.hide();
    },   
    init: function(){
        $$('#contacts li').invoke("observe", 'mouseover', Contacts.showActions.bindAsEventListener());
        $$('#contacts li').invoke("observe", 'mouseout', Contacts.hideActions.bindAsEventListener());
    }
}

var General = {
    change_language: function(path){
        window.parent.location=path+"?language=" + $('language').getValue() + "&back=" + window.parent.location;
    },
    show_message_on_down: function(element_id, message, options){
        defaultOptions = {
            noteClass: options.noteClass || "note_div",
            mouseoverClass: options.mouseoverClass || "display",
            mouseoutClass: options.mouseoutClass || "no_display",
            offset: options.offset || 3
        }
        
        element = $(element_id);
        div = new Element('div', {
            style: "position: relative"
        });
        
        element.insert({
            before: div
        });
        div.insert({
            top: element
        });
        
        element_height = element.getHeight() + defaultOptions.offset;
        element_width = element.getWidth();
        
        message_div = new Element('div');
        message_div.addClassName(defaultOptions.noteClass);
        message_div.addClassName(defaultOptions.mouseoutClass);
        message_div.setStyle({
            position: "absolute",
            left: "0px",
            top: element_height + "px",
            width: element_width + "px"
        });
        
        label_message = new Element('label');
        label_message.insert({
            top: message
        })
        
        message_div.insert({
            top: label_message
        })
        
        div.insert({
            bottom: message_div
        });
        
        Event.observe(element, "mouseover", General.showMessage.bindAsEventListener(null, message_div, defaultOptions));
        Event.observe(element, "mouseout", General.hideMessage.bindAsEventListener(null, message_div, defaultOptions));
        Event.observe(element, "keypress", General.hideMessage.bindAsEventListener(null, message_div, defaultOptions));
    },
    showMessage: function(event, elementToShow, defaultOptions){
        elementToShow.removeClassName(defaultOptions.mouseoutClass);
        elementToShow.addClassName(defaultOptions.mouseoverClass);
    },
    hideMessage: function(event, elementToHide, defaultOptions){
        elementToHide.removeClassName(defaultOptions.mouseoverClass);
        elementToHide.addClassName(defaultOptions.mouseoutClass);
    },
    change_text_when_is_empty: function(input_id, initial_text, hide_opacity){
        input = $(input_id);
        text = initial_text;
        input.writeAttribute("initial_text", text);
        
        input.observe("focus", function(event){
            if ($(input_id).getValue() == initial_text){
                $(input_id).setValue("");
                
                if (!hide_opacity){
                    $(input_id).setStyle({
                        opacity: 1.0
                    });
                }
            }
        });
        input.observe("blur", function(event){
            if ($(input_id).getValue() == ""){
                $(input_id).setValue(initial_text);
                
                if (!hide_opacity){
                    $(input_id).setStyle({
                        opacity: 0.6
                    });
                }
            }
        });
        input.setValue(initial_text)
        
        if (!hide_opacity){
            input.setStyle({
                opacity: 0.6
            });
        }
    }
    ,
    showIfInArea: function(elementArea, elementToShow){
        elementToShow.hide();
        //Event.observe(window, 'load', function() {
        var element_to_show_hover = elementToShow;
        var element = elementArea;
        
        //bounds = element.getBoundingClientRect();
        elementOffset = element.cumulativeOffset();
        bounds = {
            left: elementOffset.left,
            top: elementOffset.top,
            right: elementOffset.left + element.getWidth(),
            bottom: elementOffset.top + element.getHeight()
        }
        
        width = element_to_show_hover.getWidth();
        element_width = element.getWidth() - 1;
        element_height = element.getHeight() - 1;
            
        element.observe('mouseover', function(event){
            element_to_show_hover.show();
        });
            
        element.observe('mouseout', function(event){
            if (!(bounds.left < event.pointerX() && bounds.right > event.pointerX() && bounds.top < event.pointerY() && bounds.bottom > event.pointerY())){
                element_to_show_hover.hide();
            }
        });
            
        element_to_show_hover.observe('mouseout', function(){
            element_to_show_hover.hide();
        });
        //});
//    },
//    show_if_in_area: function(element_id, id){
//        $(id).hide();
//        Event.observe(window, 'load', function() {
//            element_to_show_hover = $(id);
//            element = $(element_id);
//
//            //bounds = element.getBoundingClientRect();
//            elementOffset = element.cumulativeOffset();
//            bounds = {
//                left: elementOffset.left,
//                top: elementOffset.top,
//                right: elementOffset.left + element.getWidth(),
//                bottom: elementOffset.top + element.getHeight()
//            }
//
//            width = element_to_show_hover.getWidth();
//            element_width = element.getWidth() - 1;
//            element_height = element.getHeight() - 1;
//
//            //element_to_show_hover.setStyle({position: 'absolute', top: bounds.top + 1, left: bounds.left + (element_width - width), background: 'white'})
//
//            Event.observe(element, 'mouseover', function(event){
//                element_to_show_hover.show();
//            });
//
//            Event.observe(element, 'mouseout', function(event){
//                if (!(bounds.left < event.pointerX() && bounds.right > event.pointerX() && bounds.top < event.pointerY() && bounds.bottom > event.pointerY())){
//                    element_to_show_hover.hide();
//                }
//            });
//
//            Event.observe(element_to_show_hover, 'mouseout', function(){
//                element_to_show_hover.hide();
//            });
//        });
    }
}

var UserMessages = {
    removeElement: function(event, id){
        $('user_div_' + id).remove();
    },
    afterUpdateFunction: function(element, value){
        selected_item = null;
        
        ($('user_name_auto_complete').childElements()[0]).childElements().each(function(element){ 
            if (element.className == "selected"){
                selected_item = element
            }
        })
        
        if (selected_item != null){
            hidden_tag = $('empty_user').cloneNode(false);
            
            if (selected_item.readAttribute("identifier") == "User"){
                hidden_tag.id = "users[" + selected_item.id + "]"
                hidden_tag.name = "users[" + selected_item.id + "]"
            }else{
                hidden_tag.id = "teams[" + selected_item.id + "]"
                hidden_tag.name = "teams[" + selected_item.id + "]"
            }
        
            div = document.createElement("div");
            
            link = document.createElement("a");
            link.href = "#";
            link.innerHTML = "(x)";
            $(link).observe("click", UserMessages.removeElement.bindAsEventListener(null, selected_item.id));
            
            span = document.createElement("span");
            span.innerHTML = element.value + " ";
            span.insert({
                bottom: link
            });
            span.addClassName("user_list");
            
            div.id = "user_div_" + selected_item.id
            div.insert({
                top: hidden_tag
            });
            div.insert({
                top: span
            });
            div.addClassName("inline_display");
        
            $('users_input').insert({
                top: div
            });
            $('user_name').setValue('');
        }
    }    
}

//  Version beta para partidos

var MatchesAndTeams = {
    removeFromPlayersList: function(event){
        element = event;
        element.ancestors()[4].remove();
        $("users_id["+element.next().id+"]").remove();
    },    
    copyToPlayersList: function(id){
        if ($("player_id["+id+"]") == null){
            contact = $("contact_" + id);
        
            picture_clone = contact.getElementsBySelector("td")[0].cloneNode(true);
            text_clone = contact.getElementsBySelector("td")[1].cloneNode(true);
        
            //$('empty_image_picture').replace(picture_clone);
            node = $('contacts').childElements().last().cloneNode(true);
            node.setStyle({
                display: "block"
            })
        
            td_nodes = node.getElementsBySelector("td");
        
            picture = td_nodes[1].getElementsBySelector("img.small_photo_image")[0];
            picture.replace(picture_clone);
            td_nodes[0].insert({
                bottom: new Element("input", {
                    type: "hidden",
                    id: id
                })
            });
        
            text_id_and_name = "player_id["+id+"]";
            text = td_nodes[2];        
            text.insert({
                top: text_clone
            });
            text.insert({
                bottom: new Element("input", {
                    type: "hidden",
                    id: text_id_and_name,
                    name: text_id_and_name
                })
            });
        
            $('contacts_players').insert({
                bottom: node
            });
            
            hidden_id_and_name = "users_id["+id+"]";
            $('hidden_players').insert({
                bottom: new Element("input", {
                    type: "hidden",
                    id: hidden_id_and_name,
                    name: hidden_id_and_name
                })
            });
        }
    }
}

var MatchFeedback = {
    copyImageToComent: function(td_id, user_id, is_ajax){
        if (is_ajax){
            MatchFeedback.copyImageToCommentAction(td_id, user_id);
        }else{
            Event.observe(window, 'load', function(){
                MatchFeedback.copyImageToCommentAction(td_id, user_id)
            });
        }
    },
    copyImageToCommentAction: function(td_id, user_id){
        player_image = $('player_' + user_id).getElementsBySelector("img.small_photo_image")[0];
        clone_image = player_image.cloneNode(true);
        clone_image.setStyle({
            width: "50px",
            height: "50px"
        });
        
        $(td_id).insert({
            top: clone_image
        });
    },
    setTeamName: function(event, team){
        content = ""
        element = event.element();
        
        if (element.getValue() == ""){
            content = element.readAttribute("defaultValue");
        }else{
            content = element.getValue();
        }
        
        team.highlight();
        team.textContent = content;
    },
    movePlayerToTeam: function(player_id, container_team_id){
        player = $("player_" + player_id);
        container_team = $(container_team_id);
        
        hidden = player.getElementsBySelector('input[type=hidden]')
        if (hidden.size() > 0){
            hidden[0].remove();
        }
        
        name = container_team_id + "_player["+ player_id +"]"
        player.insert({
            top: new Element("input", {
                type: "hidden",
                name: name
            })
        });
        
        if (container_team_id == "team_1"){
            player.getElementsBySelector('td')[0].hide();
            player.getElementsBySelector('td')[2].show();
        }else if (container_team_id == "team_2"){
            player.getElementsBySelector('td')[2].hide();
            player.getElementsBySelector('td')[0].show();
        }
        
        contact_divider = player.getElementsBySelector(".contacts_divider");
        if (contact_divider.size() > 0){
            contact_divider[0].remove();
        }
        
        if (container_team.getElementsBySelector("li").size() > 0){
            div = new Element("div");
            div.addClassName("clearfix");
            div.addClassName("contacts_divider");
            
            player.insert({
                top: div
            })
        }
        
        container_team.insert({
            bottom: player
        });
    },
    init: function(){
        Event.observe(window, 'load', function() {
            team1 = $('team_1[name]');
            team2 = $('team_2[name]');
            team1Title = $('team_title_1');
            team2Title = $('team_title_2');
        
            team1.writeAttribute("defaultValue", team1Title.textContent);
            team2.writeAttribute("defaultValue", team2Title.textContent);
        
            team1.observe('blur', MatchFeedback.setTeamName.bindAsEventListener(null, team1Title));
            team2.observe('blur', MatchFeedback.setTeamName.bindAsEventListener(null, team2Title));
        });
    }
}

// La idea de esto es resalta el boton de buscar
var InitalActions = {
    searchInFaltaUno: function(event){
        element = $('search_name');
        if (element.readAttribute("initial_text") != element.getValue()){
            $('search_in_faltauno').submit();
        }
    },
    changeSearchRightToOver: function(event){
        element = event.element();
        element.addClassName("in");
    },
    changeSearchRightToOut: function(event){
        element = event.element();
        element.removeClassName("in");
    },
    init: function(){
        searchRight = $$('.menu_search_right')[0];
        
        if (!(Object.isUndefined(searchRight))){
            searchRight.observe('mouseover', InitalActions.changeSearchRightToOver.bindAsEventListener());
            searchRight.observe('mouseout', InitalActions.changeSearchRightToOut.bindAsEventListener());
            searchRight.observe('click', InitalActions.searchInFaltaUno.bindAsEventListener());
        }
    }
}

Event.observe(window, 'load', InitalActions.init.bindAsEventListener());

var ModalboxHelper = {
    hide: function(){
        Modalbox.hide();
    }
}

var AjaxMessagePanelHelper = {
    notice: function(notice_message){
        defaultOptions = {
            insertAfter: $$('.table_title')[0] || $$('.title')[0],
            className: 'success',
            id: 'flash'
        }
        
        if ($(defaultOptions.id) != null){
            $(defaultOptions.id).remove();
        }
        
        div = new Element("div", {
            id: defaultOptions.id
        });
        div.addClassName(defaultOptions.className);
        
        link = new Element("a");
        link.addClassName("close");
        link.writeAttribute("href", "javascript:void(0);");
        link.innerHTML = "x"
        link.observe("click", function(){
            document.getElementById(defaultOptions.id).style.display = 'none';
        });
        
        div.insert({
            bottom: link
        });
        div.insert({
            bottom: notice_message
        });
        
        if (defaultOptions.insertAfter == document.body){
            defaultOptions.insertAfter.insert({
                top: div
            });
        }else{
            defaultOptions.insertAfter.insert({
                after: div
            });
        }
        
    },
    error: function(error_message){
        defaultOptions = {
            insertAfter: $$('.table_title')[0] || $$('.title')[0],
            className: 'error',
            id: 'flash'
        }
        
        if ($(defaultOptions.id) != null){
            $(defaultOptions.id).remove();
        }
        
        div = new Element("div", {
            id: defaultOptions.id
        });
        div.addClassName(defaultOptions.className);
        
        link = new Element("a");
        link.addClassName("close");
        link.writeAttribute("href", "javascript:void(0);");
        link.innerHTML = "x"
        link.observe("click", function(){
            document.getElementById(defaultOptions.id).style.display = 'none';
        });
        
        div.insert({
            bottom: link
        });
        div.insert({
            bottom: error_message
        });
        
        if (defaultOptions.insertAfter == document.body){
            defaultOptions.insertAfter.insert({
                top: div
            });
        }else{
            defaultOptions.insertAfter.insert({
                after: div
            });
        }
    }
}

var Cookies = {
    add: function(name, value, expireDays){
        var cookieTemplate = new Template('#{name}=#{value};#{aditional};path=/');
        var expireDate = new Date();
        expireDate.setDate(expireDate.getDate() + expireDays);
        
        
        document.cookie= cookieTemplate.evaluate({
            name: name, 
            value: escape(value), 
            aditional: ((expireDate == null) ? "" : "expires="+expireDate.toGMTString())
        });
    },
    get: function(name){
        cookies = document.cookie;
        if (cookies.length > 0){
            
            cookieStartIndex = Cookies._startCookieIndex(name);
            cookieEndIndex = Cookies._endCookieIndex(name);
            
            if (cookieStartIndex != -1 && cookieEndIndex != -1){
                return unescape(cookies.substring(cookieStartIndex,cookieEndIndex));
            }
        }
        return null;
    },
    _startCookieIndex: function(name){
        cookies = document.cookie;
        
        if (cookies.length > 0){
            return cookies.indexOf(name + "=");            
        }
        
        return -1;
    },
    _endCookieIndex: function(name){
        startCookieIndex = Cookies._startCookieIndex(name);
        
        if (startCookieIndex != -1){
            cookieStartIndex = cookieStartIndex + name.length + 1;
            cookieEndIndex = cookies.indexOf(";",cookieStartIndex);
            
            if (cookieEndIndex == -1){
                cookieEndIndex = cookies.length;  
            }
            
            return cookieEndIndex;
        }
        
        return  -1;
    }
}

var BoxesHelper = {
    show: function(box_id, ignore_effect){
        element = $(box_id);
        show = $("show_" + box_id);
        hide = $("hide_" + box_id);

        if (element.readAttribute("is_working") == "false"){
            show.hide();
            hide.show();
        }
        
        if (ignore_effect){
            element.show();
        }else{
            if (element.readAttribute("is_working") == "false"){
                element.writeAttribute("is_working", "true");
                window.setTimeout(function(){
                    element.writeAttribute("is_working", "false");
                }, 500);
            
                element.appear({
                    duration: 0.3,
                    from: 0.0,
                    to: 1.0
                });
                element.blindDown({
                    duration: 0.5
                });
            }
        }
        
        Cookies.add(box_id, "show", 360);
    },
    hide: function(box_id, ignore_effect){
        element = $(box_id);
        show = $("show_" + box_id);
        hide = $("hide_" + box_id);

        if (element.readAttribute("is_working") == "false"){
            show.show();
            hide.hide();
        }
        
        if (ignore_effect){
            element.hide();
        }else{
            if (element.readAttribute("is_working") == "false"){
                element.writeAttribute("is_working", "true");
                window.setTimeout(function(){
                    element.writeAttribute("is_working", "false");
                }, 500);
                
                element.fade({
                    duration: 0.5
                });
                element.blindUp({
                    duration: 0.5
                });
            }
        }
        
        Cookies.add(box_id, "hide", 360);
    },
    init: function(box_id){
        $(box_id).writeAttribute("is_working", "false");
        
        if (Cookies.get(box_id) == "hide"){
            BoxesHelper.hide(box_id, true);
        }else if (Cookies.get(box_id) == "show"){
            BoxesHelper.show(box_id, true);
        }
    }
}

CheckBoxStyler = {
    style: function(id){
        Event.observe(window, 'load', function() {
            element = $(id);
            element.hide();
        
            div = new Element("div");
            div.addClassName("input_check_emulator");
            
            if (element.checked){
                div.addClassName("input_check_emulator_check");
            }
            
            div.observe("click", CheckBoxStyler.handleClick.bindAsEventListener(null, div, element));
        
            $(id).insert({
                after: div
            })
        });
    },
    handleClick: function(event, showElement, originalElement){
        
        condition = !originalElement.checked;
        
        if (condition){
            originalElement.checked = true;
            originalElement.setValue(1);
            showElement.addClassName("input_check_emulator_check");
        }else{
            originalElement.checked = false;
            originalElement.setValue(0);
            showElement.removeClassName("input_check_emulator_check");
        }
    }
}

var Settings = {
    hideOrShowPassword: function(){
        passwordDiv = $('user_new_password');        
        
        if (passwordDiv.visible()){
            passwordDiv.hide();
        }else{
            passwordDiv.show();
        }
    },
    hideOrShowNotifications: function(){
        notificationsDiv = $('user_notifications');        
        
        if (notificationsDiv.visible()){
            notificationsDiv.hide();
        }else{
            notificationsDiv.show();
        }
    },
    hideOrShowEditProfile: function(){
        editProfileDiv = $('user_profile');        
        
        if (editProfileDiv.visible()){
            editProfileDiv.hide();
        }else{
            editProfileDiv.show();
        }
    },
    init: function(){
        Event.observe('change_password', 'click', Settings.hideOrShowPassword.bindAsEventListener());
        Event.observe('edit_profile', 'click', Settings.hideOrShowEditProfile.bindAsEventListener());
        Event.observe('change_notifications', 'click', Settings.hideOrShowNotifications.bindAsEventListener());
        Settings.hideOrShowPassword();
        Settings.hideOrShowEditProfile();
        Settings.hideOrShowNotifications();
    }
}

var Dashboard = {
    init: function(){
        $$('div.dashboard div.feed_item').each(function(element){
            actions = element.getElementsBySelector('div.actions')[0];
            
            if (actions != null){
                General.showIfInArea(element, actions);
            }
        });
    }
}
var Sex = {
    hideInputsSexF: function(){
        radioM=$('user_sex_m');
        inputWeight_f = $('weight_f');
        inputHeight_f = $('height_f');
        inputPosition_f = $('position_f');
        radioF=$('user_sex_f');
        inputWeight_m = $('weight_m');
        inputHeight_m = $('height_m');
        inputPosition_m = $('position_m');
        inputWeight_f.hide();
        inputWeight_f.disable();
        inputHeight_f.hide();
        inputHeight_f.disable();
        inputPosition_f.hide();
        inputPosition_f.disable();
        inputWeight_m.show();
        inputWeight_m.enable();
        inputHeight_m.show();
        inputHeight_m.enable();
        inputPosition_m.show();
        inputPosition_m.enable();
    },
    hideInputsSexM: function(){
        radioF=$('user_sex_f');
        inputWeight_m = $('weight_m');
        inputHeight_m = $('height_m');
        inputPosition_m = $('position_m');
        radioM=$('user_sex_m');
        inputWeight_f = $('weight_f');
        inputHeight_f = $('height_f');
        inputPosition_f = $('position_f');
        inputWeight_m.hide();
        inputWeight_m.disable();
        inputHeight_m.hide();
        inputHeight_m.disable();
        inputPosition_m.hide();
        inputPosition_m.disable();
        inputWeight_f.show();
        inputWeight_f.enable();
        inputHeight_f.show();
        inputHeight_f.enable();
        inputPosition_f.show();
        inputPosition_f.enable();
    },
    init: function(){
        Sex.hideInputsSexF();
        Event.observe('user_sex_f', 'click', Sex.hideInputsSexM.bindAsEventListener());
        Event.observe('user_sex_m', 'click', Sex.hideInputsSexF.bindAsEventListener());
        
    }
}

var Championship = {
    showNewTeam: function(){
        element = $('link_new_team').up();
        other_element = $('link_existing_teams').up();

        if (element.hasClassName("inactive_find")){
            existing_teams = $('existing_teams');
            new_team = $('new_team');

            existing_teams.blindUp({
                duration: 0.5
            });
            new_team.blindDown({
                duration: 0.5
            });

            element.removeClassName("inactive_find");
            element.addClassName("active_find");

            other_element.removeClassName("active_find");
            other_element.addClassName("inactive_find");
        }
    },
    showExistingTeams: function(){
        element = $('link_existing_teams').up();
        other_element = $('link_new_team').up();

        if (element.hasClassName("inactive_find")){
            existing_teams = $('existing_teams');
            new_team = $('new_team');

            existing_teams.blindDown({
                duration: 0.5
            });
            new_team.blindUp({
                duration: 0.5
            });

            element.removeClassName("inactive_find");
            element.addClassName("active_find");
            
            other_element.removeClassName("active_find");
            other_element.addClassName("inactive_find");
        }
    },
    init: function(){
        Actions.showOrHideSpinner();
        Actions.showOrHideElement('new_team');
        
        Event.observe('link_existing_teams', 'click', Championship.showExistingTeams.bindAsEventListener());
        Event.observe('link_new_team', 'click', Championship.showNewTeam.bindAsEventListener());

    }
}

var MatchChampionshipFeedback = {
    movePlayerToGame: function(player_container_id, container_team_id){
        player = $(player_container_id);
        container_team = $(container_team_id);

        elements = player.getElementsBySelector("td")[1];
        enable_input = elements.getElementsBySelector("input")[0].enable();
        elements.show();

        player.getElementsBySelector("td.player_cards")[0].show();
        player.getElementsBySelector("div.actions.cards")[0].show();
        player.getElementsBySelector("a.action.remove")[0].show();
        player.getElementsBySelector("a.action.add")[0].hide();

        container_team.insert({
            top: player
        });
    },
    movePlayerOutOfGame: function(player_container_id, container_team_id){
        player = $(player_container_id);
        container_team = $(container_team_id);

        elements = player.getElementsBySelector("td")[1];
        enable_input = elements.getElementsBySelector("input")[0].disable();
        elements.hide();

        player.getElementsBySelector("td.player_cards")[0].hide();
        player.getElementsBySelector("div.actions.cards")[0].hide();
        player.getElementsBySelector("a.action.remove")[0].hide();
        player.getElementsBySelector("a.action.add")[0].show();

        player.getElementsBySelector("tr.player_cards")[0].descendants().each(function(element){
            element.remove();
        })

        container_team.insert({
            top: player
        });
    },
    addYellowCard: function(container_id, team_id, player_id){
        container = $(container_id);
        
        yellow_cards = container.getElementsBySelector("input.yellow_card");
        red_cards = container.getElementsBySelector("input.red_card");

        if (red_cards.length == 0){
            if (yellow_cards.length == 1 ){
                yellow_cards[0].up().remove();
                MatchChampionshipFeedback.addRedCard(container_id, team_id, player_id);
            } else {
                yellow_card_container = new Element("div").addClassName("line_display");
                
                yellow_card_hidden = new Element("input", {
                    type: "hidden",
                    name: team_id + "[" + player_id + "][cards][]",
                    value: "1"
                });
                yellow_card_hidden.addClassName("yellow_card");

                yellow_card = $('player_yellow_card').cloneNode(false).show();

                yellow_card_container.insert({
                    top:  yellow_card_hidden
                }).insert({
                    top: yellow_card
                });
            
                container.insert({
                    bottom: yellow_card_container
                });
            }
        }
    },
    addRedCard: function(container_id, team_id, player_id){
        container = $(container_id);

        yellow_cards = container.getElementsBySelector("input.yellow_card");
        red_cards = container.getElementsBySelector("input.red_card");

        if (red_cards.length == 0){
            red_card_container = new Element("div").addClassName("line_display");

            red_card_hidden = new Element("input", {
                type: "hidden",
                name: team_id + "[" + player_id + "][cards][]",
                value: "2"
            });
            red_card_hidden.addClassName("red_card");

            red_card = $('player_red_card').cloneNode(false).show();

            red_card_container.insert({
                top:  red_card_hidden
            }).insert({
                top: red_card
            });

            container.insert({
                bottom: red_card_container
            });
        }
    }
}

var Actions = {
    showOrHideSpinner: function(id){
        if (id == null){
            Actions.showOrHideElement('spinner');
        }else{
            Actions.showOrHideElement(id);
        }
    },
    showOrHideElement: function(element_id){
        element = $(element_id);

        if (element.visible()){
            element.hide();
        }else{
            element.show();
        }
    }
}

var Requests = {
    lockInvitation: function(class_name){
        var classSelector = "." + class_name
        $$(classSelector).each(function(element){
            element.hide();
        });
    }
}

var Rankings = {
    saveStartBox: function(event, path, item_id, update, update_element){
        if (update){
            new Ajax.Updater(update_element, path, {
                parameters: {
                    "rank[value]": event.memo.rated,
                    "rank[item_id]": item_id,
                    "is_update": true
                }
            });
        }else{
            new Ajax.Request(path, {
                parameters: {
                    "rank[value]": event.memo.rated,
                    "rank[item_id]": item_id,
                    "is_update": false
                }
            });
        }
    },
    setStartBox: function(ranked_element, validator, options){
        new Starbox(ranked_element, validator, {
            stars: options.stars || 10,
            buttons: options.buttons || 10,
            max: options.max || 10,
            rerate: options.rerate || true,
            locked: options.locked || false
        });
    },
    initStartBox: function(is_ajax, ranked_element, validator, options){
        if (is_ajax){
            Rankings.setStartBox(ranked_element, validator, options);
        }else{
            Event.observe(window, 'load', function() {
                Rankings.setStartBox(ranked_element, validator, options);
            });
        }
    },
    initStartRequests: function(ranked_element, path, item_id, update, update_element){
        $(ranked_element).observe('starbox:rated', Rankings.saveStartBox.bindAsEventListener(null, path, item_id, update, update_element));
    }
}
