jQuery.extend(jQuery.easing, {
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	}
});

jQuery.fn.input_hint = function() {
	return this.each(function() {
		var $this = $(this);

		if ($this.attr('value').length == 0)
			$this.attr('value', $this.attr('title'));

		$this.focus(function() {
			if ($this.attr('value') == $this.attr('title'))
				$this.attr('value', '');
		}).blur(function() {
			if ($this.attr('value') == '')
				$this.attr('value', $this.attr('title'));
		}).parents('form').submit(function() {
			if ($this.attr('value') == $this.attr('title'))
				$this.attr('value', '');
		});
	});
};

jQuery.fn.input_hint2 = function() {
	return this.each(function() {
		var $this = $(this);

		if ($this.attr('value').length == 0)
			$this.attr('value', $this.attr('title'));

		$this.focus(function() {
			if ($this.attr('value') == $this.attr('title'))
				$this.attr('value', '');
		}).blur(function() {
			if ($this.attr('value') == '')
				$this.attr('value', $this.attr('title'));
		});
	});
};


var Site = {

	// this vars should be set in <head> server-side
	config: {
		base_url: '',
		site_url: ''
	},
	
	// this method is called on every page
	init: function() {

		// On Dom Ready
		jQuery(function($) {

			$.each($('#navigation a.subnavigation_title'), function() {
				var $this = $(this);
				$this.click(function(e) {
					e.preventDefault();
					
					// Make sure all subnav's are closed.
					$.each($('#navigation a.subnavigation_title'), function(k, v) {
						var $this = $(this);
						$this.next().hide('slow', 'easeOutQuart');
						$this.removeClass('on');
					});
					

					// Open subnav only if it's closed
					if ($this.next().css('height') == 'auto')
					{
						$('#navigation a').removeClass('on');
						$this.addClass('on');
						$this.next().animate({
							height: 'toggle'
						}, 'slow','easeOutQuart');
					}

				});

			});
			
			if ($('#landing_page_form').length)
			{
				Site.contact_info.validate();
			}
			$('input.hint, textarea.hint').input_hint();
			
			$('#infusionsoft_newsletter_signup #newsletter_signup_form').submit(function() {
				var $this = $(this);
				return Site.is_valid_email($this.find('input[type=text]').val());
			});


		});
		
		// On Window Load
		jQuery(window).load(function ($) {

		});
		
		// Load Immediately
		(function($) {
		
		})(jQuery);

		if ($.browser.msie && $.browser.version <= 6 )
		{
			// gay stuff goes here
		}
	},
	
	is_valid_email: function(email)
	{
		return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
	},
	contact_info: {
		// This function shall be called inline around a domready event.
		init: function() {
			if ($('#infusionsoft_contactus_form').length)
			{
				Site.contact_info.infusionsoft();
			}
			Site.contact_info.validate();
		},
		
		infusionsoft: function() {
			var $trs = $('#infusionsoft_contactus_form tbody:first > tr');

			// Removing headers
			$trs.find('td[colspan=99]').remove();
			
			// Removing submit button
			$trs.eq($trs.length - 1).remove();
			
			$trs = $('#infusionsoft_contactus_form tbody:first > tr');

			var $form_label, $td1, $td2, $input, valid_form_types = ['text', 'checkbox', 'textarea'];

			$trs.each(function(k, v) {
				var $this = $(this);
				var $tds = $this.children('td');
				if ($tds.length == 2)
				{
					$td1 = $tds.eq(0);
					$td2 = $tds.eq(1);
					
					$td1.addClass('form_label');
					$td2.addClass('form_element');
					$td2.find('input[type=text]').addClass('text');

					if ($td2.find('input[type=text]').length == 0 && $td2.find('textarea').length == 0)
					{
						$td2.addClass('nontext');
					}

					if ($td1.text().indexOf('*') >= 0)
					{
						$td1.text('* ' + $td1.text().replace('*', ''));
						$td2.find('input[type=text]').addClass('validate').attr('rel', 'required');
					}
					

				}
			});

			$('#infusionsoft_contactus_form form').append('<div class="submit_button_wrapper"><input type="image" src="/files/home/images/submit_button.png" value="Submit" /></div>');
			$('#infusionsoft_contactus_form form').submit(function(e) {
				var form_success = true;
				var $this = $(this);
				$this.find('input[type=text]').each(function() {
					var $input = $(this);
					if ($input.attr('rel') == 'required' && ($input.val() == '' || $input.val() == $input.attr('title')))
					{
						form_success = false;
						return false;
					}
				});
				return form_success;
			});

			$('#infusionsoft_contactus_form').show();
			
		},

		validate: function() {
			(function($) {
				$('.tooltip').bt();
			
				$(".validate").blur(function(){
					var el = $(this);
					if (!el.attr('rel'))
						return false;
					$.ajax({
						type: 'POST',
						url: '/validate/',
						data: { action: 'validate', rule: el.attr('rel'), value: el.val() == el.attr('title') ? '' : el.attr('value') },
						async: false,
						success: function(str) {
							el.next('span').remove();

							var result = str.split("|");
							var valid = result[0];
							var tip = result[1];

							if(valid) {
								el.after('<span style="margin:0 0 0 7px;"><img src="/files/icons/accept.png" border=0 ></span>') ;
								el.css( 'background-color', '#FFFFFF' );
							} else if(str.length > 0) {
								el.after('<span class="tooltip" title="' + tip + '" style="margin:0 0 0 7px;"><img src="/files/icons/exclamation.png" border="0"></span>'); 
								el.css( 'background-color', '#FFFF99' );
								$('.tooltip').bt();
							}
						}
					});
				});
			
			})(jQuery);

		}
	}
	
};

Site.init();