/*

*/

// collection of global variables for the site
var G_DEBUG_ON = true;
var G_CURRENT_LOAD_OPTIONS = 'HOME';
var G_STATS_POLL_DELAY = 0;	// milliseconds. set to [0] for dev to switch off loop repeat.
var G_STATS_ROWS_TO_RETURN = 100;
var G_STATS_POLL_TIMER = null;
var G_MENU_RESET_TIMER = null;
var G_EVENT_PENDING = false; 
var G_USER_AGENT = '';
var G_LEFT_MENU_ANIMATION_PENDING = false;

// attach function to window resize events.
$(window).resize(function() {
	setFooterPosition(24);
});

// attach function to apply when the page finishes loading.
$(document).ready(function() {
	G_USER_AGENT = (navigator.userAgent.toLowerCase().indexOf('msie') > 1) ? 'msie' : 'other';
	//setFooterPosition(24);	// inital position of the footer element
	setTimeout("setFooterPosition(24)", 2000);
	G_STATS_POLL_TIMER = setTimeout('initGameStats()', 500);	// setup players/games display after 1/2 second
	if (G_USER_AGENT == 'msie') { fixupIE_CSS_Deficiences(''); }
	
	// attach event handlers for auto generated elements in the menu and submenu
	//$('.main_menu_item a').mouseleave(function() { resetSubmenu(); });
	//$('#sub_menu').mouseenter(function(){ cancelResetSubMenu(); });
	//$('#sub_menu').mouseleave(function(){ resetSubmenu(); });
 // onmouseover='javascript:subMenuAnimation("play_online");'
	// load up the carousel JS functions if required.
	{ initCarousel(); }

});

// resets the position of the footer relative to the content. if the content isn't high enough for the window height, the content 
// will be resized to fit, otherwise it'll fit the content and the page will display a regular vertical scroll bar.
function setFooterPosition(offset) {
	// use offset to allow for margins etc.
	// we need to remove the bottom border for IE based browsers as they include this in the height calculation
	if (G_USER_AGENT == 'msie') { offset -=12; }	// the height of the bottom border in [#content_wrapper]
	if (G_USER_AGENT != 'msie') 
	{
	    var documentHeight = $(document).height();
	    var contentHeight = 0;
	    $("#content_container .content_block").each(function(index, el) { contentHeight += $(el).height(); });
	    var footerHeight = $('#footer_wrapper').height();
	    var contentTop = $('#content_wrapper').offset().top;


	    // set the content height based on the size of the window, but not less than the
	    // actual content.
	    var newContentHeight = contentHeight; // default height, override if required.
	    newContentHeight = documentHeight - contentTop - footerHeight - 17;
	    $("#content_wrapper").css('height', newContentHeight);

	    if (G_DEBUG_ON) {
	        $('#debugTxt').text('setFooterPosition.' +
						', contentHeight:' + contentHeight +
						', footerHeight:' + footerHeight +
						', contentTop:' + contentTop +
						', newContentHeight:' + newContentHeight +
						', UA:' + G_USER_AGENT);

	    }
	}

}

// ajax loader for the sub-menu based on the passed in variable
function subMenuAnimation(mainMenuItem) {
	if (G_MENU_RESET_TIMER !=null) { clearTimeout(G_MENU_RESET_TIMER); G_MENU_RESET_TIMER=null; }
	var mainMenu = mainMenuItem.length > 0 ? mainMenuItem : G_CURRENT_LOAD_OPTIONS;
	var url = "./includes/core/sub_menu.inc.php?submenu=" + mainMenu;
	//$("#sub_menu").load(url);
	$('#sub_menu').load(url, function() {
		if (G_USER_AGENT == 'msie') { fixupIE_CSS_Deficiences('sub'); }
	});
}

// timer function that allows the subnav to reset to the current page after the user has finished
// searching through the other menu items and has settled on the current page.
function resetSubmenu() {
	if (G_MENU_RESET_TIMER !=null) { clearTimeout(G_MENU_RESET_TIMER); G_MENU_RESET_TIMER=null; }

	// set a long timer to reset the menu to the current site navigation page after a delay
	G_MENU_RESET_TIMER = setTimeout('subMenuAnimation("");', 3000);
}

function cancelResetSubMenu() {
	if (G_MENU_RESET_TIMER !=null) { clearTimeout(G_MENU_RESET_TIMER); G_MENU_RESET_TIMER=null; }
}

function fixupIE_CSS_Deficiences(menuToFix) {
	// main menu, sub menu and footer need background images stripped from the 'first-child' element
	
	if ((menuToFix == 'main') || (menuToFix == '')) {
		$("#main_menu .main_menu_item").each(function (index, el) {
			$(el).css("background", "none"); 
			$(el).css("padding-left", "0"); 		
			return false;	// exits the LOOP not the function
		});
	}

	if ((menuToFix == 'sub') || (menuToFix == '')) {
		$("#sub_menu .sub_menu_item").each(function (index, el) {
			$(el).css("background", "none"); 
			$(el).css("padding-left", "0"); 		
			return false;	// exits the LOOP not the function
		});
	}
	
	if ((menuToFix == 'foot') || (menuToFix == '')) {
		$("#footer a").each(function (index, el) {
			$(el).css("background", "none"); 
			$(el).css("padding-left", "0"); 		
			return false;	// exits the LOOP not the function
		});
	}
}



		

// handle scrolling the leftmenu menu up/down
function leftMenuScroll(scrollUp) {
	
	if (G_LEFT_MENU_ANIMATION_PENDING) { }	// one at a time
	else {
		// [currentTop] is the relative position of the menu scroller within it's container.
		var currentTop = $('#left_menu_container').offset().top - $('#left_menu_scroll_container').offset().top;
		var currentHeight = $('#left_menu_container').height();
		var menuContainerHeight = $('#left_menu_scroll_container').height();

		if (G_DEBUG_ON) {
			$('#debugTxt').text('currentTop: ' + currentTop + ', currentHeight: ' + currentHeight +  ', menuContainerHeight: ' + menuContainerHeight);
		}
		
		
		if (scrollUp==0) {
			// scroll up 
			if ((currentTop + currentHeight) > (menuContainerHeight+25)) {
				G_LEFT_MENU_ANIMATION_PENDING = true;
				$('#left_menu_container').animate( { top: '-=25' }, 222, function() { G_LEFT_MENU_ANIMATION_PENDING = false;});
			}
		}
		else {
			// scroll down
			if (currentTop < 0) { 
				G_LEFT_MENU_ANIMATION_PENDING = true;
				$('#left_menu_container').animate( { top: '+=25' }, 222, function() { G_LEFT_MENU_ANIMATION_PENDING = false;});
			}
		}
	}	
}



