$(document).ready(function() {
	

	
	// toggle Quick Links
	$("div#contents .view-id-scripts .attachment-before .moreLink a").toggle(function() {
			$(this).text("Shrink Quick Links");
			$("div#contents .view-id-scripts .attachment-before span").show();
	}, function() {
		   $(this).text("Show All Quick Links");
		   $("div#contents .view-id-scripts .attachment-before span.startHidden").hide();
	});
	
	// toggle Quick Links
	$("div#contents .quicklinks .moreLink a").toggle(function() {
			$(this).text("Shrink Quick Links");
			$("div#contents  .quicklinks span").show();
	}, function() {
		   $(this).text("Show All Quick Links");
		   $("div#contents .quicklinks span.startHidden").hide();
	});
	
	
	    /*
	     * 
	     * Browsing Preferences:
	     * 
	     * 
	     */
		

		$("#scriptBrowsing input").click(function(){
			
			switch($(this).val()) {
			case '1':
				$(".script-recording").hide();
				$.cookie('browsingPreference', '1');
			break;
			
			case '2':
				$(".script-recording").show();
				$.cookie('browsingPreference', '2');
			break;
			}
			
		});
		
		/*
		 * 
		 * 
		 * End Browsing Preferences;
		 * 
		 */
		
		
		
	
	
	  $("#scriptsNav .options input").click(function(){
			
			
			var scriptCategoryName = $(this).val();
			$.cookie('scriptType', scriptCategoryName);
			
			
			
			switch(scriptCategoryName) {
			case 'english-adult':
				$("#scriptsNav .categories li").hide();
				$("#scriptsNav .categories li.english-adult").show();
			break;
			
			case 'spanish':
				$("#scriptsNav .categories li").hide();
				$("#scriptsNav .categories li.spanish").show();
			break;
			
			case 'english-children':
				$("#scriptsNav .categories li").hide();
				$("#scriptsNav .categories li.english-children").show();
			break;
			
			}
			
			$("#scriptsNav .categories .col1 li").show();
		
	   }); // end click

	 
	  $("#scriptsNav a").click(function(){
		  
		  
		  if(!$.cookie('registered')) {
			  document.location.href="http://www.edgestudio.com/scripts.htm";
			  return false;
		  }
		  
	  });
	  
	  /*
	   * 
	   * AJAX comment posting form:
	   * 
	   */
	  $("a.critiqueLink").live('click', function() {
		  
		  //remove previous copies of the form, and reveal the link to open the form
		  $("div#commentLinkDiv").next().show().prev().slideUp(750).remove();
		  
		  
		  // copy the link so that you can append it after the new comment
		  var critiqueParagraph = $(this).parent().parent().attr("id","commentLinkDiv").clone();
		  $(critiqueParagraph).removeAttr("id").hide().addClass("critiqueParagraph");
		  var linkHref = $(critiqueParagraph).find(".critiqueLink").attr("href");
		  $(critiqueParagraph).find("p").html('<a class="critiqueLink" href="' + linkHref + '">Add another comment or critique</a>');
		  $("#commentLinkDiv").after(critiqueParagraph);
		  
		  
		  
		 
		  
		  	  
			  $("#commentLinkDiv p").load($(this).attr('href') + " #comment-form", function() {
				 //alert('Load was performed.');
				 
				 
				 // fix some style issues
				  $("div#commentLinkDiv").css("text-align","left");
				  $("#edit-comment").attr('rows','6');
  
				 

				  
				  		$("#comment-form #edit-submit").live('click',function(){
						  
							  
							  if( $("#edit-comment").val()) {
								  $("#comment-form").hide().parent().append('<div id="thanks"><img src="/images_misc/progress_animation.gif" alt="submitting . . . " /></div>');
									
								  
									$.ajax({  
											  type: "POST",  
											  url: $("#comment-form").attr('action'),  
											  data: $("#comment-form").serialize(),  
											  success: function(data) { 
											   
												
													
													if($(data).find("#comment-form").size() > 0) {
														var newFormData = $(data).find("#contents form").html();
														$("#comment-form").html(newFormData).slideDown(500);
														$("#thanks").remove();
													}
													else {
														//$(data).find(".comment:last .comment-inner")
														
														$("#commentLinkDiv").after($(data).find(".comment:last"))
													    .hide()  
													    .fadeIn(1500);
														$("#commentLinkDiv").remove();
														$(".critiqueParagraph").show().removeClass("critiqueParagraph");
													}								
											  } // end success 
									
									 });  // end ajax
							  }
							  else {
								  alert('Oops.  You must fill in a comment to submit!');
							  }
							
							  
							  return false;
						  });
				 
				  
				});
			  
			  return false;
			  
		  });

	  /*
	   * 
	   * AJAX script recording form:
	   * 
	   */
	  $("a.recordingFormLauncher").live('click', function() { 
		  var uploadLink = $(this).parent().clone();
		  var parentTarget = $(this).parent();
		  

		  $(parentTarget).load($(this).attr('href') + " #node-form", function() {
			  var currentNodeForm = $(parentTarget).find("form");
			  $(currentNodeForm).attr('target','hiddenForm');
			  $(parentTarget).after('<iframe name="hiddenForm" id="hiddenForm" style="display: none;"></iframe>');
			  
			  $(this).css('text-align','left');
			  $(this).css('border-top','1px solid #FFCC33');
			  $(this).addClass('block yellow');
			 
			  $(currentNodeForm).find("fieldset, #edit-field-mp3-file-0-filefield-upload, #edit-field-category-path-0-value-wrapper").hide();
			  
			  $(currentNodeForm).find("#edit-title-wrapper").hide();
			  $(currentNodeForm).find(".form-required").hide();
			  
			  		  $(currentNodeForm).find("#edit-submit").click(function(){
			  			
			  			var ext = $('#edit-field-mp3-file-0-upload').val().split('.').pop().toLowerCase();
			  			
			  			var allow = new Array('mp3','mpeg3');
			  	
			  			
			  			if ($('#edit-field-mp3-file-0-upload').val() == "") {
			  				
			  				alert('Oops! Please pick a file before submitting!');
			  				return false
			  			}
			  			
			  			else if($.inArray(ext, allow) == -1) {
			  				
				  			  alert('Oops!  "' + ext + '" is not a valid file extension!');
				  			  return false;
				  		}
			  			
			  			else if($('#edit-field-mp3-file-0-upload').val().indexOf('&') > -1) {
			  				
				  			  alert('Please rename the file so it doesn\'t contain the ampersand character ("&").');
				  			  return false;
				  		}
			  			
			  			

	
			  			  
			  			else {
			  			  
			  			$(parentTarget).removeClass('white').removeClass('uploadLinks').addClass('yellow');
			  			  
					  $(currentNodeForm).hide().after('<div id="thanks"><p>Congratulations, your recording will be submitted!</p><p>You will receive email notification when someone comments on your recording.</p><p><b>Please critique a few fellow voice-talents\' recordings now, just as YOU HOPE OTHERS DO FOR YOU SOON!</b></p></div>')
					
			  			}
					  /* 
					  $.ajax({  
							  type: "POST",  
							  url: $("#node-form").attr('action'),  
							  data: $("#node-form").serialize(),  
							  success: function(data) { 
							  
							  alert('test');
							    $(parentTarget).html('upload successful!')
							    .hide()  
							    .fadeIn(1500).after($(uploadLink));
							    $("#thanks").remove();
							    
							  }  
						 });  
						 
						 */
						  
						// return false;
					  
					  
					  
					  });
					  
					
		  
		  });
		  
		 
		  return false;
		  
	  });
	  

});

