function initMenuSearchCategory(){
	$$('ul.list_Filters a.tit_Filter').each(function(el){Element.cleanWhitespace(el.parentNode);el.onclick=function(){switchMenuCategory(this);return false};});
	$$('ul.list_Filters a.link_OpenClose').each(function(el){el.onclick=function(){expandSubMenuCategory(this);return false};});
}

function switchMenuCategory(thisMenu){
	var parentNode = thisMenu.parentNode;
	if (parentNode.hasClassName('li_Closed'))
		expandMenuCategory(parentNode);
	else
		collapseMenuCategory(parentNode);
}

function expandMenuCategory(thisMenu){
	thisMenu.removeClassName('li_Closed');
	thisMenu.addClassName('li_Open');
}

function collapseMenuCategory(thisMenu){
	thisMenu.removeClassName('li_Open');
	thisMenu.addClassName('li_Closed');
}

function expandSubMenuCategory(thisMenu){
	Element.cleanWhitespace(thisMenu.parentNode);
	var list = thisMenu.parentNode.nextSiblings();
	list.each(function(el){el.removeClassName('li_Closed'); });
	thisMenu.parentNode.hide();
}

/*  Zoom Image  */
function initZoomImage(){
	var container = $('list_RisultatiRicerca');
	var thumbnails = container.select('img.box_Img');
	var thumbnailsFY = container.select('img.box_ImgFY');
	thumbnails.each(function(el){new ZoomImage(el,container);});
	thumbnailsFY.each(function(el){new ZoomImage(el,container);});
}

var ZoomImage = Class.create();
ZoomImage.prototype = {
	initialize: function(element,container) {
		var options = Object.extend({
			min_distance_x: 10,
			min_distance_y: 10,
			delta_x: 0,
			delta_y: 0,
			zindex: 999
		}, arguments[2] || {});
		
		this.element = element;
		this.container = container || document.body;
		this.options = options;
		var rel = element.getAttribute('rel');
		if (rel==null)
			return;

		this.imageContainer = document.createElement('div');
		this.imageContainer.className = rel.indexOf('www.mselectro.it') > 0 ? 'box_ZoomElectro': 'box_ZoomFY';
		this.imageContainer.style.display = 'none';

		var objImage = document.createElement("img");
		objImage.setAttribute('src', rel);
		
		
		/*** 3-1-2012 RIDIMENSIONAMENTO IMMAGINI start *****/
				
				objImage.onload = function() {
					if(this.width >= this.height)
							{
								if(this.width > 360)
								{	
									this.height = ( parseInt( ( 360 * this.height ) / this.width ) );
									this.width = "360";		
								}
							}
							else
							{
								if(this.height > 360)
								{	
									this.width = ( parseInt( ( 360 * this.width ) / this.height ) );
									this.height = "360";		
								}		
					}
				};
				
				if(objImage.width >= objImage.height)
				{
					if(objImage.width > 360)
					{	
						objImage.height = ( parseInt( ( 360 * objImage.height ) / objImage.width ) );
						objImage.width = "360";		
					}
				}
				else
				{
					if(objImage.height > 360)
					{	
						objImage.width = ( parseInt( ( 360 * objImage.width ) / objImage.height ) );
						objImage.height = "360";		
					}		
				}
				
		/*** 3-1-2012 RIDIMENSIONAMENTO IMMAGINI end *****/

		this.imageContainer.appendChild(objImage);
		document.body.appendChild(this.imageContainer);

		this.eventMouseOver = this.show.bindAsEventListener(this);
		this.eventMouseOut = this.hide.bindAsEventListener(this);
		this.eventMouseMove  = this.move.bindAsEventListener(this);
		this.eventOnClick  = this.leftClick.bindAsEventListener(this);

		this.registerEvents();
	},

	destroy: function() {
		Event.stopObserving(this.element, "mouseover", this.eventMouseOver);
		Event.stopObserving(this.element, "mouseout", this.eventMouseOut);
		Event.stopObserving(this.element, "mousemove", this.eventMouseMove);
		Event.stopObserving(this.element, "click", this.eventOnClick);
	},

	registerEvents: function() {
		Event.observe(this.element, "mouseover", this.eventMouseOver);
		Event.observe(this.element, "mouseout", this.eventMouseOut);
		Event.observe(this.element, "mousemove", this.eventMouseMove);
		Event.observe(this.element, "click", this.eventOnClick);
	},

	move: function(event){
		Event.stop(event);
		var mouse_x = Event.pointerX(event);
		var mouse_y = Event.pointerY(event);
	
		var dimensions = Element.getDimensions( this.imageContainer );
		var element_width = dimensions.width;
		var element_height = dimensions.height;
		var x = mouse_x + this.options.min_distance_x;
		var y = mouse_y + this.options.min_distance_y;

		if (!Position.within(this.container,x + element_width,y))
			x = (x - element_width) - this.options.min_distance_x;
		if (!Position.within(this.container,x,y + element_height) && Position.within(this.container,x,y - element_height))
			y = (y - element_height) - this.options.min_distance_y;
	
		this.setStyles(x, y);
	},
	
	show: function(event) {
		Event.stop(event);
		this.move(event);
		new Element.show(this.imageContainer);
	},
	
	setStyles: function(x, y){
		// set the right styles to position the tool tip
		Element.setStyle(this.imageContainer, { position:'absolute',
											top:y + this.options.delta_y + "px",
											left:x + this.options.delta_x + "px",
											zindex:this.options.zindex
										});
	},

	hide: function(event){
		new Element.hide(this.imageContainer);
	},
	
	leftClick: function(event){
		new Element.hide(this.imageContainer);
	}
}

function initializeSearchPage(){
	initMenuSearchCategory();
	initZoomImage();
}

Event.observe(window,'load',initializeSearchPage);
