$(document).ready(function(){
	$('#imageReplace a').live('click', function(){
		$('#uploadButton').click();
		return false;
	});

	$('#uploadForm').submit(function(){
		$('#TB_ajaxContent .error').remove();
		$('#uploadForm :submit')
		.attr('title', $('#uploadForm :submit').attr('value') )
		.attr('value', 'Uploading...');
	});

	$('#uploadIframe').load(function(){
		$('#uploadForm :submit').attr('value', $('#uploadForm :submit').attr('title') );

		if ( $('#uploadIframe').contents().find('#image').length == 1 )
		{
			$('#imageReplace').remove();
			var path = $('#uploadIframe').contents().find('#image').text();
			$( '<p id="imageReplace"><img src="' + path + '" /><input type="hidden" name="84ad04e1-a931-4938-aafe-474290f7b052" value="' + path + '" /><br><a href="#">Replace the image</a></p>' ).insertBefore('#uploadButton');
			$('#uploadButton').hide();
			$('#TB_ajaxContent .error').remove();
			tb_remove();
		}
		else
		{
//			$( '<p class="error">' + $('#uploadIframe').contents().find('div:first').html() + '</p>' ).insertBefore('#uploadForm');
			$( '<p class="error">Please upload only image files. (JPG, GIF or PNG format)</p>' ).insertBefore('#uploadForm');
		}
	});

	$('#joinForm').submit(function(){
		$('input.error, select.error', this).removeClass('error');
		$('span.error', this).remove();
		
		$('.required', this).each(function(){
			if ( $(this).is('input') )
			{
				if ( '' == $(this).val() )
				{
					$(this).addClass('error');
				}
			};
			if ( $(this).is('select') )
			{
				if ( '' == $(':selected', this).val() )
				{
					$(this).addClass('error');
				}
			};
		});

		if ( $('.product :checked').length == 0 )
		{
			$('<br /><span class="error">Please Choose one.</span>').appendTo('.chooser');
		}
		
		if ( $('.error', this).length > 0 ) 
		{
			$('<span class="error">Please complete all fields marked below.</span>').insertAfter('#joinForm h3');
			window.location = '#top'; 
			return false;
		}

		// show preview

		$('#previewLink')
		.attr('href', 'join_preview.php?' + $('#joinForm').serialize() + '&TB_iframe=true&height=490&width=740')
		.click();
		return false;

	});
	
	$('#.product :checkbox').click(function(){
		$('#.product :checkbox').not(this).removeAttr('checked');
		$(this).attr('checked', 'checked');
		
		$(this).closest('div.product')
		.addClass('chosen');
	
	});
	
	$('.product .button')
	.prepend('<a href="#" class="checkbox">&nbsp;</a> Choose this one.')
	.find(':checkbox')
	.hide();
	
	$('.product .checkbox').click(function(){
		
		if($(this).closest('div.product').find('input').val() == "Free")
		  $(".premium").hide();
		else
		  $(".premium").show();
		  
		  
		var product = $(this).closest('div.product');
		$('.product :checkbox').removeAttr('checked');
		$('div.product').removeClass('chosen');
		$(product).addClass('chosen');
		$(':checkbox', product).attr('checked', 'checked');
		return false;
	});

	$('<b class="count">220 Characters</b>')
	.insertAfter('.textarea');

	$('.textarea')
	.keyup(function(){
		$(this).next('b').text( (220 - $(this).val().length) +  ' Characters');
		if ( $(this).val().length > 220 )
		{
			$(this).val( $(this).val().substring(0,219) );
		}
	});

	// populate form
	if ( $('#listing_3ff94507-4532-4ccf-8ecc-2bada1a06b79').length == 1 ) 
	{
		$('#enhanced .checkbox')
		.click();
	}
	else
	{
		if ( $('#listing_20e5d0f1-e70b-4ed7-b8dc-e9fe2d1a43e3').length == 1 ) 	
		{
			$('#basic .checkbox')
			.click();
		}
	}

	if ( $('#listing_20e5d0f1-e70b-4ed7-b8dc-e9fe2d1a43e3').length == 1 ) $('#20e5d0f1-e70b-4ed7-b8dc-e9fe2d1a43e3 option[value=' + $('#listing_20e5d0f1-e70b-4ed7-b8dc-e9fe2d1a43e3').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_875c2c06-7495-4b05-b812-777dae5865ad').length == 1 ) $('#875c2c06-7495-4b05-b812-777dae5865ad option[value=' + $('#listing_875c2c06-7495-4b05-b812-777dae5865ad').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_b7a81982-35b3-445f-8c21-134fad0ea735').length == 1 ) $('#b7a81982-35b3-445f-8c21-134fad0ea735 option[value=' + $('#listing_b7a81982-35b3-445f-8c21-134fad0ea735').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_c298f23d-5d50-4ac3-8bc8-a8480c963c86').length == 1 ) $('#c298f23d-5d50-4ac3-8bc8-a8480c963c86 option[value=' + $('#listing_c298f23d-5d50-4ac3-8bc8-a8480c963c86').val()  + ']').attr('selected', 'selected');
});