var agendaLocationSelector = {varGet : "loc", loc : "Sarthe"};
var origW = 0;/*pour les pubs*/

function bounce(pos) {
	/*fonction easing emile.js*/
	if (pos < (1/2.75)) {
		return (7.5625*pos*pos);
	} else if (pos < (2/2.75)) {
		return (7.5625*(pos-=(1.5/2.75))*pos + .75);
	} else if (pos < (2.5/2.75)) {
		return (7.5625*(pos-=(2.25/2.75))*pos + .9375);
	} else {
		return (7.5625*(pos-=(2.625/2.75))*pos + .984375);
	}
  }

function loadAgenda() {
	var urlAgenda = "lib/inc/callers/concerts_next.php?" + agendaLocationSelector.varGet + "=" + agendaLocationSelector.loc;
	if ($$("widgetAgenda").hasClass("from1stLevel")) {
		urlAgenda = "../" + urlAgenda;
	}
	
	DOMAssistant.AJAX.get(urlAgenda, function (r) {
		$$("widgetAgenda").cssSelect(".content").first().replaceContent(r);
		$$("widgetAgenda").cssSelect("a.out").setAttributes({
			target : "_blank"
		});
	});
}

function autoSelect(varGet, choix) {
	/*chargement auto du contenu au changement de select*/
	agendaLocationSelector.varGet = varGet;
	agendaLocationSelector.loc = choix;
	loadAgenda();
}

function autoJump(varGet, choix) {
	/*chargement auto de la page au changement de select*/
	var curUrl, urlAgenda;
	agendaLocationSelector.varGet = varGet;
	agendaLocationSelector.loc = choix;
	curUrl = window.document.location.href.split(".php");
	urlAgenda = curUrl[0] + ".php?" + agendaLocationSelector.varGet + "=" + agendaLocationSelector.loc;
	window.document.location.href = urlAgenda;
}

function loadPub () {
	var c;
	origW = parseInt($$("pub-content").getStyle("width"));
	$$("pub").setStyle({
		width : $$("pub-link").offsetWidth + "px"
	});
	//alert(origW);//debug
	$$("pub-link").cssSelect("a").first().addEvent("click", function (e) {
		DOMAssistant.preventDefault(e);
		//alert("ok");//debug
		togglePub();
	});
	c = readCookie("pub");
	//alert(c);//debug
	if (c === null) {
		togglePub(true);
	}
}

function togglePub (auto) {
	var status = $$("pub-content").getStyle("display");
	if (status !== "none") {
		if (auto !== true && readCookie("pub") === null) {
			createCookie("pub", "ok", "demain");
		}
		$$("pub-content").cssSelect("a").setStyle({
			visibility : "hidden"
		});
		emile($$("pub-content"), 'width: 0px', { 
			duration : 500,
			easing : bounce,
			after : function () {
				$$("pub-content").setStyle({
					display : "none"
				});
				$$("pub-content").cssSelect("a").setStyle({
					visibility : "visible"
				});
				//alert($$("pub-link").offsetWidth);
				$$("pub").setStyle({
					width : $$("pub-link").offsetWidth + "px"
				});
			}
		});
	}
	else {
		$$("pub").setStyle({
			width : parseInt($$("pub-link").offsetWidth + origW + 22) + "px"
		});
		emile($$("pub-content"), 'width: ' + origW + 'px', { 
			duration : 250,
			after : function () {
				$$("pub-content").setStyle({
					display : "inline",
					width : origW + "px"
				});
			}
		});
	}
}

/*gestion cookies (pour la pub par ex)*/

function createCookie(name,value,availTime) {
	if (availTime) {
		switch (availTime) {
			case "demain" :
				/*à 0 heure demain*/
				var d1 = new Date();
				var ch = (d1.getMonth() + 1) + "/" + (d1.getDate() + 1) + "/" + d1.getFullYear();
				//alert(ch);//debug ie
				var d2 = new Date(ch);
				availTime = d2.getTime() + (24*3600);
				//alert(availTime);//debug ie
				break;
			default :
				/*dans 24 heures à partir de maintenant*/
				var d1 = new Date();
				availTime = d1.getTime() + (24*3600);
				break;
		}
		var dateset = new Date();
		dateset.setTime(availTime);
		var expires = "; expires="+dateset.toGMTString();
		//alert(expires);//debug
	}
	else var expires = "";
	//alert(name+"="+value+expires+"; path=/");//debug
	document.cookie = name+"="+value+expires+"; path=/";
	//alert(document.cookie);//debug
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


DOMAssistant.DOMReady(function () {
	var urlAnnonces;
	$("a.out").setAttributes({
		target : "_blank"
	});
	if ($("#widgetAnnonces").length > 0) {
		urlAnnonces = "lib/inc/callers/annonces_next.php";
		if ($$("widgetAnnonces").hasClass("from1stLevel")) {
			urlAnnonces = "../" + urlAnnonces;
		}
		DOMAssistant.AJAX.get(urlAnnonces, function (r) {
			$$("widgetAnnonces").replaceContent(r);
		});
	}
	if ($("#widgetAgenda").length > 0) {
		loadAgenda();
	}
	if ($("#DADiapo").length > 0) {
		$("#DADiapo").diapo();
	}
	if ($(".photoGal").length > 0) {
		$(".photoGal img").each(function () {
			var urlImg, aTag, repl, urlTh;
			urlImg = $$(this).src + "?width=550&height=500";
			aTag = $$(this).parentNode;
			$$(aTag).setAttributes({
				href : urlImg
			}).addClass("dawin");
			urlTh = $$(this).src.replace(/illustre\//g, "illustre/thumb/");
			//alert(urlTh);//debug
			$$(this).setAttributes({
				src : urlTh
			});
			DOMAssistant.windopener.ready();
		});
	}
	if ($("#pub").length > 0) {
		loadPub();
	}
	if (typeof autoScroll !== "undefined" && $("#slider").length > 0) {
		autoScroll('slider','groupList',7,true);
	}
});
