/*
 * by Diwa G. Fernandez
 * Date: 2009-12-07 15:45 +8 GMT
 * Revision: 1
 */


/*************************************************
 * Global Helper
 *************************************************/
function DO(domId) { return window.document.getElementById(domId); }

jQuery.fn.extend({ scrollTo : function(speed, easing) { return this.each(function() { var targetOffset = $(this).offset().top; $('html,body').stop().animate({scrollTop: targetOffset}, speed, easing);});}});

	function openIt($divID,$postID) {
		var $story = $($divID).find(".story");
		$($divID).find("#preload").fadeIn(100);
	
		if ($story.hasClass('loaded')) {
			checkStory($divID,$postID);
		} else {
		$story.load( $template_directory + '/simple.php?page=' + $postID,
			function() {
					checkStory($divID,$postID);
					$story.addClass('loaded');
			})
		}
	}	
	function checkStory($divID,$postID){
		var $thepoet = $("#thepoet").height();
		//checkNewsTabActive(linkId);
		var $story = $($divID).find(".story");
			$("#preload").fadeOut(1000,function() {
				var $hyt = $story.height();
				$($divID).stop().animate({height: $hyt+75 },1000,function() {
						$($divID).find(".view-post").hide();
				$($divID).find(".story").stop().animate({ 
					opacity: 'toggle'
				},500,function(){
						$("#preload").remove();
						$($divID).find(".close-post").fadeIn(500);
						$($divID).find("#thumb").fadeOut(300);
					});
				})
				$($divID).scrollTo(500);
			});
	}
    function hideIt($divID,$hyt) {
		var $bheight = $(window).height();
		$($divID).find(".story").stop().animate({ 
				opacity: 'toggle'
		},1000, function() {
			$($divID).animate({height: $hyt },500,function(){
				$($divID).find(".view-post").fadeIn(500);
				$($divID).find(".close-post").fadeOut(100);
				$($divID).find("#thumb").fadeIn(300);
			});
		});
    }
function handler(event) {
		var $postID = $(this).find("a").attr("data");
		var $thisID = '#post-'+$postID;
		var $story = $($thisID).find(".story");
		//alert($thisID)
		if ($story.is(':hidden')) {
		$($thisID).find(".entry").after('<div id="preload"><img src="'+$template_directory+'/images/ajax-loader.gif" /></div>');			
			openIt($thisID,$postID)
		} else {
			hideIt($thisID,'74')
		}
		//$(this).unbind();
		return false;
}
function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'facebook','toolbar=0,status=0,width=626,height=436');return false;}
function tweet_click() {u=location.href;t=document.title;window.open('http://twitter.com/home?status=Currently reading '+encodeURIComponent(u),'tweeter','toolbar=0,status=0,width=626,height=436');return false;}
function linkedin_click() {url=location.href;source=document.domain;title=document.title;window.open('http://www.linkedin.com/shareArticle?mini=true&url= '+encodeURIComponent(url)+'&title='+encodeURIComponent(title)+'&source='+encodeURIComponent(source),'linked_in','toolbar=0,status=0,width=626,height=436');return false;}
function delicious_click() {url=location.href;source=document.domain;title=document.title;window.open('http://delicious.com/save?v=5&noui&jump=close&url= '+encodeURIComponent(url)+'&title='+encodeURIComponent(title),'delicious','toolbar=0,status=0,width=650,height=550');return false;}
$(document).ready(function(){
	var $thepoet = $("#thepoet").height();
	var $bheight = $(window).height();
	var $shdwleft = $(".bgshdwleft");
	var $shdwryt = $(".bgshdwright");
	var $this = $(this);
	
	$shdwleft.css({
		height: $thepoet
				  });
	$shdwryt.css({
		height: $thepoet
				  });
	
	$(".view-post").bind("click", handler)	
	$(".minititle").bind("click", handler)	
	$(".close-post").bind("click", handler)
	$('#gototop').click(function(){$('#rap').ScrollTo(700);return false});
	$('.btn-slide').toggle(function(){
			$("#panel").slideToggle("slow");
			$('.btn-slide').animate({ backgroundColor:'#51676a'}, 500);
			$(this).css({color: '#a8d6dc'});
	},function(){
			$("#panel").slideToggle("slow");
			$('.btn-slide').animate({backgroundColor:'#d3db23'}, 500);
			$(this).css({color: '#563311'});
	});
});


//////////////////// scroll by jquery ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};

Cufon.set("selector",jQuery);Cufon.replace("h3");Cufon.replace("h2");Cufon.replace("h1");Cufon.replace(".category");Cufon.replace(".description");Cufon.replace(".datepost");Cufon.replace('.category'); 


