$(document).ready(function(){
	//Creates the sliding box
	$('#navi').after('<div id="nav_hover"><img src="/en/templates/ascendian/images/bgHoverSides.png" class="first-child" /><img src="/en/templates/ascendian/images/bgHoverSides.png" class="last-child" /></div>');
	//wraps the dropdowns in a div for smoother sliding
	$('#navi ul ul').wrap('<div class="drop"></div>');
	
	var oHover = $('#nav_hover');
	var aWidth = 0;
	var aLeft = 0;
	var aOpacity = 0;

	//Resets box default position and mountian offset on resize
	function setOnSize(){
		var oActive = $('#navi .menu > li.active');
		var bActive = oActive.length == 0 || oActive.css('display') == 'none';
		var oMountian = $('.mountain div');
	
		if(bActive){
			aWidth = 0;
			aLeft = $('#navi').offset().left;
			aOpacity = 0;
			oHover.css('opacity',aOpacity);
		} else {
			aWidth = oActive.width();
			aLeft = oActive.offset().left + 2;
			aOpacity = 1;
		}

		oHover.css('left', aLeft).css('width', aWidth).show();
		oMountian.css('margin-left', ($('body').width() - 1500) / 2);
	}
	$(window).resize( setOnSize );
	setOnSize();

	//Slides the box and drops the menu
	$('#navi .menu > li').mouseenter(function(){
		var oItem = $(this);
		
		if(oItem.width() > oItem.children('.drop').width()){
			oItem.children('.drop').css('width', oItem.width());
		}
		
		if(oItem.offset().left == oHover.offset().left){
			oItem.children('.drop').slideDown(150);
		} else {
			oHover.stop().animate({ 
				left: oItem.offset().left + 2,
				width: oItem.width(),
				opacity: 1
			}, 250, 'swing', function(){ oItem.children('.drop').slideDown(150); });
		}
	}).mouseleave(function(){
		var oItem = $(this);
		oItem.children('.drop').stop().hide().css('height','');

		oHover.stop().animate({ 
			left: aLeft,
			width: aWidth,
			opacity: aOpacity
		}, 250, 'swing');
	});

	if( $('#k2Container').length > 0 ){
		//Fixes K2 breadcruimbs and titles
		var sCat =  $('#navi .menu > li.active > a > span').html();
		$('#title h2').html( sCat );
		$('.sub-nav h3').html($('#title h2').html());
	} else {
		// Fixes some title issues
		var sCat = $.trim($('span.article-section').html());
		if(sCat!=''){
			$('#title h2').html(sCat);
			// $('div.article-tools').remove();
			$('span.article-section').remove();
		}
		$('.sub-nav h3').html($('#title h2').html());
	}
	if($('#title h2').length > 0){
		$('#title h2').addClass($('#title h2').html().replace(/[' ]/g,"-").toLowerCase());
	}
	$('#frontpage-seo').remove();
	
	$('#userForm').submit(function(){
		$('#submit').parent().html('Please Wait.').addClass('please-wait');
	});
});