$(function() {

	$('.infiniteCarousel').infiniteCarousel();

	// If JavaScript is enabled, style the login form accordingly
	$.browser.msie ? IEformStyles() : formStyles();
	
	function IEformStyles() {
		// For Login Form
		$('#login').css({'padding-top':'8px'});
		$('#login label').css({'top':'4px','left':'10px'});
		$('.sidebarlogin_otherlinks').css({'top':'32px'})
		$('#login_error').css({'top':'32px'});

		// For Get Started Form
		$('#user-info').css({'padding':'10px','padding-top':'6px','padding-bottom':'0'});
		$('#user-info p').css({'margin-bottom':'0'});
		$('#user-info label').css({'left':'10px','top':'5px'});
	}

	function formStyles() {
		// For Login Form
		$('#login').css({'padding-top':'8px'});
		$('#login label').css({'top':'4px','left':'10px'});
		$('.sidebarlogin_otherlinks').css({'top':'32px'})
		$('#login_error').css({'top':'32px'});
		
		// For Get Started Form
		$('#user-info').css({'padding':'10px','padding-top':'6px','padding-bottom':'0'});
		$('#user-info p').css({'margin-bottom':'0'});
		$('#user-info label').css({'left':'10px','top':'5px'});
	}


	// Hide real submit, put link submit that triggers a click on the real submit. Works sitewide, not doing this on tell a friend, must ajaxify it first.
	$('input[type=submit]:not(#submit_friend)').each(function() {
		var btnText = $(this).val();
		$(this).hide();

		$('<a href="javascript:;" class="button">' + btnText + '</a>')
		.insertAfter(this)
		.bind('click', function() {
			$(this).siblings('input[type=submit]').trigger('click');
		});
	});


	// Hide the "Additional Revision" form and insert a button to slide it down upon click.
	$('#design-revision').hide();
	$('<div id="addrev-wrap"><p>If you would like an additional round of revisions, they will be charged at $150 USD. Please click the button to show the feedback form and start an additional round of revisions.</p><a id="addrev-button" href="javascript:;">Additional Round of Revisions</a>').insertBefore('#design-revision');
	$('#addrev-button').click(function() {
		$('#design-revision').slideDown();
	});


	// Auto-submit all Paypal Submit pages
	setTimeout('document.paypal_form.submit()',4000);



	// Attach fancy infield text hiding/fading upon user input for login form
	$('#login label').inFieldLabels();
	
	// Attach fancy infield text hiding/fading upon user input on the get-started form
	$('#user-info label').inFieldLabels();
	
//***********************************************************//
//************ Abstracted form controls *********************//
//***********************************************************//
	// Hide/show and reset checkbox other field when checkbox is clicked based on other span wrap '.jq-other-text' 
	var checkWrap = $('.jq-other-text');

	// Determine if the checkbox is checked upon page load. If so, show the input field.
	$(checkWrap).each(function() {
		if ($(this).children('input:checkbox').attr('checked') == '') {
			$(this).children('input:text').hide();
		}
	});

	// Functionality of hiding & clearning the input field and showing the input field.
	$(checkWrap).children('input:checkbox').click(function() {
		if ($(this).is(':checked')) {
			$(this).siblings(':input').show();
		} else {
			$(this).siblings(':input').val('').hide();
			
		}
	});

	/**********************************************************/
	/** RADIO BUTTON DESCRIPTION BLOCKS HIDE / SHOW **/
	$('input.sib-hide').each(function() {
		if ($(this).attr('checked') == '') {
			$(this).siblings('.desc-block').hide();
		}
	});
	
	$('input.sib-hide').click(function() {
		var name = $(this).attr('name');
		var others = $('input[name='+name+']:radio');
		$(others).siblings('.desc-block').children(':input').val('');
		$(others).siblings('.desc-block').hide();
		$(this).siblings('.desc-block').show();
	})


	/**********************************************************/
	// The like/dislike (and other add next buttons?)
	$('.add-next').each(function() {
		if ($(this).children().children(':input').val() == ('' || 'http://')) {
			$(this).hide();
		}
	});

	$('.add-next').before('<p><a href="javascript:;" class="add-next-btn">Add another</a></p>');

	$('.add-next-btn').click(function() {
		$(this).parent().next('.add-next').show();
		$(this).parent().hide();
	});


	// Initialize the 'pretty photo' behavior for images
	$('.gallery-item a').prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 15, /* padding for each side of the picture */
		opacity: 0.7, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: ' of ', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		modal: false, /* If set to true, only the close button will close the window */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){} /* Called when prettyPhoto is closed */
	}); 


	// Add 'rel' attribute to all images in a specific gallery
	$('.gallery a').each(function() {
		var galleryId = $(this).parents('.gallery').attr('id');
		$(this).attr('rel', 'prettyPhoto['+ galleryId +']');
	});


	// Tell a Friend fancybox
	$('.page-item-16 a').attr('href', 'http://2000dollarwebsite.com/wp-content/themes/2kw/_taf_form.php?iframe=true');
	$('.page-item-16 a').fancybox({
		'hideOnContentClick': false,
		'autoDimensions': false,
		'height': 350
	})


/* MAYBE LATER
	// If JS is enabled...replace the textareas for jQuery Max Input with the appropriate divs
	$('<div class="max-200"><?=stripslashes(@$fields["comments"])?></div>').insertAfter('.max-200');
	$(':input.max-200').hide();

	// jQuery Max Input initialization
	$('.max-200').maxinput({
		position	: 'topleft',
		showtext 	: true,
		limit		: 130
	});
*/
});


// Hide real submit, put link submit that triggers a click on the real submit. Works sitewide.
function prettifyButtons(theButton) {
	$(theButton).hide();
	
	$('<a href="javascript:;" class="button">' + $(theButton).val() + '</a>')
		.insertAfter(theButton)
		.bind('click', function() {
			$(this).parents('form').trigger('submit');
		});
}


function ajaxifyForm(theForm) {
	// hide the async_notifications div at start.
	$('#async_notifications').toggle();

	$(theForm).submit(
		function () {
			$('#is_async').val(true);
			var request = $(':input').serializeArray();
			$('#async_notifications').html('<h4>Status</h4><p>' + 'Processing the form...</p>').fadeIn('fast');
			
			$.post(
				this.action,
				request,
				function(response) {
					var caption = (response.result == true) ? 'Confirmed' : 'Error';
					// show the response message
					if (response.errors.length > 0) {
						$('#async_notifications').html('<div class="error">Please fix the following errors:\n<ul></ul></div>');
						// Print out errors in a 
						$.each(response.errors, function() {
							$('<li>' + this + '</li>').appendTo('#async_notifications ul');
						});
					} else {
						$(theForm).hide();
						$('#async_notifications').hide().html('<h4>' + caption + '</h4><p>' + response.message + '</p>').fadeIn('slow');
					}
					
					// hide the newsletter signup 'saving your information' div.
					if (response.result == true) {
						$(theForm).fadeOut('slow');
					}
				},
				'json'
			);
			return false;
	});
}