var browserUserAgent = navigator.userAgent.toLowerCase();
var operaBrowser = (browserUserAgent.indexOf("opera") != -1);
var ieBrowser = (browserUserAgent.indexOf("msie") != -1) && (document.all) && (!operaBrowser);
var ieBrowser6 = (ieBrowser) && (browserUserAgent.indexOf("msie 6") != -1);
var _query='*';

function replaceStr (str, searchStr, replaceStr) {
	if (str) {
		var re = new RegExp(searchStr, "g");
		return str.replace(re, replaceStr);
	}
}
function add_event (elm, evType, fn, useCapture) {	
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;		
	} else {
		elm ['on' + evType] = fn;
	}	
}
function get_element(id) {  
	if (document.getElementById)
		return document.getElementById(id);
	else if (document.all)
		return document.all[id];
	else if (document.layers)
		return document.layers[id];
}
function _get_element(obj,id,type) {
	if (type == null)
		type = "div";
	if (obj.getElementsByTagName) {
		var divs = obj.getElementsByTagName(type);
		for (var n=0; n < divs.length; ++n) {
			if (divs[n].id==id)
				return divs[n];
		}
	}
}
function findHTML(node) {
	if (node.parentElement) {
		if (node.parentElement.tagName=="HTML")
			return node.parentElement;
		else
			return findHTML(node.parentElement);
	} else {
		if (node.parentNode.tagName=="HTML")
			return node.parentNode;
		else
			return findHTML(node.parentNode);
	}
}

function imgLoad (img) {
	var img = get_element(img);
	//
	if (img) {
		add_event (img, 'mouseover', rollerOver, false);
		add_event (img, 'mouseout', rollerOut, false);
	}
	
	var _width = img.width;
	var _height = img.height;
	if  ((_width == 0) || (_height == 0))
		return;
	if  ((_width == 1) || (_height == 1)) {
		img.src = remotedir+"/eurocat/resources/replace-eurocat.jpg";
		return;
	}
	//alert(_width+"  "+_height);
	/*if (_width > _height) {
		var ratio = 100 / _width;
		_width = 100;
		_height = _height * ratio;
	} else if (_height < 100) {
		var ratio = 100 / _height;
		_height = 100;
		_width = _width * ratio;
	}*/
	if (_width < _height && _width >=100) {
		var ratio = 100 / _width;
		_width = 100;
		_height = _height * ratio;
	} else if(_width >= _height && _height >=100) {
		var ratio = 100 / _height;
		_height = 100;
		_width = _width * ratio;
	}
	img.width = _width;
	img.height = _height;
}
function changeStyleBlock(elem) {
	var el = get_element(elem);
	if(el){
		el.style.display = (el.style.display == 'block' ? 'none' : 'block' );
		return true;
	}
}
function showSummary(suffixID) {
	var _content = get_element("content_"+suffixID);
	if (_content) {
		var temp_display = _content.style.display;
		if (temp_display.indexOf("none") != -1) {
			var _summary = get_element("summary_"+suffixID);
			var _openID = get_element("open_"+suffixID);
			var _closeID = get_element("close_"+suffixID);
			
			if (_openID) _openID.src = remotedir+"/eurocat/resources/minus.png";
			if (_closeID) _closeID.style.display = "";
			if (_summary) _summary.style.display = "none";
			if (_content) _content.style.display = "";
		} else {
			var _summary = get_element("summary_"+suffixID);
			var _openID = get_element("open_"+suffixID);
			var _closeID = get_element("close_"+suffixID);
			
			if (_openID) _openID.src = remotedir+"/eurocat/resources/plus.png";
			if (_closeID) _closeID.style.display = "none";
			if (_summary) _summary.style.display = "";
			if (_content) _content.style.display = "none";	
		}
	}
}
/*
function showtags(target,suffix) {
	var targetBox = get_element(target);
	if (targetBox) {
		var entity_id = _get_element(targetBox,"entity_"+target+parseInt(suffix));
		var entityh4_id = get_element("entityh4_"+target+parseInt(suffix));
		var plus_id = get_element("plus_"+target+parseInt(suffix));
		if (entity_id){
			var contenutodisplay = entity_id.style.display;
			
			if(contenutodisplay.indexOf("none")!= -1) {
				entity_id.style.display = "block";
				if(entityh4_id != null) entityh4_id.style.display = "none";
				if(plus_id.src.indexOf("plus.gif") != -1) {
					plus_id.src = remotedir+"/europages/resources/minus.gif";}
				if(plus_id.src.indexOf("plus_g.jpg") != -1) {
					plus_id.src = remotedir+"/europages/resources/minus_g.jpg";}
			}
			if (contenutodisplay.indexOf("block")!= -1){
				entity_id.style.display = "none";
				if(entityh4_id != null) entityh4_id.style.display = ""; 
				if(plus_id.src.indexOf("minus.gif") != -1) {
					plus_id.src = remotedir+"/europages/resources/plus.gif";}
				if(plus_id.src.indexOf("minus_g.jpg") != -1) {
					plus_id.src = remotedir+"/europages/resources/plus_g.jpg";}
					
			}
		}
	}
}
*/
function search(e) {
	var searchinput = get_element("searchinput");
	
	if (searchinput) {
		if ((searchinput.value != null) && (searchinput.value.length > 0)) {
			if (ieBrowser6) {
				e.returnValue=false;
				e.cancel = true;		
			}	
			//alert ("Hai cercato "+searchinput.value);
			var string_temp = searchinput.value;
			string_temp = string_temp.replace(" ","+");
			//string_temp = replaceStr(string_temp, " ", "+");
			//alert(string_temp);
			//string_temp = escape(string_temp);
			string_temp = string_temp.replace('+', '%2B');
			string_temp = string_temp.replace('%20', '+');
			string_temp = string_temp.replace('*', '%2A');
			string_temp = string_temp.replace('/', '%2F');
			string_temp = string_temp.replace('@', '%40');
			//alert(string_temp);
			location.href = remoteURL+"/"+urlricerca+"/"+string_temp+".html";
		} else {
			alert ("Inserire il testo da ricercare");
			searchinput.focus();
		}
	}
}
function searchinput_press(event) {
	if (document.all){
  		if (event.keyCode == 13){
		   event.returnValue=false;
		   event.cancel = true;
		   search(event);
  		} 
	} else if (document.getElementById){
  		if (event.which == 13) {
   			event.returnValue=false;
   			event.cancel = true;
		   	search(event);
  		} 
 	} else if(document.layers) {
	  	if (event.which == 13) {
   			event.returnValue=false;
   			event.cancel = true;
		   	search(event);
   		} 
 	} 
}
function search_bott(e) {
	var searchinput = get_element("searchinput_bottom");	
	if (searchinput) {
		if ((searchinput.value != null) && (searchinput.value.length > 0)) {
			if (ieBrowser6) {
				e.returnValue=false;
				e.cancel = true;		
			}	
			//alert ("Hai cercato "+searchinput.value);
			var string_temp = searchinput.value;
			//string_temp = string_temp.replace(" ","+");
			string_temp = replaceStr(string_temp, " ", "+");
			//string_temp = escape(string_temp);
			string_temp = string_temp.replace('+', '%2B');
			string_temp = string_temp.replace('%20', '+');
			string_temp = string_temp.replace('*', '%2A');
			string_temp = string_temp.replace('/', '%2F');
			string_temp = string_temp.replace('@', '%40');
			location.href = remoteURL+"/"+urlricerca+"/"+string_temp+".html";
		} else {
			alert ("Inserire il testo da ricercare");
			searchinput.focus();
		}
	}
}
function searchinput_press_bott(event) {
	if (document.all){
  		if (event.keyCode == 13){
		   event.returnValue=false;
		   event.cancel = true;
		   search_bott(event);
  		} 
	} else if (document.getElementById){
  		if (event.which == 13) {
   			event.returnValue=false;
   			event.cancel = true;
		   	search_bott(event);
  		} 
 	} else if(document.layers) {
	  	if (event.which == 13) {
   			event.returnValue=false;
   			event.cancel = true;
		   	search_bott(event);
   		} 
 	} 
}

function onloadUltimiProdotti() {
	var queryObj = get_element("searchinput");
	if (queryObj) {
		if ((_query != null) && (_query != '') && (_query != '*')) {
			queryObj.value = _query;
			queryObj.select();
		}
	}
	var fun = (function () {
		var searchButton = get_element("searchButton");		
		if (searchButton) 
			add_event (searchButton, 'click', search, false);
		var searchinput = get_element("searchinput");		
		if (searchinput) {
			add_event (searchinput, 'keydown', searchinput_press, false);
			searchinput.focus();
		}
	});
	setTimeout(fun,100);
}
function onloadResultsProdotti() {
	var queryObj = get_element("searchinput");
	if (queryObj) {
		if ((_query != null) && (_query != '') && (_query != '*')) {
			queryObj.value = _query;
			queryObj.select();
		}
	}
	var fun = (function () {
		var searchButton = get_element("searchButton");		
		if (searchButton) 
			add_event (searchButton, 'click', search, false);
		var searchinput = get_element("searchinput");		
		if (searchinput) {
			add_event (searchinput, 'keydown', searchinput_press, false);
			searchinput.focus();
		}
		
		var searchButton = get_element("searchButton_bottom");		
		if (searchButton) 
			add_event (searchButton, 'click', search_bott, false);
		var searchinput = get_element("searchinput_bottom");		
		if (searchinput) {
			add_event (searchinput, 'keydown', searchinput_press_bott, false);
		}
	});
	setTimeout(fun,100);
}

function getY(element) {
	var y = 0;
	
	for (var e = element; e; e = e.offsetParent)
		y += e.offsetTop;
		
	for (e = element.parentNode; e && e != document.body; e = e.parentNode)
		if (e.scrollTop) y -= e.scrollTop;
		
	return y;
}
function getX(e) {
	var x = 0;
	
	while(e) {
		x += e.offsetLeft;
		e = e.offsetParent;
	}
	
	return x;
}

function rollerOver(e) {
	closeModalWindow();
	var _target = e.currentTarget ? e.currentTarget : e.srcElement;
	var fun = (function () {
		var _img = _target; 
		//alert ("Sono rimasto 1 sec sul immagine");
		
		var _width = _img.naturalWidth;
		var _height = _img.naturalHeight;
		if (_width) {} else {
			_width = _img.width;
			_height = _img.height;
		}
		var _x, _y;
		_x = _img.offsetLeft;
		_y = _img.offsetTop;
		if ( (_x == 0) && (_y == 0) ) {
			_x = getX(_img);
			_y = getY(_img);
		}
		/*
		_x = _img.offsetLeft;
		_y = _img.offsetTop;
		(_x == 0)
			_x = _img.offsetLeft+ _img.parentNode.offsetLeft;
			_y = _img.offsetTop + _img.parentNode.offsetTop;
		}
		*/
		
		if (_width > _height) {
			var ratio = 300 / _width;
			_width = 300;
			_height = _height * ratio;
		} else if(_width <= _height) {
			var ratio = 300 / _height;
			_height = 300;
			_width = _width * ratio;
		}
		/*
		_img.width = _width;
		_img.height = _height;
		*/
		openModalWindow(_width, _height, _x, _y, _img.src);
	});
	idTimeout = setTimeout(fun,800);
}
function rollerOut(event) {
	clearInterval(idTimeout);
	//closeModalWindow();
}


function openModalWindow(width,height,coordX,coordY,urlImg) {
	var _width = width;
	var _height= height;
	width = width + 10;
	height= height+ 10;
	
	// calcolo coordinate posizionamento
	x = coordX;
	y = coordY;

	var divMW = document.createElement("div");
	divMW.id="modalWindow";
	divMW.style.position="absolute";
	divMW.style.left=x+"px";
	divMW.style.top=y+"px";
	divMW.style.height=height+"px";
	divMW.style.width=width+"px";
	divMW.style.padding="5px";
	divMW.style.zIndex="2001";
	divMW.style.backgroundColor="#FFFFFF";
	divMW.style.border="1px solid #C5C5C5";
	//div.stylebackgroundColor="transparent";
	document.body.appendChild(divMW);
	
	innerHTML = '<img id="imgOver" onMouseOut="javascript:closeModalWindow();" style="padding:5px;" width="'+_width+'" height="'+_height+'" src="'+urlImg+'"/>';
	
	divMW.innerHTML = innerHTML;
}
function closeModalWindow() {
	var htmlNode;
	if (document && document.body) {
		htmlNode = findHTML(document.body);
		if (htmlNode)
			htmlNode.style.overflow="auto";
	}
	var div = get_element('modalWindow');
	if (div)
		div.parentNode.removeChild (div);
}