// Check if something exists
jQuery.fn.exists = function(){return jQuery(this).length>0;}


$(document).ready(function(){
	// Font Resize Controls
	$('a.fontsizeswitch').click(function(){
		var $mainText = $('#maincol');
		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
	});
	
	// Fake Subscribe Form Redirect
	// TODO: Add email validation
    $('.subscribelink').click(function () {
          var href = $(this).attr('href');
          var email = $(this).prev().val();
          if (email=="enter your email here") {
              return false;
          } else {
              href = href+"?email="+email;
              $(this).attr('href',href);
          }
    });

	// Fix checkbox layout on /hearing-loss-guide.aspx page
    $('li.optin-hearing-guide div.label').css('display','none');
    var label1 = $('li.optin-hearing-guide div.label span').text();
    label1 = '<label>'+label1+'</label>';
    $('li.optin-hearing-guide div.field').append(label1);

    $('li.optin-hearing-check div.label').css('display','none');
    var label2 = $('li.optin-hearing-check div.label span').text();
    label2 = '<label>'+label2+'</label>';
    $('li.optin-hearing-check div.field').append(label2);

	// Hide Intro Msgs on forms on submit
	if ($('div.form-box span.received').exists()) {
	    $('div.form-box div#intromsg').hide();
	}

	// Add required note to forms
	$('div.form-box-inner input[type="submit"]').before('<p class="required"><span>*</span> denotes a required field</p>');

});

// Cufon Font Replacement
Cufon.replace('#main-nav ul li a', {
    hover: true,
    textShadow: '#fff 0 1px'
});
Cufon.replace('.tile a', {
    hover: true,
    textShadow: '#fff 0 1px'
});
Cufon.replace('.rmItem a', {
    hover: true,
    textShadow: '#fff 0 1px'
});
Cufon.replace('.tile-alt a', {
    hover: true,
    textShadow: '#69041a 0 -1px'
});
Cufon.replace([ '#main-nav ul li.textresize span','.panel h1','.panel h2','.panel h3','#home-about h1','#home-about h2','#home-about h3','#maincol h1','#maincol h2','#maincol h3','#maincol h4' ]);
