var m_on = false;
$(document).ready(function() {
  // load the nav
	var current = $(".current").children("a");
	var menu_on = false;
	var menu_item = false;
	var current_on = false;
	var current_menu = false;
	if ($("#nav")) {
		$("#nav li").mouseover(	function(e) {
			if(menu_on) clearTimeout(menu_on);
			if(menu_item) clearTimeout(menu_item);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);

			//HIDE CURRENT MENU
			current.children("ul").filter(":visible").slideUp();
			current.removeClass("over");
			$(this).siblings("li").removeClass("current");
		});
		$("#nav li").mouseout(	function(e) {
			//alert("1");
			if(menu_on) clearTimeout(menu_on);
			if(menu_item) clearTimeout(menu_item);


		});
		$("#nav>ul>li").mouseover( function(e) {
			if(menu_on) clearTimeout(menu_on);
			if(menu_item) clearTimeout(menu_item);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);
			//HIDE OPEN MENUS
			$(this).siblings("li").children("ul").filter(":visible").slideUp();
			$(this).siblings("li").children("a").removeClass("over");

			//ADD ROLLOVER
			$(this).children("a").addClass("over");

			//SHOW SUBMENU
			$(this).children("ul").slideDown();
		});
		$("#nav>ul>li").mouseout(function(e) {

				//alert("2");
			//CLEAR ROLLOVER
			//menu_item = $("#nav>ul>li").children("a").removeClass("over");
			menu_item = setTimeout('$("#nav>ul>li").children("a").removeClass("over")', 500);
			//CLEAR SUBMENU
			
			//menu_on = $("#nav>ul>li").children("ul").filter(":visible").hide();
			menu_on = setTimeout('$("#nav>ul>li").children("ul").filter(":visible").slideUp()', 500);
	  	});
		$("#nav>ul>li li").mouseover(function(e){

			if(menu_item) clearTimeout(menu_item);
			if(menu_on) clearTimeout(menu_on);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);
			//SHOW SUBMENU
			$("ul", $(this)).filter(":hidden").slideDown();
		});
		$("#nav>ul>li li").mouseout( function(e){
			if(menu_item) clearTimeout(menu_item);
			if(menu_on) clearTimeout(menu_on);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);
			$("ul", $(this)).filter(":visible").slideUp();
		});
	}
	
	// operations block map slider
	if($('#operation-block-map').length > 0 && $('#map-content').length > 0){
			contentHeight = $('#main-content').height();
			if(contentHeight > 336){
      	$( '#map-content' ).height(contentHeight);
			}
			$( '#operation-block-map' ).scrollFollow();
	}
	

	// load the search onfocus/blur events
	if (search_el = $("#search")) {
		search_el.focus(function(e) {
			$(this).val("")
		});
		search_el.blur(function(e) {
			if ($(this).val() == "") $(this).val("search")
		});
		search_el.val("search");
	}
	
	if($('#gallery').length > 0){
      $('#gallery a').lightBox();
	}
	
	if($('.bodyimage a[rel=lightbox]').length > 0) {
		$('.bodyimage a[rel=lightbox]').lightBox();
	}
	
	if(page_image = $('#page-image')) {
		if(($('#page-image').children('img').length) > 1) {
		pageImage = {
			i:0,
			 showImage: function(i) {
				var banrs = $('#page-image').children('img');
				var num_banrs = banrs.length -1;
				$('#page-image').children('img').filter(':visible').fadeOut(600);
				if (pageImage.i==num_banrs) {pageImage.i=0;}else{pageImage.i=pageImage.i+1;}
				$(banrs[pageImage.i]).fadeIn(1000);
				setTimeout(pageImage.showImage,4000);
			}
		}
		pageImage.showImage();
	}
	}
	if($(".operations").length > 0) {
		// if the function argument is given to overlay, it is assumed to be the onBeforeLoad event listener
		$(function() { 
		    // if the function argument is given to overlay, 
		    // it is assumed to be the onBeforeLoad event listener 
				$("area[rel]").overlay({
				expose: '#333', 
				opacity: 0.7, 
				closeSpeed: 400,
				onBeforeLoad: function() { 
				// grab wrapper element inside content 
				var wrap = this.getContent().find("div.wrap");
				wrap.load(this.getTrigger().attr("href"));
				}
				});
		});
		
		$(function() { 
		    // if the function argument is given to overlay, 
		    // it is assumed to be the onBeforeLoad event listener 
				$(".financials a[rel]").overlay({
				expose: '#333', 
				opacity: 0.7, 
				closeSpeed: 400,
				onBeforeLoad: function() { 
				// grab wrapper element inside content 
				var wrap = this.getContent().find("div.wrap");
				wrap.load(this.getTrigger().attr("href"));
				}
				});
		});
	}
	


	
});

