// ________________________________________________________
// Display menu
function __displayMenu(){
	$("ul#menu li.btn").unbind("click");
	$("ul#menu ul.sm").animate({ marginLeft: 0, opacity: 'show' }, 500, "easeOutQuad", function(){
		//$("ul#menu li.btn").hover(null, __hideMenu);
	});
	$("object").hide();
	$("select").hide();
	$("#so").hide();
}

// ________________________________________________________
// Hide menu
function __hideMenu(){
	$("ul#menu li.btn").unbind("click");
	$("ul#menu ul.sm").animate({ marginLeft: -185, opacity: 'hide' }, 500, "easeInQuad", function(){
		$("object").show();
		$("select").show();
		$("#so").show();
	});
}

// ________________________________________________________
// Get page size
function __getPageSize(){
	var xScroll, yScroll, pageHeight, pageWidth;
	if(window.innerHeight && window.scrollMaxY){	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}else if(document.body.scrollHeight > document.body.offsetHeight){
		// All but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}else{
		// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if(self.innerHeight){
		// All except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		}else{
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){
		// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}else if(document.body){
		// Other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// For small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	}else{ 
		pageHeight = yScroll;
	}
	// For small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	}else{
		pageWidth = windowWidth;
	}
	var arrayPageSize = [pageWidth,pageHeight,windowWidth,windowHeight];
	return arrayPageSize;
}

// ________________________________________________________
// Get page scroll
function __getPageScroll(){
	var xScroll, yScroll;
	if(self.pageYOffset){
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	}else if(document.documentElement && document.documentElement.scrollTop){
		// Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	}else if(document.body){
		// All other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	var arrayPageScroll = [xScroll,yScroll];
	return arrayPageScroll;
}

// ________________________________________________________
// Init
function __init(){
	$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
	
	// Menu bar
	$("ul#menu ul.sm").hide();
	$("ul#menu li.btn a").click(function(){
		__hideMenu();
		//return false;
	});
	$("ul#menu a.menu").click(function(){
		__displayMenu();
		return false;
	});
	$("ul#menu ul.sm").click(function(){
		__hideMenu();
	});
	$("ul#menu a.menu").mousedown(function(){
		return false;
	});
	$("div#container").append('<div id="patch"></div>');
	
	// Memorian
	if($.browser.msie6){
		var arr = __getPageSize();
		$("p#memorian").css("top", arr[1]);
	}
	
	// Text highlight
	$("a.zone").hover(function(e){
		var obj = window.event ? window.event.srcElement : e ? e.target : null;
		if(obj.nodeName != "A"){ obj = obj.parentNode; }
		$("span." + obj.getAttribute("id")).addClass("hover");
	},function(e){
		var obj = window.event ? window.event.srcElement : e ? e.target : null;
		if(obj.nodeName != "A"){ obj = obj.parentNode; }
		$("span." + obj.getAttribute("id")).removeClass("hover");
	});
	
	// Comments
	$(".divComments").hide();
	$(".btnComments a").click(function(e){
		if(this.onclick){
			return false;
		}
		if($(".divComments").css("display") == "none"){
			$(".divComments").slideDown("fast");
			if($.browser.msie){
				$("p#memoriam").css("visibility", "hidden");
			}
		}else{
			$(".divComments").slideUp("fast");
		}
		return false;
	});
	$(".btnSend a").click(function(e){
		var arr = {};
		var elm = document.forms[0].elements;
		for(var i = 0; i < elm.length; i++){
			if($(elm[i]).attr("type") == "checkbox" || $(elm[i]).attr("type") == "radio"){
				if($(elm[i]).attr("checked") === true){
					arr[elm[i].name] = $(elm[i]).val();
				}
			}else{
				arr[elm[i].name] = $(elm[i]).val();
			}
		}
		
		$.post($("form").attr("action"), arr,
			function(data){
				$(".msg").remove();
				$("form").prepend(data);
				$(".msg").hide();
				$(".msg").slideDown("slow");
			}
		);
		return false;
	});
	
	// Spam token
	$.get("../inc/token.php",function(txt){
		$("#formComments").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});
	
	if($(".index").length === 0 && $(".index2").length === 0 && window.location.href.indexOf("/blog/") < 0){
		//$("#supportingText").append("<p>" + $("#ariane").html() + "</p>");
	}
	
	// External link
	/*$("a[rel*='external']").click(function(){
		this.target = "_blank";
	});*/
}

$(document).ready(function(){
	__init();
});
