function addLoadEvent(func) { // Manage Load Event
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addResizeEvent(func) { // Manage Resize Event
    var oldonresize = window.onresize;
    if (typeof window.onresize != 'function') {
        window.onresize = func;
    } else {
        window.onresize = function() {
            oldonresize();
            func();
        }
    }
}

window.lastone = 0;

/*soirees*/
function showTeritorySoirees(number) {

	$("#SoireesMap .Background").find(".r" + number).animate({ opacity: 'show' }, 360);

	if(number != window.lastone) {
		$("#SoireesMap .Background").find(".r" + window.lastone).animate({ opacity: 'hide' }, 350);
	}

	window.lastone = number;

}

/*soirees*/
function hideTeritorySoirees(number) {
	return true;
}


function compareheight(element) {
	var countofelements = $(element).length;
	for(i = 0; i < countofelements; i=i+2) {
		if($(element).eq(i).height() > $(element).eq(i+1).height())
			$(element).eq(i+1).css('height', $(element).eq(i).height() + 'px');
		else if($(element).eq(i).height() < $(element).eq(i+1).height())
			$(element).eq(i).css('height', $(element).eq(i+1).height() + 'px');
		else
			continue;
	}
}

// sets up jCarousel with buttons for paging
$.fn.pagedCarousel = function (pagingElem, numberofvisible, options) {
	elem = $(this);
	pagingElem = $(pagingElem);

	var numberofelements = Math.ceil($("li", elem).length/numberofvisible);

	function drawButtons(on) {
		//pagingElem.html(' ');
		pagingElem.empty();
		if(numberofelements > 1)
			for(i = 0; i < numberofelements; i++) {
				if(i != on)
					pagingElem.append('<a href="#" title="'+(i*numberofvisible+1)+'"><img src="img/icon_jc-point-blank.gif" alt="Point" /></a>');
				else
					pagingElem.append('<a href="#" title="'+(i*numberofvisible+1)+'"><img src="img/icon_jc-point-full.gif" alt="Point" /></a>');
			}
	}

	function reDrawButton(whichone) {
		$("a", pagingElem).removeClass('active');
		$("a", pagingElem).eq(whichone).addClass('active').find("img").attr('src','img/icon_jc-point-full.gif');
		$("a", pagingElem).not('.active').find("img").attr('src','img/icon_jc-point-blank.gif');
	}

	drawButtons(0);

	function mycarousel_initCallback(carousel) {
		$("a", pagingElem).bind('click', function() {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('title')));
			reDrawButton((jQuery(this).attr('title')-1)/numberofvisible);
			return false;
		});
	};

	function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		var currentpage = Math.ceil(idx/numberofvisible)-1;
		reDrawButton(currentpage);
	};

	options = $.extend(options, {
		visible: numberofvisible,
		itemFirstInCallback: mycarousel_itemFirstInCallback,
		initCallback: mycarousel_initCallback
	});

	elem.jcarousel(options);
}

function sidePager(urlPrefix) {
	// handle block "Les organismes se présentent"

	$("#sidePager").each(function() {
		// loads content using ajax request and sets up a carousel
		function loadCarousel(n) {
			$.get(urlPrefix + n + ".txt", { }, function (data) {
				$("#sidePagerContent").empty().html(data);
				$("#sidePagerNav").empty();
				$('#sidePagerContent a').Tooltip({
					delay: 0,
					showURL: false,
					track: true
				});

				$("#sidePagerContent ul.cards").jcarousel({
					scroll: 1,
					animation: 'slow',
					initCallback: function (carousel) {
						// create buttons for carousel navigation
						var nav = $("#sidePagerNav");
						$("#sidePager ul.cards li").each(function() {
							nav.append("<a href='#sidePager'>" + $(this).attr("jcarouselindex") +  "</a>");
						});
						nav.find('a')
							.click(function() {
								carousel.scroll($.jcarousel.intval($(this).text()));
								return false;
							})
							.slice(0, 1).addClass("current");

						// dirty method for centering block with unknown width
						nav.css("marginLeft", -1 * nav.width() / 2);
					},
					itemLoadCallback: {
						onBeforeAnimation: function () { },
						onAfterAnimation: function (carousel) {
							// mark proper button as active
							$("#sidePagerNav a").removeClass("current");
							$("#sidePagerNav a").slice(carousel.last - 1, carousel.last).addClass("current");
						}
					}
				});
			});
		}
		loadCarousel(1);
		$("#sidePager select").change(function() {
			loadCarousel($(this).val());
		});
	});
} // sidePager()

$(function($) {
	$(".slidingTips a").each(function() {
		var s = $(this).attr("title");
		$(this).attr("title", "");
		$(this).attr("tooltip", s);
	});

	$(".slidingTips li").hover(
		function(){
			$(".slidingTip").animate({ opacity: 'hide' }, 150);
			$(this).append('<div class="slidingTip">'+ $("a", this).attr('tooltip') +'</div>');
			$(".slidingTip", this).animate({ opacity: 'show' }, 350);
		},
		function(){
			setTimeout(function(){
				$(".slidingTip").animate({ opacity: 'hide' }, 350);
			}, 500);
		}
	);

	// Rechercher fields
	$(".RechercheInput").bind("focus", function(){ if(this.value == "RECHERCHER") this.value = ""; });
	$(".RechercheInput").bind("blur", function(){ if(this.value == "") this.value = "RECHERCHER"; });

	// Newsletter fields
	$(".NewsletterInput").bind("focus", function(){ if(this.value == "Votre email") this.value = ""; });
	$(".NewsletterInput").bind("blur", function(){ if(this.value == "") this.value = "Votre email"; });

	// Bookmarker
	$("ul.tools li.fav a").click(function() {
		$.get("source/bookmarker.txt", {}, function (data) {
			$("body").append(data);
			var m = parseInt($("#bookmarker").css("margin-top"));
			$("#bookmarker")
				.css("margin-top", m + $(window).scrollTop())
				.show()
				.bgiframe()
			$("#bookmarker a.close").click(function() {
				$("#bookmarker").remove();
				return false;
			});
		});
		return false;
	});

	$(".selectCheckBoxButton").hover(
		function(){
			$(".selectCheckBoxButton").click(function() {
				$(".selectCheckBox", this).show(333);
			});
		},
		function() {
			setTimeout(function(){
				$(".selectCheckBox", this).hide(333);
			}, 260);
		}
	);
});