$(function() {

	var mouseX;
	var mouseY;
	var oneOut = 0;
	var openSubM;
	
	/**
	===================================================================
		NavMenu dropdowns
	===================================================================
	*/
	
	$(document).mousemove(function(e){
		mouseX = e.pageX;
		mouseY = e.pageY;
	}); 
	
	$('#navMenu a').bind({
		"mouseover" : function (e) {
			$this = $(this);
			$('.subMenu, .submenu').each(function() {
				if ($(this).attr('rel') != $this.attr('id')) {
					$(this).slideUp("fast");	
				}					
			});
			
			// if ($(this).prop('id') != '') { console.log('id good'); } else { console.log('id bad'); }
			// var relselector = '[rel='+$(this).attr("id")+']';
			// if ($(relselector).length > 0) { console.log('rel good'); } else { console.log('rel bad'); }
			
			
			
			if ($(this).prop('id') != '' && $('[rel='+$(this).attr("id")+']').length > 0) {
				
				var $subMenu = $('[rel='+$(this).attr("id")+']');
				
				//console.log('has sub');
				
				
				
				var t_offset = $(this).offset();
				t_offset.bottom = t_offset.top + $(this).outerHeight();
				jQuery.each(jQuery.browser, function(i, val) {
					if(i=="msie" && parseFloat(jQuery.browser.version) == 7){
						//t_offset.bottom+=$("html").scrollTop();
						//console.log('7');
					}
				});
				
				//alert('.');
				
				var subLeft = t_offset.left + ($(this).width() - 300)/2;
				//alert('[rel='+$(this).attr("id")+']');
				
				//console.log(t_offset.bottom +'...'+$(this).offset().top+'....'+$(this).height()+'...'+$("html").scrollTop());
				$subMenu.css({
					/*
					"left" : $(this).offset().left,
					"top" : t_offset.bottom,
					//"background-color" : "#767676",
					//"padding" : 5,
					//"padding-bottom" : 2,
					"text-align" : "center",
					//"line-height" : "26px",
					//"border" : "1px solid black",
					"font-size" : "8px !important"
					//"width" : 300
					*/
					"left" : t_offset.left,
					"top" : t_offset.bottom,
					//"background-color" : "#767676",
					//"padding" : 5,
					//"padding-bottom" : 2,
					"text-align" : "left",
					'z-index' : 100,
					//"line-height" : "26px",
					//"border" : "1px solid black",
					"font-size" : "12px"
					//"width" : 100
				});
				
				$subMenu.find('a').css({
					/*
					"color" : "#ffffff",
					"font-family": "bebas",
					"text-decoration" : "none",
					//"width" : "100%",
					"display" : "inline-block",
					"margin-top" : "2px",
					"padding" : "3px 5px",	
					"padding-bottom" : "7px",
					"font-weight" : "normal",
					"word-spacing" : 3
					//"line-height" : "18px"
					*/
					//"color" : "#ffffff",
					
				});
				
				jQuery.each(jQuery.browser, function(i, val) {
					
					if(i=="msie" && parseFloat(jQuery.browser.version) < 9){
						$subMenu.find('a').css({
							"background-color" : "rgb(118,118,118)"
						});
					} else {
						$subMenu.find('a').css({
							"background-color" : "rgba(118,118,118, 0.8)",
							"filter":"alpha(opacity=80)"
						});
					}
				});
				
				$subMenu.slideDown('fast');	
				// $subMenu.css('display','inline-block');
				// console.log($subMenu);
				oneOut = 1;
				openSubM = $subMenu;
			} else {
				//console.log('no sub');
			}
			
		},
		"mouseout" : function() {
			if ($(this).prop('id') != '' && $('[rel='+$(this).attr("id")+']').length > 0) {
				var $subMenu = $('[rel='+$(this).attr("id")+']');
				var t_offset = $(this).offset();
				t_offset.right = t_offset.left + $(this).outerWidth();
				t_offset.bottom = t_offset.top + $(this).outerHeight();				
				
				setTimeout(function() {
					if ((mouseX < t_offset.left || mouseX > t_offset.right) || mouseY < t_offset.top) {						
						$subMenu.slideUp("fast");
					}
				}, 1000);			
			}
		}

	});		
	
	$(".subMenu").bind({
		"mouseleave" : function () {
			//console.log($(this).html());
			var $this = $(this);
			var t_offset = $(this).offset();
			t_offset.right = t_offset.left + $(this).outerWidth();
			t_offset.bottom = t_offset.top + $(this).outerHeight();
			
			setTimeout(function() {
				if ((mouseX < t_offset.left || mouseX > t_offset.right) || mouseY > t_offset.bottom) {						
					$this.slideUp('fast');						
				}
			}, 1000);						
			
		},
		"mouseenter" : function () {

		}
	});
	
	$('.subMenu a').mouseover(function() {
		// $(this).css({
			// "color" : "#99b279",
			// "background-color" : "#000000"
		// });
	});
	/*
	$('.subMenu a').mouseout(function() {
		$this = $(this);		
		jQuery.each(jQuery.browser, function(i, val) {
			if(i=="msie" && parseFloat(jQuery.browser.version) < 9){
				$this.css({
					"color" : "#ffffff",
					"background-color" : "rgb(118,118,118)"					
				});					
			} else {
				$this.css({
					"color" : "#ffffff",					
					"background-color" : "rgba(118,118,118, 0.8)"
				});
				
			}
		});
	});		
	
	*/
	
	$(window).resize(function() {
		var sel = '[id='+(openSubM.attr("rel"))+']';
		var $src = $(sel).eq(0);
		var src = $src.offset();
		src.right = src.left + $src.width();
		src.bottom = src.top + $src.height();	
		$(".subMenu:visible, .submenu:visible").css({
			'top' :	src.bottom,
			'left' : src.left
		});
	});

});	
	

