/* Site Initialize */
$(document).ready(function() {
	// Hide The Login bar //
	$("#loginbar").hide();
});

function tSlide(ele)
{
    $('#' + ele).slideToggle('normal');
}


/* toggleLogin() - Toggles the login bar */
function toggleLogin()
{
	$("#loginbar").slideToggle();
}

function showBusy(){
	$('#ajaxList').block({
		message: '<h1>Processing</h1>',
		css: {border:'3px solid #000'}
	});
}

function siteBusy(){
	$.blockUI({
		message: '<h1>Processing</h1>',
		css: {border:'3px solid #000'}
	});
}

function updatePage(html){

    $.history( { id: "#ajaxList", value: html } );

	window.setTimeout( function(){
		$('#ajaxList').html(html);
	}, 100)


}

// History Callback
$.history.callback = function ( packed, cursor ) {
    if (typeof(packed) != 'undefined')
        $(packed.id).html(packed.value);
    //else
        //console.log("Packed was undefined!");
}

/* Pagnation */
$(document).ready(function() {

	$('.ajaxPagLink').live('click', function(eve){
		eve.preventDefault();

        var link = $(this).attr('href');

		$.ajax({
			url: link,
			type: "GET",
			dataType: "html",
			beforeSend: function(){
				showBusy();
			},
		  	success: function(html) {
		    	updatePage(html);
		 	}
		});

	});

    $('#ajaxList').each(function() {
        //console.log("Pre-Loading Ajax List into history.");

        var html = $(this).html();
        $.history( { id: "#ajaxList", value: html } );
    });

});

/******* Admin Menu *******/

$(document).ready(function() {
    $('#admin-menu-bar > li').mouseover(function() {
	// Mouse-Over Admin Menu Links
	if ($(this).find('a.menu-link').size() == 1)
	{
	    // We have a menu link, hide all sections
	    $('#admin-menu-bar > li').each(function() {
		$(this).removeClass('selected');
	    });

	    $(this).addClass('selected');

	    $('#admin-menu-sections div.admin-sub-menu').each(function() {
		$(this).hide();
	    });

	    var idLink = $(this).find('a.menu-link').attr('name');

	    // Show the linked menu
	    $('#'+idLink).show();
	}
    });
});


/** Ajax-Enabled Dropdowns **/
function updateDropDown(options, dropDown) {
    dropDown.options.length = 0;

    var counter = 0;
    jQuery.each(options, function() {
	var optObject = document.createElement('option');
	optObject.text = this.val;
	optObject.value = this.id;
	dropDown.add(optObject, null);
    });
    
}

$(document).ready(function() {
    $('a.updateable-drop-down').live('click', function(eve) {
	eve.preventDefault();

        var link = $(this).attr('href');
	var drop = $(this).siblings('select');
	
	var dropMain = null;
	
	if (drop.size() > 0)
	    dropMain = drop.get(0);


	$.ajax({
		url: link,
		type: "GET",
		dataType: "json",
		timeout: 10000,
		dropDown: dropMain,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
		    $.unblockUI();
		    //console.log('Error Occured during AJAX: `' + errorThrown + '`, Text Status: `' + textStatus + '`');
		},
		beforeSend: function() {
			siteBusy();
			//console.log('Sending...');
		},
		success: function(obj) {
		    $.unblockUI();
		    //console.log('Ajax Response');
		    updateDropDown(obj, this.dropDown);
		}
	});
    });
})

/** New Window Links **/
$(document).ready(function() {
    $('a.new-window').live('click', function(eve) {
	eve.preventDefault();

	var link = $(this).attr('href');

	window.open(link);
    });
});

$(document).ready(function() {

    $(".tourney-dialog").dialog({
	autoOpen: false,
        resizable: false
    });

    $(".tourney-ajax-link").live('click', function(eve) {
	eve.preventDefault();
	//console.log("Haleluya!");

	var command = $(this).attr("href");
	var commands = command.split('-');

	if (commands.length > 0)
	{
	    var strName;

	    if (commands[0] == '#openStaggeredDialog')
		strName =  "#staggered-data-";
	    if (commands[0] == '#openArenaDialog')
		strName =  "#arena-data-";
            if (commands[0] == '#openPledgeDialog')
                strName =  "#pledge-data-";

	    //console.log("Mode Selection: " + strName);

	    if (commands.length == 2)
	    {
		var theID = commands[1];

		//console.log(theID);

		strName = strName + theID;

		//console.log("Final Selection: " + strName);

		$(strName).dialog('open');
	    }
	}
	//else
	    //console.log("Unable to figure out ID!");
    });
});