/* WORKAROUND FLASH PER IE (activex) */
function addFlash(path,larghezza,altezza,transp,variabili,name){
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="'+larghezza+'" height="'+altezza+'" id="'+name+'">');
	document.write('<param name="bgcolor" value="#fff">');
	document.write('<param name="movie" value="'+path+'">');
	document.write('<param name="quality" value="high">');
	if(transp=='yes'){ document.write('<param name="wmode" value="transparent">'); };
	if(variabili!=''){ document.write('<param name="flashvars" value="'+variabili+'">'); };
	document.write('<embed src="'+path+'" quality="high" width="'+larghezza+'" height="'+altezza+'" name="'+name+'" type="application/x-shockwave-flash" ');
	if(transp=='yes'){ document.write('wmode="transparent" '); };
	if(variabili!=''){ document.write('flashvars="'+variabili+'" '); };		   
	document.write('pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>');
};

function addFlashBrands(path,larghezza,altezza,transp,variabili,name){
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="'+larghezza+'" height="'+altezza+'" id="'+name+'">');
	document.write('<param name="bgcolor" value="#154D80">');
	document.write('<param name="movie" value="'+path+'">');
	document.write('<param name="quality" value="high">');
	if(variabili!=''){ document.write('<param name="flashvars" value="'+variabili+'">'); };
	document.write('<embed src="'+path+'" quality="high" bgcolor=#154D80 width="'+larghezza+'" height="'+altezza+'" name="'+name+'" type="application/x-shockwave-flash" ');
	if(variabili!=''){ document.write('flashvars="'+variabili+'" '); };		   
	document.write('pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>');
};


function popup(url, opt) {
	window.open(url, opt.title, 'width='+opt.w+', height='+opt.h+', toolbar=0, status=0, resizable=0, location=0, menubar=0');
}

/* Preloader per immagini dello skin */
function imagePreloader() {
	var LoadBox = document.body.appendChild(new Element('div', {id: 'loadBox'}).setStyle({position: 'absolute', top: '-10000px'}));
	var imgs = new Array('/shared/i/radio_off.gif', '/shared/i/radio_on.gif');
	$A(imgs).each(function(i) {	$('loadBox').appendChild(new Element('img', {src: i})); });
}

/* Skin per radio, select, textarea, input-text e input-password */
var Theme = {
	
	init : function(){
		imagePreloader();
		/* cerca e veste le select con class="skin" */
		var selects = new Array();
		//alert($$('select.skin').length);
		for(i=0;i<$$('select.skin').length;i++){
			id = $$('select.skin')[i].identify();
			selects.push(id);
		}
		for(i=0;i<selects.length;i++){
			Theme.skinSelect(selects[i]);
		}
		
		/* cerca e veste input-text e input-password con class="skin" */
		for(i=0;i<$$('input.skin').length;i++){
			if($$('input.skin')[i].type=='text' || $$('input.skin')[i].type=='password'){
				Theme.skinInputText($$('input.skin')[i]);
			}
		}
		
		/* cerca e veste textarea con class="skin" */
		var textareas = new Array();
		for(i=0;i<$$('textarea.skin').length;i++){
			id = $$('textarea.skin')[i].id;
			if(!id){id= $$('textarea.skin')[i].name;
			     $$('textarea.skin')[i].id=id;}
			textareas.push(id);
		}
		for(i=0;i<textareas.length;i++){
			Theme.skinTextarea(textareas[i]);
		}
		
		/* cerca e veste textarea con class="skinNoEdit" */
		var textareasNoEdit = new Array();
		for(i=0;i<$$('textarea.skinNoEdit').length;i++){
			id = $$('textarea.skinNoEdit')[i].id;
			textareasNoEdit.push(id);
		}
		for(i=0;i<textareasNoEdit.length;i++){
			Theme.skinTextareaNoEdit(textareasNoEdit[i]);
		}

		/* cerca e veste le radio (tutte!) */
		var names = new Array();
		for(i=0;i<$$('input').length;i++){ 
			if($$('input')[i].type=='radio'){
				name = $$('input')[i].name;
				names.push(name);
			}
		}
		nameSingle = names.uniq();
		for(i=0;i<nameSingle.length;i++){
			radiox = document.getElementsByName(nameSingle[i])
			arrayTemp = new Array();
			for(x=0;x<radiox.length;x++){ arrayTemp.push($(radiox[x].identify())); }
			Theme.skinRadio(nameSingle[i],arrayTemp);
		}		
	},

	skinSelect: function(objId) {
		var sel = $(objId);
		w = sel.getWidth();
		if(w<40){wOk = 40;}else{wOk = w+5;}
		wCont = wOk;
		wSkinnedSelectDropdown = wOk-3;
		wA = wOk-19;
		wInput = wOk-34;
		var selContainer = new Element('div', {id: objId + '_container',style:'width:'+wCont+'px;', className:'selContainer', position: 'relative'});
		sel.wrap(selContainer);
		$(objId + '_container').setStyle({position: 'relative'});
		var selValue = new Element('input', {type:'hidden', name: sel.name, id:objId + '_value', value:sel.options[sel.selectedIndex].value});
		var selLabel = new Element('input', {type:'text', className:'inputText',style:'width:'+wInput+'px;', id:objId + '_label', value:sel.options[sel.selectedIndex].text});
		var selDropdown = new Element('ul', {id: objId + '_dropDown', className:'skinnedSelectDropdown', style:'width:'+wSkinnedSelectDropdown+'px;'});
		var selDropdownCtrl = new Element('img', {id: objId + '_dropDownCtrl', src:'/shared/i/sel_btn.gif', className:'skinnedSelectDropdownCtrl'});
		Element.hide(selDropdown);
		selContainer.appendChild(selValue);
		selContainer.appendChild(selLabel);
		selContainer.appendChild(selDropdown);
		selContainer.appendChild(selDropdownCtrl);
		var fn = eval(sel.onchange)
		selValue.onchange = eval(sel.onchange);
		selDropdownCtrl.onclick = function() {
			if(selDropdown.getStyle('display') == 'block') {
				Element.hide(selDropdown);
				Element.removeClassName(selContainer, 'on');
			} else {
				Element.show(selDropdown);
				Element.addClassName(selContainer, 'on');
				new PeriodicalExecuter(function(pe) {
					Event.observe(document.body, 'click', Theme.closeDropdown);
					Theme.openedDropdown = selDropdown;
					pe.stop();
				});
			}
			for(i=0; i<$$('.selContainer').length; i++){
				if($$('.selContainer')[i]==$(objId + '_container')){
					$(objId + '_container').style.zIndex = 999;
				}else{
					$$('.selContainer')[i].style.zIndex = 1;
				}
			}
		}
		$A(sel.options).each(function(obj){
			selDropdown.appendChild(
				Builder.node('li',
					Builder.node('a', {
								 href: 'javascript://', style: 'cursor: pointer; width:'+wA+'px;', 
								 value: obj.value, name: sel.name, 
								 fieldId: sel.id, onclick: 'Theme.selectItem(this)'}, obj.text
					)
				)
			);
		});
		Element.remove(sel);
	},
	
	closeDropdown: function(event) {
		Element.hide(Theme.openedDropdown);
		Theme.openedDropdown = false;
		Event.stopObserving(document.body, 'click', Theme.closeDropdown);
	},
								
	selectItem: function(obj) {
		var fieldName = obj.getAttribute('name');
		var fieldId = obj.getAttribute('fieldId');
		Element.hide(fieldId + '_dropDown');
		$(fieldId + '_label').value = obj.innerHTML;
		$(fieldId + '_value').value = obj.getAttribute('value');
		if($(fieldId + '_value').onchange != null) $(fieldId + '_value').onchange();
	},
	

	skinRadio: function(groupName, radios) {// Group name, array(elem, elem, elem, ...., elem)
		var radioGroupValue = Builder.node('input', {type: 'hidden', name: groupName, id: groupName + '_value'});
		radios[0].parentNode.appendChild(radioGroupValue);
		
		$A(radios).each(function(radio){
			var newRadio = Builder.node('div', {className: 'skinnedRadioOff'}, [
				Builder.node('a', {href: 'javascript://', name: radio.name, value: radio.value, id: radio.id + '_btn'}, [
					Builder.node('img', {src: '/shared/i/radio_off.gif'})
				])
			]);
			
			Element.identify(newRadio);
			radio.wrap(newRadio);			 
			
			$(radio.id + '_btn').onclick = function() {
				if(Theme[groupName] != this) {
					this.firstChild.src = '/shared/i/radio_on.gif';
					if(Theme[groupName]) Theme[groupName].firstChild.src = '/shared/i/radio_off.gif';
					Theme[groupName] = this;
					$(this.getAttribute('name') + '_value').value = this.getAttribute('value');
				}
			}
			if(radio.checked) $(radio.id + '_btn').onclick();
			
			Element.remove(radio);
		});
	},
	
	skinTextarea: function(objId) {	
		
		txtArea = $(objId);
		var txtAreaCont = new Element('div', {id: objId + '_txtAreaCont', className:'txtAreaCont'});
		//var divHandle = new Element('div', {id: objId + '_handle', className:'handle'});
		//var divSlider = new Element('div', {id: objId + '_slider', className:'slider'});
		//var divText = new Element('div', {id: objId + '_text', className:'text'});
		//txtAreaCont.appendChild(divText);		
		//divSlider.appendChild(divHandle);
		//txtAreaCont.appendChild(divSlider);
		new Insertion.Before(txtArea,txtAreaCont);
		txtAreaCont.appendChild(txtArea);
		//$(objId + '_text').insert({bottom: '<p>'+txtArea.value+'</p>'});
		//Element.remove(txtArea);
		//var slider = new Control.Slider(objId + '_handle', objId + '_slider', {axis:'vertical'});
		/*slider.options.onSlide = function(value) {
		  hCont = $(objId + '_text').getDimensions().height;
		  hText = $(objId + '_text').down().getDimensions().height;
		  maxPos = hText-hCont;
		  $(objId + '_text').down().style.top = '-'+Math.round((value*maxPos))+'px';
		};*/
	},
		
	skinTextareaNoEdit: function(objId) {
		txtArea = $(objId);
		var txtAreaCont = new Element('div', {id: objId + '_txtAreaCont', className:'txtAreaCont'});
		var divHandle = new Element('div', {id: objId + '_handle', className:'handle'});
		var divSlider = new Element('div', {id: objId + '_slider', className:'slider'});
		var divText = new Element('div', {id: objId + '_text', className:'text'});
		txtAreaCont.appendChild(divText);		
		divSlider.appendChild(divHandle);
		txtAreaCont.appendChild(divSlider);
		new Insertion.Before(txtArea,txtAreaCont);
		$(objId + '_text').insert({bottom: '<p>'+txtArea.value+'</p>'});
		Element.remove(txtArea);
		var slider = new Control.Slider(objId + '_handle', objId + '_slider', {axis:'vertical'});
		slider.options.onSlide = function(value) {
		  hCont = $(objId + '_text').getDimensions().height;
		  hText = $(objId + '_text').down().getDimensions().height;
		  maxPos = hText-hCont;
		  $(objId + '_text').down().style.top = '-'+Math.round((value*maxPos))+'px';
		};
	},
	
	skinInputText :function(e){
		inputText = $(e);
		var div = new Element('div', { 'class': 'inpt' });
		var div2 = new Element('div', { 'class': 'inpt' });
		inputText.wrap(div2);
		div2.wrap(div)
	}
}


/* boxSlide in home page */
var boxSlide = {
	init : function(){			
		mov = false;
		ul = $('boxSlide').down().next(2).down();
		li = ul.childElements();
		heightBanner = ul.down(3).getDimensions().height;
		maxPos = (li.length*heightBanner)-heightBanner;
		$('boxSlide').getElementsByClassName('top')[0].onclick = function(){ boxSlide.premiTop(); }
		$('boxSlide').getElementsByClassName('bot')[0].onclick = function(){ boxSlide.premiBot(); }
		new PeriodicalExecuter(function(){ if(ul.style.top.split('px')[0] > -maxPos){ boxSlide.premiBot(); }else{ boxSlide.gotoTop(); } }, 5);
	},
	gotoTop : function(){
		new Effect.Move(ul, { x: 0, y: 0, mode: 'absolute', duration:.5,afterFinishInternal:function(){mov=false;}});
	},
	premiTop : function(){
		if(mov==false){
			mov = true;
			if(ul.style.top.split('px')[0] < 0){
				new Effect.Move(ul, { x: 0, y: +heightBanner, mode: 'relative', duration:.5, afterFinishInternal:function(){mov = false;}});
			}else{
				mov = false;
			}
		}
	},
	premiBot : function(){
		if(mov==false){
			mov = true;
			if(ul.style.top.split('px')[0] > -maxPos){
				new Effect.Move(ul, { x: 0, y: -heightBanner, mode: 'relative', duration:.5, afterFinishInternal:function(){mov = false;}});
			}else{
				boxSlide.gotoTop();	
			}
		}
	}	
};


/* shared on internal pages */
var actShare ='';
var shareServices = {
	init : function(){
		ul = $('aggiornamento').down('.shareDiv').down('ul');
		li = ul.childElements();
		for (i=0; i<li.length ;i++) {
		div = li[i].down('.shareLink');
		a = li[i].down('a');
		tip = li[i].down('a').getAttribute('title');
		a.onmouseover = function(){
		if (actShare) {clearInterval(autoChiudi);shareServices.hideAct();}
		shareServices.show(this);
		}
		}
		},
	show: function(elem) {
		actShare = elem.up('.shareLink').down('.shareTip');
		cont = elem.up('.shareLink').down('.shareTip');
		cont.innerHTML = '<a href="'+elem.up('.shareLink').down('a').getAttribute('href')+'">'+elem.up('.shareLink').down('a').getAttribute('title')+'</a>';
		cont.style.display = 'block';
		autoChiudi = setInterval(shareServices.hideAct, 3000);
		},
	hideAct: function() {
		actShare.style.display = 'none'
		}
}


/*window.onload = function(){
	Theme.init();
}*/

document.observe("dom:loaded", function() {
/* carico il tema per radio, select, textarea, input-text e input-password aventi class="skin" */
	Theme.init();

});


addBookmark  = function(){	
	title = document.title;
	url = window.location;	
	if (window.sidebar) { // Firefox
		window.sidebar.addPanel(title, url, '');
	}else{
		if (window.opera){ //Opera
				var a = document.createElement("A");
				a.rel = "sidebar";
				a.target = "_search";
				a.title = title;
				a.href = url;
				a.click();
		}else{
			if (document.all) { //IE
				window.external.AddFavorite(url, title);
			}
		}
	}
}

jQuery(document).ready(function() {
	jQuery("#searchText").focus(function() {
  		jQuery("#searchText").val("");
	})
});


