$(document).ready(function(){
						   
    $("#menuContainer div").hover(
        function(){ $("div.sub", this).addClass("mHover"); $("div.sub", this).fadeIn("fast");  }, 
        function() { 
        	//$("ul", this).fadeOut("fast");
			$("div.sub", this).removeClass("mHover");} 
    );
	
    $(".genreWidget").hover(
        function(){ $(this).fadeTo("fast", 0.6 );  }, 
        function(){ $(this).fadeTo("fast" , 1.0 );} 
    );

    $("tr:odd").addClass('odd');


    setTimeout("$('.success').hide('slow')" , 5000);
    	
});

	function addComment() {
		var messageVal = $("#newComment").val();
		var storyID = $("#newCommentstoryID").val();
		
		if( !messageVal )
			return false;
			
		$("#newCommentButtn").val('Sending...').attr("disabled", true);
		
		
		$.post("/rpc/addcomment.php",
			{ story_id: storyID, message: messageVal },
				function(data){
					if( data)
						$("#newComment").val('').before( data );
					else
						$("#newComment").val('').before( '<p class=\'error\'><b>ERROR:</b> There was an unknown error posting your comment</p>' );
				}
		);
	}
	
	function subSendTo( myemail ){
		
		form = document.sub;
		
		if( form.printable.checked == true ) {
			form.email.value = myemail;
			form.email.disabled=true;
		}else{
			form.email.disabled=false;
		}
	}
	
	function addSubToCart( form ){	
		
		$.post("/cart.php?ajax=true&&action=addSubscription",
			{ email: form.email.value , amount: form.amount.value , message: form.message.value } ,
			function( j ) {
				if( j.action_success )
					msg = j.action_success;
				else
					msg = j.action_errors[0];
					
				$.prompt( msg ,
					{	callback: function(v,m) {
							if( v == 'tocart' )
								window.location='/cart.php';
							//else
							//	if( continue_shopping_url )
							//		window.location=continue_shopping_url;
						},
						overlayspeed: 'normal',
						buttons: { 'Gift Another': 'continue', 'Go To Shopping Cart': 'tocart' }
					}
				);	
			},
			"json"
		);
		
		return false;
	}
	
	
	
	function formLoading(form){
		//$(".submit").val('Sending...').attr("disabled", true);
	}
	function doSearch( obj){
		str = $("#searchText").val();
		str = str.replace(/ /g, "_").replace(new RegExp( "[^a-zA-Z0-9_.]", "g" ), "" );
		
		window.location = '/search/' + str + '/';
	
		return false;
	}
	
	function submitForm(form ){
		len = form.length;
		
		var sending = {};
		
		$(form).find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea") .filter(":enabled") .each(function() { sending[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; });
	
		$(".fieldError").removeClass("fieldError");
		$("#formSuccess").hide("fast");
		$("#formError").hide("fast");
	
	
		$.post( form.getAttribute("action") + "?ajax=true",
			sending,
				function(j){
					if(j.action_success){
						$("#formSuccess p").html( j.action_success ).parent().show("fast");
	
					}else{
						if( j.action_errors && isArray(j.action_errors ) ){
							errormsg = '<ul>';
							for( i = 0 ; i < j.action_errors.length ; i++ ){
								errormsg += '<li>' + j.action_errors[i] + '</li>';
							}
							errormsg += '</ul>';
						}
						if( j.action_errorFields &&  isArray( j.action_errorFields ) ){
							for( i = 0 ; i < j.action_errorFields.length ; i++ ){
								$( form[ j.action_errorFields[i] ] ).parents("tr").addClass("fieldError");
							}
						}					
						$("#formError").html( '<h2>Error</h2>' + errormsg ).show("fast");
					}
					
					if(j.action_redirect){
						window.location = j.action_redirect;
					}
					if(j.action_javascript){
						eval( j.action_javascript);
						//Recaptcha.reload();
						//alert('reload');
					}
					if( j.action_hide == true){
						$(form).hide('fast');
					}
				}
			,
			"json"
		);
		
			
		return(false);
	}
	
	function addToNewsletter()
	{
		email = $("#newsletterEmail").val();
		$.post("/addnewsletter",
			{ action: 'addNewsletter' , email: email } ,
			function( j ) {
				$.prompt( 'Thank you for joining our newsletter.' ,
					{	
						overlayspeed: 'normal',
						buttons: { 'Ok': 'ok' }
					}
				);
			}
		);
	}
	function sendToFriend()
	{
		email = $("#sendtofriendEmail").val();
		$.post("/sendtofriend",
			{ action: 'sendToFriend' , email: email } ,
			function( j ) {
				$.prompt( 'An email has been sent to your friend.' ,
					{	
						overlayspeed: 'normal',
						buttons: { 'Ok': 'ok' }
					}
				);
			}
		);
	}
	
	function addToCart(id){
		$("#" + id + " a.addToCart").addClass("inCart").click(function(){
	        return false;
		});
		
		$.post("/cart.php?ajax=true",
			{ action: 'add' , story_id: id } ,
			function( j ) {
				if( j.action_coupon )
				{
					//WILD CARD COUPON EXCEPTION
					
					$.prompt( 'You have an unused coupon available. Would you like to use your coupon to download this story for free?' ,
						{	callback: function(v,m) {
								if( v == 'download' )
									window.location='/download?usecoupon=' + id;
								//else
								//	if( continue_shopping_url )
								//		window.location=continue_shopping_url;
							},
							overlayspeed: 'normal',
							buttons: { 'Use Coupon': 'download', 'Cancel': 'cencel' }
						}
					);
							
				}else{
				
					if( j.action_success )
						msg = j.action_success;
					else
						msg = j.action_errors[0];
						
					$.prompt( msg ,
						{	callback: function(v,m) {
								if( v == 'tocart' )
									window.location='/cart.php';
								//else
								//	if( continue_shopping_url )
								//		window.location=continue_shopping_url;
							},
							overlayspeed: 'normal',
							buttons: { 'Continue Shopping': 'continue', 'Go To Shopping Cart': 'tocart' }
						}
					);
				}
				
				
			},
			"json"
		);
	}
	
	function removeFromCart( id){
		$("#"+ id).css('display' , 'none');
		$.post("/cart.php?ajax=true",
			{ action: 'remove' , story_id: id } ,
			function( j ) {
				//ASSUME SUCCESS
			},
			"json"
		);
	}
	
	function enableGift( email , message , error )
	{
		html = '';
		
		if( error )
			html+= '<font color=red>' + error + '</font><br />';
			
		html+= '<div style="text-align: left;">Enter the email address you wish to send these gifts to, and add a personalized message if you wish.</div>';
		html+= '<b>Friends Email</b> <input type=text id=email value="' + email +'">';
		html+= '<textarea cols=30 rows=3 id=message style="display: block;position:relative; padding: 5px; margin: 5px;">' + message + '</textarea>';
		

		
		$.prompt( html , {
			callback: submitGiftOptions,
			buttons: { Gift: true , Cancel: false }
		});
	}
		function submitGiftOptions( v , m ) 
		{
			if( v )
			{
				email = m.children("#email").val();
				message = m.children("#message").val();
				$.post("/cart.php?ajax=true",
					{ action: 'enableGift' , email: email , message: message } ,
					function( j ) {
						if( j.action_success )
						{
							//Good added gift. Now refresh cart page.
							window.location = 'cart.php';
						}else{
							//Email must not have been valid...
							enableGift( email , message , 'Invalid email address.' );
						}
					},
					"json"
				);
			}	
		}
	function disableGift()
	{
		window.location = '?action=disableGift';
		/*
		$.post("/cart.php?ajax=true",
			{ action: 'disableGift' } ,
			function( j ) {
				//ASSUME SUCCESS
			},
			"json"
		);
		*/
	}
	
	function isArray(obj) {
	   if (obj.constructor.toString().indexOf("Array") == -1)
	      return false;
	   else
	      return true;
	}
	
	function registerAccount(form){
		onsubmit="return Validate()";
	}
	
	function editComment( comment_id , comment_text , customer_name , story_title , verified )
	{
			html = '<input type="hidden" id="comment_id" value="' + comment_id +'" />';
			html+= '<div style="font-weight:normal;text-align:left;font-size:12px;font-family:verdana;">';
			html+= '<b>Customer:</b> ' + customer_name + '<br />';
			html+= '<b>Story:</b> ' + story_title + '<br /><hr />';
			html+= '<p>' + comment_text + '</p>';
			html+= '</div>';
			
			if( verified ){
				$.prompt( html , {
					callback: updateComment,
					buttons: { DeActivate: 'unverify' , Delete: 'delete' , Cancel: false }
				});
			}else{
				$.prompt( html , {
					callback: updateComment,
					buttons: { Verify: 'verify' , Delete: 'delete' , Cancel: false }
				});
			}
		/**
		$.post("/admin/comments.php?ajax=true",
			{ comment_id: comment_id } ,
			function( j ) {
				if( j.action_success )
				{
					name = j.data.customer_firstname;
					comment = j.data.comment_text;
					title = j.data.story_title;
					
					html = '<b>Customer:</b> ' + name + '<br />';
					html.= '<b>Story:</b> ' + title + '<br />';
					html.= '<p>' + comment_text . '</p>';
					
					$.prompt( $("#doGift").html() , {
						callback: submitGiftOptions,
						buttons: { Gift: true , Cancel: false }
					});
				}
			},
			"json"
		);
		**/
	}
	function updateComment( v , m )
	{
		if( v )
		{
			comment_id = m.children( '#comment_id' ).val();
			
			$.post("/admin/comments/?ajax=true",
				{ action: v , comment_id: comment_id } ,
				function( j ) {
					if( j.action_success)
						$("#comment" + comment_id ).css('display' , 'none');
					else
						alert( j.action_errors[0]);
				},
				"json"
			);
		}
	}