/************************
 *	 Admin		*
 ************************/
function open_add(){
    $(function(){
        $(".button:first").click(function(){
        
            var element = $(this);
            
            $("#add_article").slideToggle(300);
            
            return false;
        });
    });
};


function delete_article_div(page_loc){
    $('.delete_update').live("click", function(){
        var ID = $(this).attr("id");
        var dataString = 'msg_id=' + ID;
        
        if (confirm("Sure you want to delete this update? There is NO undo!")) {
            $.ajax({
                type: "POST",
                url: "/" + page_loc + "/delete_article.php",
                data: dataString,
                cache: false,
                success: function(html){
                    $("#_"+ID).fadeOut('slow', function(){
                        $("#_"+ID).remove();
                        $("#edit_place_edit_"+ID).remove();

                    });		
                }
            });
        }
        return false;
    });
}

function delete_article_tr(page_loc){
    $('.delete_update').live("click", function(){

        var ID = $(this).attr("id");
        
        var dataString = 'msg_id=' + ID;

        if (confirm("Deleting this club will not only delete the club but also all results, fixtures, stats. EVERYTHING there is no undo. Are you sure you want to contiune with this request?")) {

            $.ajax({
                type: "POST",
                url: "/" + page_loc + "/delete_article.php",
                data: dataString,
                cache: false,
                success: function(html){
                    $("#" + ID).fadeOut('fast', function(){
                        $(this).closest('tr').fadeOut();
                        $(this).closest('tr').remove();
                    });
                    $("#edit_place_edit" + ID).fadeOut('fast', function(){
                        $(this).closest('tr').fadeOut();
                        $(this).closest('tr').remove();
                    });

                }
            });

        }

        return false;
    });
}


//function delete_article_div(page_loc){
//    $('.delete_update').live("click", function(){
//        var ID = $(this).attr("id");
//        var dataString = 'msg_id=' + ID;
//
//        if (confirm("Sure you want to delete this update? There is NO undo!")) {
//            $.ajax({
//                type: "POST",
//                url: "/" + page_loc + "/delete_article.php",
//                data: dataString,
//                cache: false,
//                success: function(html){
//                    $("#tr_edit_" + ID).fadeOut('slow', function(){
//                        $(this).remove();
//                    });
//                    $("#edit_place_edit_" + ID).parent().parent().fadeOut('slow', function(){
//                        $(this).remove();
//                    });
//                }
//            });
//        }
//        return false;
//    });
//}

/*******************************
 *			League Menus		*
 *******************************/
//This is used to change the leauge and the teams in the league will populate the team menu
function league_menu_options(){
    function showResponse(data){
        $("#team_id").html("");
        $("#team_id").html("<option value=\"all\">All Teams</option>");
        $.each(data, function(i, item){
            $("#team_id").html($("#team_id").html() + "<option value=\"" + item.team_id + "\">" + item.team_name + "</option>");
        });
    };
    $(function(){
        $("#league_id").change(function(){
            var options = {
                //	target:       	'#team_id',   // target element(s) to be updated with server response 
                success: showResponse, // post-submit callback 
                url: '/ajax/team_builder.php',
                type: 'post',
                dataType: 'json'
            };
            $('#season_year').attr("disabled", "disabled");
            $("#season_year[value='-1']").removeAttr("disabled");
            $('#form1').ajaxSubmit(options);
            
        });
        
    });
}

function open_edit(){
    var ID = $(this).attr("id");

    if ($("#edit_place_edit" + ID).is(':visible')) {
        $("#edit_place_edit" + ID).fadeOut(400);
    }
    else {
        $("#edit_place_edit" + ID + ":hidden").fadeIn(4).html('<img src="/images/ajax-loader.gif" align="absmiddle"> loading.....');
        $("#edit_place_edit" + ID).load("edit_article.php?id=" + ID, function(){
            $('textarea').autoResize();
        });
    }
    return false;
};

// Lets you edit articles 'live' on the current page
function edit_articles(type){
    $("a.edit_button").live("click", open_edit);
    $(".edit_form span.button").live("click", type);
}


/* Dialog pop-up */

function dialog(){

    $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 635,
        width: 969,
        modal: true,
        buttons: {
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {
        //allFields.val('').removeClass('ui-state-error');
        }
    });
}

/* team select */
function team_selection (){
    
    $('.team_selection').live('click',function() {

        //Run dialog JS to enable pop-up
        dialog();

        //Grab id from stats button
        var ID = $(this).attr("id");

        //open dialog pop-up
        $('#dialog').dialog('open');

        //Check to see if the form in which the stats selector will sit, if its hidden display loading and load stats loader page
        $("#team_selection_form:hidden").fadeIn(4).html('<img src="/images/ajax-loader.gif" align="absmiddle"> loading.....');
        $("#team_selection_form").load("team_loader.php?id=" + ID, function(){
        });
    });
};


function results_stats(){
    $('.stats_button').live('click',function() {

        //Run dialog JS to enable pop-up
        dialog();

        //Grab id from stats button
        var ID = $(this).attr("title");
        
        //open dialog pop-up
        $('#dialog').dialog('open');
            
        //Check to see if the form in which the stats selector will sit, if its hidden display loading
        $("#stats_selector_form:hidden").fadeIn(4).html('<img src="/images/ajax-loader.gif" align="absmiddle"> loading.....');

        //load stats loader page
        $("#stats_selector_form").load("stats_loader.php?id=" + ID);
    });
};

function hidden_stats(){
        
    $(".extra_button").click(function() {

        //Grab id from stats button
        var ID = $(this).attr("id");

        $('#hidden_'+ID).slideToggle('slow');

    });
}
