// JavaScript Document

var map;
var icon;
var overlay;

var gdir;
var geocoder = null;
var addressMarker;

var botteghe =new Array();

var Loader = new Request.JSON({
	url:'inc/ajax/list.botteghe.php',
	onRequest:function() {
		new Element('img', {'class':'map_loader', 'src':'inc/css/images/ajax-loader.gif'})
		.set('morph',{
			 onComplete:function(e) {
			 	e.dispose();
				map.setCenter(new GLatLng(44.1, 10.3), 9);
			 }
		})
		.inject(document.getElement('#map_filter_form .btn'),'after')
	},
	onSuccess:function(j,jtext) {
		output = '';
		j.each(function(bottega) {
			
			if (bottega.immagine) {
				imageOutput =	'  <div class="the_image_preview">'+
								'       <img src="'+bottega.immagine+'" alt="'+bottega.nome+'" />'+
								'   </div>';
			} else imageOutput = ' <div class="the_image_preview"><small>No Preview</small></div>';
			
			output +='<li class="bottega">'+
                     imageOutput+
                     '   <h3 class="the_name"><a href="?q=botteghe&amp;detail_id='+bottega.ID+'">'+bottega.nome+'</a></h3>'+
                     '   <p>'+
                     '   	<span class="the_address" title="'+bottega.map_lat+','+bottega.map_lng+','+bottega.map_zoom+'">--</span>'+
                     '   </p>'+
                     '   <a href="?q=botteghe&amp;detail_id='+bottega.ID+'" class="red btn append-top">Dettaglio &raquo;</a>'+
                     ' </li>'
			
		});
		
							
		$('botteghe_list').set('html',output)
		setBotteghe();
		
		$('map_home').morph({'border-color':['#ff9','#fff']});
		$('map_filter_form').morph({'background-color':['#ff9','#fff']});
		
		if (a=document.getElement('#map_filter_form img.map_loader')) a.morph({'opacity':0});
		
	}
})


function createMarker(index) {

	botteghe[index].marker  = new GMarker(botteghe[index].latlng, {draggable: false, icon:icon_bottega});
	
	GEvent.addListener(botteghe[index].marker,'click', function(latlng) {
		map.panTo(latlng);
		document.getElement('#map_detail')
		.set('styles',{'opacity':0})
		.set('html','<div><h3>'+botteghe[index].nome+'</h3><div class="the_image_preview"><img src="'+botteghe[index].image+'"/></div><small><em>'+botteghe[index].address+'</em></small><br />Tel: '+botteghe[index].phone+'<br /><a href="'+botteghe[index].link+'" class="more">Dettagli &raquo;</a></div>')
		.morph({'opacity':1});
		
	})

  return botteghe[index].marker;
}
function setBotteghe() {
	

	$$('.sub_region div.zona ul.botteghe li').each(function(b,i) {
		
		var bottega = {
			nome:b.getElement('.the_name').get('html'),
			"link":b.getElement('.the_name').get('href'),
			image:((b.getElement('.the_image_preview img')) ? b.getElement('.the_image_preview img').get('src') : false),
			address:b.getElement('.the_address').get('html'),
			phone:b.getElement('.the_phone').get('html'),
			fax:b.getElement('.the_fax').get('html'),
			web:b.getElement('.the_web').get('html')
		}
		botteghe[i] = bottega;
	});
}

window.addEvent('domready', function() {
	$$('#map_detail div').set('morph', {duration:750});
	$$('#map_filter_form, #map_home').set('morph', {duration:1500});				  					  
		
		var data = {
	      '06.jpg': {}, 
	      '01.jpg': {}, 
	      '02.jpg': {}, 
	      '03.jpg': {}, 
	      '04.jpg': {},
	      '05.jpg': {}
	    };
	    
		if ($('slide')) $('slide').set('styles',{'background':'none'});
		var myShow = new Slideshow(document.getElement('#slide>div'), data, { captions: false, controller: false, height: 330, hu: 'inc/css/images/slides/', width: 450, duration:3000, delay:4000 });
		
		//	SET THE WIN SCROLL FX		
		wscroll = new Fx.Scroll(window, {duration:500, offset:{x:0,y:-36}});
		
	$$('.sub_region div.zona h4.map_marker').each(function(e,index) {
		
		e.store('index',index)
		.store('actualHref',e.getNext('a').get('href'))
		
		.addEvent('click',function() {
								   
			document.getElement('#map_detail')
			.set('styles',{'opacity':0})
				.set('html','<div><h3>'+botteghe[e.retrieve('index')].nome+'</h3>'+
							'<em class="the_address">'+botteghe[e.retrieve('index')].address+'</em>'+
							((botteghe[index].fax.length>0) ? 'Tel: '+botteghe[index].phone+'<br />' : '')+
							((botteghe[index].fax.length>0) ? 'Fax: '+botteghe[index].fax+'<br />' : '')+
							((botteghe[index].web.length>0) ? 'Web: <a href="http://'+botteghe[index].web.replace(/http:\/\//g,'')+'">'+botteghe[index].web+'</a><br />' : '')+
							'<a href="'+e.retrieve('actualHref')+'" class="red btn append-top"><span><span>Dettagli &raquo;</span></span></a><br/><a class="accessibility" href="#zone">Torna a elenco zone territoriali</a></div>')
			.morph({'opacity':1});
			
			return false;
		})
	});
	
	
	setBotteghe();
	$$('.map a.map_marker').addEvent('click', function(evt) {
		$$('.sub_region div.zona').morph({'opacity':0});
		document.getElement('#map_detail').fade(0);
		
		if (e = document.getElement('.sub_region div.'+this.rel)) e.morph.delay(280,e,{'opacity':1});
		
		//if ($(this.get('rel'))) 		
		if (Browser.Engine.trident) {
			wscroll.toElement($(this.get('rel')));
			return false;
		}
		
	});
	
	$$('.sub_region div.zona').each(function(zona,index) {
		
		zona.set('morph',{'duration':250})
		.set('styles', {
		
			'position':'absolute',
			'top':0,
			'left':0,
			'opacity':0
		
		})
		//if(index==0) zona.fade(1);
	});
	
	

});
window.addEvent('load',function() {
	
	$$('#map_filter_form .btn').removeEvents().addEvent('click', function(e) {
		Loader.send('filter_zona='+$('filter_zona').get('value'));
		return false;
		e.stop();
	});

});


