$(document).ready(function() {
  //if javascript is switched on, then show tabs
  $('ul.tabs').show();

  //if there are values in the URL string read them and run search
  if (jQuery.url.param("mortgageType") || jQuery.url.param("customerType") || jQuery.url.param("depositMin")) 
  {
  mortgageSearch();
  $('.viewAllProducts').show();
  $('#column2 h1:eq(1)').hide();
//  $('#column2 p:first').hide();
	if (jQuery.url.param("customerType") == '')
		{
		if (jQuery.url.param("mortgageType") == 'fixed') $('#column2 h1.fixed').show();
		if (jQuery.url.param("mortgageType") == 'rollover') $('#column2 h1.rollover').show();
		if (jQuery.url.param("mortgageType") == 'offset') $('#column2 h1.offset').show();
		if (jQuery.url.param("mortgageType") == 'boe') $('#column2 h1.boe').show();
		if (jQuery.url.param("mortgageType") == 'ftb') $('#column2 h1.ftb').show();
		}
		else $('#column2 h1.mortgageFinderResults').show();

  }
  $('.collapsiblePanel table tr').find('th:last').css('border-right','0px');
  $('.collapsiblePanel table tr').find('td:last').css('border-right','0px');
  
});

function mortgageSearch()
{
var mortgageTypeValue = jQuery.url.param("mortgageType");
var customerTypeValue = jQuery.url.param("customerType");
var depositMinValue = jQuery.url.param("depositMin");

// Display search results confirmation sentence (you searched for such and such)
/*
searchResultsConfirmation').show();
  if (mortgageTypeValue == 'fixed') document.getElementById("mortgageTypeHolder").innerHTML = 'Mortgage type: <b>Fixed Rate Mortgage</b>,<br>';
  if (mortgageTypeValue == 'rollover') document.getElementById("mortgageTypeHolder").innerHTML = 'Mortgage type: <b>Rollover Fixed Rate Mortgage</b>,<br>';
  if (mortgageTypeValue == 'offset') document.getElementById("mortgageTypeHolder").innerHTML = 'Mortgage type: <b>Offset Mortgage</b>,<br>';
  if (mortgageTypeValue == 'boe') document.getElementById("mortgageTypeHolder").innerHTML = 'Mortgage type: <b>Bank of England Tracker</b>,<br>';
  if (mortgageTypeValue == 'ftb') document.getElementById("mortgageTypeHolder").innerHTML = 'Mortgage type: <b>First Time Buyer Mortgage</b>,<br>';
  if (customerTypeValue == 'newM') document.getElementById("customerTypeHolder").innerHTML = 'Customer type: <b>New customer moving home</b>,<br>';
  if (customerTypeValue == 'newR') document.getElementById("customerTypeHolder").innerHTML = 'Customer type: <b>New customer remortgaging</b>,<br>';
  if (customerTypeValue == 'existR') document.getElementById("customerTypeHolder").innerHTML = 'Customer type: <b>Existing customer changing deal</b>,<br>';
  if (customerTypeValue == 'existM') document.getElementById("customerTypeHolder").innerHTML = 'Customer type: <b>Existing customer moving home</b>,<br>';
  if (customerTypeValue == 'ftb') document.getElementById("customerTypeHolder").innerHTML = 'Customer type: <b>First time buyers</b>,<br>';
  document.getElementById("depositMinHolder").innerHTML = 'with a minimum deposit of <b>' + depositMinValue + '%</b>.';
*/

// Remember the choices in 'search again' form
if (customerTypeValue) $('select#customerType').val(customerTypeValue).attr("selected","selected");
if (mortgageTypeValue) $('select#mortgageType').val(mortgageTypeValue).attr("selected","selected");
if (depositMinValue) $('select#depositMin').val(depositMinValue).attr("selected","selected");
if (customerTypeValue == 'existR') {$('select#mortgageType').attr("disabled","disabled"); $('select#depositMin').attr("disabled","disabled");}

// Hide tabs top-design
//if customerType empty than display #newM and visible tabs
if (customerTypeValue != '') 
{
$('ul.tabs').hide(); 
$('#centerColumnContent .centerColumnTabContent.currentProducts').css('border', '1px solid #ccc');
$('#centerColumnContent .centerColumnTabContent.currentProducts').css('margin-top', '-20px');
$('#centerColumnContent .centerColumnTabContent.currentProducts').css('background', '#eee');
}

//Add a hash to the URL - this opens the relevant customerType 'tab'
location.hash = customerTypeValue;

}

function showProductsByCustomerType()
{ 
//Show all panels initially 
$('.collapsiblePanel').each(function(){ 			  
  $(this).prev('.collapsiblePanelShowHide').show();
  $(this).show(); 
}); 

//Show and hide the relevant panels as per selected Mortgage Type, i.e. if fixed is selected then hide all the others
var mortgageTypeValue = jQuery.url.param("mortgageType");
if (mortgageTypeValue) {
  if (mortgageTypeValue != 'fixed') {$('#fixed').hide(); $('#fixed').next('.collapsiblePanel').hide();}
  if (mortgageTypeValue != 'rollover') {$('#rollover').hide(); $('#rollover').next('.collapsiblePanel').hide();}
  if (mortgageTypeValue != 'offset') {$('#offset').hide(); $('#offset').next('.collapsiblePanel').hide();}
  if (mortgageTypeValue != 'boe') {$('#boe').hide(); $('#boe').next('.collapsiblePanel').hide();}
  if (mortgageTypeValue != 'ftb' && mortgageTypeValue != 'fixed') {$('#ftbPanel').hide(); $('#ftbPanel').next('.collapsiblePanel').hide();} //takes into account that FTB mortgages are actually fixed rates, os if fixed rate is selected, FTB mortgages should be shown too.
  if (mortgageTypeValue != 'fixed' && mortgageTypeValue != 'boe') {$('#mixed').hide(); $('#mixed').next('.collapsiblePanel').hide();}

}

		// MK show all products initially
		$('tr').show(); 
		$('tr').find('td').show(); 
		$('tr').find('th').show(); 

		// MK filter out relevant products - as per customer type
		$('tbody tr:not(.' + tabcontentId + ')').find('td').hide(); 
		$('tbody tr:not(.' + tabcontentId + ')').hide();

		// MK filter out relevant products - as per min deposit
		var depositMinValue = jQuery.url.param("depositMin");
		if (depositMinValue)
		{
			$('span.deposit').each(function(){ 
			if ( $(this).text() <= depositMinValue) {
				}
			else 
			{
			  $(this).parent('td').parent('tr').find('td').hide();
			  $(this).parent('td').parent('tr').hide();
			}
			});
		}

		// MK hide subHeadings with no products underneath the subHeading
		$('.collapsiblePanel tbody').each(function(){ 
			if($(this).find('td').is(':visible')) {}
			else 
			{ 
			$(this).prev('thead').find('th').hide();
			$(this).prev('thead').find('tr').hide();
			}
		$('div.existR').hide();
		});


		// MK hide panels with no products in them
		$('.collapsiblePanel').each(function(){ 
			if($(this).find('tbody tr td').is(':visible')) {}
			else 
			{
				$(this).hide();
			  	$(this).prev('.collapsiblePanelShowHide').hide();
			}
		$('div.existR').hide();
		});

		// MK if all panels are hidden show 'no results' message
		searchResults = 0;
		$('.collapsiblePanelShowHide').each(function(){ 
			if($(this).is(':visible')) {searchResults = searchResults + 1;}
			else {}
		 });
		if(searchResults == 0) $('.noResults').show();
		;


// Close all collapsible panels content unless this is driven by search / mortgage finder, or mortgageType links
if (jQuery.url.param("customerType") || jQuery.url.param("customerType") == '') {}
	else 
	{
		$('.collapsiblePanel').hide();
		// MK 19-Aug-2011 - Reset 'arrows' state to 'down' (non-active)
		$(".collapsiblePanelShowHide").removeClass("active"); 
	}
}

// tabs history functionality
$(function(){  
  // Get the change in # value and apply the tabs-selected class to the corresponding tab
  $(window).bind('hashchange', function(){ //alert('boo');
    var hash = location.hash;    
	var firstTabLinkValue = $('ul.tabs li:first a').attr('href'); //newM
    // loop through all tabs, apply the tabs-selected class to the selected tab
    $('ul.tabs li a').each(function(){
      var that = $(this);
	  //alert($(that).parent().hasClass('tabs-selected'));
	  
//	  alert($(this).attr('href'));
	  
	  $(that).parent()[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'tabs-selected' );	
//	  $('ul.tabs li a').parent().css('border','1px solid blue');
/*	  $(that).click(function(){
		  if ($(this).attr('href') == 'existM'){
			  alert("HURRAH");
		  }
		  
		  //alert(firstTabLinkValue);						
	  });  */
	  
    });
	
	
	
	// if the url does not have a # value or has a lone # on initial page load tab update, then select the first tab by default	
	if (hash == '' || hash == '#') {
	  $('ul.tabs li:first').addClass('tabs-selected');	  
		tabcontentId = 'newM';
		showProductsByCustomerType();
	}
	// if the url does have a # value ie. #this_is_a_value, then show the relevant products
	else { //alert('show the relevant products');
		tabcontentId = hash.split('#',2)[1];
		
		if (tabcontentId == 'newM' || tabcontentId == 'newR' || tabcontentId == 'existM' || tabcontentId == 'ftb')
		{		
		showProductsByCustomerType();
		}
		// MK existing customers custom content - PDFs etc
		else {
			if(tabcontentId == 'existR') 
			$('div.existR').show();
			// MK hide all panels
			$('.collapsiblePanel').each(function(){ 			  
			  $(this).hide();
			  $(this).prev('.collapsiblePanelShowHide').hide();	
				});
		}
	}
	// run tracking update to send hit to omniture
//	setHashToProp(hash,firstTabLinkValue);
  });
  // when another tab is clicked, or the page is loaded, fire the update
  $(window).trigger('hashchange');
   });

//tracking update where the hash value is bound to a prop(props are custom omniture variables) and sent to omniture
function setHashToProp(hashreader,firstTabLinkValueReader) {
  // BEFORE SENDING THE HASH VALUE TO OMNITURE, RUN CHECK TO SEE WHAT THE HASH VALUE IS
  // THIS IS A CLEANUP TO ENSURE THAT IF URL HAS THE SOLE VALUE OF "#" THEN CHANGE THE VALUE SENT TO OMNITURE TO BLANK SO AS NOT TO CREATE A NEW ENTRY FOR "#"
  // IF THE VALUE IS EQUAL TO THE HREF OF THE FIRST TAB, THEN THE USER IS OBVIOUSLY VIEWING TAB 1, THEREFORE SET THE VALUE AGAIN TO BLANK FOR THE SAME REASON
  // DOING THIS PREVENTS RESULTS APPEARING IN THE TRACKING FOR A "#" OR "FIRST_TAB_HREF_VALUE" SO ONLY SHOWING THE ACTUAL TABS
  /*  MK commented out as there is no tracking script in the test version 
  if (hashreader == "#" || hashreader == firstTabLinkValueReader) {
	  hashreader = "";
	  //alert("EQUALS A HASH");
	  //alert(hashreader);
	  var s=s_gi('ybsprod');
	  s.linkTrackVars='prop1';
	  s.linkTrackEvents='event1';
	  s.prop1 = s.pageName + hashreader;
	  s.tl(this,'o','Hashed Link');	
  }
  else {
	  //alert("OTHER VALUE");
	  var s=s_gi('ybsprod');
	  s.linkTrackVars='prop1';
	  s.linkTrackEvents='event1';
	  s.prop1 = s.pageName + hashreader;
	  s.tl(this,'o','Hashed Link');	  
  }*/
  //alert(s.prop1);
};








/*
function mortgageRefineSearch()
{
var mortgageTypeValue = document.getElementById('mortgageType').value;

if (mortgageTypeValue == 'fixed') document.getElementById("mortgageTypeHolder").innerHTML = 'Fixed Rate Mortgage';
if (mortgageTypeValue == 'rollover') document.getElementById("mortgageTypeHolder").innerHTML = 'Rollover Fixed Rate Mortgage';
if (mortgageTypeValue == 'offset') document.getElementById("mortgageTypeHolder").innerHTML = 'Offset Mortgage';
if (mortgageTypeValue == 'boe') document.getElementById("mortgageTypeHolder").innerHTML = 'Bank of England Tracker';
if (mortgageTypeValue == 'ftb') document.getElementById("mortgageTypeHolder").innerHTML = 'First Time Buyer Mortgage';

var customerTypeValue = document.getElementById('customerType').value;

if (customerTypeValue == 'newM') document.getElementById("customerTypeHolder").innerHTML = 'New customer moving home';
if (customerTypeValue == 'newR') document.getElementById("customerTypeHolder").innerHTML = 'New customer remortgaging';
if (customerTypeValue == 'existR') document.getElementById("customerTypeHolder").innerHTML = 'Existing customer changing deal';
if (customerTypeValue == 'existM') document.getElementById("customerTypeHolder").innerHTML = 'Existing customer moving home';
if (customerTypeValue == 'ftb') document.getElementById("customerTypeHolder").innerHTML = 'First time buyers';

var depositMinValue = document.getElementById('depositMin').value;

document.getElementById("depositMinHolder").innerHTML = depositMinValue;

}*/

function customerTypeValidation()
{
	// If existR - disable other fields as they are irrelevant
	if (document.getElementById('customerType').value == 'existR')
	{
	$('select#mortgageType').attr("disabled","disabled");
	$('select#depositMin').attr("disabled","disabled");
	}
	else 
	{
	$('select#mortgageType').attr("disabled","");
	$('select#depositMin').attr("disabled","");
	}
}

function formValidation()
{
	if (document.getElementById('customerType').value == '' || document.getElementById('customerType').value == null)
	{alert('Please select Customer Type'); }
	return false
}
