$(document).ready( function() {

	// Menu
	$("#menu_items").css( "top", $("#menu_bar").offset().top );
	initializeMenu();

	// Header
	setTimeout( headerTransition, 5000 );
	
	// Event Scroll
	if( $("#events").length > 0 )
	{
		$("#events").hover(
			function() { $(this).stop(); },
			function() { animateEvents2(); }
		);
		//$("#events").css( "height", $("#events").height() );
		//eventTimer = setInterval( "animateEvents()", 3000 );
		//var dur = $(".events").length / 2 * 1000;
		
		//$("#events").animate( dur, {top:$(this).height()/2}
		animateEvents2();
	}
	
	// Login
	if( $("#login_dialog").length > 0 )
		$("#login_dialog").dialog({
			modal: true,
			show: "clip",
			hide: "explode"
		});
	
	$(".button").button();
	$(".submit").button();
	$(".button").click( function() { window.open( $(this).attr( "rel" ), "_self" ); } );
	
	// Editor?
	$(".editor").each( function( i, elem ) {
		new nicEditor({fullPanel: true}).panelInstance(elem.id);		
	});
	
	// Datepickers
	$(".datepicker").datepicker();
	
	$(".pbar").each( function() {
		var v = parseInt($(this).attr("rel"));
		$(this).progressbar({value:v});
	})

});

var eventTimer;
var eventTimerStart;
function animateEvents()
{
	$(".event:first").hide( 1000, function() { $(this).insertAfter( $(".event:last") ); $(this).show(); } );
}
function animateEvents2()
{
	//alert( $("#events").position().top );
	var dur = $(".event").length / 2 * 2000;
	var	avg = $("#events").height() / $(".event").length;
	//alert( avg );
		dur = ($(".event").length/2 - Math.abs(parseInt( ($("#events").position().top / avg) ))) * 2000;
	
	//alert( dur );
	//var pos = $(".event:firs$("#events").position().top;
	$("#events").animate( {top:$("#events").height()/2*-1}, dur, "linear", function(){ $(this).css("top",0);animateEvents2();});
}
// -- Menu -----------------------------------------------------------------
var isAnimating = false;
function initializeMenu()
{
	$(document).click( function() { $(".sub_menu").hide(); } );

	// Main
	$("#menu_items img").each( function() { 
	
		// Sub Menu
		$(this).mouseenter( function() { $(".sub_menu").hide(); });
		if( $(this).attr( "rel" ) != undefined )
		{
			// Position Sub Menu
			var sub_menu = $("#sm_"+ $(this).attr( "rel" ))
				sub_menu.css( "top", $("#menu_bar").offset().top + 44 );
				sub_menu.css( "left", $(this).offset().left + 3 );
				sub_menu.css( "display", "block" );
				sub_menu.hide();
				sub_menu.hover( function() { hideMenu( $(this) ); }, function() { $("div .sub_menu").hide( ); } );
			
			// Hover
			$(this).hover( 
				function() {
					
					var obj = $("#sm_"+ $(this).attr( "rel" ))
					$("div .sub_menu").hide();
					$("#sm_"+ $(this).attr( "rel" )).show();
				}, 
				function() {  } 
			);
		}
	
	});
	
	// Submenus
	$(".sub_menu div").each( function() {
	
		if( $(this).attr( "rel" ) != undefined )
		{
			// Display
			$("#sm_"+ $(this).attr( "rel" )).hover(
				function() {},
				function() {
					if( $(this).attr( "rel" ) != undefined )
					{
						isAnimating = true;
						$(this).hide( 1, function() { isAnimating = false; } );
					}
				}
			);
			
			$(this).mouseenter( function() {
				var l = $(this).outerWidth() + $(this).offset().left;
				var t = $(this).offset().top-1;
				
				$("#sm_"+ $(this).attr( "rel" )).css( "left", l );
				$("#sm_"+ $(this).attr( "rel" )).css( "top", t );
				
				if( !isAnimating )
					$("#sm_"+ $(this).attr( "rel" )).show( 10 );
			});
		}
		else
		{
			if( !$(this).hasClass( "secondary" ) && !$(this).parent().hasClass( "secondary" ) )						
			$(this).mouseover( function() { $(".secondary").hide(); } );
		}
		
		$(this).hover( function() {	
			if( $(this).parent().hasClass( "secondary" ) && !$(this).parent().hasClass( "tertiary" ) )
				$(".tertiary").hide();
			
			if( !$(this).parent().hasClass( "secondary" ) )
			{
				$("#debug").html( $(this).attr( "id" ) + ": hiding" );
				$(".secondary").hide();
				$(".tertiary").hide();
			}
		},
		function() {}
	);
	
	});
	
	// Stylize
	$(".secondary").mouseenter( function() {
		var parts = $(this).attr( "id" ).split("_");
		$(this).parent().find(".sec_arrow").each( function() {
			
			if( $(this).attr( "rel" ) == parts[1] )
				$(this).addClass( "sub_menu_highlight" );
			
		});
	});
	$(".secondary").mouseleave( function() {
		var parts = $(this).attr( "id" ).split("_");
		$(this).parent().find(".sec_arrow").each( function() {
			
			if( $(this).attr( "rel" ) == parts[1] )
				$(this).removeClass( "sub_menu_highlight" );
			
		});
	});
	
	// Update Links
	$(".sub_menu a").click( function() {
		var a = $(this);
		$(this).parent().click( function() {
			window.open( a.attr( "href" ), "_self" );		
		});
	});

}
function hideMenu( obj )
{
	$("div .sub_menu").each( function() {
		if( $(this).attr( "id" ) != obj.attr( "id" ) )
			$(this).hide();
	});
}

function headerTransition()
{
	var proceed = true;
	
	$("#header img:last").fadeOut( 2500, function() {
	
		$(this).insertBefore( "#header img:first" );
		$(this).show();
		
		setTimeout( headerTransition, 7500 );
	});
}
// -------------------------------------------------------------------------
