////////////////////////////////////////////////////////////////////////////////
// sutherland.js
//

	// -- Initiailze ---------------------------------------------------------------
	$(document).ready(function() {
	
		// Menu
		$("#menu").find("a").each( function() {
		
			var elem = $(this);
			if( $(this).attr( "rel" ) != "ignore" )
			$(this).click( function(event) {
				
				// Find Position
				event.preventDefault();
				
				var anchor = $(this).attr( "rel" ).replace( "#", "" );
				var posX = (-1) * $("#a_"+ anchor).position().left;
				
				transitionContent( posX );
				
				// Highlight
				$("#menu").find("a").each( function() {
					$(this).removeClass( "selected" );
				});
				$(this).addClass( "selected" );
			});			
		
		});
		
		// Navigation
		$("img.nav").each( function() {
		
			
			$(this).css( "display", "block" );
			$(this).fadeTo( 3000, 0 );
			$(this).mouseover( function() {
			
				$(this).stop();
				$(this).fadeTo( "slow", 1 );
				
				var modifier = 1;
				var maxWidth = Math.abs($("#content").position().left);
				if( $(this).attr("rel") == "right" )
				{
					modifier = -1;
					
					maxWidth = $("#last").position().left - Math.abs($("#content").position().left) - screen.width;
					maxWidth = $("#last").position().left - Math.abs($("#content").position().left);
					
					if( maxWidth <= 0 )
						return;
				}
				
				var maxDuration = (maxWidth/250) * 650;
			
				var posX = $("#last").position().left * modifier;
				if( posX > 0 || $(this).attr("rel") != "right" )
					posX = 0;
					
				$("#content").animate({ left: posX }, maxDuration, "linear" );
			});
			$(this).mouseout( function() {
				$(this).stop();
				$(this).fadeTo( "slow", 0 );
				
				$("#content").stop();
				/*
				var cur_offset = $("#content").position().left;
				var found = false;
				alert( cur_offset );
				$("#menu").find("a").each( function() {
				
					var mnu_item = $(this);
					if( (mnu_item.position().left + cur_offset) > 0 && !found )
					{
						alert( mnu_item.attr( "rel" ) +": "+ mnu_item.position().left );
						$("#menu").find("a").each( function() {
							$(this).removeClass( "selected" );								
						});
						
						mnu_item.addClass( "selected" );
						found = true;
					}
						//alert( mnu_item.attr( "rel" ) +":"+ (mnu_item.offset().left + cur_offset) );
					
				
				});
				*/
			});
		});
		
		// Load Page
		if( $("#loadPage").length > 0 )
		{
			var lp_elem = $("#loadPage").val();
			var posX = (-1) * $("#a_"+ lp_elem).position().left;
			
			transitionContent( posX );
			
			// Highlight
			$("#menu").find("a").each( function() {
				if( $(this).attr( "rel" ) != lp_elem )
					$(this).removeClass( "selected" );
				else
					$(this).addClass( "selected" );
			});
			
		}
			
		
		// More Information
		$("img.moreinfo").each( function() {
			
			$(this).click( function() {
			
				$("#li_"+ $(this).attr( "rel" )).css( "display", "inline" );
				//$("#"+ $(this).attr( "rel" )).css( "display", "block" );
				$("#"+ $(this).attr( "rel" )).show( "slow" );
				$(this).hide();
			
			});
			
		});
		
		$("img.lessinfo").each( function() {
			
			$(this).click( function() {
			
				$("#mi_"+ $(this).attr( "rel" )).show();
				$("#"+ $(this).attr( "rel" )).hide( "slow" )
				$(this).hide();
			
			});
			
		});
		
		// Gallery
		if( $(".gi_image").length > 0 )
		{
			var galleryImage = document.createElement( "div" );
				galleryImage.id = "dialog_gi";
				
			document.body.appendChild( galleryImage );
				
			$("#dialog_gi").dialog({
				autoOpen: false,
				modal: true
			});			
			$(".gi_image").each( function() {
				var imgObj = $(this);
				$(this).css( "cursor", "pointer" );
				$(this).click( function() {
					
					var dims = imgObj.attr( "rel" ).split( "," );
					$("#dialog_gi").data("width.dialog", parseInt(dims[0]) + 27).data("height.dialog", parseInt(dims[1]) + 47).data("title.dialog", dims[2]); 
					$("#dialog_gi" ).html( '<img src="'+ this.src.replace( "_thumb.jpg", ".jpg" ) +'" />' );
					$("#dialog_gi" ).dialog( "open" );
				
				});
			
			});
		}
		
		// Events
		$("#month_next").click( function() { getEventsContent( $(this).attr( "rel" ) ); });
		$("#month_prev").click( function() { getEventsContent( $(this).attr( "rel" ) ); });
		
		enableEvents();
		
		// Links?
		$(".lnk").click( function() {
		
			window.open( $(this).attr( "rel" ), '_self' );
			
		});
	});
	// -----------------------------------------------------------------------------
	
	// -- Events -------------------------------------------------------------------
	function getEventsContent( d )
	{
		$.get( 
			"~ajax.php",
			{ eventsContent: "", date: d },
			function( data ) {
			
				$("#events_content").html( data );
				
				$("#month_next").click( function() { getEventsContent( $(this).attr( "rel" ) ); });
				$("#month_prev").click( function() { getEventsContent( $(this).attr( "rel" ) ); });
			
			}
		);
	}
	function enableEvents()
	{
		$(".eventDay").each( function() {
		
			var classes = $(this).attr( "class" ).toString().split( " " );
			for( var i = 0; i < classes.length; i++ )
			{
				var parts = classes[i].split( "_" )
				if( parts.length == 2 )
				{
					$(this).click( function() {
					
						scrollEvent( parts[1] );
					
					});
				}
			}
		
		});
	}
	function scrollEvent( id )
	{
		$(".event_item").each( function() {
		
			if( $(this).attr( "rel" ) == id )
			{
				$("#events_details").scrollTop( 0 );
				$("#events_details").scrollTop( $(this).position().top - 50 );
			}
		
		});
	}
	// -----------------------------------------------------------------------------
		
	// -- Scroll -------------------------------------------------------------------
	function transitionContent( posX )
	{
		// Stop
		//alert( $("#content").position().left );
		$("#content").stop();
		//alert( posX );
		$("#content").animate({ left: posX }, 650, "swing", transitionCallback );
		
	}
	function transitionCallback()
	{
		//alert( $("#content").position().left );
	}
	function scrollContent(  )
	{
	
	}
	// -----------------------------------------------------------------------------

//
//
////////////////////////////////////////////////////////////////////////////////
