$(function(){
	//
	initialize_buttons();
	
	$(".close_reply").livequery('click',function(){
		$('.add_reply').remove();
		$('.submitReply').remove();
		var button_img = $(this).find('img');
		var path = button_img.attr('src');
		var path_arr = path.split('/');
		var old_btn = path_arr[path_arr.length-1];
		var path_copy = path_arr;
		path_arr[path_arr.length-1]='';
		var gphx_path = path_arr.join('/');
		//
		initialize_graphics(gphx_path);
		$(this).removeClass('close_reply');
		initialize_buttons();
	});

	
	$("#episode_comments form.submitReply").livequery('submit',function(event){
		var last_post = $(this).parent().parent().parent().prev().find('h3');
			$.post($(this).attr('action'),$(this).serialize(),function(data){
					last_post.text(data.last_post);
					$.post(data.last_reply, function(data){
						$("#episode_comments").replaceWith(data);
					});
			},"json");
				return false;
	});
	//
	
	
	$("#adventure_reviews form.submitReply").livequery('submit',function(event){
		var last_post = $(this).parent().parent().parent().prev().find('h3');
			$.post($(this).attr('action'),$(this).serialize(),function(data){
					last_post.text(data.last_post);
					$.post(data.last_reply, function(data){
						$("#adventure_reviews").replaceWith(data);
					});
			},"json");
				return false;
	});
	//
	
	$("#book_reviews .submitReply").livequery('submit',function(event){	
		var last_post = $(this).parent().parent().parent().prev().find('h3');
			$.post($(this).attr('action'),$(this).serialize(),function(data){
					last_post.text(data.last_post);
					$.post(data.last_reply, function(data){
						$("#book_reviews").replaceWith(data);
					});
			},"json");
				return false;
	});
	//
	$(".blog_comments .submitReply").livequery('submit',function(event){
		var last_post = $(this).parent().parent().parent().prev().find('h3');
		var target  = $(this).parent().parent().parent().parent().parent().parent().parent();
			$.post($(this).attr('action'),$(this).serialize(),function(data){
					$.post(data.last_reply, function(data){
						target.replaceWith(data);
					});
			},"json");
				return false;
	});

	$('#adventure-star-rating > li > a').livequery('click',function(event){
		var value = $(this).text();
		$('input[name=rating]').attr('value',value);
		
		for(var p=1; p<6;p++){
			if(p > value){
				$('a.star-'+p).parent().removeClass('selected');			
			}else{
				$('a.star-'+p).parent().addClass('selected');	
			}
		}
		return false;		
	});
	
	$('#book-star-rating > li > a').livequery('click',function(event){
		var value = $(this).text();
		$('input[name=rating]').attr('value',value);
		
		for(var p=1; p<6;p++){
			if(p > value){
				$('a.star-'+p).parent().removeClass('selected');			
			}else{
				$('a.star-'+p).parent().addClass('selected');	
			}
		}
		return false;		
	});
	
		$("#episode_comments .pagination ul li a").livequery("click", function(event){
			$.post($(this).attr('href'),function(data){
					$("#episode_comments").replaceWith(data);
				return false;});
			return false;
		});
		
		$("#adventure_reviews .pagination ul li a").livequery("click", function(event){
			$.post($(this).attr('href'),function(data){
					$("#adventure_reviews").replaceWith(data);
				return false;});
			return false;
		});
			
		$("#book_reviews .pagination ul li a").livequery("click", function(event){
			$.post($(this).attr('href'),function(data){
					$("#book_reviews").replaceWith(data);
				return false;});
			return false;
		});
		
			$(".blog_comments .pagination ul li a").livequery("click", function(event){
				var target  = $(this).parent().parent().parent().parent().parent().parent().parent();
				$.post($(this).attr('href'),function(data){
					target.replaceWith(data);
					return false;});
				return false;
			});
		
});


function initialize_buttons(){
	$("a.episode_comment, a.adventure_review, a.book_review, a.blog_comment").expire('click');
	$("a.episode_comment, a.adventure_review, a.book_review, a.blog_comment").removeClass('close_reply');
	$("a.episode_comment, a.adventure_review, a.book_review, a.blog_comment").livequery('click',function(event){
		var target = $(this).parent();
		var button_img = $(this).find('img');
		var button = $(this);
		
	if(target.parent().find('.submitReply').length>0) return false;
		var url = $(this).attr('href');
			$.post(url,function(data){
				if (data=='false') {
					window.modalCall('/account/login',240,450);
				} else {
					target.before(data);
					if (document.all && document.getElementById) {
					window.setTimeout(function(){
						$('.star-rating-ul').css('display','none')
						window.setTimeout(function(){
							$('.star-rating-ul').css('display','block')
						},1);
					},1);
					}
					
					
					
					var path = button_img.attr('src');
					var path_arr = path.split('/');
					var old_btn = path_arr[path_arr.length-1];
					path_arr[path_arr.length-1]='cancel-clear.gif';
					var new_src = path_arr.join('/');
					button_img.attr('src',new_src);
					button.expire('click');
					button.addClass('close_reply')
					
				}
				return false;
				});
		return false;
	});
}

function initialize_graphics(path){
		$("a.episode_comment").find('img').attr('src',path+'post-a-comment-yellow.gif');
		$("a.adventure_review").find('img').attr('src',path+'post-a-review-yellow.gif');
		$("a.book_review").find('img').attr('src',path+'post-a-review-yellow.gif');
		$("a.blog_comment").find('img').attr('src',path+'post-a-comment-clear.gif');
}
