var row_out_color;
var row_over_color = new Array();
row_over_color[0] = "#FFEFDD";
row_over_color[1] = "#defbd3";

Event.observe(window, 'load', runAutoFunctions);

function runAutoFunctions() 
{ 
	autoBlur();
	autoOver();
	autoLastCollectionItem();
	
	$$('.auto_rowover').each(function(element) 
								 {
								 	element.onmouseover = function()
									{
										row_out_color = this.style.backgroundColor;
										this.style.backgroundColor=row_over_color[0];
									};
								element.onmouseout= function(){this.style.backgroundColor=row_out_color;};
								 });
	$$('.auto_rowover2').each(function(element) 
								 {
								 	
								element.onmouseover = function()
									{
										this.addClassName('over');
										/*row_out_color = this.style.backgroundColor;
										this.style.backgroundColor=row_over_color[1];*/
									};
								element.onmouseout= function()
									{
										if (this.hasClassName('over')) this.removeClassName('over');
									}
								
								 });							 
}

function autoBlur()
{
	$$('a', 'input[type=checkbox]', 'input[type=submit]', 'input[type=image]').each
	(
		function(element) 
		{
			element.observe('focus', blur);
		}
	);
}

function blur(event)
{
	Event.element(event).blur();
}


function autoOver()
{
	$$('.auto_over').each
	(
		function(element) 
  	    {
		  	 element.onmouseover = mouseOver;
		 	 element.onmouseout  = mouseOut;
   	    }
   	);
}

function mouseOver(obj)
{
	var element = obj;
	if((element == null || element.type == "mouseover") && this)
	{
		element = this;
	}
	
	if(element.tagName == "INPUT" || element.tagName == "IMG")
	{
  		var image_src       = element.src;
  		var extension_index = image_src.lastIndexOf('.');
  		element.src         = image_src.substring(0,extension_index) + '-over' + image_src.substring(extension_index);
	}
	else
	{
		if(!element.hasClassName('hover'))
		{
			element.addClassName('hover');
		}
	}
}

function mouseOut(obj)
{
	var element = obj;
	if((element == null || element.type == "mouseout") && this)
	{
		element = this;
	}
	
	if(element.tagName == "INPUT" || element.tagName == "IMG")
	{
		var image_src = element.src;
  		element.src   = image_src.replace('-over\.','\.');
	}
	else
	{
		if(element.hasClassName('hover'))
		{
			element.removeClassName('hover');
		}
	}
}


function getAbsoluteWidth(element)
{
	var absoluteWidth = 0;
	absoluteWidth += $(element).getWidth();
	/*absoluteWidth += parseInt($(element).getStyle('padding-right')) + parseInt($(element).getStyle('padding-left'));*/
	absoluteWidth += parseInt($(element).getStyle('margin-right')) + parseInt($(element).getStyle('margin-left'));
	/*absoluteWidth += parseInt($(element).getStyle('border-right-width')) + parseInt($(element).getStyle('border-left-width'));*/
	return absoluteWidth;
	
}
function autoLastCollectionItem()
{

	$$('.auto_last_collection_item').each (function(element)
										{
											
											var containerWidth = parseInt(element.getStyle('width'));
//											alert(containerWidth);
											var tmpWidth = 0;
											var tmpLastWidth = 0;
											
											items = element.childElements();
											
											for (var i=0; i < items.length; i++)
											{
												if (items[i].hasClassName('cleaner'))
												{
													continue;
												}
												itemAbsolutWidth = getAbsoluteWidth(items[i])
												tmpLastWidth = tmpWidth;
												tmpWidth += itemAbsolutWidth;
//												alert(itemAbsolutWidth);
												if (tmpWidth > containerWidth)
												{
													
													items[i].addClassName('last');
													tmpLastWidth += getAbsoluteWidth(items[i]);
													
													if (tmpLastWidth <= containerWidth)
													{
														tmpWidth = 0;
													}
													else
													{
														
														items[i].removeClassName('last');
														tmpWidth = itemAbsolutWidth;
													}
												}
												
												
											}
										
										});
}



function overImage(event)
{
	var image = Event.element(event);
  	var image_src = image.src;
  	var extension_index = image_src.lastIndexOf('.');
  	image.src = image_src.substring(0,extension_index) + '-over' + image_src.substring(extension_index);

}

function outImage(event)
{
	var image = Event.element(event);
	var image_src = image.src;
  	image.src = image_src.replace('-over\.','\.');

}


/*function overRow(index)
{
	
	alert("idem");
	this.style.backgroundColor = over_color[index];
	/*var cell = Event.element(event);
	var row = cell.parentNode;
	out_color = row.style.backgroundColor;
	row.style.backgroundColor = over_color;
}

function outRow(event)
{
	var cell = Event.element(event);
	var row = cell.parentNode;
	
	row.style.backgroundColor = out_color;
}

function overRow2(event)
{
	var cell = Event.element(event);
	var row = cell.parentNode;
	out_color = row.style.backgroundColor;
	row.style.backgroundColor = over_color_2;
}*/






function blur(event)
{
	Event.element(event).blur();
}

