var aboutTimer = 0;
var connectTimer = 0;
var aboutOpen = 0;
var connectOpen = 0;

(function($) {
	$.extend($.fx.step,{
		backgroundPosition: function(fx) {
			if (fx.state === 0 && typeof fx.end == 'string') {
				var start = $.curCSS(fx.elem,'backgroundPosition');
				start = toArray(start);
				fx.start = [start[0],start[2]];
				var end = toArray(fx.end);
				fx.end = [end[0],end[2]];
				fx.unit = [end[1],end[3]];
			}
			var nowPosX = [];
			nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
			nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
			fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

		   function toArray(strg){
			   strg = strg.replace(/left|top/g,'0px');
			   strg = strg.replace(/right|bottom/g,'100%');
			   strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
			   var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
			   return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
		   }
		}
	});
})(jQuery);


$(function(){
	$('.nav').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	});
});

$(window).load(function() {
	initNav();
});

var x1 = 0;
function initNav() {
	$(function() {
		$(".mainNavBtn").each(function(i){
			var w1 = $(this).width();
			x1 = (w1-54)/2;
			$(this).css("background-position",""+x1+"px -50px");
		});
		$(".mainNavBtn").hover(function(){

			if ($(this).attr('id')=='about_btn') {
				if (aboutOpen != 1) {
					aboutOpen = 1;
					openDropdown($("#about_dropdown"));
				}
			} else {
				if (aboutOpen == 1) {
					aboutTimer = setTimeout(closeAbout, 500);
				}
			}

			if ($(this).attr('id')=='connect_btn') {
				if (connectOpen != 1) {
					connectOpen = 1;
					openDropdown($("#connect_dropdown"));
				}
			} else {
				if (connectOpen == 1) {
					connectTimer = setTimeout(closeConnect, 500);
				}
			}
			
			var w1 = $(this).width();
			x1 = (w1-54)/2;
			$(this).stop().animate({
				backgroundPosition: "("+x1+"px 15px)"
				}, {duration: 300});
			},
			// ON MOUSE OUT
			function(){
				$(this).stop().animate({
					backgroundPosition: "("+x1+"px -50px)"
				}, {duration: 300});
		});
	});
	
	$("#about_dropdown").mouseover(function() {
		clearTimeout(aboutTimer);
	});
	$("#about_dropdown").mouseout(function() {
		aboutTimer = setTimeout(closeAbout, 500);
	});

	$("#connect_dropdown").mouseover(function() {
		clearTimeout(connectTimer);
	});
	$("#connect_dropdown").mouseout(function() {
		connectTimer = setTimeout(closeConnect, 500);
	});
}

function openDropdown($menu) {
	$menu.show("blind", { direction: "vertical" }, 500);
}
function closeAbout() {
	if (aboutOpen == 1) {
		aboutOpen = 0;
		$("#about_dropdown").hide("blind", { direction: "vertical" }, 500);
	}
}
function closeConnect() {
	if (connectOpen == 1) {
		connectOpen = 0;
		$("#connect_dropdown").hide("blind", { direction: "vertical" }, 500);
	}
}

//  pass the url  and  optionally a width, height, and window name
function spawnWindow(url,name,w,h){
	if(w == undefined) w = 320;
	if(h == undefined) h = 240;
	if(name == undefined) name = "popWindow";
	var newWindow = window.open(url,name,"toolbar=0,scrollbars=1,resizable=1,width="+w+",height="+h+"\"");
	newWindow.focus();				
 }

function checkExplorer()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    return true;
  } else {
	return false;
  }
}

function getIEversion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

