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

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

    $("form.post-form").submit(function(){
      /*if($(this).find("textarea[name=contents]").val() == ""){
	  alert("Please enter the contents");
	$(this).find("textarea[name=contents]").focus();
	return false;
      }*/
      var member = is_member();
      if(!member){
	var emailField = $(this).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;
	}
	if($(this).find("input[name=name]").val() == ""){
		alert("Please enter your name");
		$(this).find("input[name=name]").focus();
		return false;
	}
    }
    });
	
   $(".search-form").submit(function(){
	   if($(this).find("input[name=q]").val() == ''){
		   alert("Please enter your keyword");
		   $(this).find("input[name=q]").focus();
		   return false;
	   }
	   return true;
   });


   function submitReplyForm(replyForm){
		replyForm.ajaxForm({
                  beforeSubmit: function(){
                        if(replyForm.find("textarea").val() == "")
                        {
                              alert("Please enter your reply 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('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().parent().append(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;
	});

	$('a.quote-action').live('click',function(){
		var quoteName = $(this).parent().parent().find('.u-name').text();
		var quoteContent = $(this).parent().parent().find('.r-content').text();
		var quoteString = "[quote="+quoteName+"]\r\n"+quoteContent+"\r\n[/quote]";
		$(".post-form textarea[name=contents]").val(quoteString);
		$(".post-form textarea[name=contents]").focus();
		return false;
	});

	$('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;
	});


	$("#static-box").load('/compare/items');

	$('a.compare').live('click',function(){
		var num = 1;
		var pid = $(this).attr('pid');
		var complareList = [];
		for(var i = 0;i < 5; i++){
			var cmid = $.cookie('compare_'+i);
			if(cmid){
				complareList.push(cmid);
			}

		}
		var isExist = false;
		$.each(complareList, function(index, value){
			if(value == pid){
				alert('This product is already in the compare list.');
				isExist = true;
				return;
			}
		});
		if(isExist){
			return false;
		}
		complareList.unshift(pid);
		var index = 1;
		$.each(complareList, function(n, value){
									index = n+1;
									$.cookie('compare_'+index,value,{ path: '/' });
								});
		$("#static-box").load('/compare/items');
		return false;
	});

	$('a.remove-compare').live('click',function(){
		var cmid = $(this).attr('cmid');
		var num = $(".compare-num").text();
		num = Number(num);
		num = num - 1;
		$(".compare-num").text(num);
		$.cookie(cmid,null);
		$("#"+cmid).remove();
		return false;
	});

}); 
