// JavaScript Document
window.addEvent('domready', function()
{
	
	var menu = {
		divMenu:null,
		listeC:null,
		activeSsmenu:null,
		activeMenu:[],
		init: function()
		{
			$this = this;
			if(!$('carte') && !$('vins')) return false;
			this.divMenu = null;
			$$('#carte','#vins').each(function(el)
			{
				if(el.getElement('.menu'))
				{
					$this.divMenu = el.getElement('.menu');
				}
			});
			if(this.divMenu)
			{
				this.listeC = this.divMenu.getElement('ul');
				
				this.lis = this.listeC.getChildren('li');
				this.lis.each(function(el, index)
				{
					$this.getSousMenu(el, 0); 
				});
				this.divMenu.minHeight = this.divMenu.getSize().y; 
				this.divMenu.setStyle('height', this.divMenu.minHeight); 
				this.tw = new Fx.Morph( this.divMenu, { duration:'short',wait:false, transition: Fx.Transitions.Sine.easeOut }); 
			}
			if(this.activeMenu.length > 0)
			{
				this.activeMenu[0].getFirst('a').fireEvent('click');
			}
			
		},
		getSousMenu: function(li, level)
		{
			$this = this;
			if(li.hasClass('active')) $this.activeMenu[level] = li;
			var ssmenu = li.getElement('ul');
			if(ssmenu)
			{
				// EVENT START
				li.getFirst('a').addEvent('click', function(e)
				{
					if(e) e.stop();
					if(li.ssmenu) {
						li.ssmenu.addClass('ssmenu');
						if($this.activeSsmenu && $this.activeSsmenu != li.ssmenu){
							$this.activeSsmenu.dispose(); 
						}
						
						li.addClass('active');
						if($this.activeMenu[level]) {
							if($this.activeMenu[level] != li) $this.activeMenu[level].removeClass('active');
						}
						$this.activeMenu[level] = li;
						li.ssmenu.inject($this.listeC, 'after'); 
						$this.activeSsmenu = li.ssmenu; 
						
						$this.tw.start({ 'height': function()
						{
							if(li.ssmenu.getSize().y > $this.divMenu.minHeight) return li.ssmenu.getSize().y;
							else return $this.divMenu.minHeight;
						}}); 
					}
				});
				
				// EVENT END
				
				ssmenu.level = level;
				ssmenu.getChildren('li').each(function(el,index) 
				{
					$this.getSousMenu(el, level+1);
				});
				li.ssmenu = ssmenu.dispose();
			}else{
				li.ssmenu = false;
			}
		}
		
	};
	
	var map = {
		init:function()
		{
			this.container = $('map');
			this.inner = new Element('div', {'id':'mapContainer'}).inject(this.container);
			
			this.startlatlng = new google.maps.LatLng(43.55832735152833, 4.084825790166864);
			var myOptions = {
				zoom: 16,
				center: this.startlatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};

			this.map = new google.maps.Map(this.inner, myOptions);
			this.marker = new google.maps.Marker({
				position: this.startlatlng, 
				map: this.map,
				title:"L'Ombrine", 
				icon: new google.maps.MarkerImage('fileadmin/templates/photos/icons/map-logo.png',
					// This marker is 20 pixels wide by 32 pixels tall.
					new google.maps.Size(44, 71),
					// The origin for this image is 0,0.
					new google.maps.Point(0,0),
					// The anchor for this image is the base of the flagpole at 0,32.
					new google.maps.Point(22, 71)
				),
				shadow: new google.maps.MarkerImage('fileadmin/templates/photos/icons/map-logo-shadow.png',
					// This marker is 20 pixels wide by 32 pixels tall.
					new google.maps.Size(58, 63),
					// The origin for this image is 0,0.
					new google.maps.Point(0,0),
					// The anchor for this image is the base of the flagpole at 0,32.
					new google.maps.Point(22, 60)
				)
			});
/*
			if(console){
				google.maps.event.addListener(this.map, 'click', function(event) {
					console.log(event.latLng.b + ' -' +event.latLng.c);
				});
				google.maps.event.addListener(this.marker, 'click', function(event) {
					//console.log(event);
					console.log(map.marker);
					var div = new Element('div', {'class':'test'}).inject(event.currentTarget);
					event.currentTarget.setStyle('overflow','visible');
				});
			}
*/
		}
	};
	
	var toggleContent = {
		state:'open',
		init:function()
		{
			this.startWidth = $('main').getSize().x;
			this.bt = new Element('a',{'href':'#', 'id':'toggleContent'}).addEvent('click',this.toggle).inject('container');
			this.tw = new Fx.Morph('container', {'wait':false});
		}, 
		toggle:function(event)
		{
			if(event) event.stop();
			if(toggleContent.state == 'open')
			{
				toggleContent.state = 'closed';
				toggleContent.tw.start({'margin-left':-980});
				toggleContent.bt.addClass('closed');
			}else{
				toggleContent.state = 'open';
				toggleContent.tw.start({'margin-left':0});
				toggleContent.bt.removeClass('closed');
			}
		}
		
	};
		

	menu.init();
	if($$('#bgs li').length>1)
	{
		fadeImages.init({elements:$$('#bgs li'), container:$('bgs'), makeLazy:true, 'image':'fileadmin/templates/images/blank.gif'});
		//dynamicSlideShow.init({elements:$$('#bgs li'), container:$('bgs'), listToLoad:$$('#bgs div.list a'), targetTag: 'ul'});
	}

	if($('map')){
		map.init();
	}
	
	toggleContent.init();

});

