window.addEvent('domready', function() {

	trovatutto.init();

	if($('txtric')) {
		$('txtric').addEvent('focus', function(){
			this.value = '';	
		});	
		$('txtric').addEvent('blur', function(){
			this.value = 'Inserisci qui quello che vuoi cercare';	
		});	
				
		$$('.lancioest').addEvents({ 
			mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
				this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});
	};	
	
	if($('focus')) { //homepage
		var fx3 = new Fx.Tween($$('#focustitle h3 a')[0]);
		var fx4 = new Fx.Tween($$('#focustitle h4 a')[0]);
		var lanciopri = [$$('#focustitle h4')[0], $$('#focustitle h3')[0], $$('#focustitle p')[0], $$('#lancioprifoc img')[0]];
		lanciopri.each(function(element) {
			element.addEvents({ 
				mouseenter: function(){
					$$('#focustitle h3 a')[0].morph({'background-color': '#000000'});
					$$('#focustitle h4 a')[0].morph({'background-color': '#000000'});

				},
				mouseleave: function(){
	           		$$('#focustitle h3 a')[0].morph({'background-color': '#ED008C'});
					$$('#focustitle h4 a')[0].morph({'background-color': '#ED008C'});
				}
			});
		});
		
	
	
		$$('.lanciosecfoc').addEvents({ 
			mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});
		
		if($('cosmogirl')) {
			$('cosmogirl').addEvents({ 
				mouseenter: function(){
					$$('#cosmogirl h4 a')[0].morph({'background-color': '#ED008C'});
				},
				mouseleave: function(){
	           		$$('#cosmogirl h4 a')[0].morph({'background-color': '#000000'});
				}
			});
			
			$('dressup').addEvents({ 
				mouseenter: function(){
					$$('#dressup h4 a')[0].morph({'background-color': '#ED008C'});
				},
				mouseleave: function(){
	           		$$('#dressup h4 a')[0].morph({'background-color': '#000000'});
				}
			});
		}
			
			
		$$('.lanciosecdoppio').addEvents({ 
			mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});	
			
		$$('.lanciosec').addEvents({ 
			mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});	
		
		var lanciopri = [$('partecipa'), $$('#sondaggiolancio p a')[0]];
		lanciopri.each(function(element) {
			element.addEvent('mouseenter', function(){
				$$('#sondaggiolancio p a')[0].setStyle('color','#000000');
			});		
			element.addEvent('mouseleave', function(){
				$$('#sondaggiolancio p a')[0].setStyle('color','#ffffff');
			});	
		});
	
	};		


	if($('lancihomesez') || $('lanciodettaglio') || $('lanciarchivio')) {
	
		$$('.lancioarchivio').addEvents({ 
			 mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function() {
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});
	};
	
	if($('risricerca')) {
	
		$$('.lancioris').addEvents({ 
			 mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function() {
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});
	};
	
	
	
	if($('lancihomesez')) {
		$$('.lanciohsdoppio').addEvents({ 
			mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});			
		
		$$('.lanciohs').addEvents({ 
			mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});	
		
	}
	
	
	if($('leggianche')) {		
		var myTips = new Tips('#leggianche li a', {
			fixed: true
		});
		
		myTips.addEvent('show', function(tip){
		    tip.fade('in');
		}); 
	
		//fg.initfotopri();
	
		$$('.lancioarchivio').addEvents({ 
			 mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});
		
		if($('galleryorzmenu')) {
			fg.init('test');
		};
	};
	
	if($('altrisegni')) {
		$$('.segnotitle').addEvents({ 
			 mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});
	
	};
	
	
	if($('sezionisito')) {
		$$('.sezione').addEvents({ 
			 mouseenter: function(){
				this.getElements('a')[0].morph({'background-color': '#ED008C'});
			},
			mouseleave: function(){
           		this.getElements('a')[0].morph({'background-color': '#000000'});
			}
		});
	
	};
	
	if($('lanciapplicazioni')) {	
		$$('.lanciosecdoppio').addEvents({ 
				mouseenter: function(){
					this.getElements('a')[0].morph({'background-color': '#ED008C'});
				},
				mouseleave: function(){
	           		this.getElements('a')[0].morph({'background-color': '#000000'});
				}
		});	
		$$('cosmogirlhc li').addEvents({ 
				mouseenter: function(){
					this.getElements('a')[1].morph({'background-color': '#ED008C'});
				},
				mouseleave: function(){
	           		this.getElements('a')[1].morph({'background-color': '#000000'});
				}
		});		
			
			
	};
	
	if($('altrisondaggi') || $('altrisondaggiarchivio')) {	
		$$('.lanciosondaggio').addEvents({ 
				mouseenter: function(){
					this.getElements('a')[0].morph({'background-color': '#ED008C'});
				},
				mouseleave: function(){
	           		this.getElements('a')[0].morph({'background-color': '#000000'});
				}
		});			
	};
	
	
})


var fg	= {
	imgpos: 0,
	t: 0,
	galleryname: null,
	init: function(name) {
			fg.galleryname = name;
			
			var menu = $$('#galleryorzmenu ul li');

			menu.getElements('a')[0].addClass('currphoto');

			$('prec').setStyle('display', 'none');
			
			if(menu.length == '1') {
				$('succ').setStyle('display', 'none');
			};
			
			$('prec').addEvent('click', function(e) {
				e.stop();
				fg.precfotopri();
				$('succ').setStyle('display', 'block');
			});
			
			$('succ').addEvent('click', function(e) {
				e.stop();
				fg.succfotopri();
				$('prec').setStyle('display', 'block');
			});
			
			var url = '/cosmopolitan/images/gallery/' + fg.galleryname + '/foto_1.jpg';
					
			var imgnew = new Element('img', {id: 'myFirstElement', src: url});
			imgnew.inject($$('#scroller div')[0]);
			
			
			menu.each(function(item) {
				item.addEvent('click', function(e) {
					e.stop();			
				
					var url = '/cosmopolitan/images/gallery/' + fg.galleryname + '/foto_'+ (menu.indexOf(item)+1) + '.jpg';

					var imgnew = new Element('img', {id: 'myFirstElement', src: url});
					
					alert('corrente =' + fg.imgpos);
					alert('cliccato =' + menu.indexOf(item));
					
					if(menu.indexOf(item) > fg.imgpos) {	
						if($$('#scroller img').length == 1) {
							imgnew.inject($$('#scroller div')[1]);
							var myFx = new Fx.Scroll('imgdett', {
								offset: {
									'x': 680,
									'y': 0
								}
							}).toTop();
						} else {
							//$$('#scroller img')[0].destroy();
							imgnew.inject($$('#scroller div')[2]);
							var myFx = new Fx.Scroll('imgdett', {
								offset: {
									'x': 680,
									'y': 0
								}
							}).toTop();
						}
						
					};

					if(menu.indexOf(item) < fg.imgpos) {
						
						imgnew.inject($('scroller'), 'top');
											
						var myFx = new Fx.Scroll('imgdett', {
							offset: {
								'x': -680,
								'y': 0
							}
						}).toTop();
					};
					
					if(menu.indexOf(item) == fg.imgpos) {
						return
					};
				
					
					
					menu.each(function(item) {
						item.getElements('a')[0].className = ' ';
					});
					item.getElements('a')[0].className = ' ';
										
				    this.getElements('a')[0].className ='currphoto';
					
					fg.imgpos = menu.indexOf(item);
					
					if(fg.imgpos == menu.length-1) {
						$('succ').setStyle('display', 'none');
					} else {
						$('succ').setStyle('display', 'block');
					};
					
					if(fg.imgpos == 0) {
						$('prec').setStyle('display', 'none');
					} else {
						$('prec').setStyle('display', 'block');
					};
										
					
					
				});
				return false;
			}.bind(this)); 
			
	},
	succfotopri: function(e) {
		

		
		var menulnk = $$('#galleryorzmenu ul li a');
		
		menulnk.each(function(item) {
			item.className = ' ';
		});
		
		if(fg.imgpos == menulnk.length-2) {
			$('succ').setStyle('display', 'none');
		};
				
		menulnk[fg.imgpos+1].className = 'currphoto';
		fg.imgpos++;	
		
		var url = '/cosmopolitan/images/gallery/' + fg.galleryname + '/foto_'+ ((fg.imgpos)+1) + '.jpg';
		
		var imgnew = new Element('img', {id: 'myFirstElement', src: url});
		
		imgnew.inject($('scroller'));

		var myFx = new Fx.Scroll('imgdett', {
		    offset: {
		        'x': 680,
		        'y': 0
		    }
		}).toTop();
		
		
		//$$('#imgdett img')[0].morph({'left': '-680px'});
		
		

			
	},
	precfotopri: function() {
		var menulnk = $$('#galleryorzmenu ul li a');
		
		menulnk.each(function(item) {
			item.className = ' ';
		});
		
		if(fg.imgpos == 1) {
			$('prec').setStyle('display', 'none');
		};
				
		menulnk[fg.imgpos-1].className = 'currphoto';
		fg.imgpos--;
				
		var url = '/cosmopolitan/images/gallery/' + fg.galleryname + '/foto_'+ ((fg.imgpos)-1) + '.jpg';
		
		var imgnew = new Element('img', {id: 'myFirstElement', src: url});
		
		imgnew.inject($('imgdett'), 'top');

		var myFx = new Fx.Scroll('imgdett', {
		    offset: {
		        'x': -680,
		        'y': 0
		    }
		}).toTop();
		
		//$$('#imgdett img')[0].morph({'left': '-680px'});
		
	}
}




var trovatutto	= {
	imgpos: 0,
	t: 0,
	myTimer: null,
	galleryname: null,
	init: function(name) {
		$('trovamenu').addEvents({ 
			mouseenter: function(){
				$clear(trovatutto.myTimer);
				trovatutto.menuon();
			}		
		});
		$('menutrovatutto').addEvents({ 
			mouseenter: function(){
				$clear(trovatutto.myTimer);
			},	
			mouseleave: function(){
				trovatutto.myTimer = trovatutto.menuoff.delay(1000);	
			}
		});	
	},
	menuon: function(e) {
	
		$('menutrovatutto').setStyle('top', '0px');
		
		var newfx = new Fx.Morph('fillermenu', {duration: 500, onComplete:function() {
				$$('#menutrovatutto li#trovaamore a')[0].setStyle('margin-top', '0');  
			}
		});
		
		newfx.start({
			'width': 810   //Morphs the width from the current to 300px.
		});
	},
	menuoff: function(e) {
		$$('#menutrovatutto li#trovaamore a')[0].setStyle('margin-top', '-100px')
		var newfx2 = new Fx.Morph('fillermenu', {duration: 500, onComplete:function() {
				$('menutrovatutto').setStyle('top', '-900px');  
			}
		});
		
		newfx2.start({
			'width': 0   //Morphs the width from the current to 300px.
		});
		$clear(trovatutto.myTimer);
	},
	closeTimer: function(){
		this.hideEm = function(){this.closeBox()}.bind(this);
		this.delay = this.hideEm.delay(this.options.boxTimer);
	}	
}

