$(document).ready(function(){
	getCaptcha('form');
    form = $('.thread').html();
	formSet();
	$('.reply').hover(
		function () {
		    $(this).css("cursor","pointer");
		},
		function () {
		    $(this).css("cousor","");
		}
	).click(function(){
		removeCommet();
		newThread();
		addComment($(this).parents('.commentslist'));
		$(this).parents('.commentslist').addClass("replycomment");
		quote($(this));
	});
});

function newThread()
{
	if($('#newthread').attr('class') == 'disn'){
		$('#newthread').attr('class','newthread');
		$('.newthread').hover(
		    function () {
			    $(this).css("cursor","pointer");
			},
			function () {
			    $(this).css("cousor","");
			}
		).click(function(){
			removeCommet();
			addComment($('.thread'));
			$('#newthread').attr('class','disn');
		});
	}
}

function removeCommet()
{
	$('.commentslist').each(function(){
		$(this).removeClass("replycomment");
	})
	$(".addcomment").remove();
}

function addComment(obj)
{
	$(obj).append(form);
	if($('input[name=LinkType]').val() == 0){
		$('#link').hide();
	}
	formSet();
}

function formSet()
{
	$('#commentText,#commentImg,#commentVedio').hover(
		function () {
		    $(this).css("cursor","pointer");
		},
		function () {
		    $(this).css("cousor","");
		}
	).click(function(){
 		 var linkType = 0;
		 var linkTxt  = '';
		 if($('input[name=Link]').val() != 'http://'){
		     $('input[name=Link]').val('http://');
		 }
    	 switch($(this).attr('id'))
		 {
		     case 'commentImg':  linkType = 1; linkTxt = 'Image URL';$('#link').show();
		     break;
			 case 'commentVedio':  linkType = 2;linkTxt = 'YouTuBe URL';$('#link').show();
			 break;
		     default: linkType = 0;$('#link').hide();
		 }
	 	 $('input[name=LinkType]').val(linkType);
	     $('#LinkTxt').html(linkTxt);
	});
	$("#commentForm").submit(function(){
		var hasError = false;
		var userName = $.trim($("#userName").val());
		var email    = $.trim($("#email").val());
		var link     = $.trim($('input[name=Link]').val());
		var linkType = $.trim($('input[name=LinkType]').val());
		var comment  = $.trim($("#comment").val());
		var auth_code = $.trim($("#auth_code").val());
		$('form#commentForm span.tip').html('');
		if(userName == '' || userName == 'YOUR NAME'){
			hasError = true;
			$("#userNameTip").html('Please fill in your name.');
		}else{
			$.ajax({type: "POST",
				async: false,
				url  : "/async_commentcheck.php",
				data : "clientid=userName&userName="+userName, 
				success:function(data){
					if(data=="true"){
						$("#userNameTip").html("");
					}else{
						hasError = true;
						$("#userNameTip").html("oops! Your name contained a word it shouldn't.  Please try again.");
					}
				}
			});
		}
		if(link == 'http://'){
		    link = '';
		}
		if(linkType !=0 && (link =='' || !/^[a-zA-z]+:\/\/[^\s]*$/.test(link))){
			hasError = true;
			$("#linkTip").html('Please fill in a valid link address.');
		}
		if(email == '' || email == 'YOUR EMAIL'){
			hasError = true;
			$("#emailTip").html('Please fill in your email.');
		}else{
			var  reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;  
			if(!reg.test(email)){
				hasError = true;
				$("#emailTip").html('Please enter a valid email address.');
			}else{
				$("#emailTip").html('');
			}
		}
		if(comment=='' || comment == 'Enter your comment here...(max: 2,000 characters)'){
			hasError = true;
			$("#commentTip").html('Please fill in your comment.<br>');
		}else if(comment.length>2000){
			hasError = true;
			$("#commentTip").html("You've inputted too much comment! Please refine your comment to meet the maximum number of characters allowed.(max: 2000)");
		}else{
			$.ajax({type: "POST",
				async: false,
				url  : "/async_commentcheck.php",
				data : "clientid=comment&comment="+comment, 
				success:function(data){
					if(data=="true"){
						$("#commentTip").html("");
					}else{
						hasError = true;
						$("#commentTip").html("oops! Your comment contained a word it shouldn't.  Please try again.");
					}
				}
			});
		}
		if(auth_code==''){
			hasError = true;
			$("#auth_codeTip").html("Please fill in security code.");
		}
		return !hasError;
	}); 
	
	$('form img[alt=captcha],form a#captchaRefresh').click(function(){
		getCaptcha();
		return false;
	});
	
	$('#CancelComment').click(function(){
		removeCommet();
		newThread();
	});
	
	if($('input[name=LinkType]').val() == 0){
		$('#link').hide();
	}
	if( str = getCookie('alert')){
		alert(str);
		delCookie('alert');
	}
}
function getCaptcha(f)
{
	$.getJSON("/async_captcha.php",
			{'action': 'refresh'},
			function(data){
				$('form input#captcha_id').val(data.id);
				if(!$('form img[alt="captcha"]').attr('src')){
					var img = $('#auth_img').html('<img src='+data.url+' alt="captcha">');
				}else{
					var img = $('form img[alt="captcha"]').attr('src', data.url);
				}
				if (!img.is('img')) {
					var iePng = $('form span[title="captcha"]');
					if (iePng.is('span')) {
						iePng.attr('style', iePng.attr('style').replace(/progid:DXImageTransform\.Microsoft\.AlphaImageLoader\(src='.+',/i, "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + data.url +"',"));
					}
				}
				if(f){
					form = $('.thread').html();
				}
			}
		)
}

function clear_tips(obj,str)
{
	if(obj.value == str)
	{
		obj.value='';
		have_tips=false;
	}
	$('.userName').parent().html();
}


function getCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null){
    	var str = unescape(arr[2]);
        str = str.replace(/\+/g," ");
    	return str; 
    }
    return null;
}

function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function quote(obj)
{
	var str = $(obj).parents('.commentslist').find('.commentShow').html();
	str = specialChar($.trim(str));
	var usr = specialChar($(obj).parents(".author").find("strong").html());
	$("#comment").val("[quote]Posted by "+usr+"[hr]"+str+"[/quote]\n");
	$("#userName, #email, #comment").css('color', '#000000');
}

function specialChar(str)
{
	str = str.replace(/\<div class\=(\")*quote(\")*\>/gi,"[quote]");
	str = str.replace(/\<\/div\>(\<br\>|\n)*/gi,"[/quote]");
	str = str.replace(/(\<br\>)[\n\r]*/gi,"[hr]");
	str = str.replace(/&lt;/gi,"<");
	str = str.replace(/&gt;/gi,">");
	str = str.replace(/&amp;/gi,"&");
	str = str.replace(/&nbsp;/gi," ");
	return str;
}

