// iuman league
// main javascript
// (c)2010 iuman software

// Handle enter key in input field
function ilg_InputKeyPressed ( Event, KeyTarget )
{
  var key = Event.keyCode || Event.which;
  if ( key == 13 )
  {
    if ( Event.preventDefault )
      Event.preventDefault ();
    else
      Event.returnValue = false; 
    myFun = eval ( KeyTarget );
    myFun ();
  }
}

// set chunk
function ilg_SetChunk ( data )
{
  // update display
  $("#ilg_chunks").html ( data );
  $("#ilg_content").animate({opacity:0.8},100).slideDown(250,ilg_ChunkLoaded);
}

function ilg_ChunkLoaded ()
{
  // set focus to first form element
  $('input:visible:enabled:first').focus();
  
  // chunk post processing
  if ( typeof window.ilg_OnChunkLoaded == 'function' )
    ilg_OnChunkLoaded ();
}

// get chunk
function ilg_GetChunk ( commandName, sectionName )
{
  // get account
  accountName = ilg_UrlLookup ( "account" );
  
  // set teszt cookie
  ilg_SetCookie ( 'ilg_' + accountName + '_cc', 'ok', 300 );

  // get chunk
  $("#ilg_content").slideUp(100);
  urlPars = { cmd: commandName, section: sectionName };
  if ( accountName != "" )
    urlPars ["account"] = accountName;
  $.get ( "", urlPars, ilg_SetChunk );
  return false;
}

//
function ilg_LinkSwap ( sectionToHide, sectionToDisplay )
{
  // hide section
  $(sectionToHide).hide ();
  // display section
  $(sectionToDisplay).show ();
  // continue
  return true;
}

// set cookie
function ilg_SetCookie ( name, value, days )
{
	if ( days ) 
  {
		var date = new Date ();
		date.setTime ( date.getTime () + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString ();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

// team switch
function ilg_Switch_Commit ( section )
{
  // set cookie to $("input:checked").attr ( 'value' )
  accountName = ilg_UrlLookup ( "account" );
  cookieName = 'ilg_' + accountName + '_team';
  cookieValue = $("input:checked").attr ( 'value' );
  ilg_SetCookie ( cookieName, cookieValue, 400 );
  return ilg_GetChunk ( 'chunk', section );
}

// lookup url parameters
function ilg_UrlLookup ( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec ( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// display instructions to enable cookies
function ilg_CookieInstructions ()
{
  // init
  var userAgent = navigator.userAgent.toLowerCase();
  $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
  var instructed = false;
  
  // IE
  if ( $.browser.msie )
  {
    divString = "#ilg_cookie_ua_ie"
    $(divString).show ();
    instructed = true;
  }
  
  // CHROME
  if ( $.browser.chrome )
  {
    divString = "#ilg_cookie_ua_chrome" 
    $(divString).show ();
    $.browser.safari = false;
    instructed = true;
  }
  
  // SAFARI
  if ( $.browser.safari )
  {
    divString = "#ilg_cookie_ua_safari" 
    $(divString).show ();
    instructed = true;
  }
  
  // FIREFOX
  if ( $.browser.mozilla )
  {
    divString = "#ilg_cookie_ua_ff" 
    $(divString).show ();
    instructed = true;
  }
  
  // OPERA
  if ( $.browser.opera )
  {
    divString = "#ilg_cookie_ua_opera" 
    $(divString).show ();
    instructed = true;
  }
  
  // OTHER
  if ( instructed )
  {
    $("#ilg_cookie_instructions").hide ();
  }
  else
  {
    divString = "#ilg_cookie_ua_other" 
    $(divString).show ();
  }
  
  // ok
  return false;
}

// document ready
$(document).ready ( function ()
{
  // enable menu
	$("ul#ilg_menu a").click(function() 
  {
    menuId = $(this).attr('id');
    return ilg_GetChunk ( 'chunk', menuId );
	});
  
  // page post processing
  if ( typeof window.ilg_OnPageLoaded == 'function' )
    ilg_OnPageLoaded ();

  // setup innerfade
  if ( $('ul.ilg_innerfade').size () )
    $('ul.ilg_innerfade').innerfade({
						speed: 1000,
						timeout: 5000,
						type: 'sequence',
						containerheight: '113px'
					});

  // IE6- warning
  oldIE = $('#ilg_old_ie').size ();
  if ( oldIE )
  {
    alert ( $('#ilg_old_ie').html () );
    $('#ilg_footer').css ( "position", "static" );
  }
});

