$( document ).ready( function() {
	
	animateMenu();
	resizeMenu();
	highlightMenu();
	
	$( 'a.fancybox' ).fancybox();
	
	var t = $( '.check-table' );
	
	t.zebra();
	$( 'tr:first', t ).addClass( 'first' );
	$( 'tr', t ).each( function() { $( 'td:last', $( this ) ).addClass( 'last' ); } );

	var total = 0;
	$( '#homepage_right li' ).each( function() {
		total += $( this ).outerHeight();
	} );
	
	var diff = 290 - total;
	
	$( '#homepage_rightlinks' ).css( 'margin-top', ( diff  ) + 'px' );
	
	$( '.custom_fade' ).efade();
	
} );

function animateMenu()
{
	$('.main-nav > li').hover(
	   function() {
		  $(this).addClass("active");
		  $(this).find('ul').stop(false, true).show();
	   },
	   function() {
		  $(this).removeClass("active");
		  $(this).find('ul').stop(false, true).hide();
	   }
	);
}

function resizeMenu()
{
	var total = 0;
	var items = $( '.main-nav > li > a' );
	items.each( function() {
		total += $( this ).outerWidth();
	} );
	
	if( total > 920 ) {
		var diff = total - 920;
		var item_diff = diff / 5;
		
		items.each( function() {
			if( $( this ).parent().hasClass( 'last' ) ) {
				$( this ).css( "padding-right", ( parseInt( $( this ).css( "padding-right" ) ) - item_diff ) + 'px' ); 
			} else {
				$( this ).css( "padding-right", ( parseInt( $( this ).css( "padding-right" ) ) - item_diff ) + 'px' ); 
			}
		} );
		
		total = 0;
		items.each( function() {
			total += $( this ).outerWidth();
		} );
		
		if( total > 920 ) {
			var d = total - 920;
			$( '.main-nav > li.last > a' ).css( "padding-right", ( parseInt( $( '.main-nav > li.last > a' ).css( "padding-right" ) ) - d ) + 'px' );
		} else {
			var d = 920 - total;
			$( '.main-nav > li.last > a' ).css( "padding-right", ( parseInt( $( '.main-nav > li.last > a' ).css( "padding-right" ) ) + d ) + 'px' );
		}
	}
	
}

function highlightMenu()
{
	//get url
	var uriparts = window.location.pathname;
	
	var m = $( '.main-nav' );
	var f = $( '#footer_menu' );
	$( 'a', m ).each( function() {
		var t = $( this );
		
		var au = t.attr( 'href' );
		
		if( au == uriparts || au == uriparts.substr(1) )
		{
			var p = t.parents( 'li' );
			p.each( function() {
				var p_t = $( this );
				$( ' > a', p_t ).addClass( 'current' );
				p_t.addClass( 'current' ); 
			} );
			//t.parents( 'li' ).addClass( 'current' );
			//t.parents( 'li > a' ).addClass( 'current' );
			return false;
		}
	} );
	/*
	var d = window.location.protocol + '//' + window.location.host;
	var uriparts = window.location.pathname;
	var nav = $( '.main-nav' );
	$( 'a', nav ).each( function() {
		var h = $( this ).attr( 'href' );
		var h = h.replace( d, "" );
		if( uriparts.match( h ) )
		{
			$( this ).addClass( 'current' ).parents( "li" ).addClass( 'current' );
		}
	} );*/
}

$.fn.zebra = function()
{
	this.each( function()
	{
		$( 'tr:odd', this ).addClass( 'odd' );
	});
}


