var g_images = new Array();

function get_y( oElement )
{
	var iReturnValue = 0;
	
	while( oElement != null ) 
	{
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function get_x( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) 
	{
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function preload_images()
{
	if (document.images)
	{
		for( var i = 0; i <= preload_images.arguments.length; i++ )
		{
			g_images.push( new Image() );
			g_images[ g_images.length - 1 ].src = preload_images.arguments[i];
		}
	}
}

function show_div( id )
{
	var obj = document.getElementById( id );
	obj.style.display = "block";
}

function hide_div( id )
{
	var obj = document.getElementById( id );
	obj.style.display = "none";
}

function image_update( id, img )
{
	document.getElementById( id ).src = img;	
}

function update_menu_item( obj, hover )
{			
	var color = "#000000";
	if( hover )
	{
		color = "#222222";
	}
	obj.style.backgroundColor = color;
}

function toggle_popup( id, parent_object, position, border )
{
	if( position == null ) position = "left";
	if( border == null ) border = 0;
	var obj = document.getElementById( parent_object );
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;
	var x = get_x( obj );
	var y = get_y( obj );
	
	
	if( position == "right" ) 
		x += width + border * 2;
	y += height + border * 2;
	
	obj = document.getElementById( id );
	obj.style.display = "block";
	
	width = 0;
	if( position == "right" )
		width = obj.offsetWidth;
	obj.style.left = ( x - width ) + 'px';
	obj.style.top = y + 'px';
	
	
}

function toggle_div( id )
{	
	var obj = document.getElementById( id );
	if( obj.style.display == "none" )
		obj.style.display = "block";
	else
		obj.style.display = "none";
}

function hide_popups( class_name )
{
	var divs = document.getElementsByTagName( "div" );
	for( var i = 0; i < divs.length; i++ )
	{
		var currentDiv = divs[ i ];
		if( currentDiv.className == class_name )
			currentDiv.style.display = "none";
	}		
}

function show_divs( class_name, display )
{
	var divs = document.getElementsByTagName( "div" );
	for( var i = 0; i < divs.length; i++ )
	{
		var current_div = divs[ i ];
		if( current_div.className == class_name )
			current_div.style.display = display;

	}		
}

function hide_click()
{
	hide_div( 'click' );
	toggle_selects( 'visible' );
	hide_popups( 'popup' );
}

function show_click()
{
	show_div( 'click' );
	toggle_selects( 'hidden' );
}


function toggle_selects( state ) 
{
	// stupid IE6 fix!
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	if ( ( browser == "Microsoft Internet Explorer" ) && ( version <= 6 ) )
	{
	var elements = document.documentElement.getElementsByTagName('select');    
		for (var i=0; i<elements.length; i++) {
			elements[i].style.visibility = state;
		}
	}
} 

function show_message( msg )
{
	var div_bg = document.createElement( 'div' );
	div_bg.className = "message_box_bg";
	div_bg.id = "message_box_bg";
	document.body.appendChild( div_bg );
	
	var form = document.createElement( 'form' );
	form.name = "message_box";
	form.id = "message_box";
	form.method = "post";
	form.action = "";
	
	var div = document.createElement( 'div' );
	div.className = "message_box";

	var span = document.createElement( 'span' );
	//var txt = document.createTextNode( msg );
	//p.appendChild( txt );
	span.innerHTML = msg;
	div.appendChild( span );
	
	var div_button = document.createElement( 'div' );
	div_button.style.textAlign = "right";
	
	var input = document.createElement( 'input' );
	input.className = "input_button";
	input.name = "continue";
	input.type = "button";
	input.value = "Continue";
	//input.setAttribute ( 'onClick', "hide_message(); return false;" );
	input.onclick = function() { hide_message(); return false; };
	div_button.appendChild( input );
	
	var div_clear = document.createElement( 'div' );
	div_clear.className = "clear";
	//var txt2 = document.createTextNode( "<!-- //-->" )
	//div_clear.appendChild( txt2 );
	div_button.appendChild( div_clear );
	
	div.appendChild( div_button );
	form.appendChild( div );
			
	document.body.appendChild( form );
}

function hide_message()
{
	var div1 = document.getElementById( 'message_box' );
	div1.parentNode.removeChild( div1 );
	var div2 = document.getElementById( 'message_box_bg' );
	div2.parentNode.removeChild( div2 );
}


function redirect_form(form_id, url)
{
	var obj = document.getElementById( form_id );
	obj.action = url;
	obj.submit();
}

function update_image( obj, image )
{
	if( typeof( obj ) != "object" )
	{
		obj = document.getElementById( obj );
	}
	obj.src = image;
}

function update_background( obj, image )
{
	if( typeof( obj ) != "object" )
	{
		obj = document.getElementById( obj );
	}
	obj.style.backgroundImage = "url(" + 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( 'div' );
	
	// 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 )
			{	
				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.id != "button_basket" )
				{
					if( obj.addEventListener )
					{
						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 );
	
						obj.addEventListener( "click", function ( a ) { return function(){ menu_item_click( a ); } }( href ), false );
					}
					else
					{
						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 ) );
						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 onload_tabs()
{
	var objs = document.getElementsByTagName( 'div' );
	
	if( objs )
	{
		var num_objs = objs.length;
		if( num_objs > 1 )
		{
			for( var i = 0; i < num_objs; i++ )
			{
				var obj = objs[ i ];
				if( obj.className.match( "tabs" ) && obj.id )
				{
					var lis = obj.getElementsByTagName( 'li' );
					var num_lis = lis.length;
					var count = 0;
					for( var j = 0; j < num_lis; j++ )
					{
						var li = lis[ j ];
						
						var parts = li.className.split( " " );
						
						var match_found = false;
						for( var k = 0; k < parts.length; k++ )
						{
							if( parts[ k ] == "tab" )
							{
								match_found = true;	
							}
						}
						
						if( match_found )
						{
							
							var a = li.getElementsByTagName( 'a' )[ 0 ];
							if( typeof( a ) !== "undefined" )
							{
								if( a.addEventListener )
								{
									a.addEventListener( "click", function ( x, y ) { return function(){ update_tabs( x, y ); } }( obj.id, j ), false );
									a.addEventListener( "click", function ( e ) { e.preventDefault(); }, false );
								}
								else
								{
									a.attachEvent( "onclick", function ( x, y ) { return function() { update_tabs( x, y ); } }( obj.id, j ) );
									a.attachEvent( "onclick", function ( e ) { e.returnValue = false; } );
								}
							}
							count++;
						}
					}
				}
			}
		}
	}
}

function update_tabs( id, index )
{
	
	var parent = document.getElementById( id );
	var objs = parent.getElementsByTagName( 'li' );
	var num_objs = objs.length;
	
	var count = 0;
	for( var i = 0; i < num_objs; i++ )
	{
		var obj = objs[ i ];
		if( obj.className.match( /tab/ ) )
		{
			
			if( index == count )
			{
				obj.className = "tab tab_selected"	
			}
			else
			{
				obj.className = "tab";	
			}
			
			if( i == 0 )
			{
				obj.className += " tab_left";	
			}
			
			count++;
		}
	}
	
	var objs = parent.getElementsByTagName( 'div' );
	var num_objs = objs.length;
	
	var count = 0;
	for( var i = 0; i < num_objs; i++ )
	{
		var obj = objs[ i ];
		if( obj.className == "tab_content" )
		{
			if( count == index )
			{
				obj.style.display = "block";	
			}
			else
			{
				obj.style.display = "none";	
			}
			count++;
		}
	}
}

function toggle_status( obj )
{
	var img = obj.childNodes[ 0 ];
	var div = document.getElementById( 'toolbar_status' );
	
	if( div.style.display == "block" )
	{
		div.style.display = "none";	
		img.src = "images/arrow_down.gif";
	}
	else
	{
		div.style.display = "block";
		img.src = "images/arrow_up.gif";
	}
	
	return false;
}

var g_slide_pos = new Array();
var g_slide_width = 716;

function slide_set( id )
{
	g_slide_pos[ id ] = 0;	
}

function slide_to( id, x )
{
	x = x - 1;
	g_slide_pos[ id ] = x;
	new Effect.Move( id, { x: x * g_slide_width * -1, y: 0, mode: 'absolute' });
	
	var parent = document.getElementById( id + "_tabs" );
	
	var objs = parent.getElementsByTagName( 'a' );
	num_objs = objs.length;
	
	var count = 0;
	
	for( var i = 0; i < num_objs; i++ )
	{
		var obj = objs[ i ];
		if( obj.className.match( /tab_number/ ) )
		{
			
			if( count == x )
			{
				obj.className = "tab_number tab_number_selected";	
			}
			else
			{
				obj.className = "tab_number";	
			}
			count++;
		}
	}
	return false;
}

function slide( id, direction, pages )
{
	x = g_slide_pos[ id ] + direction;
	if( x < 0 ) x = 0;
	if( x > ( pages - 1 ) ) x = ( pages - 1 );
	return slide_to( id, x + 1 );
}

