/*
	
*/
function showauthor() {
	document.getElementById('buyerslide').style.display='none';
	document.getElementById('authorslide').style.display='block';

	document.getElementById('buyertab').setAttribute("class", "buyeroff" );
	document.getElementById('authortab').setAttribute("class", "authoron")
	
}
function showbuyer(){
	document.getElementById('buyerslide').style.display='block';
	document.getElementById('authorslide').style.display='none';

	document.getElementById('buyertab').setAttribute("class", "buyeron" );
	document.getElementById('authortab').setAttribute("class", "authoroff")
}


function mojo_handle_js_inputs( name, file_name, uploaded_file_name )
{	
	if( jQuery('#jquery_form_' + file_name ).length )
	{
		jQuery('#jquery_form_' + file_name ).val( name );
		jQuery('#form_' + file_name + 'Queue div.saved-file-name').text( 'File Saved As : ' + uploaded_file_name );
	}
	else
	{
		jQuery('#seller-upload-form').append( '<input type="hidden" name="jquery_form_' + file_name + '" id="jquery_form_' + file_name + '" value="' + name + '" />' );
		jQuery('#form_' + file_name + 'Queue').append( '<div class="saved-file-name">File Saved As : ' + uploaded_file_name + '</div>' );
	}
}

function mojo_check_user_name( user_name )
{
	 var message = jQuery.ajax(
		{
			type: 'POST',
			url: '/wp-content/themes/mojo/_app/library/ajax.php?a=check-user-name',
			data: 'user_name=' + user_name,
			dataType: 'html',
			async: false,
			success : function( msg ) { return msg; }
		}
	);
	
	return message.responseText;
}

function mojo_check_user_email( user_email )
{
	var message = jQuery.ajax(
		{
			type: 'post',
			url: '/wp-content/themes/mojo/_app/library/ajax.php?a=check-user-email',
			data: 'user_email=' + user_email,
			dataType: 'html',
			async: false,
			success: function( msg ) { return msg; }
		}
	);
	
	return message.responseText;
}

function mojo_check_both( user_name, user_email )
{
	var message = jQuery.ajax(
		{
			type: 'post',
			url: '/wp-content/themes/mojo/_app/library/ajax.php?a=check-both',
			data: 'user_name=' + user_name + '&user_email=' + user_email,
			dataType: 'html',
			async: false,
			success: function( msg ) { return msg; }
		}
	);
	
	return message.responseText;
}


/* jQuery Suckerfish Drop Down */

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};

// jQuery onDocument Ready functions
// Always start with the jQuery variable
// then pass the $ variable threw the function command
// to prevent issues with other scripts that might use $
jQuery(document).ready( 

	function($)
	{
		// Category Navigation Sub Navs
		
		$("#nav-cat li.sub-nav-cat-hover").hover(
			function(){ $("ul", this).fadeIn("fast"); }, 
			function() { } 
		);
		if(document.all) {
			$("#nav-one li.sub-nav-cat-hover").hoverClass ("sfHover");
		}
		
		
		
		// Lightbox
		$('a.lightbox').lightBox();
		
		// Screen shot Previews

		screenshotPreview();
		
		// If the home-category tabs are on the current page
		if( $('#home-category-tabs').length )
		{
			// Switch the content and the current state of the tab when clicked
			$('#home-category-tabs a').click(

				function()
				{
					var parent_class = $(this).parent('li').attr('class');

					if( '-off' == parent_class.substring( parent_class.length - 4 ) )
					{
						var switch_to = parent_class.substring( 0, parent_class.length - 4 ) + '-cat'

						$('.category-tab-content').hide()

						$('#' + switch_to).show()

						$('#home-category-tabs li').each(

							function()
							{
								var link_class = $(this).attr('class')

								var class_end = link_class.substring( link_class.length - 3 )

								$(this).attr( 'class', link_class.replace( '-on', '-off' ) )
							}

						)

						$(this).parent('li').attr( 'class', switch_to.replace( '-cat', '-on' ) )
					}

					return false
				}

			)
		}
		
		// creates a close action when button is click
		// uses .live() for any a#back-to-form buttons are inserted via JS
		$('a#back-to-form').live( 'click',
		
			function()
			{
				$(document).trigger('close.facebox');
				return false;
			}
			
		);
		
		/* Seller Upload Form */

		if( $('#seller-upload-form').length )
		{
			$('textarea#form_about_the_item').tinymce(
				{
					theme : 'simple'
				}
			);
			
			// Insert the check username and email links next to the Joint Forces section
			$('#row-section-join-forces-1 .row-left').append( '<p class="jquery_added js-links"><a href="#" rel="form_join_forces_person_name_one" class="js-check-user-name">Check Username</a><a href="#" rel="form_join_forces_person_email_one,form_join_forces_person_name_one" class="js-check-user-email">Check Both</a></p>' );
			$('#row-section-join-forces-2 .row-left').append( '<p class="jquery_added js-links"><a href="#" rel="form_join_forces_person_name_two" class="js-check-user-name">Check Username</a><a href="#" rel="form_join_forces_person_email_two,form_join_forces_person_name_two" class="js-check-user-email">Check Both</a></p>' );
			
			// Click on action for check user_name
			$('a.js-check-user-name').click(
				
				function()
				{
					$(this).removeClass('js-links-correct').removeClass('js-links-wrong');
					
					$(this).addClass('js-links-loading');
					
					var id = $(this).attr('rel');
					
					var user_name = $('#' + id ).val();
					
					if( 'TRUE' == mojo_check_user_name( user_name ) )
					{
						var element = $(this);
						var t1 = setTimeout( function() { element.removeClass('js-links-loading').addClass('js-links-correct') }, 300 );
						var t2 = setTimeout( function() { element.removeClass('js-links-correct') }, 3000 );
					}
					else
					{
						var element = $(this);
						var t1 = setTimeout( function() { element.removeClass('js-links-loading').addClass('js-links-wrong') }, 300 );
						var t2 = setTimeout( function() { element.removeClass('js-links-wrong') }, 3000 );
					}
					
					return false;
				}
				
			);
			
			// Click action for check user_email
			$('a.js-check-user-email').click(
				
				function()
				{
					$(this).removeClass('js-links-correct').removeClass('js-links-wrong');
					
					$(this).addClass('js-links-loading');
					
					var id = $(this).attr('rel');
					
					var items = id.split(',');
					
					if( 'TRUE' == mojo_check_both( $('#' + items[1]).val(), $('#' + items[0]).val() ) )
					{
						var element = $(this);
						var t1 = setTimeout( function() { element.removeClass('js-links-loading').addClass('js-links-correct') }, 300 );
						var t2 = setTimeout( function() { element.removeClass('js-links-correct') }, 3000 );
					}
					else
					{
						var element = $(this);
						var t1 = setTimeout( function() { element.removeClass('js-links-loading').addClass('js-links-wrong') }, 300 );
						var t2 = setTimeout( function() { element.removeClass('js-links-wrong') }, 3000 );
					}
					
					return false;
				}
				
			);
			
			// insert hidden input to notify php that this form is edited by jQuery
			$('#seller-upload-form').append( '<input type="hidden" name="form_jquery" value="jquery" id="form_jquery" />' );

			// Choose your upload type
			$('a.choose-upload-type').click(

				function()
				{
					// variables from link
					var link = $(this).attr( 'href' );
					var rel = $(this).attr( 'rel' );
					var id = $(this).attr( 'id' );
					
					$('#seller-upload-form').attr( 'action', '?upload_type=' + rel );
					
					// Loop through and hide all the non clicked product type
					$('a.choose-upload-type').each(
						
						function()
						{
							if( $(this).attr( 'rel' ) != rel )
							{
								$(this).hide();
								$(this).parent().hide();
							}
							
							
						}
						
					);
					
					$(this).addClass( 'active' );
					
					$('#choose-your-upload').hide();
					$('#your-upload-type, .choose-new-upload-type').show();
					
					// show the correct form
					$('#product-start, #product-' + rel + ', #product-end').fadeIn();
					
					
					$('#js-product-types .product-type-section').each(
						
						function()
						{
							var section_id = $(this).attr( 'id' );
							var keep = 'product-' + rel;
							
							if( 'product-start' != section_id && 'product-end' != section_id && keep != section_id )
							{
								$(this).remove();
							}
						}
						
					);
					
					$('#seller-upload-form').append( '<input type="hidden" id="form_product_category" name="form_product_category" value="' + rel + '" />' );

					return false;
				}

			);
			
			// Validations for when user hits submit button on the seller upload form
			$('#seller-upload-form').submit(
				
				function()
				{
					var product_category = $('#form_product_category');
					var theme_type = '';
					var theme_style = '';
					var files_included = '';
					
					var error = false;
					var message = '<div class="alert alert-error"><h2>Whoopss! Let\'s See What happened.</h2><ul>';
					
					$('#form_theme_type :selected').each( function() { theme_type += $(this).val() + ','; } );
					$('#form_theme_style :selected').each( function() { theme_style += $(this).val() + ','; } );
					$('#form_files_included :selected').each( function() { files_included += $(this).val() + ','; } );
					
					post = {
						item_name : $('#form_item_name').val(),
						about_the_item : $('#form_about_the_item').val(),
						theme_type : theme_type,
						theme_style : theme_style,
						thumbnail : $('#jquery_form_thumbnail').val(),
						theme_preview_images : $('#jquery_form_theme_preview_images').val(),
						screenshots : $('#jquery_form_screenshots').val(),
						all_theme_files : $('#jquery_form_all_theme_files').val(),
						files_included : files_included,
						demo_url : $('#jquery_form_demo_url'),
						keywords : $('#form_keywords').val(),
						agree_to_seller : $('#form_agree_to_seller:checked').val(),
						agree_to_terms : $('#form_agree_to_terms:checked').val()
					}
					
					if( '' == post.item_name )
					{
						error = true;
						message += '<li>You must included the <span>Item Name.</span></li>';
					}
					
					if( '' == post.about_the_item )
					{
						error = true;
						message += '<li>You must include a <span>description</span> of your theme.</li>';
					}
					
					if( '' == post.theme_type )
					{
						error = true;
						message += '<li>You must choose at least one <span>Theme Type.</span></li>';
					}
					
					if( '' == post.theme_style )
					{
						error = true;
						message += '<li>You must choose at least one <span>Theme Style.</span></li>';
					}
					
					if( '' == post.thumbnail || undefined == post.thumbnail )
					{
						error = true;
						message += '<li>You must include a <span>Thumbnail</span> of your theme.</li>';
					}
					
					if( '' == post.theme_preview_images || undefined == post.theme_preview_images  )
					{
						error = true;
						message += '<li>You must include <span>Preview Images</span> for your theme.</li>';
					}
					
					if( '' == post.all_theme_files || undefined == post.all_theme_files  )
					{
						error = true;
						message += '<li>You must include <span>All Your Theme Files</span> in a ZIP file.</li>';
					}
					
					if( '' == post.screenshots || undefined == post.screenshots  )
					{
						error = true;
						message += '<li>You must include <span>Screenshots</span> of your theme.</li>';
					}
					
					if( '' == post.files_included )
					{
						error = true;
						message += '<li>You must choose the <span>Files Included</span> in your theme.</li>';
					}
					
					if( '' == post.demo_url )
					{
						error = true;
						message += '<li>You must include a <span>Demo URL.</span></li>';
					}
					
					if( '' == post.keywords )
					{
						error = true;
						message += '<li>You must include <span>Keywords</span>.</li>';
					}
					
					if( 'on' != post.agree_to_seller )
					{
						error = true;
						message += '<li>You must accept the <span>Seller to Terms</span></li>';
					}
					
					if( 'on' != post.agree_to_terms )
					{
						error = true;
						message += '<li>You must accept the <span>Terms & Conditions</span> to continue.</li>';
					}
					
					if( 'wordpress' == product_category || 'magento' == product_category || 'joomla' == product_category || 'drupal' == product_category )
					{
						var compatible_browsers = '';
						$('input.form_compatible_browsers :checked').each( function() { compatible_browsers += $(this).val() + ','; } );
						post.compatible_browsers = compatible_browsers;
						
						post.layout = $('#form_layout').val();
						post.layout_columns = $('#form_layout_columns').val();
						post.platform_version = $('#form_platform_version').val();
						
						if( '' == post.compatible_browsers )
						{
							error = true;
							message += '<li>You must choose what <span>Browsers</span> you have tested in.</li>';
						}
						
						if( '' == post.layout )
						{
							error = true;
							message += '<li>You must describe your <span>Layout</span> type ex. Fluid, Fixed etc.</li>';
						}
						
						if( '' == post.layout_columns )
						{
							error = true;
							message += '<li>You must included how many <span>Columns</span> this theme supports.</li>';
						}
						
						if( '' == post.platform_version )
						{
							error = true;
							message += '<li>You must the <span>Version of your Platform</span> this theme is for. ex. Wordpress 2.9, or Joomla 1.4.</li>';
						}
					}
					
					if( 'tumblr' == product_category )
					{
						var compatible_browsers = '';
						$('input.form_compatible_browsers :checked').each( function() { compatible_browsers += $(this).val() + ','; } );
						post.compatible_browsers = compatible_browsers;
						
						post.layout = $('#form_layout').val();
						post.layout_columns = $('#form_layout_columns').val();
						
						if( '' == post.compatible_browsers )
						{
							error = true;
							message += '<li>You must choose what <span>Browsers</span> you have tested in.</li>';
						}
						
						if( '' == post.layout )
						{
							error = true;
							message += '<li>You must describe your <span>Layout</span> type ex. Fluid, Fixed etc.</li>';
						}
						
						if( '' == post.layout_columns )
						{
							error = true;
							message += '<li>You must included how many <span>Columns</span> this theme supports.</li>';
						}
					}
					
					if( 'html' == product_category )
					{
						var compatible_browsers = '';
						$('input.form_compatible_browsers :checked').each( function() { compatible_browsers += $(this).val() + ','; } );
						post.compatible_browsers = compatible_browsers;
						
						post.layout = $('#form_layout').val();
						post.layout_columns = $('#form_layout_columns').val();
						
						if( '' == post.compatible_browsers )
						{
							error = true;
							message += '<li>You must choose what <span>Browsers</span> you have tested in.</li>';
						}
						
						if( '' == post.layout )
						{
							error = true;
							message += '<li>You must describe your <span>Layout</span> type ex. Fluid, Fixed etc.</li>';
						}
						
						if( '' == post.layout_columns )
						{
							error = true;
							message += '<li>You must included how many <span>Columns</span> this theme supports.</li>';
						}
					}
					
					message += '</ul>';
					
					join_forces_message = '<h2>Join Forces Errors</h2><ul>';
					join_forces_error = false;
					
					first_person_name = first_person_email = second_person_name = second_person_email = false;
					
					post.join_forces_person_name_one = $('#form_join_forces_person_name_one').val();
					post.join_forces_person_email_one = $('#form_join_forces_person_email_one').val();
					
					post.join_forces_person_name_two = $('#form_join_forces_person_name_two').val();
					post.join_forces_person_email_two = $('#form_join_forces_person_email_two').val();
					
					if( '' != post.join_forces_person_name_one || '' != post.join_forces_person_email_one )
					{
						if( '' != post.join_forces_person_name_one )
						{
							if( 'TRUE' == mojo_check_user_name( post.join_forces_person_name_one ) )
							{
								first_person_name = true;
							}
							else
							{
								first_person_name = false;
								join_forces_error = true;
								join_forces_message += '<li>Join Forces #1 : the user <span>' + post.join_forces_person_name_one + '</span> does NOT exist.</li>';
							}
						}
						else
						{
							join_forces_error = true;
							join_forces_message += '<li>Join Forces #1 : You must include the person\'s username</li>';
						}
						
						if( '' != post.join_forces_person_email_one )
						{
							if( 'TRUE' == mojo_check_user_email( post.join_forces_person_email_one ) )
							{
								first_person_email = true;
							}
							else
							{
								first_person_email = false;
								join_forces_error = true;
								join_forces_message += '<li>Join Forces #1 : The email <span>' + post.join_forces_person_email_one + '</span> does NOT exist.</li>';
							}
						}
						else
						{
							join_forces_error = true;
							join_forces_message += '<li>Join Forces #1 : You must include the person\'s email</li>';
						}
						
						if( first_person_email && first_person_name )
						{
							if( 'TRUE' != mojo_check_both( post.join_forces_person_name_one, post.join_forces_person_email_one ) )
							{
								join_forces_error = true;
								join_forces_message += '<li>Join Forces #1 : The username and email address do not match.</li>';
							}
						}
						
					}
					
					if( '' != post.join_forces_person_name_two || '' != post.join_forces_person_email_two )
					{
						if( '' != post.join_forces_person_name_two )
						{
							if( 'TRUE' == mojo_check_user_name( post.join_forces_person_name_two ) )
							{
								second_person_name = true;
							}
							else
							{
								second_person_name = false;
								join_forces_error = true;
								join_forces_message += '<li>Join Forces #2 : the user <span>' + post.join_forces_person_name_two + '</span> does NOT exist.</li>';
							}
						}
						else
						{
							join_forces_error = true;
							join_forces_message += '<li>Join Forces #2 : You must include the person\'s username</li>';
						}
						
						if( '' != post.join_forces_person_email_two )
						{
							if( 'TRUE' == mojo_check_user_email( post.join_forces_person_email_two ) )
							{
								second_person_email = true;
							}
							else
							{
								second_person_email = false;
								join_forces_error = true;
								join_forces_message += '<li>Join Forces #2 : The email <span>' + post.join_forces_person_email_two + '</span> does NOT exist.</li>';
							}
						}
						else
						{
							join_forces_error = true;
							join_forces_message += '<li>Join Forces #2 : You must include the person\'s email</li>';
						}
						
						if( second_person_email && second_person_name )
						{
							if( 'TRUE' != mojo_check_both( post.join_forces_person_name_two, post.join_forces_person_email_two ) )
							{
								join_forces_error = true;
								join_forces_message += '<li>Join Forces #2 : The username and email address do not match.</li>';
							}
						}
					}
					
					join_forces_message += '</ul>';
					
					if( join_forces_error )
					{
						error = true;
						message += join_forces_message;
					}
					
					if( error )
					{
						message += '<a href="#" class="close-facebox" id="back-to-form">Back To Form</a></div>';
						$.facebox( message );
						return false;
					}
					else
					{
						return true;
					}
				}
				
			);
						
		}
		
		// If the withdrawal form is present on the current page
		if( $('#withdrawal-form').length )
		{
			// Submit actions for the withdrawal form to validate information before sending
			$('#withdrawal-form').submit(
				
				function()
				{
					var error = false;
					var message = '<div class="alert alert-error"><h2>Whoopss! Let\'s See What happened.</h2><ul>';
					
					var post = {
						amount : $('#form_amount').val(),
						fee_amount : $('#form_fee_amount').val(),
						paypal : $('#form_paypal').val()
					};
					
					if( '' == post.amount )
					{
						error = true;
						message += '<li>You must include the <span>amount to withdraw</span></li>';
					}
					
					if( 1 != post.fee_amount && 0 != post.fee_amount )
					{
						error = true;
						message += '<li>You must choose a <span>Withdrawal Type</span></li>';
					}
					
					if( '' == post.paypal )
					{
						error = true;
						message += '<li>You must include your <span>paypal email address</span> to get paid.<li>';
					}
					
					if( error )
					{
						$.facebox( message + '</ul><a href="#" class="close-facebox" id="back-to-form">Back To Form</a></div>' );
						return false;
					}
					else
					{
						return true;
					}
				}
				
			);
		}

	}

);
