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;	
}
