﻿$j = jQuery;

$j(document).ready(
	function(){
	
		var $ = $j;
	
		$(".navigation-bar > ul > li").mouseover(
		
			function(){
				close_current_child_menu();
			}
		
		);

		$(".navigation-bar > ul > li > ul").parent().mouseover(
			
			function(){
				open_child_menu(this);
			}
		);
		
		$(".navigation-bar > ul > li > ul").parent().children("a:first").attr("href","#");
		
		function findPosX(obj)
		{
			var curleft = 0;
			if(obj.offsetParent)
			while(1) 
			{
			  curleft += obj.offsetLeft;
			  if(!obj.offsetParent)
			    break;
			  obj = obj.offsetParent;
			}
			else if(obj.x)
			curleft += obj.x;
			return curleft;
		}
		
		function findPosY(obj)
		{
			var curtop = 0;
			if(obj.offsetParent)
			    while(1)
			    {
			      curtop += obj.offsetTop;
			      if(!obj.offsetParent)
			        break;
			      obj = obj.offsetParent;
			    }
			else if(obj.y)
			    curtop += obj.y;
			return curtop;
		}
		
		function close_current_child_menu(){
			$(".child-menu").remove();
			$(".navigation-bar .hover").removeClass("hover");
		}
		
		function open_child_menu(item){
			var $menu = $(item);
			$menu.addClass("hover");
			
			var menuX = findPosX(item);
			
			var firstMenuItem = $(".navigation-bar > ul > li:first");
			
			var x = findPosX(firstMenuItem.get(0)) + 10;
			var y = findPosY(firstMenuItem.get(0));
			var height = firstMenuItem.height();
			y += height;
							
			var childMenu = $j(item).children("ul").clone();
			$("body").append(childMenu);
			childMenu.addClass("child-menu");
			childMenu.css("top",y);
			childMenu.css("left",menuX);
			childMenu.mouseleave(function(){
				close_current_child_menu();
			});
			
			// Highlight Child
			
			var childrenLinks = childMenu.find("a");
			
			var currentPage = window.location.toString();
			
			for(var i = 0; i < childrenLinks.length; i++ ){
				if(childrenLinks.get(i).href == currentPage){
					$(childrenLinks.get(i)).addClass("current");
					break;
				}
			}
		}
		
		//CLOSING POPUP
		//Click the x event!
		$j("#popupContactClose").click(function(){
			disablePopup();
		});
		
		//Click out event!
		$j("#backgroundPopup").click(function(){
			disablePopup();
		});
		
		//Press Escape event!
		$j(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});
	}
);

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

function openBlogPopup()
{
	loadPopup();
	centerPopup();
}

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus == 0){
		
		$j("#backgroundPopup").css({
			"opacity": "0.7"
		});
		
		$j("#backgroundPopup").fadeIn("slow");
		$j("#popupContact").fadeIn("slow");
		
		popupStatus = 1;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	var popupHeight = $j("#popupContact").height();
	var popupWidth = $j("#popupContact").width();
	
	//centering
	$j("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2 - popupHeight/2,
		"left": windowWidth/2 - popupWidth/2
	});
	
	//only need force for IE6
	$j("#backgroundPopup").css({
		"height": windowHeight
	});
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus == 1){
		$j("#backgroundPopup").fadeOut("slow");
		$j("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

