﻿/**
 * JavaScript blog
 */
 
$(function(){
	$("#txtPostSearch01").val("Search")
		.removeClass("hasText");
	saveInputTips($("#txtPostSearch01"));
	
	//feature posts slide
	// slidePost();
	function slidePost(){
		var jQObj_slide = $("#featurePostsList");
		if(jQObj_slide.length == 0){
			return false;
		}
		var jQObj_slide_wrap = jQObj_slide.parent();
		var jQObj_slide_content = jQObj_slide.find("li");
		var objWidth = jQObj_slide_content.width() + parseInt(jQObj_slide_content.css("padding-right"));
		var count = jQObj_slide_content.length;
		var slideWidth = objWidth * count;
		var slide = function(){
			if(jQObj_slide_wrap.scrollLeft() >= slideWidth){
				jQObj_slide_wrap.scrollLeft(0);
			}
			var dur = Math.ceil((slideWidth - jQObj_slide_wrap.scrollLeft())*1500/objWidth);
			jQObj_slide_wrap.animate({
				scrollLeft: slideWidth
			}, dur, "linear", slide);
		}
		jQObj_slide.hover(
			function(){
				jQObj_slide_wrap.stop();
			}, 
			function(){
				slide();
			}
		);
		jQObj_slide.find("li").clone(true).appendTo(jQObj_slide);
		slide();
	}
	
	//search inputbox
	$("#txtPostSearch01").bind("focus", function(){
		if($(this).val() == $(this).data("tips")){
			$(this).val("");
		}
		$(this).addClass("hasText");
	}).bind("blur", function(){
		if($(this).val() == ""){
			$(this).removeClass("hasText")
				.val($(this).data("tips"));
		}
	});

	//delete
	$(".btnDel01").bind("click",  {}, del);
	function del(event){
		if(confirm('Are you sure to delete this comment?') == false) {
			return;
		}
		var obj = $(this).parent().children("h3").children("a");
		var delId = obj.parent().children("input:hidden").val();

		var data = { id: delId};
		var url  = 'deleteComment.php';

		$.post(url, data, function(json){
			if(json.status == 'successful') {
				window.location.reload();
			} else {
				alert(json.msg);
			}
		}, "json");
	}

	//reply
	$(".btnReply01").bind("click", {}, reply);
	function reply(event){
		var obj = $(this).parent().parent().children("h3").children("a");
		var userReplyTo = obj.text();
		var userReplyToId = obj.parent().children("input:hidden").val();
		$("#userReplyTo").text(userReplyTo);
		$("#comment_parent").val(userReplyToId);
		$("#replyTips").show();
		$("#commentform").css({
			height: "auto"
		});
		event.stopPropagation();
	}
	
	//reply undo
	$("#replyUndo").click(function(){
		$("#comment_parent").val(0);
		$("#replyTips").hide();
	});
	
	// Submit comments
	$("#btnPostComment").click(function(){
		var content = $.trim($("#txtComments").val());
		
		if(content == "") {
			alert("Please leave a message!");
			$("#txtComments").focus();
			return false;
		}
		
		var id = $("#comment_post_ID").val();
		var parentId = $("#comment_parent").val();
		var data = { comment_parent:parentId, comment_post_ID:id, comment:content};
		var url  = 'wp-comments-post.php';
		
		$.post(url, data, function(json){
			if(json.status == 'successful') {
				json = json.data;
				if(parentId == 0) {
					// window.location.href = baseUrl+"#bg-comments";
					if($("#bg-comments ul.commentsList").length == 0) {
						$("#bg-comments #paginationTop").after('<a name="comments"></a><ul class="commentsList"></ul>');
						$("#commentsCount").html("<strong>Showing 0</strong> of 0 Comments");
						$(".commentsTips01").remove();
					}
					
					var classLi = "odd";
					if($("#bg-comments .commentsList li:first").hasClass("odd")){
						classLi = "even";
					}
				  	tmpHtml =
						'<li class="'+classLi+'" id="commentsId' + json.comment_ID + '">' +
						'	<h3><a href="' + json.comment_author_url + '">' + json.comment_author + '<input type="hidden" value="' + json.comment_ID + '" /></a> <span>@ ' + json.comment_date + '</span>' +
						'	<p>' + json.comment_content + '</p>' +
						'<div id="btnBg" class="Reply">' +
						'	<a href="#addCommentsWrap" class="Reply btnReply01">Reply</a>' +
						'</div>' +
						'</li>';
					$("#bg-comments ul.commentsList").prepend(tmpHtml);
					$("#bg-comments a.btnReply01:first").bind("click", {jQ_this: $(this)}, reply);
					var txtCount = $("#commentsCount").html();
					var r = txtCount.match(/\d+/g);
					txtCount = txtCount.replace(r[1], parseInt(r[1])+1);
					txtCount = txtCount.replace(r[0], parseInt(r[0])+1);
					$("#commentsCount").html(txtCount);
					window.location.href = "#bg-comments";
				} else {
					// window.location.href = baseUrl+"&page="+page+"#commentsId"+parentId;
					$("#currAnchor").remove();
				  	tmpHtml =
						'<li class="sub01">' +
						'	<div id="currAnchor">&nbsp;</div>' +
						'	<h3><a href="' + json.comment_author_url + '">' + json.comment_author + '</a></h3>' +
						'	<p>' + json.comment_content + '</p>' +
						'</li>';
					$("#commentsId" + parentId).after(tmpHtml);
					$("#replyUndo").click();
					window.location.href = "#currAnchor";
				}
				$("#txtComments").val("");
				if($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")){
					document.body.style.zoom = 1.1;
					document.body.style.zoom = 1;
				}
			} else {
				if(json.status == 'pending') {
					alert("We're sorry that your comment has some sensitive words, please wait for our approval.");
					if(parentId != 0) {
						$("#replyUndo").click();
					}
					$("#txtComments").val("");
				} else {
					alert(json.msg);
				}
			}
		}, "json");
		//$("#commentform").submit();
	});
	
	//login box
	$("#addCommentsTips04 a").click(function(){
		var currHeight = $("#addCommentsWrap").height();
		var minHeight = 64;
		var currScrollTop = $(document).scrollTop();
		var fixedScrollTop = $(document).height() - $(window).height() - (currHeight - minHeight);
		if((currScrollTop > fixedScrollTop) && ($(".col01").height() > $(".col02").height())){
			$(document).scrollTop(fixedScrollTop);
		}
		$("#addCommentsWrap").animate({
			height: minHeight
		}, "fast", "swing");
		return false;
	});
	
	$("#btn-login").click(function(){
		login($(this).parents("form")[0]);
	});

/*	
	//images load
	function enhancedImage(src, fn){
		var img = new Image();
		img.src = src;

		if(img.complete){
			fn.call(img);
		}else{
			img.onload = function(){
				fn.call(img);
			}
		}
	}
	function bImgWidth(obj, maxWidth){
		enhancedImage(obj.src, function(){
			if(this.width > maxWidth){
				$(obj).addClass("postImages");
			}
		});
	}
*/
}); 

$(window).load(function(){
						
	//set maxWidth of images in post
	$(".postText01 img").addClass("postImages");
	if($.browser.msie && $.browser.version == "6.0"){
		$(".postText01 img.postImages").each(function(){
			if($(this).width() > 580){
				$(this).width(580);
			}
		});
		document.body.style.zoom = 1.1;
		document.body.style.zoom = 1;
	}
});

