$(function(){
    var member = is_member();
    if(member){
      $("form.post-form .user-info").hide();
      $("form.post-form .welcome").show();
      $("form.post-form .welcome span").html(member);
      $("div.member-area").html('Welcome, <a href="/account/">'+member+'</a> (<a href="/account/signout">logout</a>)');
    }

    $(".top-new a").click(function(){
       if($(this).is(".current")){
          return false;
       }
       $(".top-new a").removeClass('current');
       $(this).addClass('current');
       $(".list-frame").hide();
       $($(this).attr("href")).show();
       return false;
    });

    $(".post-form input[name=welcome]").val("3");


	// product page start
	$("div.reviews-sort a").live("click",function(){
				if($(this).is(".current"))
				{
				if($(this).attr("order") == "desc"){
				$(this).attr("order","asc");
				$(this).find("span.ui-icon").removeClass("icon-desc").addClass("icon-asc");
				}else{
				$(this).attr("order","desc");
				$(this).find("span.ui-icon").removeClass("icon-asc").addClass("icon-desc");
				}
				}else{
				$("div.reviews-sort a").removeClass("current");
				$(this).addClass("current");
				}
			var target = $(this).attr("href")+"&order="+$(this).attr("order");
			getReviewAjax(target);
			return false;
			 });

	$("div.review-list div.page-index a").live("click",function(){
				                getReviewAjax($(this).attr("href"));
						return false;
	});

	function getReviewAjax(targetUrl){
		$("div.review-list").children().animate({opacity:0.3},1000);
		$("div.review-list").append("<span class='loading'></span>");
		$("div.review-list").attr("href",targetUrl);
		$.ajax({
			url : targetUrl,
			success: function(data){
				$("div.review-list").html(data);
				}
			});
	}
	// product page end
	$('a.vote-reply-yes').live('click', function(){
		var voteDiv = $(this);
		var rid = $(this).attr('rid');
		var voteType = 'yes';
		$.get('/ajax/rateHelpful',{item: rid, type: voteType}, function(data){
			voteDiv.find('span').html(data);
		});
		return false;
	});

	$('a.vote-reply-no').live('click', function(){
		var voteDiv = $(this);
		var rid = $(this).attr('rid');
		var voteType = 'no';
		$.get('/ajax/rateHelpful',{item: rid, type: voteType}, function(data){
			voteDiv.find('span').html(data);
		});
		return false;
	});

	//reply function
	function submitReplyForm(replyForm){
		replyForm.ajaxForm({
                  beforeSubmit: function(){
                        if(replyForm.find("textarea").val() == "")
                        {
                              alert("Please enter the content.");
                              replyForm.find("textarea").focus();
                              return false;
                         }
                         member  = is_member();
                         if(member){
                             return true;
                         }else{
                             if(replyForm.find("input[name=name]").val() == ""){
                                   alert("Please enter your name.");
                                   replyForm.find("input[name=name]").focus();
                                   return false;
                             }
                             var emailField = replyForm.find("input[name=email]");
							 if(emailField.val() == ""){
								alert("Please enter your email");
								emailField.focus();
								return false;
							 }
							 if(!is_email(emailField.val())){
							   alert("Please enter a valid email");
							   emailField.focus();
							   return false;
							 }
                         }
                         replyForm.html('<img src="/image2011/loading.gif" height="20px"/> Loading');
                         return true;
                 },
                 success :  function(data){
                              replyForm.before(data);
			                  replyForm.remove();
                            }
        });
	}

	$('a.reply-action').live('click', function(){
		$(this).parent().parent().find("form.reply-form").remove();
		var replyId = $(this).attr("reply");
		var replyForm = $("form.reply-form:last").clone();
		$(this).parent().after(replyForm);
		replyForm.show();
		replyForm.find("input[name=id]").val(replyId);
		replyForm.find('textarea').focus();
		replyForm.find('button.cancel').click(function(){
			replyForm.remove();
			return false;
		});
		submitReplyForm(replyForm);
		return false;
	});
        
}); 
