//***************************************************************
// Code for fading elements
//***************************************************************

// *******************************************************************
// for index.php
// *******************************************************************

function deleteMsg( eid, msgid ) {
    $.post( "/processmsg.php", {
		"action": "Delete",
		"msgid": msgid },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#"+eid).fadeOut(300, function() {
					   var c = $("#messagecount");
					   var ctxt = $("#messagecounttxt");
					   if( c.length==0 || ctxt.length == 0 ) {
					       return;
					   }
					   var num = c[0].innerHTML - 1;
					   c[0].innerHTML = num;
					   if( num == 0 ) {
					       $("#messagebox").css("display","none");
					   } else {
					       var txt = num + " message";
					       if( num > 1 ) txt += "s";
					       ctxt.html( txt );
					   }
				       } );
		} // if status
	    },
	    "html" );
}

/* Don't need this, but keep the code around for later
var gameId = 0;
var gameTimer = 0;

function createGameDialog() {
    return;
    $('#gamedialog').addClass('flora').dialog(
	{ height: 300,
	  width: 300,
	  draggable: false,
	  resizable: false,
 	  position: [500,350],
	  modal: false,
	  autoOpen: false
	});
}
*/

function updateGameInfo( id, me ) {
    /*clearTimeout( gameTimer );*/
    $.ajax({
	       url: "/game.php?ajax=1&gameid=" + id + "&teamid=" + me,
	       cache: false,
	       success: function(html) {
 		   $("#gameinfo").html(html);
		   /*$("#gameinfo").dialog("open");*/
	       }
	   });
}

/*
function closeGameDialog() {
    alert( "Close");
    clearTimeout( gameTimer );
    setTimeout( function() {
		    try {
			$('#gamedialog').dialog('close');
		    } catch ( x ) {
		    } }, 500 );
}
*/

// *******************************************************************
// For nllteams.php page - clicking on a team logo gives you the stats
// for that team
// *******************************************************************

function clickOnTeam( team, my_team_id ) {
    $.post( "/nllteams-a.php", {
		"team": team,
		"my_team_id": my_team_id },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#teamlist").html( data );
		}
	    },
	    "html" );
}

// *******************************************************************
// For compare.php page - Selecting a user from selector 1 will populate
// selector 2, and selecting a user from selector 2 will do the comparison
// *******************************************************************

function compare_selector_one() {
    s1 = document.getElementById('sel1');
    sel_value = s1.value;
    if( sel_value == "" ) {
        s2 = document.getElementById('sel2');
        s2.innerHTML = "<option selected=\"selected\" value=\"\">" +
	    "Select team 1 first</option>\n";
        d = document.getElementById('comparediv');
        d.innerHTML = "";
        return;
    }
    $.post( "/compare.php", {
		"ajax": 1,
		"action": "getsel2",
		"id": sel_value },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#sel2").html( data );
		    $("#comparediv").html( "" );
		}
	    },
	    "html" );
}

function compare_selector_two() {
    s2 = document.getElementById('sel1');
    sel1_value = s1.value;
    s2 = document.getElementById('sel2');
    sel2_value = s2.value;
    if( sel1_value == "" || sel2_value == "" ) return;
    $("#comparediv").html("<img src=\"/images/ajax-loader.gif\" />" );
    $.post( "/compare.php", {
		"ajax": 1,
		"action": "compare",
		"id1": sel1_value,
		"id2": sel2_value },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#comparediv").html( data );
		}
	    },
	    "html" );
}

// *******************************************************************
// for choosegames.php
// *******************************************************************
function choosegames_select_week( clear ) {
    if( clear ) {
	$("#extra").html( "" );
    }
    var w = $("#weeks")[0].value;
    $.post( "/choosegames.php", {
		"action": "SelectWeeks",
		"ajax": "1",
		"week": w },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#gamelist").html( data );
		}
	    },
	    "html" );
}

// *******************************************************************
// for gamepicks.php
// *******************************************************************
function gamepicks_select_week() {
    var w = $("#weeks");
    if( w.length == 0 ) return;
    $.post( "/gamepicks.php", {
		"action": "SelectWeeks",
		"ajax": "1",
		"week": w[0].value },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#gamelist").html( data );
		}
	    },
	    "html" );
}


// *******************************************************************
// for allgames.php
// *******************************************************************
function pick_season() {
    var sel = document.getElementById('season_id');
    sel_value = sel.value;
    if( sel_value == "" ) {
        d = document.getElementById('gamediv');
        d.innerHTML = "";
        return;
    }
    $("#gamediv").html("<img src=\"/images/ajax-loader.gif\" />" );
    $.post( "/allgames.php", {
		"ajax": 1,
		"action": "getgames",
		"season_id": sel_value },
	    function( data, status ) {
		if( status == "success" ) {
		    $("#gamediv").html( data );
		}
	    },
	    "html" );
}

// *******************************************************************
// for parsestats.cgi
// *******************************************************************
function addPlayer( first, last, team, goalie, idnum ) {
    var id = "#add-" + idnum;
    $.post( "/admin/addplayer.php", {
		"ajax": 1,
		"action": "addplayer",
		"last": last,
		"first": first,
		"team": team,
		"goalie": goalie },
	    function( data, status ) {
		if( status == "success" ) {
		    $(id).html( data );
		}
	    },
	    "html" );
}

function addName( pid, first, last, idnum ) {
    var id = "#add-" + idnum;
    $.post( "/admin/addplayer.php", {
		"ajax": 1,
		"action": "addname",
		"last": last,
		"first": first,
		"id": pid },
	    function( data, status ) {
		if( status == "success" ) {
		    $(id).html( data );
		}
	    },
	    "html" );
}
