/*
	init.js
	require jQuery
*/
//current directory
$(function(){
	var path = location.pathname.split('/')[1];
	if(path.match('index') || path == '') {
		path = '/';
	}else {
		path = '/'+path+'/';
	}
	$('#global-nav').find('.btn').parent().each(function(){
		var reg = new RegExp('^'+$(this).attr('href')+'$');
		//console.log(path.match(reg));
		if(path.match(reg)) {
			//console.log("yes");
			var orgImg = $(this).find('img');
			orgImg.attr('src',orgImg.attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
		}
	})
	
})

//rollover
$(function(){
	var postfix = '_on';
	$("img.btn, input.btn").not('[src*="'+ postfix +'."]').hover(function(){
		if(!$(this).parent().hasClass('selected')) {
			$(this).attr('src',$(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'))
		}
	},function(){
		if(!$(this).parent().hasClass('selected')) {
			$(this).attr('src',$(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, '$1$2'));
		}
	}).each(function(){
		$('<img>').attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'))
	});
})

//external link
$(function() {
	$('.extlink').click(function(){
		window.open(this.href,'_blank')
		window.moveTo(0,0);
		window.resizeTo(screen.availWidth,screen.availHeight);
		return false;
	})
});

//global nav pulldown
$(function() { 
	function addMega(){
		//$(this).addClass("hovering");
		//$(this).find(".nav-lv2").stop().fadeTo('fast', 1).show();
		$(this).find(".nav-lv2").show();
	};
	function removeMega(){
		//$(this).removeClass("hovering");
		/*$(this).find(".nav-lv2").stop().fadeTo('fast', 0, function() {
			$(this).hide(); 
		});*/
		$(this).find(".nav-lv2").hide(); 
	};
	$('.nav-lv1 li').hoverIntent({
		interval: 0,
		sensitivity: 4,
		over: addMega,
		timeout: 100,
		out: removeMega
	});
});

//search
$(function() { 
	$('#q').focus(function(){
		if($(this).val() == $(this).prop('defaultValue')) {
			$(this).val('');
		}
	}).blur(function(){
		if(jQuery.trim($(this).val()) == "") {
			$(this).val($(this).prop('defaultValue'));
		}
	});
});


