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;

/* MAP */

var defaultMapId;

function showTeritory(number) {
	
	$MapBackground = $("#Map .Background");
	$MapBackground.find(".r" + number).animate({ opacity: 'show' }, 360);
	$MapContent = null;
	if(number != window.lastone) {
		$MapBackground.find(".r" + window.lastone).animate({ opacity: 'hide' }, 350);
		$MapContent = $("#Map .Content");
		$MapContent.animate({ opacity: 'hide' }, 250);
		var html = $.ajax({url: getMapContent(number), async: false}).responseText;
			setTimeout(function(){
				$MapContent.html(html);
				setTimeout(function(){
                $("#mapArea"+number).attr("href",$("#Map .Content a").attr("href"));
					$MapContent.animate({ opacity: 'show' }, 250);
				}, 100);
		}, 350);
	}
	window.lastone = number;	
}

function initMap() {
//// alert("base_home initMap")
$Map =$("#Map"); 
  if ($Map.size() < 1) { return; }

  // Hides teritorys' elements
  	$MapBackgroundter = $("#Map .Background .ter");
	$MapBackgroundter.animate({ opacity: 'hide' }, 50);

  // Init teritory event handlers
  mapAreas = $Map.find("area");
  $(mapAreas).each(
    function() {
      var href = $(this).attr("href");
      numb = href.replace("#","");
      $(this).attr("id","mapArea"+numb);
     // $(this).mouseover(function(){ var mapAreaId=$(this).attr("id"); number=mapAreaId.replace("mapArea",""); showTeritory(number); }); commented on 19/06/2009 by jaadds essential function
    }
  );
  $("#Map .Background").hover(function(){},function(){showTeritory(defaultMapId)});
  $MapBackgroundter.click(function() { curClass=$(this).attr("class"); number=curClass.replace("ter r",""); document.location.href=$("#mapArea"+number).attr("href"); });

  // Get and init Default Teritory
  defaultMapId = $("#Map .Content").attr("default");
  showTeritory(defaultMapId);

}

function checkPair(intParam){return ((intParam-1)%2); }

function compareheight(element) {
	/*$(element).each(function(i){
		if ((checkPair(i) == 1) || (checkPair(i) == -1)) {
			//if( $(element).next().is("div") ){
			//if( $(element).eq(i+1).is("div") ){
			//// alert($(element).size());
			if( $(element).size() != i ){
				// alert(i);
				$(this).addClass("toto" + i);
				$(this).next().addClass("toto" + (i+1));
				
				if( $(this).height() > $(this).next().height() )
					$(this).next().css('height', $(this).height() + 'px');
				else if( $(this).height() < $(this).next().height() )
					$(this).css('height', $(this).next().height() + 'px');
			}
		}
	});*/
	
	/*var countofelements = $(element).length;
	for(i = 0; i < countofelements; i=i+2) {
		if( $(element).eq(i+1).is("div") ){
			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;
		}
	}*/
	var heightBlockMax=0;

	$(element).each(function(){if( $(this).height() > heightBlockMax ) heightBlockMax = $(this).height(); });
	// get max height
	//$(element).each(function(){ $(this).css("height",heightBlockMax); }); // assign max height
	$(element).css("height",heightBlockMax); // assign max height
	heightBlockMax=0;
}

// sets up jCarousel with buttons for paging
$.fn.pagedCarousel = function (pagingElem, numberofvisible, resourcepath, options) {
	elem = $(this);
	pagingElem = $(pagingElem);
	
	var numberofelements = Math.ceil($("li", elem).length/numberofvisible);
	
	function drawButtons(on) {
		//pagingElem.html(' ');
		pagingElem.empty();
		if(numberofelements > 1){
			var stringElement = "";
			for(i = 0; i < numberofelements; i++) {
				if(i != on)
					//pagingElem.append('<a href="#" title="'+(i*numberofvisible+1)+'"><img src="' + resourcepath + 'icon_jc-point-blank.gif" alt="Point" /></a>'); commented on 19/06/2009 by jaadds
					stringElement +=  '<a href="#" title="'+(i*numberofvisible+1)+'"><img src="' + resourcepath + 'icon_jc-point-blank.gif" alt="Point" /></a>';
				else
					//pagingElem.append('<a href="#" title="'+(i*numberofvisible+1)+'"><img src="' + resourcepath + 'icon_jc-point-full.gif" alt="Point" /></a>');commented on 19/06/2009 by jaadds
					stringElement +=  '<a href="#" title="'+(i*numberofvisible+1)+'"><img src="' + resourcepath + 'icon_jc-point-full.gif" alt="Point" /></a>';
			}
			
			pagingElem.html(stringElement);
		}
			//// alert("base_home pagedCarousel loop end");
	}
	
	function reDrawButton(whichone) {

		$pagingElement = $("a", pagingElem);
		$pagingElement.removeClass('active');
		$pagingElement.eq(whichone).addClass('active').find("img").attr('src',resourcepath + 'icon_jc-point-full.gif');
		$pagingElement.not('.active').find("img").attr('src',resourcepath + '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);
	//// alert("base_home pagedCarousel end");
}


function sidePager(urlPrefix) {
	// handle block "Les organismes se pr?sentent"

	$sidePager = $("#sidePager");
	$sidePager.each(function() {
	$sidePagerContent = $("#sidePagerContent");
	$sidePagerNav = $("#sidePagerNav");
	$sidePagerNava = $sidePagerNav.find('a');
	$sidePagerContenta =  $sidePagerContent.find("a");
		// 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();
				$sidePagerContenta.Tooltip({
					delay: 0,
					showURL: false,
					track: true
				});
				$sidePagerContentulcards = $sidePagerContent.find("ul.cards"); 
				$sidePagerContentulcards.jcarousel({
					scroll: 1,
					animation: 'slow',
					initCallback: function (carousel) {
						// create buttons for carousel navigation
						var nav = $sidePagerNav;
						$appendSidePager = "";
						$sidePagerContentulcards.find("li").each(function() {
							$appendSidePager += "<a href='#sidePager'>" + $(this).attr("jcarouselindex") +  "</a>";
							//nav.append("<a href='#sidePager'>" + $(this).attr("jcarouselindex") +  "</a>"); commented on 20/06/2009 jaadds
						});
						nav.append($appendSidePager);						
						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
							$sidePagerNava.removeClass("current");
							$sidePagerNava.slice(carousel.last - 1, carousel.last).addClass("current");
						}
					}
				});		
			});
		}
		loadCarousel(1);
		$sidePager.find("select").change(function() {
			loadCarousel($(this).val());
		});
	});
	//// alert("base_home sidePager end");
} // sidePager()

function divPager(urlPrefix){
	// handle block "Les organismes se pr?sentent"
//// alert("base_home divPager start");
 $siderPager =$("#sidePager"); 
	$siderPager.each(function() {
	$sidePagerContent = $("#sidePagerContent");
	$sidePagerNav = $("#sidePagerNav");
	$sidePagerNava = $sidePagerNav.find('a');
	$sidePagerContenta =  $sidePagerContent.find("a");
		// loads content using ajax request and sets up a carousel
		function loadCarousel(n) {										 
			var data = $('#'+ urlPrefix + n).html();			
			$sidePagerContent.empty().html(data);			
			$sidePagerNav.empty();
			
			$sidePagerContenta.Tooltip({
				delay: 0,
				showURL: false,
				track: true
			});
			
			$sidePagerContentulcards = $sidePagerContent.find("ul.cards");
			$sidePagerContentulcards.jcarousel({
				scroll: 1,
				animation: 'slow',
				initCallback: function (carousel) {
					// create buttons for carousel navigation
					$sidePagerContentulcardsli = $sidePagerContentulcards.find("li");
					var cards = $sidePagerContentulcardsli;
					var nav = $sidePagerNav;
					$appendStr = "";
					$sidePagerContentulcardsli.each(function() {
						$appendStr += "<a href='#sidePager'>" + $(this).attr("jcarouselindex") +  "</a>";
					});
					
					var navLnks = $sidePagerNava;
					nav.append($appendStr);
						$(navLnks).click(function() {
							carousel.scroll($.jcarousel.intval($(this).text()));
							return false;
						})
						.slice(0, 1).addClass("current");
					
                    $(nav).addClass("onlyText");
                    

					if ($(cards).size() > 10) {
                        $(navLnks[0]).addClass("bigger");
					    var j=1,limitPage=(navLnks).size();
						$appendSidePgr = "";
					    for (var i=1; i<limitPage; i++) {
                          if (j != 4) {
                            $(navLnks[i]).addClass("offleft");
                            j++;
                            if (i == (limitPage-1)){
								$appendStr +=  "<a href='javascript:void(0);' class='bigger disabled'>" + ((i-j+6)) +  "</a>";
                              //$sidePagerNav.append("<a href='javascript:void(0);' class='bigger disabled'>" + ((i-j+6)) +  "</a>"); commented on 20/06/2009 jaadds
                            }
                          }
                          else {
                            $(navLnks[i]).addClass("bigger");
                            j=0;
                          }
					    }
						if($appendStr.length > 1){
							$sidePagerNav.append($appendStr);
							}
					}
					else {
                      $(nav).removeClass("onlyText");
                      $(navLnks).removeClass("bigger").removeClass("offleft");
					}
					// dirty method for centering block with unknown width
                    nav.css("marginLeft", Math.round(-1 * nav.width() / 2));
					
				},
				itemLoadCallback: {
					onBeforeAnimation: function () { },
					onAfterAnimation: function (carousel) {
						// mark proper button as active
						var biggersLnks=$sidePagerNav.find("a.bigger");
						$sidePagerNava.removeClass("current");
						if ($(biggersLnks).size() > 0) { $sidePagerNava.not(".bigger").addClass("offleft"); }
						curLink = $sidePagerNava.slice(carousel.last - 1, carousel.last);
						$(curLink).removeClass("offleft").addClass("current");
						if (($(biggersLnks).size() > 0) && ($(curLink).not(".bigger"))) { $sidePagerNav.css("marginLeft", Math.round(-1 * $sidePagerNav.width() / 2)); }
					}
				}
			});	
			
			$("div .logos").hoverClass("sfhover");
			
		}
		loadCarousel(1);
		$siderPager.find("select").change(function() {
			loadCarousel($(this).val());
		});			
		
	});
	// alert("base_home divPager end");
} // sidePager()

$(function($) {
		   // alert("base_home final global function");
	// 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"; });*/
	//// alert($("#salonsTabs"));
	$("#salonsTabs").tabs();
	
	if ($.browser.msie) {
		$("#salonsMap li.city").hover( 
			function() { $(this).addClass("cityHover"); },
			function() { $(this).removeClass("cityHover"); }
		);
	}
	
	//$("#dropMenu li ul").bgiframe();
	$("#liste_etablissement").css("visibility","visible");
	
});

function initNewsletter() {
	// alert("base_home initNewsletter");
	$newsLetterForm = $("#newsletterForm");
	var newsletterResponseURL = $newsLetterForm.attr("action") + "?";
	newsletterInputs = $newsLetterForm.find("input");
	$(newsletterInputs).each( function(){ newsletterResponseURL += $(this).attr("name") + "=" + $(this).attr("value") + "&"; } );
	newsletterResponseURL = newsletterResponseURL.substr(0, newsletterResponseURL.length-2);
        // // alert(newsletterResponseURL);
	// $.get("source/sendtofriend.txt", {}, function (data) {
	$.get(newsletterResponseURL, {}, function (data) {
			$("body").append(data);
			$newsletterResult = $("#newsletterResult");
			var m = parseInt($newsletterResult.css("margin-top"));
			$newsletterResult
				.css("margin-top", m + $(window).scrollTop())
				.show()
				.bgiframe()
			$newsletterResult.find("a.close").click(function() {							  
				$newsletterResult.remove();
				return false;
			});
		});
		return false;
}

function addToFavorites(curTitle,curURL) {
	// alert("base_home addToFavorites");
		if (window.sidebar) {
	   		window.sidebar.addPanel(curTitle.toString(),curURL.toString(),"");
	    } else if( window.external ) {
	   		window.external.AddFavorite(curURL, curTitle);
	    }
	}