$(function(){
  // Put recaptcha in thickbox.
  var btn = '';

  if ($('input.comment-submit').length){
    btn = 'input.comment-submit';
  }
  else if(window.location.pathname === '/'){
    btn = 'input#home-submit';
  }

  if(btn !== ''){
    $(btn).live('click', function(e){
      e.preventDefault();
      var t = this.title || this.name || null;
      var a = this.href || this.alt;
      var g = this.rel || false;
      tb_show(t,a,g);
      this.blur();
      Recaptcha.create('6Lf5twYAAAAAABHbISRf3jh1KuepOtTPfIx8PpbV', // public key
        'TB_ajaxContent', {
          theme   : 'custom',
          callback: Recaptcha.focus_response_field
        }
      );
      $('#recaptcha_submit').remove();
      $('#recaptcha_widget').after('<div id="recaptcha_submit" style="clear:both;padding:10px;text-align:right;"><input type="image" value="submit" src="images/contact-form-submit.jpg" /></div>');
    });

    // Capture necessary data from recaptcha, add to form and submit.
    $('#recaptcha_submit input').live('click', function(e){
      e.preventDefault();
      $(btn).before('<input id="recaptcha_challenge_field" type="hidden" value="' + $('#recaptcha_challenge_field').val() + '" name="recaptcha_challenge_field" />');
      $(btn).before('<input id="recaptcha_response_field" type="hidden" value="' + $('#recaptcha_response_field').val() + '" name="recaptcha_response_field" autocomplete="off" />');
      tb_remove();
      $('#recaptcha').remove();
      $(btn).unbind('click').trigger('click');
    });
  }
});

var Site = {
	init: function(){
		$('.flip-button').click(function(){
			var effect = 'slide';
			var currentSide = $(this).attr('rel');
			if(currentSide != '2'){
				$(this).parents('.home-box').children('.home-box-content-container').children('.side-1').hide(effect,{},500);
				$(this).parents('.home-box').children('.home-box-content-container').children('.side-2').show(effect,{},500);
				$(this).attr('rel','2');
			}else{
				$(this).parents('.home-box').children('.home-box-content-container').children('.side-2').hide(effect,{},500);
				$(this).parents('.home-box').children('.home-box-content-container').children('.side-1').show(effect,{},500);
				$(this).attr('rel','1');
			}
			return false;
		});

		var formInputControl = function(selector){
			$(selector).each(function(){
				var startText = $(this).val();
				jQuery.data($(this).get(0),'def',startText);
				$(this).bind('focus',function(e){
					if($(this).val() == jQuery.data($(this).get(0),'def')){
						$(this).val('');

					}
				}).bind('blur',function(e){
					if($(this).val() == ''){
						$(this).val(jQuery.data($(this).get(0),'def'));

					}
				});
			});
		}

		var subNavLength = $('#secondary-nav ul li a').length;
		if(subNavLength > 0){
			var totalWidth = 798 - (subNavLength + 1);
			var eachWidth = Math.floor(totalWidth/subNavLength);
			var finalWidth = eachWidth + (totalWidth - (eachWidth * subNavLength));

			var linkCount = 0;
			$('#secondary-nav ul li a').each(function(){
				if(linkCount == 0){
					$(this).width(finalWidth);
				}else{
					$(this).width(eachWidth);
				}
				linkCount++;
			});
		}

		formInputControl('form input,textarea');
        $('#what-if').each(function(){ $(this).cycle({ timeout: 5500 }); });
        $("#home-fa").each(function(){ $(this).accordion({event: "mouseover"});});
	}
};
$(function(){
	Site.init();
});