jQuery.noConflict();
jQuery(document).ready(function($) {

    $("#flashMessage").show("normal",
        function()
        {
            $("#flashMessage").fadeOut(10000);
        }
    );
    
    
    
    
    
    //================ Post Comment / Submit Reasons Tabs.... //
    

	$("#comment_form > ul").tabs();

	$("#joke_reasons_submit > ul").tabs();



	var reasons_login_options = {
			dataType:		'json',
		    resetForm: 		true,
		    beforeSubmit:   validateReasonsLogin,
			success:	    reasonsLoginResponse
	};
	
	$('#reasons_login').ajaxForm(reasons_login_options);
	
	function validateReasonsLogin(formData, jqForm, options) {
		var form = jqForm[0];
		
		if(!form.user_name.value) {
			alert("You need to enter a login name.");
			return false;
		}
		if(!form.password.value) {
			alert("Enter a password to login.");
			return false;
		}
		
	
		
	}
	
	function reasonsLoginResponse(responseText, statusText) {
			
			var login_status = responseText.login_status;
			
			if(login_status == 'failed') {
				$("#reasons_failed_status").fadeIn("slow");
				$("#reasons_failed_status").fadeOut(5000);
			}
			else if(login_status == 'success') {
				var user_name = responseText.user_name;
				var email     = responseText.email;
				var success_out = responseText.success_out;
				
					   
				$("#reasons_quick_login").html(success_out);
				$('#joke_reasons_submit > ul').tabs('disable', 0);
				$('#joke_reasons_submit > ul').tabs('disable', 2);
				
			}
			//alert(return_url);
			//location.href = return_url;
			//alert(login_status);
	}
	
	
	//================================================

	
	var comment_login_options = {
			dataType:		'json',
		    resetForm: 		true,
		    beforeSubmit:   validateCommentLogin,
			success:	    commentLoginResponse
	};
	
	$('#comment_login').ajaxForm(comment_login_options);
	
	
	function validateCommentLogin(formData, jqForm, options) {
		var form = jqForm[0];
		
		if(!form.user_name.value) {
			alert("You need to enter a login name.");
			return false;
		}
		if(!form.password.value) {
			alert("Enter a password to login.");
			return false;
		}
		
	
		
	}
	
	function commentLoginResponse(responseText, statusText) {
			
			var login_status = responseText.login_status;
			
			if(login_status == 'failed') {
				$("#failed_status").fadeIn("slow");
				$("#failed_status").fadeOut(5000);
			}
			else if(login_status == 'success') {
				var user_name = responseText.user_name;
				var email     = responseText.email;
				var success_out = responseText.success_out;
				
					   
				$("#quick_login").html(success_out);
				$('#comment_form > ul').tabs('disable', 0);
				$('#comment_form > ul').tabs('disable', 2);
				
			}
			//alert(return_url);
			//location.href = return_url;
			//alert(login_status);
	}
	
	//============================
	
	var reasons_form_guest_options = {
			dataType:		'json',
		    resetForm: 		true,
		    beforeSubmit:   validateReasonsFormGuest,
			success:	    reasonsFormGuestResponse
	};
	
	$('#reasons_form_guest').ajaxForm(reasons_form_guest_options);

	function validateReasonsFormGuest(formData, jqForm, options) {
		var form = jqForm[0];
		
		if(!form.submitter_name.value) {
			alert("Don't be a stranger.  Enter your name?");
			return false;
		}
		if(!form.submitter_email.value) {
			alert("An email address is needed. Don't worry, we will never share your email.");
			return false;
		}
		
		if(!validateReasonsCommon(form)) {
			return false;
		}	
	
		
	}
	
	function reasonsFormGuestResponse(responseText, statusText) {
			
			var return_url = responseText.return_url;
			window.location = return_url;
	}
	
	function validateReasonsCommon(form) {
		if(!form.reasons_textarea.value) {
			alert("Don't be weird. Enter a comment before submitting okay?");
			return false;
		}
		
		return true;
	}
	
	//============================
	
	
	var comment_form_guest_options = {
			dataType:		'json',
		    resetForm: 		true,
		    beforeSubmit:   validateCommentFormGuest,
			success:	    commentFormGuestResponse
	};
	
	$('#comment_form_guest').ajaxForm(comment_form_guest_options);

	function validateCommentFormGuest(formData, jqForm, options) {
		var form = jqForm[0];
		
		if(!form.submitter_name.value) {
			alert("Don't be a stranger.  Enter your name?");
			return false;
		}
		if(!form.submitter_email.value) {
			alert("An email address is needed. Don't worry, we will never share your email.");
			return false;
		}
		
		if(!validateCommentCommon(form)) {
			return false;
		}	
	
		
	}
	
	function commentFormGuestResponse(responseText, statusText) {
			
			var return_url = responseText.return_url;
			window.location = return_url;
	}
	
	function validateCommentCommon(form) {
		if(!form.comment.value) {
			alert("Don't be weird. Enter a comment before submitting okay?");
			return false;
		}
		
		return true;
	}
	
	
	
	    //-------------
        // for Rating
        
        $('a.rater').click(function()
        	{
        	
        		
        		var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
				var parameterTokens = parameterString.split("/"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
	
				//alert(parameterTokens);
				//alert(parameterString);
				
				//alert(paramterString);
				var theratingID = parameterTokens[7];
				var theVote = parameterTokens[6];
				var theuserIP = parameterTokens[9];
				var theunits = parameterTokens[10];
				var thetype = parameterTokens[8];
				
				//alert(parameterTokens);
				//alert(theratingID + ' ' + theVote + ' ' + theuserIP + ' ' + theunits + ' ' + thetype);
				
				$.ajax({
	  				type: "POST",
	  				url: "/common/ratings/do_add_ratings",
	  				data: 	'vote=' + theVote
	  							+ '&id=' + theratingID
	  							+ '&ip=' + theuserIP
	  							+ '&units=' + theunits
	  							+ '&type=' + thetype,
	  				success: function(msg) {
	  					//alert(msg);
	     				//$('#tags_list').hide().show("slow");
	     				$("#ratingblock"+theratingID).replaceWith(msg);
	  				}
	 			})
	 	
				return false; // prevents the sending the standard http request
        	}
        	
        	  // end for Rating
        );
   
        
}); 

function joke_view_scroll_to_reasons_submit() {
	//alert('boo');
	//jQuery.scrollTo( '#submit_joke_reasons_link', 800, {easing:'elasout'} );
	jQuery('#joke_reasons_submit').show();
}

function joke_view_scroll_to_comment_reason_submit(reason_id) {	
    //content = jQuery("#comment_form").html();
	//jQuery("#div_reason_action_" + reason_id).html(content);
	
	jQuery("[name=reason_id]").val(reason_id);
	
}


function toggle_reasons_meta() {
	//alert('boo');
	jQuery(".reason_meta").slideToggle("slow");
}

// allows you to toggle a div as specified by div_id input
// to use,  in the <a href="#" onclick=toggle_div('my_div'); ?>
function toggle_div(div_id) {
	jQuery('#' + div_id).toggle();
	return true;		
}

function content_swap(div_target_id, content_id) {
	var content = jQuery('#' + content_id).html()
	jQuery('#' + div_target_id).html(content);
	return true;
}


//=========================
// Joke Star Rating
//=========================
function do_object_rating(object_type, object_id, value) {
 		
	jQuery.ajax({	
		type: "POST",
		url: "/common/ratings/do_add_rating/",
		data: 'object_type=' + object_type + '&object_id=' + object_id + '&value=' + value,
		success: function(msg) {
	  		alert(msg);
	     	//$('#tags_list').hide().show("slow");
	     	//$(".ratingblock"+theratingID).replaceWith(msg);
	    }	
	})
}


//=========================
// Reasons Voting
//=========================

function vote_reason_up(joke_id, reason_id) {
	jQuery.ajax({
		type: "POST",
		dataType: "json",
		url: "/jokes/do_vote_reason_up/",
		data: 'joke_id=' + joke_id + '&reason_id=' + reason_id,
		success: function(json_data) {
			var vote_status = json_data.vote_status;
			var display = json_data.display;
				//alert(display);
				reason_vote_div = jQuery('#rv' + reason_id);
				reason_vote_num = jQuery('#rvnum' + reason_id);
				
				if(vote_status != '') {
					curr = reason_vote_num.html();
					reason_vote_num.hide();
					reason_vote_num.html('<i>' + vote_status + '</i>');
					reason_vote_num.show();
					reason_vote_num.fadeOut(3000, 
					function()
					{
						reason_vote_num.html(curr);
						reason_vote_num.show();
					}
					);
					
					
				}
				else {
					reason_vote_div.hide();
					reason_vote_div.html(display);
					reason_vote_div.show();
				}
			
			
			}
		})
}

function vote_reason_down(joke_id, reason_id) {
	jQuery.ajax({
		type: "POST",
		dataType: "json",
		url: "/jokes/do_vote_reason_down/",
		data: 'joke_id=' + joke_id + '&reason_id=' + reason_id,
		success: function(json_data) {
			var vote_status = json_data.vote_status;
			var display = json_data.display;
			
				//alert(display);
				reason_vote_div = jQuery('#rv' + reason_id);
				reason_vote_num = jQuery('#rvnum' + reason_id);
				
				if(vote_status != '') {
					curr = reason_vote_num.html();
					reason_vote_num.hide();
					reason_vote_num.html('<i>' + vote_status + '</i>');
					reason_vote_num.show();
					reason_vote_num.fadeOut(3000, 
					function()
					{
						reason_vote_num.html(curr);
						reason_vote_num.show();
					}
					);
					
					
				}
				else {
					reason_vote_div.hide();
					reason_vote_div.html(display);
					reason_vote_div.show();
				}
			}
			
			
		})
}