// JavaScript Document
var ind=0;
var imgs_to_fade = null;
var pausa = false;
var intervalReference = null;
var retrieverIntervalReference = null;

var xCord;
var yCord;
var tmpContent = null;
var tooltip = null;

//costanti di configurazione
var waitTime = 6500;
var retrieveWaitTime = 5000;
var jsonImages = null;
var pointerJsonImages = 3;
var numberOfImagesToAddToDOM = 3;

function idbody(){
 var div = document.getElementsByTagName("body")[0].getAttribute("id");
 return div;
}

function classbody(){
 var el = document.getElementsByTagName("body")[0].className;
 return el;
}

function langhtml(){
 var el = document.getElementsByTagName("html")[0].lang;
 return el;
}

function transition(effect, div){
	document.getElementById(div).style.display = "inline";	
	if (effect == 'Fade'){
		Effect.Fade(div);
	}
}

function externalLinks() {
	var links = $$("#main_text a");
	links.each(function (link) {
		if ( link.getAttribute("rel")== "external" )
			link.target = "_blank";
	});   
 }
 
function swapFlashImages() {
	Effect.Fade(imgs_to_fade[ind], { duration:3, from:1.0, to:0.0 });
	if (tmpContent!=null)
		reAddTitle();
	
	ind++;	
	if (ind == jsonImages.length)
		ind = 0;		
    
	Effect.Appear(imgs_to_fade[ind], { duration:3, from:0.0, to:1.0 });

	//infine aggiorno anche il contenuto del tooltip
	updateTooltipContent();			
}

function togglePause() {
	var togglePause = $('togglePause');
	if (pausa == true) {
		intervalReference = setInterval('swapFlashImages()', waitTime);
		togglePause.writeAttribute("src", "img/cogliati_stop.gif");
	}
	else {
		if (intervalReference != null)
			clearInterval(intervalReference);
		togglePause.writeAttribute("src", "img/cogliati_play.gif");			
	}
	
	pausa = !pausa;	
}

function createTooltipHtml() {
	tooltip = new Element("div", {
		className: "tooltip",
		id: "tooltip",
		style: "top: 300px; left:600px"
	});
	Element.insert($(document.body),tooltip);
	updateTooltipContent();
	//anche riuscendo a posizionare il mouse sopra il tooltip viene comq eseguito il play/stop
	tooltip.observe('click', togglePause );
}

function updateTooltipContent() {
	var thisimage = imgs_to_fade[ind];

	var content = thisimage.title.stripScripts().strip();
	//pulisco il tag title per nn mostrarlo
	thisimage.title = "";
	  
	tooltip.hide().update(content);
	Effect.Appear(tooltip, { duration:3, from:0.0, to:1.0 });
	
	//salvo il contenuto del title
	tmpContent = content;	
}

function reAddTitle() {
		imgs_to_fade[ind].title = tmpContent;
		tmpContent = "";
}

function followTheMouse(e) {
	xCord = Event.pointerX(e)+15;
	yCord = Event.pointerY(e)-20;
	
	/*if (xCord >= 925)
		xCord = 925;*/

	tooltip.setStyle({
		top: yCord+"px",
		left: xCord+"px"						
	})
}

function subNavHover() {
	var lis = $$("#navmenu-h li");
	lis.each( function (li) {
		li.onmouseover=function() {
			li.addClassName("iehover");
		};
		li.onmouseout=function() {
			li.removeClassName("iehover");
		};
	});	
}

function writeToDOM(howmany) {

	var where = $$("#main_text p.flash")[0];
	var imgsToWrite = jsonImages.clone().splice(0,howmany);
	var tmpImg;	
	
	imgsToWrite.each(function (img) {
		tmpImg = new Element("img", {
			src		: img.src,
			title		: img.title
		});
		tmpImg.setStyle({display:"none"});			
			
		Element.insert(where,tmpImg);
	})

}

function appendImageToDOM() {
	
	if (pointerJsonImages == jsonImages.length) {
		clearInterval(retrieverIntervalReference);
		return;		
	}
	
	var img = jsonImages[pointerJsonImages];	
	var where = $$("#main_text p.flash")[0];		
	
	tmpImg = new Element("img", {
		src		: img.src,
		title		: img.title
	});
	tmpImg.setStyle({display:"none"});			
		
	Element.insert(where,tmpImg);	

	imgs_to_fade = $$("#main_text p.flash img");	
	pointerJsonImages++;
}

function toggleTooltip() {
	element.onmouseover = function() {
			$('tooltip').hide();
	}
	element.onmouseout = function() {
			$('tooltip').show();					
	}	
}
		
var myrules = {

        'a#slfi' : function(element) {
			var thisIdBody = idbody();
				if (thisIdBody != 'filo') {
					element.onmouseover = function() {
							$('main_text').hide();
							Effect.Appear('jfil');
					}
					element.onmouseout = function() {
							Effect.Fade('jfil');
							$('main_text').show();						
					}
				}
				if (thisIdBody == "galleria") {
					toggleTooltip();
				}
        },
        'a#slaz' : function(element) {
			var thisIdBody = idbody();			
				if (thisIdBody != 'azienda') {
					element.onmouseover = function() {
							$('main_text').hide();						
							Effect.Appear('jazi');
					}
					element.onmouseout = function() {
							Effect.Fade('jazi');
							$('main_text').show();					
					}
				}
				if (thisIdBody == "galleria") {
					toggleTooltip();
				}							
        },
        'a#slga' : function(element) {
				if (idbody() != 'galleria') {		
					element.onmouseover = function() {
							$('main_text').hide();
							Effect.Appear('jgal');
					}
					element.onmouseout = function() {
							Effect.Fade('jgal');
							$('main_text').show();						
					}
				}
				else {
					toggleTooltip();					
				}				
        },
        'a#slco' : function(element) {
			var thisIdBody = idbody();				
				if (idbody() != 'collezione') {		
					element.onmouseover = function() {
							$('main_text').hide();
							Effect.Appear('jcol');
					}
					element.onmouseout = function() {
							Effect.Fade('jcol');
							$('main_text').show();						
					}
				}
				if (thisIdBody == "galleria") {
					toggleTooltip();
				}								
        },
        'a#slct' : function(element) {
			var thisIdBody = idbody();				
				if (idbody() != 'contatti') {		
					element.onmouseover = function() {
							$('main_text').hide();
							Effect.Appear('jcon');
					}
					element.onmouseout = function() {
							Effect.Fade('jcon');
							$('main_text').show();						
					}
				}
				if (thisIdBody == "galleria") {
					toggleTooltip();
				}							
        }
}

Behaviour.register(myrules);

// My window onload event
Behaviour.addLoadEvent(
	function() {
		//recupero l'id della pagina
		var pageId = idbody();
		var classId = classbody();
				//sempre		if (classId != "fx" && pageId == "galleria") {

			var options = {
			        method:"get",
			        onComplete:function(response) {
						jsonImages = response.responseText.evalJSON().imgs;
						writeToDOM(numberOfImagesToAddToDOM);
						
						imgs_to_fade = $$("#main_text p.flash img");
						imgs_to_fade[0].show();
						if (imgs_to_fade.length > 1) {
							intervalReference = setInterval('swapFlashImages()', waitTime);
							
							createTooltipHtml();
							
							var container = $$("#main_text p.flash")[0];
							container.observe('click', togglePause);
							container.observe('mousemove', followTheMouse.bind(this));
							
							retrieverIntervalReference = setInterval('appendImageToDOM()', retrieveWaitTime);							
						}				
			        }					
			};
			var par = $$("#main_text p")[0];
			if ( par.hasClassName("flash") ) {
				if ( par.hasClassName("it") )	
					new Ajax.Request("json_imgs_2008_it.txt",options);
				else if ( par.hasClassName("en") )	
					new Ajax.Request("json_imgs_2008_en.txt",options);							
			}						
		}
		else if (pageId == "collezione") {
			subNavHover();			
		}		
		//principale
		if (classId == 'fx') {
			switch ( pageId ){
/*
				case "home" :
					if (langhtml() == 'it')
						var xmlfile="content/news_it.txt";
					else if (langhtml() == 'en')	
						var xmlfile="content/news_en.txt";
					new ajax_ticker(xmlfile, "ticker", "someclass", 5000, "fade");
					break;
*/
				case "filo" :
				  transition("Fade", "jfil");
				  break;
				case "azienda" :
				  transition("Fade", "jazi");
				  break;
				case "galleria" :			
				  transition("Fade", "jgal");
				  break;
				case "collezione" :
				  transition("Fade", "jcol");
				  break;
				case "contatti" :
				  transition("Fade", "jcon");
				  break;
			}
		}

		//adding target="_blank" to all external links
		externalLinks();

	}
);
