
// FUNCTIONS
// ===============================

// thanks to Yus [ http://wordpress.org/extend/plugins/reply-to/ ]
function yus_replyTo(commentID, author) {
	var inReplyTo = '@<a href="#' + commentID + '">' + author + '<\/a>: ';
	var myField;
	if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') {
		myField = document.getElementById('comment');
	} else {
		return false;
	}
	if (document.selection) {
		myField.focus();
		var sel = document.selection.createRange();
		sel.text = inReplyTo;
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		myField.value = myField.value.substring(0, startPos) + inReplyTo + myField.value.substring(endPos, myField.value.length);
		cursorPos += inReplyTo.length;
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
	}
	else {
		myField.value += inReplyTo;
		myField.focus();
	}
};
function cReply(eThis){
	var cId = $(eThis).parent().attr("id");
	var cAuthor = $(eThis).siblings(".comment-body").children(".vcard").children("cite").text();
	yus_replyTo(cId,cAuthor);
};

// ONLOAD
// ===============================

$(function(){
	//
	// NAV Button ----------------

	$("a.nav-button").click(function(){// for nav submenu
		if(this.id=="nav-gallery")return;
		if($(this).parent().hasClass("on")){// remove parent <li> class
			$(this).parent().removeClass("on");
			$("#" + this.id + "-sub").hide();
		}else{// set parent <li> class
			$(this).parent().siblings().removeClass("on").children("div").hide();
			$(this).parent().addClass("on");
			$("#" + this.id + "-sub").slideDown(100);
		}
		return false;
	});
	document.body.onclick = function(){// clear <li> class
		$("#nav .nav-sub").hide();
		$("#nav li").removeClass("on");
	};
	$("#go-home").mouseenter(function(){
		$("#random").stop(true,true).hide().siblings("#intro").animate({width: 'show'});
	}).mouseleave(function(){
		$("#intro").stop(true,true).hide().siblings("#random").fadeIn();
	});

	//
	// For Pages -----------------

	// Page: About (movie)
	if(document.getElementById("movie")){
		//init
		$(".movie-item").one('click',function(){
			$(this).delay(300).fadeIn("slow",function(){
				$(this).delay(1830).fadeOut(300,function(){
					$(this).next().trigger("click");
				});
			});
		});
		// End Show All
		$("#movie-last").one("click",function(){
			$("#movie").removeClass("movie-on").addClass("movie-off");//diff style between movie playing and End Show, for hr.
			$(".movie-item:not(#movie-start)").fadeIn(2600);
		});
		// Start Movie
		$("#movie-start")
				.fadeIn("slow").delay(500).fadeOut("slow").delay(150)
				.fadeIn("slow").delay(400).fadeOut("slow").delay(150)
				.fadeIn("slow").delay(500)
				.slideUp(1000,function(){
			$(this).next().delay(1400).trigger("click");
		});
	}
	// Page: Single Post
	if(document.getElementById("p-header")){ // for prev & next button
		$("#p-header>h2").mouseenter(function(){
			$("#p-header>h2>div").fadeIn("fast");
			$("#p-header>h2>div>a").fadeIn("fast");
		}).mouseleave(function(){
			$("#p-header>h2>div>a").fadeOut("fast");
		});
	}
	// Page: Single Post [having comment]
	if(document.getElementById("p-comments")){ // for comment reply button & highlight
		// Comment REPLY Button
		$(".comment-body").parent().append('<div class="c-reply" onclick="cReply(this);">Reply<br/></div>')
				.mouseenter(function(){$(this).children(".c-reply").slideDown("fast");})
				.mouseleave(function(){$(this).children(".c-reply").slideUp("fast");});
		$("#p-comments a[href|=#comment]").click(function(){
			var target = $($(this).attr("href"))
			target.stop().attr("style","background-color: #EEE").effect("highlight", { "colors" : "#ABC" }, 5000);
		});
		// Comment highlight on pageload
		var montage = window.location.href.split("#comment-");
		if(montage.length == 2)
		var cmtId = montage[1];
		$('#comment-'+cmtId).effect("highlight", { "colors" : "#ABC" }, 5000);
	}

	// Page: Image Attachment
	if(document.getElementById("p-image")){
		// logo nav
		$("#pix-logo-hover").mouseenter(function(){
			$("#pix-parent").stop(true,true).hide().animate({width: 'show'});
		}).mouseleave(function(){
			$("#pix-parent").stop(true,true).fadeOut("fast");
		});
		// title comment button
		$("#pix-cmt-count").click(function(){
//			$("#p-comments").show(0,function(){
				location.href = location.href.split("#")[0] + "#p-comments";
//			});
			return false;
		});
		// shortcuts
		$("#commentform input, #commentform textarea").focus(function(){// set focus stat of forms
			document.forms["commentform"].focused = true;
		}).blur(function(){
			document.forms["commentform"].focused = false;
		});
		$(document).bind('keydown', 'left', function(){
			if( !document.forms["commentform"].focused ){
				location.href = document.getElementById('key-left').childNodes[0].href;
			}
		}).bind('keydown', 'j', function(){
			if( !document.forms["commentform"].focused ){
				location.href = document.getElementById('key-left').childNodes[0].href;
			}
		}).bind('keydown', 'right', function(){
			if( !document.forms["commentform"].focused ){
				location.href = document.getElementById('key-right').childNodes[0].href;
			}
		}).bind('keydown', 'k', function(){
			if( !document.forms["commentform"].focused ){
				location.href = document.getElementById('key-right').childNodes[0].href;
			}
		}).bind('keyup', 'return', function(){// Post Comment
			if( !document.forms["commentform"].focused ){
				$("#p-comments").show(0,function(){
					document.getElementById('comment').focus();
				});
			}
		}).bind('keyup', 'c', function(){// Show Comment
			if( !document.forms["commentform"].focused ){
				$("#pix-cmt-count").click();
			}
		});
	}
});

function bodyOnloadFn(){
	// Page: Image Attachment [gallery style]
	if(document.getElementById("p-image")){
		$("#image-loading").fadeOut("fast",function(){// show image
			$("#p-image img").delay(200).fadeIn("slow");
		});
	}
}

function cancelEvent(e)
{
	e = e ? e : window.event;
	if (e.stopPropagation)
		e.stopPropagation();
	if (e.preventDefault)
		e.preventDefault();
	e.cancelBubble = true;
	e.cancel = true;
	e.returnValue = false;
	return false;
}

function galleryHomeFunc(){
	$(".gallery-item img").removeAttr("width").removeAttr("height");
}

function galleryImgInit(jId){
	$(jId + " img").hide().load(function(){$(this).fadeIn("slow")});
	// hover effect
	$(jId + " a").mouseenter(function(){$(this).children("img").fadeTo(300,0.6)})
			.mouseleave(function(){$(this).children("img").fadeTo("slow",1)});
}

// Custom Lightbox style for Gallery Img Page

function showFullImg(url){
	// simple lightbox
	var eShadow = document.createElement("div");
	eShadow.id = "shadowBg";
	eShadow.setAttribute("style","min-height:100%; min-width:100%; z-index:999; background-color: #333; position: absolute; top: 0; display: none; text-align: center");
	eShadow.setAttribute("onclick","hideFullImg();");
	if(!$Id("shadowBg")){// add bg and img
		document.body.appendChild(eShadow);
		var pix = new Image();
		pix.src = url;
		$Id("shadowBg").appendChild(pix);
	}
//	$Id("wrapper").style.display = "none";
	$("#wrapper").fadeOut(150,function(){

	$("#shadowBg").fadeIn(100);
	});
}

function hideFullImg(){
	$("#shadowBg").hide(0,function(){
		$Id("wrapper").style.display = "block";
	})
}

// To remove

function a(){alert("test")};