////////////////////////////////////////////////////////////////////////////////
// BEGIN
//

	// -- Header ---------------------------------------------------------------
	function headerTransition()
	{
		var proceed = true;
		
		$("#header img:last").fadeOut( "slow", function() {
		
			$(this).insertBefore( "#header img:first" );
			$(this).show();
			
			setTimeout( headerTransition, 5000 );
		});
	}
	function reelTransition()
	{
		if( reelAnimation )
		{
			// Reel
			// .. Left?
			var items = $("#top_stories .rcBox").length;
			var item_width = 234;
			var curPos = $("#top_stories").position().left;
			
			var cur_node = parseInt( Math.abs( $("#top_stories").position().left ) / 234 );
			
			if( (Math.abs( curPos ) + item_width) <= (( Math.ceil( items/2 ) )*item_width) )
			{
				$("#top_stories").animate( {left: -(Math.abs( cur_node * 234 ) + item_width)}, 700, "swing", function(){ reelTimeout = setTimeout( reelTransition, reelTimeoutDuration ); reelNavigation(); } );			
			}
			else
			// .. Reset?
			{
				//$("#top_stories").animate( {left: 0}, 500, "linear", function(){ reelTimeout = setTimeout( reelTransition, reelTimeoutDuration ); reelNavigation(); } );
				$("#top_stories").css( "left", 0 );
				reelTimeout = setTimeout( reelTransition, 0 );
				reelNavigation();
			}
		}
	}
	// -------------------------------------------------------------------------
	// -- Reel -----------------------------------------------------------------
	function reelTransitionTo( index )
	{
		var curPos = $("#top_stories").position().left;
		$("#top_stories").stop();
		$("#top_stories").animate( {left: -(index*234)}, 500, "linear", function() { reelNavigation(); } );
	}
	
	function reelNavigation()
	{
		var cur_node = Math.ceil( Math.abs( $("#top_stories").position().left ) / 234 );
		//alert( cur_node );
		// Reset Nodes
		$("#reel_nav img.reel_node").attr( "src", "images/layout/icon_reel_inactive.gif" );
		
		// Set New Nodes
		var highlight_nodes = 3;
		var found = false;
		var highlighted_nodes = 0;
		var ctr = 0;
		var total_nodes = $("#reel_nav img.reel_node").length;
		var p_node = cur_node;
		
		if( p_node >= total_nodes )
			p_node = 0;
		
		var h_nodes = new Array();
		for( var i = 0; i < total_nodes; i++ )
		{
			h_nodes.push( p_node );
		
			p_node++;
			if( p_node >= total_nodes )
				p_node = 0;
			
			if( h_nodes.length == 3 )
				break;
		}
		$("#reel_nav img.reel_node").each( function() {
			
			if( in_array( ctr, h_nodes ) )
			{
				//alert( ctr );
				$(this).attr( "src", "images/layout/icon_reel_active.gif" );
				highlighted_nodes++;
				found = true;
			}
			
			ctr++;
		});
		
		/*
		$("#reel_nav img.reel_node").each( function() {
			
			if( (cur_node == ctr || found) && highlighted_nodes < highlight_nodes )
			{
				$(this).attr( "src", "images/layout/icon_reel_active.gif" );
				highlighted_nodes++;
				found = true;
			}
			
			ctr++;
		});*/
	}
	function in_array( needle, haystack )
	{
		for( var i = 0; i < haystack.length; i++ )
			if( needle == haystack[i] )
				return true;
		
		return false;
	}
	var reelAnimation = true;
	var reelTimeout;
	var reelTimeoutDuration = 6000;
	// -------------------------------------------------------------------------
	// -- Menu -----------------------------------------------------------------
	function initializeMenu()
	{
		$(document).click( function() { $("div .sub_menu").hide(); } );
	
		// Main
		$("#menu img").each( function() { 
		
			// Sub Menu
			if( $(this).attr( "rel" ) != undefined )
			{
				// Position Sub Menu
				var sub_menu = $("#sm_"+ $(this).attr( "rel" ))
					sub_menu.css( "top", $(this).position().top + 34 );
					sub_menu.css( "left", $(this).position().left + 3 );
					sub_menu.css( "display", "block" );
					sub_menu.hide();
					sub_menu.hover( function() { hideMenu( $(this) ); }, function() { $("div .sub_menu").hide( "fast" ); } );
				
				// Secondary Menu?
				$("#sm_"+ $(this).attr( "rel" ) +" div").each( function() {
				
					if( $(this).attr( "rel" ) != undefined )
					{
						// Display
						$("#sm_"+ $(this).attr( "rel" )).hover(
							function() {},
							function() {
								isAnimating = true;
								$(this).hide( "fast", function() { isAnimating = false; } );
							}
						);
						
						$(this).mouseenter( function() {							

							$(".secondary").hide();
							
							var l = $(this).width() + $(this).position().left + 33;
							var t = $(this).position().top-1;
							$("#sm_"+ $(this).attr( "rel" )).css( "left", l );
							$("#sm_"+ $(this).attr( "rel" )).css( "top", t );
							
							if( !isAnimating )
							$("#sm_"+ $(this).attr( "rel" )).show( "fast" );							
						
						})
					}
					else
					{
						if( !$(this).hasClass( "secondary" ) && !$(this).parent().hasClass( "secondary" ) )						
						$(this).mouseover( function() { $(".secondary").hide( "fast" ); } );
					}
				
				});
				
				// 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
				$("#sm_"+ $(this).attr( "rel" ) +" a").each( function() {
				
					var ahref = $(this);
					
					if( $(this).hasClass( "member_only" ) && !isMember )
					{
						$(this).parent().click(function(){});
						$(this).parent().addClass( "disabled" );
						$(this).parent().html( $(this).html() );
					}					
					else if( $(this).attr( "class" ) != "ignore" )
					$(this).parent().click( function() {
					
						window.open( ahref.attr( "href" ), "_self" );
					
					});
					
					
				
				});
				
				// Hover
				$(this).hover( 
					function() {
						
						var obj = $("#sm_"+ $(this).attr( "rel" ))
						$("div .sub_menu").hide();
						$("#sm_"+ $(this).attr( "rel" )).show( "fast" );
					}, 
					function() {  } 
				);
			}
		
		});
		
		// Members
		if( $("a.member_menu").length > 0 )
		{
			$("a.member_menu").each( function() {
			
				// Position Sub Menu
				var sub_menu = $("#sm_"+ $(this).attr( "rel" ))
					sub_menu.css( "top", $(this).offset().top + 22);
					sub_menu.css( "left", $(this).position().left + 10 );
					sub_menu.css( "display", "block" );
					sub_menu.hide();
					sub_menu.hover( function() { }, function() { $("div .sub_menu").hide( "fast" ); } );
					
				// Hover
				$(this).hover( function() { $("div .sub_menu").hide(); $("#sm_"+ $(this).attr( "rel" )).show( "fast" ); }, function() {  } );
			
			});
		}
	}
	function hideMenu( obj )
	{
		$("div .sub_menu").each( function() {
			if( $(this).attr( "id" ) != obj.attr( "id" ) )
				$(this).hide();
		});
	}
	// -------------------------------------------------------------------------
	// -- Initialize -----------------------------------------------------------
	var isMember = false;
	var isAnimating = false;
	$(document).ready(function() {

		// Menu?
		if( $("#menu img").length > 0 )
			initializeMenu();
		
		// Header?
		if( $("#header img").length > 1 )
			setTimeout( headerTransition, 5000 );
		
		// Reel?
		if( $("#top_stories" ).length > 0 )
		{
			// Animation
			// .. Default
			reelTimeout = setTimeout( reelTransition, reelTimeoutDuration );
			// .. Hover
			$("#top_stories").hover(
				function() { $(this).stop(); reelAnimation = false; clearTimeout( reelTimeout ); },
				function() { reelAnimation = true; reelTimeout = setTimeout( reelTransition, reelTimeoutDuration ); }
			);			
			
			// Navigation
			// .. Nodes
			var ctr = 0;
			$("#reel_nav img.reel_node").each( function() {
			
				$(this).click( function() {
					
					clearTimeout( reelTimeout );
					var rel = $(this).attr( "rel" );
					if( rel >= $("#reel_nav img.reel_node").length - 3 )
						rel = $("#reel_nav img.reel_node").length - 3;
					reelTransitionTo( rel );
				
				});
			
			});
			// .. Left			
			$("#reel_nav img:first").click( function() {
				var cur_node = Math.ceil( Math.abs( $("#top_stories").position().left ) / 234 );
				clearTimeout( reelTimeout );
				if( (cur_node - 1) < 0 )
					reelTransitionTo( $("#reel_nav img.reel_node").length - 3 );
				else
					reelTransitionTo( cur_node - 1 );
			});
			// .. Right
			$("#reel_nav img:last").click( function() {
				var cur_node = Math.ceil( Math.abs( $("#top_stories").position().left ) / 234 );
				if( cur_node >= $("#reel_nav img.reel_node").length - 3 )
					reelTransitionTo( 0 );
				else
					reelTransitionTo( cur_node + 1 );
			});
		}
		
		// Login Box?
		$("form[name=form_login]").find("input").focus( function() { $(this).select(); } );
		$("form[name=form_login]").find("input[name=password]").focus( function() { 
			
			var inp = document.createElement( "input" );
				inp.type = "password";
				inp.name = "password";
				
			$(this).replaceWith( inp );
			inp.focus();
		});
			
			
		//.focus( function() { $(this).attr( "type", "password" ); } );
	});
	// -------------------------------------------------------------------------

//
// END
////////////////////////////////////////////////////////////////////////////////