/*////////////////////////////////////////////////////////////////
#
  * Dom - written 23/11/2009 for Cuckoo Design Ltd
#
  * Includes:
#
  * document ready function for Guidance and support menu
#
  *////////////////////////////////////////////////////////////////
  
//Checks if broweser supports index of and if not appends the function to the Array object
if (!Array.indexOf) 
	{
	  	Array.prototype.indexOf = function (obj, start) {
		
		for (var i = (start || 0); i < this.length; i++) 
		{
		  if (this[i] == obj) 
		  {
			return i;
		  }
		}
	  }
	}
	
$(document).ready(function() {
			
			//Hide Guidance and support menu forms
			$('#apply-now-form').hide();
			$('#tools-guides-form').hide();
			
			///assign onclick functions
			
			$('#apply-now').bind("click", function(){
				
				hideAndShow('#apply-now');
						
			});
			
			$('#tools-guides').bind("click", function(){
				
				hideAndShow('#tools-guides');
						
			});
			
			
			//Apply to close button class
			$('.close-button').bind("click", function(){
				
				//Get name of form that .close-button a tag is in
				var idName = $(this).parent().attr('id');
				//Replace the word -form with blank to remove it
				var idNameId = '#' + idName.replace("-form", "");
				//Now we have the id name of the div that our .close-button a tag is in
				//it is this that we want to use in the hideAndShow method
				hideAndShow(idNameId);
						
			});
															  
	

});///// End document ready

/*-----------------------------------------------------------------------------
#
  *
#
  * function - hideAndShow - hides and shows hidden forms
#
  *
#
  *-----------------------------------------------------------------------------
#
  *
#
  * @param: id name of the element you want to hide or show and another variable to confirm if has form or not
#
#
  *///-------------------------------------------------------------------------- 

function hideAndShow(id)
{

//If element does has class 'closed'
						if($(id).hasClass('closed'))
						{
							//remove class closed
							$(id).removeClass('closed');
							//assign class open
							$(id).addClass('open');
							//Fade out other elements
							whatToHide(id, 'fadeOut');
							//Fade in the form
							var idForm = id + '-form';
							$(idForm).slideToggle(700);
						}
						else if($(id).hasClass('open'))
						{
							//element must be open 
							//Fade out the form
							var idForm = id + '-form';
							$(idForm).slideToggle(400);
							//Fade back in other elements
							whatToHide(id, 'fadeIn');
							$(id).removeClass('open');
							//assign class open
							$(id).addClass('closed');
						}
}

/*-----------------------------------------------------------------------------
#
  *
#
  * function - whatToHide
#
  *
#
  *-----------------------------------------------------------------------------
#
  *
#
  * @param: id name of the element that needs to stay visible and method you would like to use
#
#
  *///-------------------------------------------------------------------------- 

function whatToHide(id, method)
{
	//create array of possible options
	var optionArray = new Array(5);
	optionArray[0] = '#apply-now';
	optionArray[1] = '#member-login';
	optionArray[2] = '#tools-guides';
	optionArray[3] = '#latest-news';
	optionArray[4] = '#contact-us';
	
    //Find position of submitted usiing indexOf which returns the array position of a value if it is in the array. 
	//Then remove the element from that position 
	
	//IE doesn't support array function index of.
	//Below we check for it, if it isn't there then we run the following for loop
	
		//The second parameter is the number of items you want to remove from the array position, we only want to remove one.
		optionArray.splice(optionArray.indexOf(id),1);
	

	//We remove the passed in id to ensure every element but that is hidden
	
	if(method == 'fadeIn')
	{
		//Iterate through elements left in the array and hide their parent divs
		for(i = 0; i < optionArray.length; i++)
		{
			//If 'fadeIn' then fade elements in
			var selection = $(optionArray[i]).parent();
			selection.slideToggle(500);
		}
	}
	else if(method == 'fadeOut')
	{
		//Iterate through elements left in the array and hide their parent divs
		for(i = 0; i < optionArray.length; i++)
		{
			//If 'fadeIn' then fade elements in
			var selection = $(optionArray[i]).parent();
			selection.slideToggle(500);
		}
	}
		
}
