$(function() {

    $('.page-title').css({'text-transform': 'uppercase'});

    /* Replace Search box default value when focused */
	$('#search_q').focus(function(){
        var rel = $(this).val();
        if(rel == "Search this site"){ $('#search_q').val(''); }
        return false;
	});
	$('#search_q').blur(function(){
        var rel = $(this).val();
        if(rel == ""){ $('#search_q').val('Search this site'); }
        return false;
	});
	$('#searchform').submit(function(){
		var search_q = $('#search_q').val();
		if(search_q == 'Search this site' || search_q == '' || search_q == 'undefined'){
			alert('Please enter keyword');
			$('#search_q').focus();
			return false;
		}
	});
	
	$('div.wysiwyg').each(function(){
		$(this).find('img').each(function(){
			var src = $(this).attr('src').replace('../','');
			$(this).attr('src', src);
		});
	});

    /* Language drop down */
	$('.arrow-down').click(function(){
        $('#language-drop').slideToggle(90);
        return false;
	});

    /* Language drop down hover */
    $("#language-drop li").hover(function(){
        $(this).css({background: "#f5ece5"});
    },function(){
        $(this).css({background: "#FFFFFF"});
    });

    /* Drop down menu hover */
    $("#nav li ul li a").hover(function(){
        $(this).parent().css({background: "#ffe7ec"});
    },function(){
        $(this).parent().css({background: "#dbdade"});
    });

    /* Search button hover */
    $("#search-area .button").hover(function(){
        $(this).attr('src', 'images/search_hover.gif');
    },function(){
        $(this).attr('src', 'images/search.gif');
    });
	
	
	/* Toggle section on Info Center page */
	$(".info-center-section-head a").click(function(){
		$(this).toggleClass("closed").parent().next().toggle();
		return false;
	});
	/* Toggle section on Info Center page */
	$("a.excerpt").click(function(){
		$(this).next().toggle();
		return false;
	});

    /* Hide language when hover out */
    var hideDropDown = {
         sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
         interval: 200, // number = milliseconds for onMouseOver polling interval
         over: hoverLang, // function = onMouseOver callback (REQUIRED)
         timeout: 500, // number = milliseconds delay before onMouseOut
         out: hoveroutLang // function = onMouseOut callback (REQUIRED)
    };
    $("#language").hoverIntent( hideDropDown );          

    /* Bottom navigation hover */
    var footerHover = {
         sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)
         interval: 100, // number = milliseconds for onMouseOver polling interval
         over: hoverFooter, // function = onMouseOver callback (REQUIRED)
         timeout: 300, // number = milliseconds delay before onMouseOut
         out: hoveroutFooter // function = onMouseOut callback (REQUIRED)
    };
    $("#bottom-nav ul li").hoverIntent( footerHover );    

	mainmenu(); // main menu function
});

function hoverLang(){ }
function hoveroutLang(){ $('#language-drop').fadeOut(120);}

function hoverFooter(){
    if(!($(this).hasClass('hovered'))){
        $(this).find('.hovered').fadeIn();
        $(this).addClass('hovered');
    }
}
function hoveroutFooter(){
    if($(this).hasClass('hovered')){
        $(this).removeClass('hovered');
        $(this).find('.hovered').fadeOut();
    }
}

function hoverMenu(){
	$(this).addClass("hover");
	var aclass = $(this).find("a").attr("class");
	if(aclass == "naval_systems"){
    	$(this).find('ul').css({width: 360}); // dropdown menu width
	}
	else if(aclass == "news"){
    	$(this).find('ul').css({width: 150}); // dropdown menu width
	}
	else{
    	$(this).find('ul').css({width: 220}); // dropdown menu width
	}
    $(this).find('ul').css({opacity: 0.90}); // show dropdown menu
    $(this).find('ul').slideDown(220); // slide down drop down menu
}
function hoveroutMenu(){
	$(this).removeClass("hover");
	$(this).find('ul').slideUp(200); // hide dropdown menu
}

function mainmenu(){
    $("#nav ul").css({display: "none"}); // Opera Fix

    var menuHover = {
         sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)
         interval: 100, // number = milliseconds for onMouseOver polling interval
         over: hoverMenu, // function = onMouseOver callback (REQUIRED)
         timeout: 200, // number = milliseconds delay before onMouseOut
         out: hoveroutMenu // function = onMouseOut callback (REQUIRED)
    };
    $("#nav > li").hoverIntent( menuHover );
}
