/*******************************
 *			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);
            
        });
        
    });
}

/**************************
 *					Comments					*
 **************************/
function comment(page_loc){

    $(function(){
    
        
        $(".comment").click(function(){
        
            var element = $(this);
            var I = element.attr("id");
            
            $("#slidepanel" + I).slideToggle(300);
            $(this).toggleClass("active");
            
            return false;
        });
    });
    
    
    $(function(){
    
        $(".comment_submit").click(function(){
        
            var element = $(this);
            var Id = element.attr("id");
            
            var test = $("#textboxcontent" + Id).val();
            var dataString = 'textcontent=' + test + '&com_msgid=' + Id;
            
            if (test == '') {
                alert("Please Enter Some Text");
            }
            else {
                $("#flash" + Id).show();
                $("#flash" + Id).fadeIn(400).html('<img src="/images/ajax-loader.gif" align="absmiddle"> loading.....');
                
                var var_1_id = $(document).getUrlParam("var_1_id");
                var var_2_id = $(document).getUrlParam("var_2_id");
                
                $.ajax({
                    type: "POST",
                    url: "/" + page_loc + "/add_comment.php?var_1_id=" + var_1_id + "&var_2_id=" + var_2_id + "",
                    data: dataString,
                    cache: false,
                    success: function(html){
                        $("#loadplace").append(html);
                        $("#flash" + Id).fadeOut();
                        $("#slidepanel").slideUp();
                    }
                });
            }
        });
    });
    delete_comment(page_loc);
}

function delete_comment(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(){
                        $(this).remove();
                    });
                }
            });
            
        }
        
        return false;
    });
}



/**************************
 *	Players             *
 **************************/
//Used to populate players menu from diffent seasons
function players_menu(){

    function showResponse(data){
        
        $("span.rowcount").fadeOut("slow", function(){
            $(this).html(data.length).fadeIn("slow");
        });
            
        $("#player_list").fadeOut("slow");
        $("#player_list").html("");
            
        $.each(data, function(i, item){            
            $("#player_list").html($("#player_list").html() + "<li><a href=\"profile.php?var_1_id=" + item.user_id + "\">" + item.id + " " + item.fname + " " + item.sname + "</a></li>");
        });
            
        $("#player_list").fadeIn("slow");
    };

    $(function(){
        $("#players_season_year").change(function(){
            var options = {
                //	target:       	'#team_id',   // target element(s) to be updated with server response
                success: showResponse, // post-submit callback
                url: '/ajax/player_builder.php',
                type: 'post',
                dataType: 'json'
            };
            $('#form1').ajaxSubmit(options);
        });
            
    });
}


/*************************/
/*			Video Search Box			*/
/*************************/

function video_search(){

    $(function(){

        $("#video_search").keyup($.debounce( 250, function(){
        
            $("#vid_display").html("");
            
            var searchbox = $(this).val();
            var dataString = 'video_search=' + searchbox;
            
            if (searchbox == '') {
                $("#vid_display").html("");
            }
            else {
            
                $("#flash").show().html('<img src="/images/ajax-loader.gif" align="absmiddle"> loading.....');
                
                $.ajax({
                    type: "POST",
                    url: "search_results.php",
                    data: dataString,
                    cache: false,
                    success: function(html){
                    
                        $("#flash").hide();
                        $("#vid_display").html("");
                        $("#vid_display").html(html).show();
                        
                        $('body').unbind('click').click(function(e){
                            if (!$(e.originalTarget).hasClass('video_search')) {
                                $("#vid_display").slideUp(500);
                            }
                        });
                    }
                });
            }
            return false;
        })
        );
        
        /* If anything in the search bar */
        $("#video_search").mousedown(function(e){
            $("#vid_display").html("");
            $("#video_search").val("Search Videos Here...");
        });
        /* Adds search here to the search bar after use. */
        $("#video_search").Watermark("Search Videos Here...");
    });
}

/*************************/
/*			Video Search Box			*/
/*************************/

/******************
 * Display and Hide Big Loading
 */


//Display Loading Image
function Display_Load(){
    $("#loading").fadeIn(3,0);
    //$("#loading").html("<img src='/images/loading.gif' />");
}
//Hide Loading Image
function Hide_Load(){
    $("#loading").fadeOut('fast');
};
