//------------------------------------------------------------------------
// Package: Events Handler
//------------------------------------------------------------------------

//------------------------------------------------------------------------
// Class: LW_Events_Handler
// Used to handle events used by DOM elements.
//------------------------------------------------------------------------
LW_Events_Handler =
{
	
	id_counter: 1,
	events: {},

	//--------------------------------------------------------------------
	// Function: addEvent()
	// Adds an event to an element. If there was one already assigned it
	// will chain them so that as many as the user wants can be assigned
	// without problems.
	//
	// Parameters:
	//     element - The element that you'd like to assign an event to.
	//     event_type - The type of event, eg: "onclick" "onmouseover"
	//     event_handler - The function that you would like to handle this
	//                     event.
	//     event_id - (Optional) The ID that you'd like to assign to this
	//                event. If you assign an ID, it can be used later to
	//                manage the event.
	//--------------------------------------------------------------------
	addEvent: function( element, event_type, event_handler, event_id )
	{
		
		// If we don't have an ID for this handler, add one.
		if ( !event_handler.LW_event_id && event_id == null ) 
			event_handler.LW_event_id = "LW_EVENT_" + LW_Events_Handler.id_counter++;
		else if ( event_id != null )
			event_handler.LW_event_id = event_id;
		
		// If no events array, create one.
		if ( !element.LW_events ) element.LW_events = {};
		
		// Get the element's event handlers for this type.
		var handlers = element.LW_events[event_type];

		// If no handlers yet,create the array.
		if ( !handlers )
		{
			// Create the array.
			handlers = element.LW_events[event_type] = {};
			
			// If there already is an event assigned, add it as the first handler.
			if ( element[event_type] )
				LW_Events_Handler.addEvent( element, event_type, element[event_type], "LW_BASE_EVENT" );
		}
		
		// Store the event handler.
		handlers[event_handler.LW_event_id] = event_handler;
		
		// Store it in our system.
		if ( !LW_Events_Handler.events[event_handler.LW_event_id] ) 
			LW_Events_Handler.events[event_handler.LW_event_id] = [];
			
		LW_Events_Handler.events[event_handler.LW_event_id].push( { "element": element, "type": event_type, "handler": event_handler } );
		
		// Set the element's "event_type" event to be handled by us.
		element[event_type] = function( e ){ return LW_Events_Handler.handleEvent( this, e, event_type ); };
		
		// Return the event's ID.
		return event_handler.LW_event_id;

	},
	
	
	//--------------------------------------------------------------------
	// Function: removeEvent()
	// Removes an event from a particular element.
	// Note that you can usually just call this function with the exact
	// same parameters you used to add the event.
	//
	// Parameters:
	//     element - The element that the event handler was assigned to.
	//     event_type - The type of event, eg: "onclick" "onmouseover"
	//     event_handler - The function that's currently handling this
	//                     event.
	//--------------------------------------------------------------------
	removeEvent: function( element, event_type, event_handler )
	{
		
		if ( !element.LW_events && !element.LW_events[event_type] )
			return;
		
		// Deleting one handler or the whole event?
		delete element.LW_events[event_type][event_handler.event_id];
		
	},
	
	
	//--------------------------------------------------------------------
	// Function: removeEventByID()
	// Removes the particular event(s) for the particular event ID.
	//
	// Parameters:
	//     event_id - The event ID that you'd like to remove. Will remove
	//                all events assigned to this ID used by any element.
	//--------------------------------------------------------------------
	removeEventByID: function( event_id )
	{
		
		if ( !LW_Events_Handler.events[event_id] )
			return;
			
		// Loop through all the event handlers attached to this event ID.
		for ( var i in LW_Events_Handler.events[event_id] )
		{
			
			var event = LW_Events_Handler.events[event_id][i];	
			
			// Remove the event.
			LW_Events_Handler.removeEvent( event.element, event.type, event.handler );			
			delete LW_Events_Handler.events[event_id][i];
			
		}
		
		// Delete from global array.
		delete LW_Events_Handler.events[event_id];
		
	},
	
	
	//--------------------------------------------------------------------
	// Function: removeEventType()
	// Removes all the event handlers for a particular event type.
	// Note that if you've assigned multiple handlers to a particular
	// event, this will remove all of them.
	//
	// Parameters:
	//     element - The element that the event handlers were assigned to.
	//     event_type - The type of event that you'd like to completely
	//                  remove, eg: "onclick" "onmouseover"
	//--------------------------------------------------------------------
	removeEventType: function( element, event_type )
	{
		
		if ( !element.LW_events && !element.LW_events[event_type] )
			return;
			
		// Loop through.
		for ( var i in element.LW_events[event_type] )
			delete element.LW_events[event_type][i];
			
		delete element.LW_events[event_type];
		
	},
	
	
	//--------------------------------------------------------------------
	// (Exclude)
	// Function: handleEvent()
	// Handles a particular event for a particular element.
	//--------------------------------------------------------------------
	handleEvent: function( element, event, event_type )
	{
		
		var ret = true;
		
		// Get the event's handlers.
		var handlers = element.LW_events[event_type];
		
		// Loop through and call each handler.
		for ( var i in handlers )
		{
			
			if ( !event )
				event = null;
			
			if ( handlers[i]( event ) === false )
				ret = false;
				
		}
		
		return ret;
		
	},
	
	
	//--------------------------------------------------------------------
	// (Exclude)
	// Function: unloadEvents()
	// Safely unloads all the events used by the system. Called when the
	// page unloads.
	//--------------------------------------------------------------------
	unloadEvents: function()
	{
		
		// Loop through all the events and remove them.
		for ( var id in LW_Events_Handler.events )
		{
			if ( !LW_Events_Handler.events[id] )
				LW_Events_Handler.removeEventByID( id );
		}
		
	},


	//--------------------------------------------------------------------
	// Function: getCursorPos()
	// Returns the position of the cursor at the time a particular event
	// fired in page coordinates.
	//
	// Parameters:
	//     event - The event that was fired.
	//--------------------------------------------------------------------
	getCursorPos: function( event )
	{

		var pos = new LW_Structure_Point();

		if ( !event ) var event = window.event;

		if ( event.pageX || event.pageY )
		{

			pos.X = event.pageX;
			pos.Y = event.pageY;

		}
		else if ( event.clientX || event.clientY )
		{

			pos.X = event.clientX + document.body.scrollLeft;
			pos.Y = event.clientY + document.body.scrollTop;

		}

		// Finally, return the cursor position.
		return pos;

	}

}

// Make sure we unload everything when the page unloads.
LW_Events_Handler.addEvent( window, "onunload", LW_Events_Handler.unloadEvents );// First, before we do anything, load the Yahoo! UI DOM library.
document.writeln("<script type='text/javascript' src='" + LW_PATH_TO_API + "/yahoo-ui/yahoo-min.js'><" + "/script>");
document.writeln("<script type='text/javascript' src='" + LW_PATH_TO_API + "/yahoo-ui/dom-min.js'><" + "/script>");


//------------------------------------------------------------------------
// Name: LW_DOM_Library
// Desc: Some helper functions to make working with the DOM easier.
//------------------------------------------------------------------------
LW_DOM_Library =
{

	//------------------------------------------------------------------------
	// Class Constants
	//------------------------------------------------------------------------
	CREATE_IFRAME:   0,
	CREATE_HTML:     1,
	CREATE_DOMNODE:  2,

	//----------------------------------------------------------------------
	// Name: noteToString()
	// Desc: Iterates through the node's children and retrieves
	//       all the node's text.
	//----------------------------------------------------------------------
	nodeToString: function( node )
	{

		var node_string = "";
		var suffix      = "";

		// Loop through each child node.
		for ( var i = 0; i < node.childNodes.length; i++ )
		{

			// Get the child node.
			child_node = node.childNodes[i];

			// Only store the text if it's not null.
			if ( child_node.data != undefined )
			  node_string += child_node.data;

			// If this node has a tag name, store the tags.
			if ( child_node.tagName != undefined )
			{
			   node_string += "<" + child_node.tagName + ">";
				 suffix += "</" + child_node.tagName + ">";
			}

			// If this node has child nodes, recurse.
			if ( child_node.childNodes.length != 0 )
			  node_string += LW_DOM_Library.nodeToString( child_node );

			// Add the suffix.
			node_string  = node_string + suffix;
			suffix       = "";

		}  // Next child node.

		// Return the collected string.
		return node_string;

	},


	//----------------------------------------------------------------------
	// Name: getViewportWidth()
	// Desc: Returns the viewport's width.
	//----------------------------------------------------------------------
	getViewportWidth: function()
	{
		
		return YAHOO.util.Dom.getViewportWidth();

	},


	//----------------------------------------------------------------------
  // Name: getViewportHeight()
  // Desc: Returns the viewport's height.
  //----------------------------------------------------------------------
	getViewportHeight: function()
	{

	  return YAHOO.util.Dom.getViewportHeight();

	},


	//----------------------------------------------------------------------
	// Name: getDocumentWidth()
	// Desc: Returns the document's width.
	//----------------------------------------------------------------------
	getDocumentWidth: function()
	{

		return YAHOO.util.Dom.getDocumentWidth();

	},


	//----------------------------------------------------------------------
	// Name: getDocumentHeight()
	// Desc: Returns the documents's height. 
	//----------------------------------------------------------------------
	getDocumentHeight: function()
	{

		return YAHOO.util.Dom.getDocumentHeight();

	},


	//----------------------------------------------------------------------
	// Name: getScrollXOffset()
	// Desc: Returns the scrollbar's X offset value.
	//----------------------------------------------------------------------
	getScrollXOffset: function()
	{

		return YAHOO.util.Dom.getDocumentScrollLeft();

	},


	//----------------------------------------------------------------------
	// Name: getScrollYOffset()
	// Desc: Returns the scrollbar's Y offset value.
	//----------------------------------------------------------------------
	getScrollYOffset: function()
	{

		return YAHOO.util.Dom.getDocumentScrollTop();

	},


	//----------------------------------------------------------------------
	// Name: getStyle()
	// Desc: Gets a property from the CSS style of the passed in element.
	//----------------------------------------------------------------------
	getStyle: function( element, property )
	{
		
		return YAHOO.util.Dom.getStyle( element, property );

	},


	//----------------------------------------------------------------------
	// Name: setStyle()
	// Desc: Sets a property from the CSS style of the passed in element, to
	//       the new value passed in.
	//----------------------------------------------------------------------
	setStyle: function( element, property, new_value )
	{

		YAHOO.util.Dom.setStyle( element, property, new_value );

	},


	//----------------------------------------------------------------------
	// Name: getWidth()
	// Desc: Returns the width of the passed in element.
	//----------------------------------------------------------------------
	getWidth: function( element )
	{

		return element.offsetWidth;

	},


	//----------------------------------------------------------------------
	// Name: getHeight()
	// Desc: Returns the height of the passed in element.
	//----------------------------------------------------------------------
	getHeight: function( element )
	{
		
	  	return element.offsetHeight;

	},


	//----------------------------------------------------------------------
	// Name: getDimensions()
	// Desc: Returns the dimensions of the passed in element.
	//----------------------------------------------------------------------
	getDimensions: function( element )
	{

		return LW_Structure_Dimensions( element.offsetWidth, element.offsetHeight );

	},


	//----------------------------------------------------------------------
	// Name: getX()
	// Desc: Returns the X value on the page of the passed in element.
	//----------------------------------------------------------------------
	getX: function( element )
	{

	  return LW_DOM_Library.getXY( element ).X;

	},


	//----------------------------------------------------------------------
	// Name: getY()
	// Desc: Returns the Y value on the page of the passed in element.
	//----------------------------------------------------------------------
	getY: function( element )
	{

	  return LW_DOM_Library.getXY( element ).Y;

	},


	//----------------------------------------------------------------------	
	// Name: getXY()
	// Desc: Returns the X and Y value on the page of the passed in
	//       element.
	//----------------------------------------------------------------------
	getXY: function( element )
	{

		// Get the positions.
    	pos = YAHOO.util.Dom.getXY( element );
    	
		// Return the position of the element.
		return new LW_Structure_Point( pos[0], pos[1] );

	},


	//----------------------------------------------------------------------
	// Name: setX()
	// Desc: Sets the X value on the page grid of the passed in element.
	//----------------------------------------------------------------------
	setX: function( element, X )
	{

		YAHOO.util.Dom.setX( element, X );

	},

	//----------------------------------------------------------------------
	// Name: setY()
	// Desc: Sets the Y value on the page grid of the passed in element.
	//----------------------------------------------------------------------
	setY: function( element, Y )
	{

		YAHOO.util.Dom.setY( element, Y );

	},


	//----------------------------------------------------------------------
	// Name: setXY()
	// Desc: Sets the X and Y value on the page grid of the passed in
	//       element.
	//----------------------------------------------------------------------
	setXY: function( element, point )
	{

		YAHOO.util.Dom.setXY( element, [ point.X, point.Y ] );

	},
	
	
	//----------------------------------------------------------------------
	// Name: getElementsByClassName()
	// Desc: Returns an array of elements with the class passed in.
	//----------------------------------------------------------------------
	getElementsByClassName: function( name, tag, root )
	{

		return YAHOO.util.Dom.getElementsByClassName( name, tag, root );

	},


	//----------------------------------------------------------------------
	// Name: createContainer()
	// Desc: Creates a container element in the document.
	//----------------------------------------------------------------------
	createContainer: function( create_type, creation_data, dimensions, position, align )
	{

		position = (position != null) ? position : new LW_Structure_Position( null, null, null, null );

		// Create a new div for the floating container.
		var container = document.createElement( "div" );

		// Specify its parameters.
		LW_DOM_Library.setStyle( container, "position", "absolute" );

		// Dimensions.
		if ( dimensions != null )
		{

			if ( dimensions.width  != null ) LW_DOM_Library.setStyle( container, "width", dimensions.width + "px" );
			if ( dimensions.height != null ) LW_DOM_Library.setStyle( container, "height", dimensions.height + "px" );

		}

		// Alignment.
		if ( align != null )
		{
			
			// Retrieve the window height and width.
			var client_height = LW_DOM_Library.getViewportHeight();
			var client_width  = LW_DOM_Library.getViewportWidth();
			
			// Get the element's dimensions.
			// We get it here in case the dimensions are set with CSS.
			var elem_dimensions = new LW_Structure_Dimensions( container );

			// Horizontal align.
			if ( align.halign == "left" )
				position.left = (position.left != null) ? position.left : 0;

			if ( align.halign == "center" )
				position.left = (position.left != null) ? (client_width * 0.5 - elem_dimensions.width * 0.5 + position.left) : (client_width * 0.5 - elem_dimensions.width * 0.5);
			
			if ( align.halign == "right" )
				position.left = (position.left != null) ? (client_width - elem_dimensions.width + position.left) : (client_width - elem_dimensions.width);

			// Vertical align.
			if ( align.valign == "top" )
				position.top = (position.top != null) ? position.top : 0;

			if ( align.valign == "center" )
				position.top = (position.top != null) ? (client_height * 0.5 - elem_dimensions.height * 0.5 + position.top) : (client_height * 0.5 - elem_dimensions.height * 0.5);

			if ( align.valign == "bottom" )
				position.top = (position.top != null) ? (client_height - elem_dimensions.height + position.top) : (client_height - elem_dimensions.height);

		}
		
		// Positions.
		if ( position.top    != null ) LW_DOM_Library.setStyle( container, "top", position.top + "px" );
		if ( position.right  != null ) LW_DOM_Library.setStyle( container, "right", position.right + "px" );
		if ( position.bottom != null ) LW_DOM_Library.setStyle( container, "bottom", position.bottom + "px" );
		if ( position.left   != null ) LW_DOM_Library.setStyle( container, "left", position.left + "px" );
		
		// Now add the content.
		if ( create_type == LW_DOM_Library.CREATE_IFRAME )
		{

			// Create the iframe.
			container.innerHTML = "<iframe width='" + dimensions.width + "' height='" + dimensions.height + "' frameborder='0' src='" + creation_data + "'></iframe>";

		}  // End if iframe passed.
		else if ( create_type == LW_DOM_Library.CREATE_HTML )
		{

			// Add the HTML to the container.
			container.innerHTML = creation_data;

		}  // End if HTML passed.
		else if ( create_type == LW_DOM_Library.CREATE_DOMNODE )
		{

			// Append the DOM node to the container.
		  container.appendChild( creation_data );

		}  // End if DOM node passed.

		// Append the div to the document.
		document.body.appendChild( container );

		// Return the new container element.
		return container;

	}

};


//--------------------------------------------------------------------
// Name: addImageRollovers()
// Desc: An array of IDs of images is passed in and it adds rollovers
//       to each one.
//--------------------------------------------------------------------
function addImageRollovers( id_array )
{	
	
	addImageRollovers.active_element = "";
	
	if ( document.getElementById( "menu" ) )
		addImageRollovers.active_element = LW_DOM_Library.getElementsByClassName( "active", "div", document.getElementById( "menu" ) )[0].id;
	
	// We use this function so that we can correctly form a closure
	// around the element.
	function addRollover( id )
	{
		    
	    var element = document.getElementById( id );
	    
	    // The function used for mousing onto the menu item..
	    function rollOver()
	    {
	    	
	    	if ( id == addImageRollovers.active_element )
				element.className = "hover_active";
			else
				element.className = "hover";
			
		}
		
		// The function used for mousing off of the menu item.
		function rollOut()
		{
			
			if ( id == addImageRollovers.active_element )
				element.className = "active";
			else
				element.className = "";
				
		}
	    
	    // Set the events.
	    LW_Events_Handler.addEvent( element, "onmouseover", rollOver );
	    LW_Events_Handler.addEvent( element, "onmouseout", rollOut );
	
	}

    // Get the image's elements.
    for ( var i = 0; i < id_array.length; i++ )
    {
        
        addRollover( id_array[i] );
        
    }  // Next image.
    
}


//--------------------------------------------------------------------
// Name: preloadImages()
// Desc: Used to preload images before they are shown.
//--------------------------------------------------------------------
function preloadImages( image_array )
{
	
	// Create the preloaders array.
	var preloaders = new Array( image_array.length );
	
	// Loop through every image that we want to preload.
	for ( var i in image_array )
	{
		
		// Preload the image.
		preloaders[i] = new Image();
		preloaders[i].src = image_array[i];
		
	}
	
}


//--------------------------------------------------------------------
// Name: toggleListing()
// Desc: Used to preload images before they are shown.
//--------------------------------------------------------------------
function toggleListing( id )
{
	
	// Loop through all the listings and hide them.
	var listings = LW_DOM_Library.getElementsByClassName( "listing" );
	for ( var i = 0; i < listings.length; i++ )
		LW_DOM_Library.setStyle( listings, "display", "none" );
		
	if ( id != 0 && id != toggleListing.active_id )
	{
		
		var listing = document.getElementById( "listing" + id );
		
		// Show the listing.
		LW_DOM_Library.setStyle( listing, "display", "block" );
		
		// Scroll to the top of the listing.
		window.scroll( 0, LW_DOM_Library.getY( listing ) - 100 );
		
		// Set as active listing.
		toggleListing.active_id = id;
		
	}
	else
		toggleListing.active_id = "";
	
}

toggleListing.active_id = "";