
oUi = 
{
	//handles setting up UI elements
	fInit : function()
	{
		jQuery( 'div#feedback a' ).toggle( function(){ jQuery( 'div#feedbackContent' ).hide(); }, function(){ jQuery( 'div#feedbackContent' ).show(); } );
		
		//jQuery( 'div#contentNav ul li a' ).click( function( event ){ alert( "Big Link" ); event.preventDefault(); } );
		jQuery( 'div#contentNav ul li a span' ).click( function( event ){
				//get parent list item
				var myListItemParent = jQuery( this ).parent().parent();
				//check how the list item is set
				if( myListItemParent.hasClass( 'open' ) )
				{
					myListItemParent.addClass( 'close' );
					myListItemParent.removeClass( 'open' );
				}
				else if( myListItemParent.hasClass( 'close' ) )
				{
					myListItemParent.removeClass( 'close' );
					myListItemParent.addClass( 'open' );
				};//end if
				event.stopPropagation();
				event.preventDefault();
		} );
	}//end function fInit
};//end object literal declaration oUi

jQuery(document).ready(function() { 

  oUi.fInit(); 
  
  // auto submit language change form
  $('#culture').change(function(){
    $('#setLanguageForm').submit();
  });
  
  if(typeof $.innerfade != 'undefined')
  {
    $('#bannerSales').innerfade({ speed: 1500, timeout: 4000, type: 'sequence', containerheight: '150px' });
  }
  
  // if there is an iframe on the page, customise the page specific to this user
  if($('iframe').is('*'))
  {
    $.ga.customisePage();
  }
  
  // disabled nav items are not clickable
  $('#contentNav a.disabled').click(function(){
    return false;
  });
  
  // Replace <audio> tags with flash players for browsers that don't support them (most browsers)
  $.each(document.getElementsByTagName('audio'), function(i){
    if(!this.canPlayType || (!this.canPlayType('audio/mp3') && !this.canPlayType('audio/mpeg')))
    {
      var generated_id = 'audio_player_' + i;
      $(this).attr('id', generated_id);
      swfobject.embedSWF('/flash/player_mp3.swf', generated_id, 300, 70, '8.0.0', "", { mp3: $(this).attr('src') });
    }
  });
  
  // load form validation and behaviors
  if($('#accountForm').is('*')) $.getScript("/js/forms/_account_form.js");
  if($('#registerProgramForm').is('*')) $.getScript("/js/forms/_register_program.js");
  if($('#loginForm').is('*')) $.getScript("/js/forms/_login.js");
  if($('#contactForm').is('*')) $.getScript("/js/forms/_contact.js");
  if($('#userRegForm').is('*')) $.getScript("/js/forms/_user_registration.js");
  if($('#paymentForm').is('*')) $.getScript("/js/forms/_payment_form.js");
  if ($('#programPurchaseForm').is('*')) $.getScript("/js/forms/_payment_form.js");
  if($('#forgottenPasswordForm').is('*')) $.getScript("/js/forms/_reset_password.js");

  
  // focus on username field in login form
  $('#loginForm #username').focus();
	
	// check if liq is complete before letting a user attempt to pay for a program
	$('#btnLiqComplete_xxxx').click(function(){
	$.get('/user/checkLiqComplete', {}, function(xml){
			alert(xml);
		})
	});
	
  // open info windows in a narrow window
  $('a.infowin').click(function(){
	  window.open(this, "infowin","menubar=1,resizable=1,scrollbars=1,status=1,width=760,height=" + $(window).height() / 3 * 2);
	  return false;
  });
  
  // open dialog windows 
  $('a.dialog').click(function(){
	  window.open(this, "dialog","menubar=1,resizable=1,scrollbars=1,status=1,width=565,height=300,top=" + (Math.round(($(window).height() - 450) / 2)) + ",left="+ (Math.round(($(window).width() - 550) / 2)));
	  return false;
  });  
  
  // handle adding programs to member groups in portal
  $('#btnMemberGroupProgram').click(function(){
	  var html = '';
	  var id = $('#selectMemberGroupProgram').val();
	  var text = $('#selectMemberGroupProgram option:selected').text();
	  html += '<tr><td>' + text + '</td>';
	  html += '<td><input type="text" maxlength="8" name="program[' + id + '][LicenseMax]"></td>';
	  html += '<td><input type="text" maxlength="8" name="program[' + id + '][LicenseCount]"></td></tr>';
	  $('#selectMemberGroupProgram option[value=' + id + ']').remove();
	  $('table.membergroupprograms').append(html);
  });
  
});

jQuery.hardReload = function(anchor) {
	document.location.href = document.location.pathname + '?nocache=' + Math.floor(Math.random()*1000000001) + (anchor ? '#' + anchor : '');
}
