/* compostelle ça marche javascript */
/* admin function © amin ladhani */
function adminConnect(){
	xhrConnect = new XMLHttpRequest();
	xhrConnect.onreadystatechange = function(){
		if(xhrConnect.readyState == 4 && xhrConnect.status==200){
			var getFileContent = xhrConnect.responseText;
			var connDiv = document.createElement('div');
			connDiv.id = "connect";
			document.body.appendChild(connDiv);
			document.getElementById('connect').innerHTML = getFileContent;
			document.getElementById('connect').style.display = "block";
		}
	}
	xhrConnect.open("GET","xhr.connexion.php");
	xhrConnect.send(null);
}
function adminConnectExit(){
	document.body.removeChild(document.getElementById('connect'));
}
/* navigation / display script © sam rossetti | contreforme sàrl */
function linksInit(){
	$each($$('#footer a'),function(el){
		if(el.getAttribute('rel') == 'external'){
			el.setAttribute('target','_blank');
		}
	});
	$each($$('#article a'),function(el){
		if(el.getAttribute('rel') && el.getAttribute('rel').contains('lightbox')){
			el.slimbox({}, null, function(el) {
			});
			el.setStyle('border','none');
		}else if(el.getAttribute('rel') == 'external'){
			el.setAttribute('target','_blank');
		}
	});
	$$('img.insertedImg').each(function(el,index){
		if(el.get('rel') == 'lightbox'){
			el.setStyle('cursor','pointer');
			el.addEvent('click',function(ev){
				Slimbox.open(el.get('src'));
			})
		}
	});
}

function siteInit(){
	var timer = 0;
	var fx = [];
	$each($$('#sousmenu li'),function(el,i){
		el.i = i;
		el.setStyle('margin-left','-155px');
		timer += 150;
		fx[i] = new Fx.Morph(el,{duration:350,transition:'back:in:out',link:'cancel'});
		fx[i].start.delay(timer, fx[i], {'margin-left':0});
	});
}

function cominit(){
	var myAcc = new Accordion(
		$$('.toggler'),
		$$('.stretcher'),{
		display:false,
		alwaysHide:true
	});
	$$('button.submit').each(function(el,index){
		el.addEvent('click',function(ev){
			var ev,p,smsref,lang,name,email,com;
			ev = new Event(ev).stop();
			p = this.getParent().getParent().getParent();
			smsref = p.getElement('input[name=smsref]').get('value');
			lang = p.getElement('input[name=lang]').get('value');
			name = escape(p.getElement('input[name=name]').get('value'));
			email = p.getElement('input[name=email]').get('value');
			com = escape(p.getElement('textarea').get('value'));
			var xhr = new Request({
				method:'post',
				evalScripts:true,
				url: '__internal/commentsms.php',
				onComplete:function(response){
					if(!response.contains('erreur') && !response.contains('error')){
						p.getElement('input[name=smsref]').set('value','');
						p.getElement('input[name=lang]').set('value','');
						p.getElement('input[name=name]').set('value','');
						p.getElement('input[name=email]').set('value','');
						p.getElement('textarea').set('value','');
					}
					alert(response);
				}
			}).send('smsref='+smsref+'&lang='+lang+'&name='+name+'&email='+email+'&comment='+com);
		});
	});
	$$('a.lngtoggler').each(function(el,index){
		var eP = el.getParent().getElement('div.lngcont');
		el.h = eP.getSize().y;
		el.tog = false;
		el.i = index;
		eP.setStyle('overflow','hidden');
		_lngdivs[index] = new Fx.Tween(eP);
		_lngdivs[index].set('height',120);
		
		el.addEvent('click',function(ev){
			var ev = new Event(ev).stop();
			if(!this.tog){
				this.set('text','fermer / close');
				_lngdivs[this.i].start('height',this.h);
			}else{
				this.set('text','lire la suite / read more');
				_lngdivs[this.i].start('height',120);
			}
			this.tog = !this.tog;
		});
	});
	
	if($('gmap')){
		if(GBrowserIsCompatible()){
			var map = new GMap2(document.getElementById("gmap"),{mapTypes:[G_HYBRID_MAP,G_NORMAL_MAP,G_PHYSICAL_MAP]});	// load googlemap into gmap div with two map types
			map.addControl(new GLargeMapControl());									// small buttons zoom/pan
			map.addControl(new GMapTypeControl());										// type control
			var point = new GLatLng(42.683166,-6.5);								// get point
			map.setCenter(point,7);														// assign it to map
			
			var coords = [
				new GLatLng(42.3414,-3.6995),
				new GLatLng(42.3383,-3.9254),
				new GLatLng(42.2883,-4.1388),
				new GLatLng(42.2873,-4.2599),
				new GLatLng(42.2585,-4.3472),
				new GLatLng(42.2674,-4.4068),
				new GLatLng(42.3169,-4.5433),
				new GLatLng(42.3398,-4.6012),
				new GLatLng(42.3286,-4.8065),
				new GLatLng(42.3834,-5.0359),
				new GLatLng(42.4478,-5.3641),
				new GLatLng(42.5999,-5.5718),
				new GLatLng(42.5073,-5.6866),
				new GLatLng(42.4635,-5.8362),
				new GLatLng(42.4581,-5.9302),
				new GLatLng(42.4883,-6.2601),
				new GLatLng(42.4917,-6.3425),
				new GLatLng(42.5349,-6.4943),
				new GLatLng(42.5490,-6.5836),
				new GLatLng(42.5963,-6.7164),
				new GLatLng(42.6060,-6.8021),
				new GLatLng(42.6691,-6.9808),
				new GLatLng(42.7253,-7.0223),
				new GLatLng(42.7299,-7.3251),
				new GLatLng(42.7777,-7.4145),
				new GLatLng(42.7982,-7.6132),
				new GLatLng(42.8448,-7.7440),
				new GLatLng(42.8742,-7.8689),
				new GLatLng(42.9127,-8.0116),
				new GLatLng(42.9272,-8.1642),
				new GLatLng(42.9147,-8.3510),
				new GLatLng(42.8804,-8.5463)
				];
			
			var names = [
				'Burgos',
				'Hornillos',
				'Castrojerix',
				'Itero',
				'Boadilla',
				'Fromista',
				'Villalcazar',
				'Carrion de los Condes',
				'Calzadilla',
				'Sahagun',
				'Province de Leon',
				'Leon',
				'Chozas',
				'Villavante',
				'Sanitibanez',
				'Rabanal Viejo',
				'Foncebadon',
				'Molinaseca',
				'Ponferrada',
				'Cacabelos',
				'Villafranca',
				'Las Herrerias',
				'Pedrafita Do Cebreiro',
				'Samos',
				'Sarria',
				'Portomarin',
				'Ventas',
				'Palas de Rei',
				'Melide',
				'Arzua',
				'A Rua',
				'Santiago de Compostella !'
				];
			
			var polyline = new GPolyline(coords, "#ff0000", 5);
			map.addOverlay(polyline);
			
			var shell = new GIcon();
			shell.image = "/__statics/images/coquille_small.png";
			shell.iconSize = new GSize(15,12);
			shell.iconAnchor = new GPoint(8,6);	
			shell.infoWindowAnchor = new GPoint(15,0);
			
			for(var i=0;i<coords.length-1;i++){
				map.addOverlay(createMarker(coords[i],names[i],shell));
			}
			
			var spot = coords[coords.length-1];
			var icon = new GIcon();
			icon.image = "/__statics/images/carole_walk.png";
			icon.iconSize = new GSize(30,50);
			icon.iconAnchor = new GPoint(15,45);
			icon.infoWindowAnchor = new GPoint(15,0);
			var carole = new GMarker(spot,icon);
			GEvent.addListener(carole, "click", function(){
				carole.openInfoWindowHtml('<b style="font-family:Tahoma,_sans;float:left;margin:20px 60px;">'+names[names.length-1]+'</b>');
			});
			map.addOverlay(carole);
		}
	}
}
function createMarker(coords,name,ic){
	var m = new GMarker(coords,ic);
	GEvent.addListener(m, "click", function(){
		m.openInfoWindowHtml('<b style="font-family:Tahoma,_sans;float:left;margin:20px 60px;">'+name+'</b>');
	});
	return m;
};

var _pops, _popready, _lngdivs;

window.addEvent('domready', function(){
	_pops = [];
	_lngdivs = [];
	_popready = true;
	linksInit();
	siteInit();
	cominit();
});