/*
10/12/2008
Ghiaia Valmarino - JS application file
author: Alessandro Chinazzo
*/


function switch_language(language) {
	var f = $('language_form');
	f.language_code.value = language;
	f.submit();
}


function unshowPageContent(pageId, div, a) {
	Effect.Fade(div, { duration: 0.5, afterFinish: function(ele) {loadPageContent(pageId, div, a)} });
}

function loadPageContent(pageId, div, a) {
	var url = "/get_content_for_page/"+pageId;
	setBrowserAddress(a);
	new Ajax.Updater({success:div,failure:'error'}, url, {method: 'get', asynchronous:true, evalScripts:true, onComplete: function(req) {showPageContent(div)}});
}

function showPageContent(div) {
	Effect.Appear(div, { duration: 1 });
}

function showGallery(pageId) {
	Effect.Appear('gallery', { duration: 1 });
}

function unshowGallery(pageId) {
	Effect.Fade('gallery', { duration: 0.5, afterFinish: function(ele) {loadGallery(pageId)} });
}


function loadGallery(pageId) {
	var url = "/get_gallery_for_page/"+pageId;
	new Ajax.Updater({success:'gallery',failure:'error'}, url, {method: 'get', asynchronous:true, evalScripts:true, onComplete: function(req) {showGallery()}});
}


function inInPath(id) {
	var l = openPath.length;
	while (l--) {
		if (openPath[l] == id) return true;
	}
	return false;
}

function setMainPosition(){
	var elt         = $('main');
	var eltDims     = elt.getDimensions();
	var browserDims = document.viewport.getDimensions();
	var y  = (browserDims.height - eltDims.height) / 2;
	var x = (browserDims.width - eltDims.width) / 2;
	var styles = { position : 'absolute',
		top      : y + 'px',
		left     : x + 'px' };
	elt.setStyle(styles);
}

function setClassHover(){
	$$("#main-nav li").each(function(e){
		e.observe('mouseover', function(){
			$(this).addClassName("hover");
		});
		e.observe('mouseout', function(){
			$(this).removeClassName("hover");
		});
	});
}

function createScrollbar(){
	var scrollbar = new Control.ScrollBar('draggable-content', 'sliderbox');
}

document.observe("dom:loaded", function() {
	
	// Centro il box 'main'
	setMainPosition();
	Event.observe(window, 'resize', setMainPosition);
	
	// Aggiungo la classe 'hover' sul mouseover e la tolgo sul mouseout (IE6) 
	setClassHover();
	
	// Creo la scrollbar se ce n'è bisogno
	createScrollbar();
		
});

var lastNodeClicked = "";

var heightSaveTable = {};





function setAllNodesClosed() {

	var lis = document.getElementsByClassName("nodes"); // all the links
	var uls = document.getElementsByClassName("childrenNodes"); // the uls that contains the childrens
	var l = lis.length;
	
	for (var i=0; i<l; i++) { // first i check the lis and set the active class
		var e = Element.extend(lis[i]);
		var id = e.getAttribute("id");
		//alert("check "+id);
		if (inInPath(id.substr(5))) {
			var a = e.childElements()[0];
			e.addClassName("active");
			//setBrowserAddress(a);
			var bg = a.getAttribute("rel");
			if (bg != null) loadBackground(bg);
			lastNodeClicked = a.href;
		}
	}
	
	
	l = uls.length;
	for (var i=0; i<l; i++) { // then i check the uls 
		var e = uls[i];
		var id = e.getAttribute("id");
		e.style.overflow = "hidden";
		heightSaveTable[id] = e.getHeight();
		if (inInPath(id.substr(5))) {
			
		} else {
			e.style.height = "0px";	
		}
	}
}



function loadContent(a) {
	Element.extend(a);
	new Ajax.Updater({success:'content',failure:'error'}, a.href, {method: 'get', asynchronous:true, evalScripts:true, onComplete: function(req) {showContent()}});
}

function showContent() {
	Effect.Appear('content', { duration: 1.0, afterFinish: function(ele) {
			$('content').style.background = 'transparent url(\'/images/bg-content.png\') no-repeat 0 0';
			createScrollbar();
		}
	});
}


function unshowContent(a) {
	Element.extend(a);
	Effect.Fade('content', { duration: 0.5, afterFinish: function(ele) {
			loadContent(a)
		}
	});
}

function setBrowserAddress(a) {
	Element.extend(a);
	//window.location.href = "#" + a.pathname;
	window.location.assign("#" + a.pathname);
}


function loadBackground(bg) {
	$('bg').style.background = '#000 url(\'' + bg + '\') no-repeat 0 0';
}


function onNodeClicked(a) {
	Element.extend(a);
	var bg = a.getAttribute("rel");
	if (lastNodeClicked == a.href) return;
	lastNodeClicked = a.href;
	var theLi = a.ancestors()[0];
	deselectAllNodes(theLi);
	//theLi.setAttribute("class", "active");
	theLi.addClassName("active");
	openMenuNode(theLi);
	var id = theLi.getAttribute("id").substr(5);
	unshowContent(a);
	setBrowserAddress(a);
	if (bg != null) loadBackground(bg);
}

function deselectAllNodes(butLi) {
	Element.extend(butLi);
	var mainnav = $('root_nav');
	var lis = $A(mainnav.getElementsByTagName("LI"));
	var l = lis.length;
	for (var i=0; i<l; i++) {
		var e = lis[i];
		//alert(butLi.parentNode.getAttribute("id") + " "+ e.parentNode.getAttribute("id"));
		if (butLi.parentNode.getAttribute("id").substr(5) == e.getAttribute("id").substr(5)) {
			
		} else {
			deselectNode(e);	
		}
		
	}
}

function deselectNode(theLi) {
	Element.extend(theLi);
	theLi.removeClassName("active");
	//theLi.setAttribute("class", "");
	var id = theLi.getAttribute("id").substr(5);
	var ul = $("node_"+id);
	if (ul != null) {
		//ul.style.display = "none";
		new Effect.Morph(ul, {
  			style: 'height:0px;', // CSS Properties
  			duration: 0.4 // Core Effect properties
		});
	}
}

function openMenuNode(theLi) {
	Element.extend(theLi);
	var id = theLi.getAttribute("id").substr(5);
	var ul = $("node_"+id);
	if (ul != null) {
		//ul.style.display = "block";
		var dHeight =  heightSaveTable[ul.getAttribute("id")];
		new Effect.Morph(ul, {
  			style: 'height:'+ dHeight + 'px;', // CSS Properties
  			duration: 0.8 // Core Effect properties
		});
	}
}






