$(document).ready(function(){
	$.postJSON = function(url, data, callback) { $.post(url, data, callback, "json"); };
	/*
		Handles Toggling of adding and removing from "my stuff"	
		TODO: remove the checking of .html(), its messy and was done this way initially
	*/
	$('.myStuffTxt').click( function() {
		$this = $(this)
		
		if($this.html().toLowerCase() == 'add to my stuff' || $this.html() == '<img src="http://static.stg.ringtone.com/images/add.gif" class="add_btn" border="0">') {
			// Execute add process to add to favorites
			$.postJSON('/mystuff/add/' + $this.prev().val() + '/', '', function(data) {
				if(data.status) {
					// Execution Successful
					if($this.html().toLowerCase() == 'add to my stuff') {
						$this.html('Remove from my stuff');
					} else {
						$this.html('<img src="http://static.stg.ringtone.com/images/remove.gif" class="add_btn" border="0">')
					}
				} else {
					// Execution failed
					if(data.code == 2) {
						// Anonymous User: Display Login
						window.location = '/login/'
					} else {
						alert(data.msg);
					}
				}
			});
		} else {
			// Execute remove process to remove from favorites
			$.postJSON('/mystuff/delete/' + $this.prev().val() + '/', '', function(data) {
				if(data.status) {
					// Execution Successful
					if($this.html().toLowerCase() == 'remove from my stuff') {
						$this.html('Add to my stuff');
					} else {
						$this.html('<img src="http://static.stg.ringtone.com/images/add.gif" class="add_btn" border="0">')
					}
				} else {
					if(data.code == 2) {
						// Anonymouse User: Display Login
						window.location = '/login/'
					} else {
						alert(data.msg)		
					}
				}
			});
		} return false;
	})
	
	function test(v_this){
		x = 500 - v_this.val().length;
		if (x >=0){
			$(".commentCount").html(x);
		}else {
			$(".commentCount").html('0');
			v_this.val((v_this.val().substring(0,500)));
		}
	}
	$(".commentTxtAra").change(function(){
		test( $(this));
	});	
	$(".commentTxtAra").keyup(function(){
		test( $(this));
	});	
	$(".commmentFrm").submit(function(){
		if($('.commentTxtAra').length <= 0) {
			alert('Error: please enter a comment')
			return false;
		}

		$.post("/comment/submit/", {  recaptcha_response_field: $('#recaptcha_response_field').val(), recaptcha_challenge_field: $('#recaptcha_challenge_field').val() ,comment: $(".commentTxtAra").val(), namehoney: $(".namehoney").val(), libraryid: $(".libraryid").val(), authorid: $(".authorid").val() },
			function(data){
				if (data.substring(0,7) == 'Sucess:'){
					msg = data.substring(7)
					if ($.trim($(".viewComments").html()) == '<div class="noComments">No comments.</div>'){
						$(".viewComments").html('<div class="UserCommentCtn"><div class="userImg"></div><div class="commentDetails"><div class="userSays">' + msg + '<span> says</span></div><div class="comment">' + $(".commentTxtAra").val() + '</div></div><div class="clearInline"></div></div>')
					} else {
						$(".viewComments").prepend('<div class="UserCommentCtn"><div class="userImg"></div><div class="commentDetails"><div class="userSays">' + msg + '<span> says</span></div><div class="comment">' + $(".commentTxtAra").val() + '</div></div><div class="clearInline"></div></div>')
					}
				} else {
					alert(data);
				}
				Recaptcha.reload();
			}
		);
		return false;
	});
});
/*
v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substring(1,100)(v_this.val().substr
*/
