(function($){
	$(document).ready(function() {
		$('form#subscribe_form').jqTransform({imgPath:'jqtransformplugin/img/'});
		function gforms_abs_label() {
			if ( ! $(this).find('form').hasClass('js_gform') ) {
				$('.contact-us .gform_wrapper form').addClass('js_gform')
				$('.contact-us .gform_wrapper').bind('contentchange', gforms_abs_label);
				$('.contact-us .gfield input, .contact-us .gfield textarea').each(function(){
					if ( $(this).val() != '' ) {
						$(this).parents('.gfield').find('label').hide();
					};
				});
				$('.contact-us .gform_wrapper').bind('contentchange', gforms_abs_label);
			};
		}

		$('.contact-us .gfield input, .contact-us .gfield textarea').live('focus', function(){
			$(this).parents('.gfield').find('label').hide();
		}).live('blur', function(){
			if ( $(this).val() == '' ) {
				$(this).parents('.gfield').find('label').show();
			};
		})

		$('input[name="pm_type"]').change(function(){
			if ( $(this).val() == 'paypal' && $(this).is(':checked') ) {
				$('#subscribe_form .contactus-submit-button, .cc_row').hide();
				$('#pay_with_paypal').show();
				$('.cc_row input, .cc_row select').attr('disabled', 'disabled');
				if ( ! $('#cb_address_same').is(':checked') ) {
					$('#cb_address_same').attr('checked', 'checked').change();
				};
			} else if ( $(this).val() != 'paypal' && $(this).is(':checked') ) {
				$('#subscribe_form .contactus-submit-button, .cc_row').show();
				$('#pay_with_paypal').hide();
				$('.cc_row input, .cc_row select').removeAttr('disabled');
				if ( $('#cb_address_same').is(':checked') ) {
					$('#cb_address_same').removeAttr('checked', 'checked').change();
				};
			};
		});

		$('select[name="cc_type"]').change(function(){
			if ( $(this).val() == 'Amex' ) {
				$('input[name="ccn[]"]:eq(3)').attr('maxlength', '3');
			} else {
				$('input[name="ccn[]"]:eq(3)').attr('maxlength', '4');
			};
		})

		$('input[name="ccn[]"], input[name="phone1[]"], input[name="phone2[]"]').bind('keyup', function(){
			var name = $(this).attr('name');
			// this returns 1 for the first element(in this case at least), so :eq(index) means the next element
			var index = $(this).parents('span').index();
			var val = $(this).val();
			if ( val.length == $(this).attr('maxlength') ) {
				if ( $('input[name="' + name + '"]:eq(' + index + ')').length != 0 ) {
					$('input[name="' + name + '"]:eq(' + index + ')').focus();
				};
			};
		})

		gforms_abs_label();
		$('.contact-us .gform_wrapper form').addClass('js_gform')
		$('.contact-us .gform_wrapper').bind('contentchange', gforms_abs_label);
		
		$('.f-nav > ul > li:last').addClass('last');
		$('.blink').focus(function(){
			var val = $(this).val();
			if ( val == $(this).attr('title') ) {
				$(this).val('');
			};
		}).blur(function(){
			var val = $(this).val();
			if ( val == '' ) {
				$(this).val($(this).attr('title'));
			};
		});
		
		$('#set-zip').submit(function(){
			var field = $(this).find('.field');
			if ( $(this).find('.field').val() != '' ) {
				var zip = field.val();
				if ( ! zip.match(/[0-9]+/) ) {
					alert('Please, enter only numbers.');
					field.focus();
					return false;
				};
				
				return true;
			};
			alert('Please, enter your Zip Code.');
			
			return false;
		})

		$('.show-more-info').click(function(){
			if ( $('.more-info').is(':visible') ) {
				$(this).html('More Info &raquo;');
			} else {
				$(this).html('Less Info');
			};
			$('.more-info').slideToggle();
			return false;
		})
		
		$('#navigation > ul > li:last').addClass('last');
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			DD_belatedPNG.fix('.bg-top , .bg , .bg-bottom , .footer-left p img');
		};

		$('#cb_address_same').change(function(){
			var fields = ('.billing-information input[type="text"], .billing-information select');
			if ( $(this).is(':checked') ) {
				$(fields).attr('disabled', 'disabled');
			} else {
				$(fields).removeAttr('disabled');
			};
		})

		$('#ss_start_date input').datepicker({dateFormat: 'd/m/yy'});
		$('.ss_start').change(function(e){
			if ( $(this).val() == 'date' && $(this).is(':checked') ) {
				$('#ss_start_date').show();
				$('#ss_start_date input').removeAttr('disabled').show().focus();
			} else if ( $(this).val() == '' && $(this).is(':checked') ) {
				$('#ss_start_date').hide();
				$('#ss_start_date input').attr('disabled', 'disabled');
			};
		})
		$('.datepick').click(function(){
			$(this).parents('.row').find('label').click();
			return false;
		})
	});

	// custom event for monitoring elements content change - use this only if there is no other way, since it gets executed every 100 ms... :)
    var interval;

    $.event.special.contentchange = {
		setup: function(){
			var self = this,
			$this = $(this),
			$originalContent = $this.html();
			interval = setInterval(function(){
				if( $originalContent != $this.html() ) {
					$originalContent = $this.html();
					$.event.handle.call(self, {type:'contentchange'});
				}
			},100);
		},
		teardown: function(){
			clearInterval(interval);
		}
	};
})(jQuery)
