$(document).ready(function(){

    // initialise Superfish 
    $('ul.sf-menu').supersubs({ 
		minWidth:    12,   // minimum width of sub-menus in em units 
		maxWidth:    27,   // maximum width of sub-menus in em units 
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family
    }).superfish({
	    delay:       500,                              // one second delay on mouseout 
        animation:   {opacity:'show', height:'show'},  // fade-in and slide-down animation 
        speed:       'fast',                           // faster animation speed 
        autoArrows:  true,                            // disable generation of arrow mark-up 
        dropShadows: false                             // disable drop shadows
	});
	
	// Add home-menu class to the navigation
	$('.front ul.sf-menu > li:first-child').addClass('home-active');
	
	
	// Font Resize Controls
	$('a.fontsizeswitch').click(function(){
		var $mainText = $('#main-content-content');
		var currentSize = $mainText.css('font-size');
		var num = parseFloat(currentSize, 10);
		var unit = currentSize.slice(-2);
		if (this.id == 'increasefont'){
			num = num * 1.2;
			if (num > 20) {
				num = 20;
			}
		} else if (this.id == 'decreasefont'){
			num = num / 1.2;
			if (num < 11) {
				num = 11;
			}
		}
		$mainText.css('font-size', num + unit);
		return false;
	});
	
	// Font Resize - Index Page
	$('a.fontsizeswitchindex').click(function(){
	        var $homePanels = $('#home-panels, #home-about');
	        var currentSize = $homePanels.css('font-size');
	        var num = parseFloat(currentSize, 10);
	        var unit = currentSize.slice(-2);
	        if (this.id == 'increasefont'){
	        	num = num * 1.2;
	        	if (num > 14) {
	        		num = 14;
	        	}
	        } else if (this.id == 'decreasefont'){
	        	num = num / 1.2;
	        	if (num < 11) {
	        		num = 11;
	        	}
	        }
	        $homePanels.css('font-size', num + unit);
	        return false;
	    });
	
	// Animate Slideshow
	$('#slideshow ul').innerfade({
		animationtype: 'fade',
		speed: 750,
		timeout: 8000
	});
	
	// Infield Labels
	$('#sidebar-right #edit-distance-postal-code-wrapper label').text('Postcode');
	$('#sidebar-right #edit-distance-postal-code-wrapper label').inFieldLabels();
	
	// Validate Postcodes on the Sidebar Location Searches
	if ($('#sidebar-right #views-exposed-form-clinic-block-default').length) {
		$('#sidebar-right #edit-submit-clinic-block').click(function () {
			var reg = /^([0-9]{4})$/;
			var postcode = $('#sidebar-right #edit-distance-postal-code').val();
			
			if (reg.test(postcode) == false) {
				alert('Please enter a valid Postcode');
				return false;
			}
		});
	}

	// Make the Search Forms usable & pretty
	$('#views-exposed-form-clinic-block-page-1 div.views-exposed-widget:first, .view-id-clinic_search_page div.views-exposed-widget:first, form#views-exposed-form-clinic-search-page-default div.views-exposed-widget:first').before('<div id="search-title"><p>Search by</p></div>');
	$('#views-exposed-form-clinic-block-page-1 div.views-exposed-widget:first, .view-id-clinic_search_page div.views-exposed-widget:first, form#views-exposed-form-clinic-search-page-default div.views-exposed-widget:first').after('<div id="search-divider"><p>or</p></div>');
	
	// When selecting a State, clear Postcode input
	$('#edit-province').change(function() {
		if ( $('#edit-distance-postal-code').length ) {
			$('#edit-distance-postal-code').val(''); 
		}
	});
	
	// When entering a Postcode, set the State Field to default
	$('#edit-distance-postal-code').focus(function() {
		if ( $('#edit-province').length ) {
			$('#edit-province').val('All'); 
		}
	});
	
	// // Fake Subscribe Form Redirect
	// // TODO: Add email validation
    $('.subscribelink').click(function () {
          var href = $(this).attr('href');
          var email = $(this).prev().val();
	   	  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

          if (email=="enter your email here") {
              return false;
          } else {
	
			  if (reg.test(email) == false) {
			      alert('Please enter a valid Email Address');
			      return false;
			   } else {
                  href = href+"?email="+email;
                  $(this).attr('href',href);
		  	  }
          }
    });

	// Newsletter Subscribe Page - Node 42
	if ($('body#node-42').length) {
		
		function queryParam(name) {
		    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		    var results = (new RegExp("[\\?&]"+name+"=([^&#]*)")).exec(window.location.href);
		    return results ? results[1] : "";
		}
	
		var email = queryParam('email').replace(/%40/, "@");
	    $('#node-42 input#edit-email').val(email);
	
	}
	
	// Refer a Friend Form
	if($('body#node-8').length) {
		// Add Helpful Titles
		$('#taguchi-build-form > div').prepend("<h3>Your details</h3>");
		$('#taguchi-build-form #edit-email-wrapper').after("<h3>Your friend's details</h3>");
	}

});



// Cufon Font Replacement

Cufon.replace('#textresize span')('.panel h2')('.panel h2 a')('#home-about h2')('#main h1')('#main h2')('#main h3')('#main h4')('#taguchi-build-form h3');

Cufon.replace('#site-menu ul li a', { hover: true, textShadow: '#fff 0 1px' });
Cufon.replace('.tile a', { hover: true, textShadow: '#fff 0 1px'});
Cufon.replace('.tile-alt a', { hover: true, textShadow: '#69041a 0 -1px'});
Cufon.replace('#sidebar-right .block-views h2', { hover: true, textShadow: '#69041a 0 -1px'});
