// JavaScript Document
var Menu = {
	ajaxZone1: "imgTestata",
	ajaxZone2: "content",
	init: function(options){
		this.last = "";
		this.current = "";
		this.first = "vm1";
		this.ajaxZone1 = "imgTestata";
		this.ajaxZone2 = "content";
		

		if(window.location.toString().contains('#')){
			var location = window.location.href.match(/^[^#]*/)[0] + '#';
			var loc = window.location.toString();
			this.first = "vm"+loc.substring(location.length);
		}

		this.ajaxZone1 = "imgTestata";
		this.ajaxZone2 = "content";
		//this.spot = $('spotContent');

		$each($$('#menuUl li'), function(el){
			el.onclick = this.click.pass(el, this);
			el.onmouseover = this.hover.pass(el, this);
			el.onmouseout = this.out.pass(el, this);
		}, this);

		$each($$('ul.submenu li'), function(el){
			el.onclick = this.click2.pass(el, this);
		}, this);

		this.fx = {
			testata: $(this.ajaxZone1).effect('opacity', {duration: 2000, transition: Fx.Transitions.Expo.easeInOut})
		};

		// FIRST
		//this.current = this.first;
		//alert(this.first);
		this.last = this.first;
		if(this.first == "vm6" || this.first =="vm7" || this.first =="vm2")
			this.submenu(2);
		if(this.first == "vm8" || this.first =="vm9" || this.first =="vm11" || this.first =="vm3")
			this.submenu(3);
		if(this.first == "vm10" || this.first =="vm4")
			this.submenu(4);
		
		//startMenu();

		//this.current = this.first;	
		//this.ajax1(this.first);
		///this.ajax2(this.first);
		//if(this.current){
		//	if($(this.current)){
				//alert("in");
		//		$(this.current).addClass("selected");
		//	}
		//}
		//alert("selinit");
	},

	click: function(list){
		//alert("click1: "+list+" - "+this.current);
		if(list != this.current){
			this.open(list);
			if(this.current){
				if($(this.current)){
					$(this.current).removeClass("selected");
					//alert("tolto a "+this.current);
				}
					//alert("non c'è l'elemento "+this.current);
			}
				//alert("non c'è la variabile "+this.current);
			this.current = list;
			this.ajax1(list);
			
			if($(list)){
				$(list).addClass("selected");
			}
			this.ajax2(list);
			
			//alert(list+" sel1");		
		}
	},

	click2: function(list){
		number = list.id;
		//alert("click2: "+number+" - "+this.current);
		//number = list;
		if(number != this.current){
			if(this.current)
				if($(this.current))
					$(this.current).removeClass("selected");
			if($(number))
				$(number).addClass("selected");
			//alert("sel2");
			this.current = number;			
			this.ajax1(number);
			this.ajax2(number);
		}
	},

	hover: function(list){
		$(list.id).setStyle("background-image","url('img/imgRollover.jpg')");
		/*$(list.id).setStyle('color','white');*/
	},

	out: function(list){
		$(list.id).setStyle("background-image","none");
		/*$(list.id).setStyle('color','white');*/
	},

	dovesiamo: function(){
		//document.write "<scr" + "ipt src='http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA7SoPPNVhBiaKf-SkluU2qhS-x_jqlJcXtni5hRDPLRPU4OMXphRHfO3ow2kHa9uaNTPan2a4EhYqJg'>" + "<\/script>";
		//var newS	= document.createElement('script');
		//newS.type	= "text/javascript";
		//newS.src	= "http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA7SoPPNVhBiaKf-SkluU2qhS-x_jqlJcXtni5hRDPLRPU4OMXphRHfO3ow2kHa9uaNTPan2a4EhYqJg";
		//this.gjs = new Element('script', {'type': 'text/javascript', 'src': 'http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA7SoPPNVhBiaKf-SkluU2qhS-x_jqlJcXtni5hRDPLRPU4OMXphRHfO3ow2kHa9uaNTPan2a4EhYqJg'}).injectInside(document.body);
		//document.getElementsByTagName("head")[0].appendChild(newS);

		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAA7SoPPNVhBiaKf-SkluU2qhS-x_jqlJcXtni5hRDPLRPU4OMXphRHfO3ow2kHa9uaNTPan2a4EhYqJg&async=2&callback=loadMap";
		document.body.appendChild(script);
	},

	//	Chiamata Ajax 1 - Testata - In realtà è un Assets
	ajax1: function(list){
		/*
		$(this.ajaxZone1).empty();
		$(this.ajaxZone1).setOpacity(0);
		random = Math.floor(Math.random()*1000);
		new Asset.image(
			'testata.php?sez='+list.substring(2)+'&rand='+random,{
				id: 'imgtestata',
				onload: function(){
					Menu.fx.testata.start(0,1);
				}
			}
		).injectInside(Menu.ajaxZone1);
		*/
		setParam(list.substring(2));

	},

	//	Chiamata Ajax 2 - pagina
	ajax2: function(list){
		url = "http://www.umpi.it/pagine/ita/pagina"+list.substring(2)+".php";
		//url = "http://umpi:800/pagine/ita/pagina"+list.substring(2)+".htm";
		this.ajax3(url);
		//if(list.substring(2)=="4")
			//this.dovesiamo();
	},

	ajax3: function(urlo){
		//alert(urlo);		
		$(Menu.ajaxZone2).empty();
		$(Menu.ajaxZone2).setHTML("<img src='img/indicator.gif' />");
		var ajax = new Ajax(urlo, {
			update: $(Menu.ajaxZone2),
			method: 'get',
			evalScripts: true
		});
		ajax.request();
	},

	open: function(div){
		//this.fx.dropUp.start(-this.spot.getStyle("height").toInt()).chain(function(){
		//alert("open: " + this.last + " " + this.current);
		//this.last = this.current;
		//this.current = div;
		Menu.submenu(div.substring(2));
		//Menu.spot.setHTML($(Menu.current).getText());
		//Menu.fx.dropDown.start(0);
		//});
	},

	submenu: function(number){
		//	tolgo quello che c'è
		if(Menu.last){
			if($('submenu' + this.last.substring(2))){
				$('submenu' + this.last.substring(2)).setOpacity(0);
				$('submenu' + this.last.substring(2)).setStyle('display','none');
			}
			Menu.last = "vm"+number;
		}

		// 	metto il nuovo submenu
		if($('submenu' + number)){
			$('submenu' + number).setOpacity(0);
			$('submenu' + number).setStyle('display','block');
			$('submenu' + number).effect('opacity', {duration: 1000, transition: Fx.Transitions.Expo.easeInOut}).start(0,1);
		}
	}
};


window.addEvent('domready',function(){
	Menu.init();
});

latitudine = 43.926600;
longitudine = 12.569217;
zoom = 10;
messaggio = "BEDETTI ESCAVAZIONI<br>Corso Europa, 19<br>Fratte di Sassofeltrio (PU)<br>Tel. 0541.97.44.71<br>Fax 0541.97.47.44<br<a href=\"mailto:info@bedettiescavazioni.it\" target=\"_blank\">info@bedettiescavazioni.it</a>";
function loadMap(){
	if (GBrowserIsCompatible()) {
		var map = new GMap2($("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(latitudine, longitudine), zoom);
		var point = new GLatLng(latitudine,longitudine);
		var marker = new GMarker(point);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(messaggio);
		})
		map.addOverlay(marker);
	}
}

function getAncora(){
	if(window.location.toString().contains('#')){
		var location = window.location.href.match(/^[^#]*/)[0] + '#';
		var loc = window.location.toString();
		//alert('getAncora'+loc.substring(location.length));
		return loc.substring(location.length);
	}
	return "null";
}
function thisMovie(movieName) {
  // IE and Netscape refer to the movie object differently.
  // This function returns the appropriate syntax depending on the browser.
  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window.parent[movieName];
	
  } else {
    return parent.document[movieName];
	}
}
// Checks if movie is completely loaded.
// Returns true if yes, false if no.
function movieIsLoaded (theMovie) {
  // First make sure the movie's defined.
  if (typeof(theMovie) != "undefined") {
    // If it is, check how much of it is loaded.
    return theMovie.PercentLoaded() == 100;
  } else {
    // If the movie isn't defined, it's not loaded.
    return false;
  }
}


function playmovie(sezione) {
	if (movieIsLoaded(thisMovie('testate'))) {

		thisMovie(movieName).TGotoLabel("/", sezione); 
	}
}

function setParam(sezione){
	//alert("ff"+sezione)
	thisMovie('testate').SetVariable("sezione",sezione);

}
