function showFaq(element_class, element_id){
	$(element_class).hide(); //hide all questions
	$(element_id).css({'display': 'block'}); //show wanted question
}

function setCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 ;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}		


// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


function getIEVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

var currentSection = 'faq'; // Some other than game

/*
	http://forums.devx.com/showthread.php?t=152308
*/

var tmpVar1, tmpVar2, tmpVar3;

function changeSectionTimer(section, iframe, old_url)
{
	tmpVar1 = section;
	tmpVar2 = iframe;
	tmpVar3 = old_url;
		
	/*
	// Wait until new page has been loaded
	if (iframe.src == old_url){
		setTimeout('changeSectionTimer(tmpVar1, tmpVar2, tmpVar3)', 100);
		return 0;
	}
	*/
	
	// Now change the tab
	setTimeout("changeSection(tmpVar1)", 500);
}


// section: the Section's name; {game|faq|help|chat}
function changeSectionOptional( section, tab, param )
{
	if( section == currentSection ) return;
	
	// if game load data based on tab
	if (section=="game") {
		
		var iframe = document.getElementById( "gameFrame" );
		old_url = iframe.src;
		iframe.src = "game_index.php?tab="+tab+param;
		changeSectionTimer(section, iframe, old_url);
	}
	
}

// section: the Section's name; {game|faq|help|chat}
function changeSection( section)
{
	if( section == currentSection ) return;
	
	var curHolder = document.getElementById( currentSection + "Holder" );
	var newHolder = document.getElementById( section + "Holder" );
	var curLi = document.getElementById( currentSection + "Li" );
	var newLi = document.getElementById( section + "Li" );

	// Hide the previous section
	curHolder.style.visibility = "hidden";
	curHolder.style.height = "0";
	
	// set display values, if user uses IE (on FF setting display reloads content)
	if (getIEVersion() > -1) {
		curHolder.style.display = "none";
	}

	if (newHolder.style.display == "none") {
		newHolder.style.display = "inline";
	}

	// Bring out the new one
	newHolder.style.visibility  = "visible";
	newHolder.style.height = "540";
	curLi.className = "";
	newLi.className = "tabs-selected";
	
	currentSection = section;
}

function logout()
{
	var gameFrame = document.getElementById( "gameFrame" );
	gameFrame.src = "game_index.php?lg_logout=1";
}


function giveUpGame(confirmMessage)
{
	var msg = confirm(confirmMessage);
	if (msg) {
		Spry.Utils.loadURL("GET", "handler_give_up.php", true, handleGiveUpGame);
	}
}

function handleGiveUpGame( request )
{		
	window.location = "game_index.php";
}

function giveUpGameWhileSearching(confirmMessage)
{
	var msg = confirm( confirmMessage);
	if (msg) {	
		window.location = "handler_give_up_searching.php";
	}
}

function checkFlashVersion()
{
	var flashinstalled = -1;
	var flashversion = 0;
	// This doesn't work on IE
	if (navigator.plugins && navigator.plugins.length)
	{
		flash = navigator.plugins["Shockwave Flash"];
		if (flash)
		{
			flashinstalled = 1;
			if (flash.description)
			{
				desc = flash.description; // "Shockwave Flash <v>.<subv> r<release>"
				v = desc.match(/[\d]+/g);
				flashversion = 1* v[0];
			}
			else
				flashversion = 1;
		}
		else
			flashinstalled = 0;
		if (navigator.plugins["Shockwave Flash 2.0"])
		{
			flashinstalled = 1;
			flashversion = 2;
		}
	}
	else if (navigator.mimeTypes && navigator.mimeTypes.length)
	{
		flash = navigator.mimeTypes['application/x-shockwave-flash'];
		if (flash && flash.enabledPlugin)
			flashinstalled = 1;
		else
			flashinstalled = 0;
	}
	else {
		// IE flash detection.
		for (var i=12; i>0; i--){
			try{
				var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
				flashinstalled = 1;
				flashversion = i;
				break;
			}
			catch(e){
			}
		}
	}
	return flashversion;
}

function inviteShow()
{
	var holder = document.getElementById( "inviteHolder" );
	var frame = document.getElementById( "inviteFrame" );
	if (holder.style.visibility == "visible") {
		inviteHide();
		return;
	}
	
	frame.src = "invite.php";
	
	holder.style.position = "absolute";
	holder.style.display = "inline";
	holder.style.top = (inviteY - 500) + "px";
	holder.style.left = "0px";	
	holder.style.height = "480px";
	holder.style.width = "auto";
	holder.style.visibility = "visible";
}

function inviteHide()
{
	var holder = document.getElementById( "inviteHolder" );

	holder.style.visibility = "hidden";
	holder.style.display = "none";
}

function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } else { 
    window.onload = function() { 
      if (oldonload) { 
        oldonload(); 
      } 
      func(); 
    } 
  } 
} 
 
function topRankingsShow(id){
	Spry.Utils.loadURL("GET", "getTopRankings.php?id=" + id, true, handleGetTournamentResults);
}

function handleGetTournamentResults( request )
{		
	var response = request.xhRequest.responseText;
	if (!response) return;
	
	var link = document.getElementById('topRankingsShow');
	link.style.visibility = "hidden";
	
	var topRankingsHolder = document.createElement('div');
	topRankingsHolder.setAttribute('id','topRankingsHolder');
	
	Spry.Utils.setInnerHTML(topRankingsHolder, response );
	document.getElementById('tournamentPageLeftCol').appendChild(topRankingsHolder);
}

function hideTopRankingsShow(){
	var topRankingsHolder = document.getElementById('topRankingsHolder');
	document.getElementById('tournamentPageLeftCol').removeChild(topRankingsHolder);
	
	var link = document.getElementById('topRankingsShow');
	link.style.visibility = "visible";
}	

function showProductDetails(id){
	Spry.Utils.loadURL("GET", "getProductDetails.php?id=" + id, true, handleShowProductDetails);
}
function handleShowProductDetails( request )
{		
	var response = request.xhRequest.responseText;
	if (!response) return;
	
	var productDetailsHolder = document.createElement('div');
	productDetailsHolder.setAttribute('id','productDetailsHolder');
	
	Spry.Utils.setInnerHTML(productDetailsHolder, response );
	document.getElementById('products').appendChild(productDetailsHolder);
}
function hideProductDetails(){
	var productDetailsHolder = document.getElementById('productDetailsHolder');
	productDetailsHolder.parentNode.removeChild(productDetailsHolder);
}

function showPremiumTournaments(){	
	var div = document.getElementById('premiumTournamentsHolder');
	div.style.visibility = "visible";
	div.style.display = 'inline';
}

function hidePremiumTournaments(){
	var div = document.getElementById('premiumTournamentsHolder');
	div.style.visibility = 'none';
	div.style.display = 'none';
}

   function showFaq(element_class, element_id){
   		$(element_class).hide(); //hide all questions
		$(element_id).css({'display': 'block'}); //show wanted question
	}
