// JavaScript Document

var menuDefaultColor;
var menuGlow_prev;
var menuGlow_aniSpeed;
var returnGlow;
var returnGlowPos;
var initReturnGlow = true;
var defaultGlowPos = '-200px';
var arrPages = new Array();//["home.cfm","SFM_landscape_services.cfm","SFM_janitorial_services.cfm","SFM_Security_services.cfm"];

(function($) {
	jQuery.fn.backgroundPosition = function() {
		var p = $(this).css('background-position');
		if(typeof(p) === 'undefined') return $(this).css('background-position-x') + ' ' + $(this).css('background-position-y');
			else return p;
	};
})(jQuery);

function moveMenuGlow(obj,returnGlowBit) {
	
	clearTimeout(returnGlow);
	
	if(returnGlowBit) {
		$('#mainMenu2 .glow').animate({backgroundPosition:returnGlowPos + 'px bottom'},600);
		//$('#mainMenu2 .glow').css({backgroundPosition:returnGlowPos + 'px bottom'});
		//$('#mainMenu2 .glow').css({visibility:'visible'});
		return false;
	}
	
	$('#mainMenu2 .glow').stop();
	//$(menuGlow_prev).not('.on').animate({color:'#abbccf'},500);
	
	//var hashPos = location.hash;
	
	/*
	pageName = location.href.split('/')[3];
	hashPos = $.inArray(pageName,arrPages);
	*/
	
	//hashPos = hashPos.split('#')[1];
	//console.log(hashPos);
	
	if(obj == undefined && hashPos != undefined) {
		obj = $('#mainMenu2 a:eq(' + hashPos + ')');
		obj.addClass('on');
	} else if(location.href.indexOf('home') >= 1 && hashPos == undefined) {
		//alert('home');
		obj = $('#mainMenu2 a:eq(0)');
		obj.addClass('on');
	} else if(location.href.indexOf('comingsoon') >= 1 && hashPos == undefined) {
		//alert('coming soon');
		obj = $('#mainMenu2 a:eq(11)');
		obj.addClass('on');
		//menuGlow_aniSpeed = 100;
	}
	
	$(obj).stop();
	
	$(obj).animate({color:'white'},200);
	var bgWidth = $(obj).width();
	var objPos = $(obj).offset();
	
	if(objPos == null) {
		objPos = defaultGlowPos;
	} else {
		objPos = objPos.left;
	}
	
	var bgLeft = ((objPos + (bgWidth/2)) - (171/2)) + 5;
	
	/// speed check ///
	//var glowPos = $('#mainMenu2 .glow').css('background-position').split('px')[0];

	/*
	var glowPos = $('#mainMenu2 .glow').backgroundPosition().split('px')[0];
	var greater = Math.max(glowPos,bgLeft);
	var smaller = Math.min(glowPos,bgLeft);
	var diff = Math.abs(greater - smaller);
	menuGlow_aniSpeed = diff;
	*/
	
	menuGlow_aniSpeed = 250;
	menuGlow_prev = obj;
	
	if(hashPos != undefined && initReturnGlow == true) {
		returnGlowPos = bgLeft;
		initReturnGlow = false;
		$('#mainMenu2 .glow').css({backgroundPosition:returnGlowPos + 'px bottom'});
		return false;
	}
	
	$('#mainMenu2 .glow').animate({backgroundPosition:bgLeft + 'px bottom'},menuGlow_aniSpeed);

}

$(document).ready(function() {
	
	//menuDefaultColor = '#abbccf';
	menuDefaultColor = $('#mainMenu2 a').not('.on').css('color');
	
	$('#mainMenu2 a').each(function(){
		var thisLink = $(this).attr('href').split('/')[1];
		arrPages.push(thisLink);
		pageName = location.href.split("/")[3].split("?")[0];
		hashPos = $.inArray(pageName,arrPages);
	});
	
	moveMenuGlow();
	
	$('#mainMenu2 a').mouseover(function(){
		moveMenuGlow(this);
	})
	$('#mainMenu2 a').mouseout(function(){
		returnGlow = setTimeout(moveMenuGlow,300,this,1);
		$(this).stop();
		$(this).not('.on').animate({color:menuDefaultColor},500); //'#abbccf'
	})
	
	$('#mainMenu2 a').click(function(){
		var thisLink = $(this).attr('href');
		var thisIndex = $(this).prevAll().length;
		
		//console.log(thisIndex);
		location.href = thisLink;// + '#' + thisIndex;
		return false;
	})
});

$(window).resize(function() {
	moveMenuGlow($('#mainMenu2 a:eq(' + returnGlow + ')'));
});
