// JavaScript Document
var g_images = new Array();
var g_services = new Array();

function preload_images()
{
	if (document.images)
	{
		for( var i = 0; i < preload_images.arguments.length; i++ )
		{
			var img = new Image();
			img.src = preload_images.arguments[i];
			window.g_images.push( img );
			// window.g_images[ window.g_images.length - 1 ].src = preload_images.arguments[i];
		}
	}
}

function update_image( obj, image )
{
	if( typeof( obj ) != "object" )
	{
		obj = get_object( obj );
	}
	obj.src = image;
}

function update_class( obj, class_name )
{
	obj.className = class_name;	
}

function onload_hover( type )
{
	var class_name = "hover";
	var objs = document.getElementsByTagName( type );
	
	// loop through all the onmouseover a tags
	for( var i = 0; i < objs.length; i++ )
	{
		var parent = objs[ i ];
		var obj = null;

		
		if( parent.nodeName == "INPUT" && parent.className == class_name )
		{
			obj = parent;
		}
		else
		{
			if( parent.className == class_name && parent.childNodes.length > 0  )
			{
				obj = parent.childNodes[ 0 ];
			}
		}

		if( obj )
		{
			var file1 = obj.src;
			var postfix = file1.split( '.' );
			postfix = postfix[ postfix.length - 1 ];
			postfix = "." + postfix;
			
			var file2 = file1.replace( postfix, "_over" + postfix );
			preload_images( new Array( file2 ) );
			
			if( obj.addEventListener )
			{
				obj.addEventListener( "mouseover", function ( a, b ) { return function(){ update_image( a, b ); } }( obj, file2 ), false ); 
				obj.addEventListener( "mouseout", function ( a, b ) { return function(){ update_image( a, b ); } }( obj, file1 ), false );
			}
			else
			{
				obj.attachEvent( "onmouseover", function ( a, b ) { return function(){ update_image( a, b ); } }( obj, file2 ) ); 
				obj.attachEvent( "onmouseout", function ( a, b ) { return function(){ update_image( a, b ); } }( obj, file1 ) );
			}			
		}
	}
}

function onload_menu_items( class_name )
{
	var objs = document.getElementsByTagName( 'li' );
	
	// loop through all the onmouseover a tags
	for( var i = 0; i < objs.length; i++ )
	{
		var obj = objs[ i ];
		
		if( obj )
		{
			if( obj.className == class_name || obj.className == class_name + "_selected" )
			{	
				var a = obj.getElementsByTagName( 'a' );
				var href = null;
				if( a.length > 0 )
				{
					a = a[ 0 ];	
				}
				
				href = a.href;
			
				class_out = class_name;
				class_over = class_name + "_over";
				
				if( obj.addEventListener )
				{
					if( obj.className == class_name )
					{
						obj.addEventListener( "mouseover", function ( a, b ) { return function(){ menu_item_over( a, b ); } }( obj, class_over ), false ); 
						obj.addEventListener( "mouseout", function ( a, b ) { return function(){ menu_item_out( a, b ); } }( obj, class_out ), false );
					}
					if( obj.parentNode.id != "tb_services" )
					{
						obj.addEventListener( "click", function ( a ) { return function(){ menu_item_click( a ); } }( href ), false );
					}
				}
				else
				{
					if( obj.className == class_name )
					{
						obj.attachEvent( "onmouseover", function ( a, b ) { return function() { menu_item_over( a, b ); } }( obj, class_over ) ); 
						obj.attachEvent( "onmouseout", function ( a, b ) { return function() { menu_item_out( a, b ); } }( obj, class_out ) );
					}
					if( obj.parentNode.id != "tb_services" )
					{
						obj.attachEvent( "onclick", function ( a ) { return function() { menu_item_click( a ); } }( href ) );
					}
				}			
			}
		}
	}
}

function menu_item_over( obj, image )
{
	obj.style.cursor = "pointer";
	update_class( obj, image );	
}

function menu_item_out( obj, image )
{
	obj.style.cursor = "default";
	update_class( obj, image );
}

function menu_item_click( url )
{
	window.location = url;	
}

function show_menu( obj )
{
	if( !document.getElementById( 'services_menu' ) )
	{
		//var img = parent.childNodes[ 0 ];
		var x = get_x( obj );
		var y = get_y( obj ) + get_height( obj );
		
		//alert( x + " and " + y );
		var div = document.createElement( 'div' );
		
		div.id 			= "services_menu";
		div.className 	= "toolbar_menu";
		div.style.top 	= y - 1 + "px";
		div.style.left 	= x + "px";
		
		for( var i = 0; i < g_services.length; i++ )
		{
			div.appendChild( g_services[ i ] );
		}
		
		document.body.appendChild( div );
	}
}

function hide_menu( e )
{
	e = ( !e && window.event) ? window.event : e;
	
	// get the mouse coordinates
	if( document.getElementById( 'services_menu' ) )
	{
		var mx = mouse_x( e );
		var my = mouse_y( e );
		
		var x1, y1, x2, y2 = 0;
		
		var hide = true;
		
		// check the button dimensions
		var obj1 = document.getElementById( 'tb_services' );
		if( in_rect(	mx,
						my,
						get_x( obj1 ),
						get_y( obj1 )  - 1,
						get_x( obj1 ) + get_width( obj1 ),
						get_y( obj1 )  - 1 + get_height( obj1 ) ) )
		{
			hide = false;	
		}
		else
		{
			var obj2 = document.getElementById( 'services_menu' );
			if( in_rect(	mx,
							my,
							get_x( obj2 ),
							get_y( obj2 ),
							get_x( obj2 ) + get_width( obj2 ),
							get_y( obj2 ) + get_height( obj2 ) ) )
			{
				hide = false;	
			}	
		}
		
		if( hide )
		{
			document.body.removeChild( document.getElementById( 'services_menu' ) );
			var img_obj = document.getElementById( 'tb_services' );
		}
	}
}

function create_service( name, url )
{
	var div = document.createElement( 'div' );
	div.className = "toolbar_menu_item";
	
	var a = document.createElement( 'a' );
	a.href = url;
	a.innerHTML = name;
	
	div.appendChild( a );
	
	g_services.push( div );
}

function get_x( obj )
{
	var curleft = 0;
	if( obj.offsetParent )
	{
		while( 1 ) 
		{
			curleft += obj.offsetLeft;
			if( !obj.offsetParent )
			{
				break;
			}
			obj = obj.offsetParent;
		}
	}
	else if( obj.x )
	{
		curleft += obj.x;
	}
	return curleft;
}

function get_y( obj )
{
	var curtop = 0;
	if( obj.offsetParent )
	{
		while( 1 )
		{
	  		curtop += obj.offsetTop;
	  		if(!obj.offsetParent)
			{
				break;
			}
	  		obj = obj.offsetParent;
		}
	}
	else if( obj.y )
	{
		curtop += obj.y;
	}
	return curtop;
}

function get_height( obj )
{
	var result = 0;
	if( obj.offsetHeight )
	{
		result = obj.offsetHeight;	
	}
	else if( obj.style.pixelHeight )
	{
		result = obj.style.pixelHeight;	
	}
	return result;
}

function get_width( obj )
{
	var result = 0;
	if( obj.offsetWidth )
	{
		result = obj.offsetWidth;	
	}
	else if( obj.style.pixelWidth )
	{
		result = obj.style.pixelWidth;	
	}
	return result;
	
}

function mouse_x( e ) 
{
	var result = null;
	
	if( e.pageX )
	{
		result = e.pageX;
	}
	else if ( e.clientX )
	{
	   result = e.clientX + ( document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft );
	}
	return result;
}

function mouse_y( e ) 
{
	var result = null;

	if( e.pageY )
	{
		result = e.pageY;
	}
	else if( e.clientY )
	{
		result = e.clientY + ( document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop );
	}
    return result;
}

function in_rect( dx, dy, x1, y1, x2, y2 )
{
	result = false;
	if( dx >= x1 && dx <= x2 && dy >= y1 && dy <= y2 )
	{
		result = true;	
	}
	return result;
}

function hide_divs( class_name )
{
	var objs = document.getElementsByTagName( 'div' );
	
	// loop through all the onmouseover a tags
	for( var i = 0; i < objs.length; i++ )
	{
		var obj = objs[ i ];
		
		if( obj && obj.className == class_name )
		{
			obj.style.display = "none";
		}
	}
}

function show_div( id )
{
	document.getElementById( id ).style.display = "block";	
}

function light_over( index )
{
	slides.resetSlides( index );
	slides.play = false;
	update_lights( index );
}

function light_out()
{
	slides.play = true;
}

function update_lights( index )
{
	var ps = document.getElementsByTagName( 'p' );
	var num_ps = ps.length
	for( var i = 0; i < num_ps; i++ )
	{
		var p = ps[ i ];
		if( p.className && p.className == "lights" )
		{
			
			var imgs = p.getElementsByTagName( 'img' );
			var num_imgs = imgs.length; 
			for( var j = 0; j < num_imgs; j++ )
			{
				var img = ( j == index ) ? "light_on.gif" : "light_off.gif";
				imgs[ j ].src = "images/" + img;
			}
		}
	}
}

function update_slide()
{
	var index = slides.counter;
	var obj = document.getElementById( 'slide_link' );
	obj.href = slide_links[ index ];
	obj.target = slide_targets[ index ];
	update_lights( index );
}

function slide_over()
{
	slides.play = false;
	slides.resetSlides( slides.counter );
}

function slide_out()
{
	slides.play = true;	
}

			
