/************************************************
* slowbirdsFrameWork
* since:	2009-05-26
* modified: 2009-05-26
* edited:   slowbirds@gmail.com
************************************************/

$(function() {
	var _Globalnav = {};
	_Globalnav.open = function(direction){
		var $navBody = $("#globalNavBody");
		var $navBtn = $("#globalNavController");
		var minleft = 25;
		if($navBody.data("opening") == "animating") return;
		if($navBody.data("opening") == "close") {
			$navBody.data("opening","animating");
			$navBody.animate({
				left: 0
			},1500);
			$navBody.data("opening","open");
			$navBtn.children("img").attr("src","/shared/images/global_nav_btn_close.png");
			return;
		}
		else if(direction == "close" && $navBody.data("opening") == "open") {
			$navBtn.children("img").attr("src","/shared/images/global_nav_btn.png");
			$navBody.data("opening","animating");
			$navBody.animate({
				left: minleft-$("#globalNavBodyWrapper").width()
			},1500);
			$navBody.data("opening","close");
		}
	}
	_Globalnav.controll = function() {
		var $navBody = $("#globalNavBody");
		var $navBtn = $("#globalNavController");
		$navBody.data("opening","open");
		$navBtn
			.css("cursor","pointer")
			.mouseover(function() {
				_Globalnav.open();
			})
			.click(function() {
				_Globalnav.open("close");
			})
			.click();
		$("#navPushedRemote").click(function() {
			if($navBody.data("opening") == "open") {
				$navBtn.click();
			}else {
				$navBtn.mouseover();
			}
			return false;
		});
	};
	var topTumblr = function() {
		$("#topTumblr").load("/modules/tumblr/index.php",function(){
			$("#topTumblr").find("li")
				.mouseover(function() {
					var imgpath;
					$(this).find("img").each(function(){
						imgpath = $(this).attr("src");
					});
					var imgSrc = imgpath.replace(/^\/modules.+img=/,"");
					imgSrc = imgSrc.replace(/&width=.+/,"");
					$(this).append('<p id="topTumblrImageTip" style="position:absolute;background:#fff;border:solid 2px #999;display:none;"><img src="'+imgSrc+'" /></p>');
				})
				.mousemove(function(e){
					var $tipbox = $(this).children("p");
					var $imgItem = $tipbox.children("img");
					if($tipbox.css("display") == "none") $tipbox.show("fast");
					if(window.innerHeight) {
						var windowHeight = window.innerHeight;
					}else {
						var windowHeight = document.body.clientHeight;
					}
					if($(window).width()/2 > e.pageX){
						$tipbox
							.css("left",e.pageX+5-0+"px");
					}else {
						$tipbox
							.css("left",e.pageX-$imgItem.width()-5+"px");
					}
					if(windowHeight/2 > e.clientY){
						$tipbox
							.css("top",e.pageY+5+"px");
					}else {
						$tipbox
							.css("top",e.pageY-$imgItem.height()-5+"px");
					}
					
				})
				.mouseout(function(){
					$("#topTumblrImageTip").remove();
				});
		});
	}
	//event handling
	_Globalnav.controll();
	$("#topTumblr").ready(function() {
		setTimeout(function(){
			topTumblr()
		},1500);
	});
});