$(document).ready(function(){
	if ( (/MSIE 6\.0/).test(navigator.userAgent) ) {
		DD_belatedPNG.fix(".fix-png");
		//or DD_belatedPNG.fix(".Sample, .SampleDivLoopPNG, .SampleDivPositionPNG");
	}
	$("#input-search").bind("focus", function () {
		var jE = $(this);
		focusInput("Rechercher", jE);
	});
	$("#input-search").bind("blur", function () {
		var jE = $(this);
		blurInput("Rechercher", jE);
	});
	$("#input-email").val('Votre Email');
	$("#input-email").bind("focus", function () {
		var jE = $(this);
		focusInput("Votre Email", jE);
	});
	$("#input-email").bind("blur", function () {
		var jE = $(this);
		blurInput("Votre Email", jE);
	});		
	$("#button-email").bind("click", function () {
		return validate ();
	});

	$("#menu > ul > li").each(function(e){
		var thisLi = $(this);
		thisLi.bind("click", function() {		
			Cufon.refresh("#menu > ul > li > a");
			//return false;
		});
	});
	$("#hidden-submenu").hide();
	$(".wrap-button").hide();
	$(".selectType").addClass("hidecontent");
	$(".showcontent").removeClass("hidecontent");
	$(".subnav-extra > ul > li").each(function(e){
		var thisLi = $(this);
		thisLi.bind("mouseover", function() {
			thisLi.addClass("mouse-over");
		});
		thisLi.bind("mouseout", function() {
			thisLi.removeClass("mouse-over");
		});
	});
	if( jQuery(".SelectUI").length > 0) {
		jQuery(".SelectUI").addSelectUI({
			scrollbarWidth: 15 //default is 10
		});
	}
	$(".showcontent").bind("click",function() {										
		if ($(".boxsort").hasClass("hidecontent")){
			$(".boxsort").removeClass("hidecontent");
			$(".showcontent").addClass("selected");
		}
		else{
			$(".boxsort").addClass("hidecontent");
			$(".showcontent").removeClass("selected");
		}
		return false;
	});
	/*if(jQuery("#videoplayer").length == 1) {
		embedFlash ({
			url: "http://www.rte-france.fr/swf/video-article.swf",
			width: "455", 
			height: "328",
			flashPlayerID: "videoplayer",
			flashVersion: "9",
			wmode: 'transparent'
		});
	}*/
	
	function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
		
		// The index() method calculates the index from a
		// given index who is out of the actual item range.
		/*if( mycarousel_itemList.length > 1 ) {
			var idx = carousel.index(i, mycarousel_itemList.length);
			//carousel.add(idx, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
            AddText($(item).find('h4 > a'), mycarousel_itemList[idx - 1].heading);
            $(item).find('div.content > p:first').prepend(htmlspecialchars(mycarousel_itemList[idx - 1].text));
           	$(item).find('div.content > p:first a').append(htmlspecialchars(mycarousel_itemList[idx - 1].textlink));
		}*/
		jQuery('.jcarousel-control a:nth-child('+carousel.index(i, mycarousel_itemList.length)+')').addClass('active');
	};
	
	function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
		
		if(i > mycarousel_itemList.length) i = i%mycarousel_itemList.length || mycarousel_itemList.length;
		jQuery('.jcarousel-control a:nth-child('+i+')').removeClass('active');
		//carousel.remove(i);
		if(carousel.list.queue('goto').length > 0) carousel.list.dequeue('goto');
	};
	
	function mycarousel_initCallback(carousel) {
		
		var slider = jQuery('.jcarousel-control'),
			boundRight = -(slider.width()-498),
			boundLeft = 32,
			duration = 110 * (jQuery('.jcarousel-control a').length - 8),
			rectStep = function(now, fx) {
				
				var n = Math.floor(now)
				slider.css('clip', 'rect(0,'+(494-n)+'px,29px,'+(36-n)+'px)');
			},
			n = mycarousel_itemList.length;
		
		while(n--) {
			carousel.add(n, mycarousel_getItemHTML(mycarousel_itemList[n]));
		}
		
		jQuery('.jcarousel-prev').mouseenter(function() {
			
			slider.animate({ left: boundLeft },{ duration: duration, step: rectStep });
		});
		jQuery('.jcarousel-next').mouseenter(function() {
			
			slider.animate({ left: boundRight },{ duration: duration, step: rectStep });
		});
		jQuery('.jcarousel-prev').mouseleave(function() {
			
			slider.stop();
		});
		jQuery('.jcarousel-next').mouseleave(function() {
			
			slider.stop();
		});
		jQuery('.jcarousel-control a').mouseenter(function() {
			
			if(carousel.animating){
				
				var i = jQuery.jcarousel.intval($(this).prevAll().length+1);
				carousel.list.queue('goto', function(){
					
					mycarouselposition = i;
					if(carousel.list.queue('goto').length === 0) carousel.scroll(mycarouselposition);
					else carousel.list.dequeue('goto');
				});
			}
			else{
				
				mycarouselposition = jQuery.jcarousel.intval($(this).prevAll().length+1);
				carousel.scroll(mycarouselposition);
			}
			return false;
		});
		
		// setup autoCycle start/stopAuto builtin ne marche pas...
		
		var setupCycle = function(){
				
				if(mycarouselintervalid === null || mycarouselintervalid === undefined){
					
					mycarouselintervalid =  window.setInterval(function(){
						//console.log(carousel, carousel.index());
						if(++mycarouselposition > mycarousel_itemList.length) mycarouselposition = 1;
						carousel.scroll(mycarouselposition);
					}, 6000);
				}
				/*else{
					
					console.log('catastrophic failure avoided on cycle', mycarouselintervalid);
				}*/
			},
			teardownCycle = function(){
				
				window.clearInterval(mycarouselintervalid);
				mycarouselintervalid = null;
			},
			mycarouselintervalid,
			mycarouselposition = 1;
		
		jQuery('#mycarousel').mouseleave(setupCycle);
		jQuery('#mycarousel').mouseenter(teardownCycle);
		
		setupCycle();
		
		carousel.next = function(){
			
			if(carousel.animating) return;
			if(++mycarouselposition > mycarousel_itemList.length) mycarouselposition = 1;
			carousel.scroll(mycarouselposition);
		}
		carousel.prev = function(){
			
			if(carousel.animating) return;
			if(--mycarouselposition === 0) mycarouselposition = mycarousel_itemList.length;
			carousel.scroll(mycarouselposition);
		}
	};
	
	/**
	 * Item html creation helper.
	 */

	
	function mycarousel_getItemHTML(item) {
		if(item){
			return '<div class="item-wrapper"><img src="' +
					item.url + '" alt="' +
					item.title + '" width="530" height="316"/><div class="content fix-png"><h4><a ' +
					(item.external ? 'target="_blank" ' : '') + 'href="' +
					item.link + '" title="' +
					item.heading + '">' +
					item.heading + '</a></h4><p>' +
					htmlspecialchars(item.text) + ' <a ' +
					(item.external ? 'target="_blank" ' : '') + 'href="'
					+ item.link + '" title="' +
					item.textlink + '" class="fix-png">' +
					htmlspecialchars(item.textlink) + '</a></p></div></div>';
		}
	};
			
	if(jQuery("#mycarousel").length == 1) {
	
		if( mycarousel_itemList.length > 8 ) {
			$("#mycarousel-no-javascript").hide();
			$("#mycarousel").show();
			jQuery("#mycarousel > ul").jcarousel({
				start: sl,
				wrap: 'circular',
				scroll: 1,
				initCallback: mycarousel_initCallback,
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
			});
		}
		else {
			$("#mycarousel-no-javascript").hide();
			$("#mycarousel").show();
			jQuery('.jcarousel-control').css('left', '16px');
			jQuery("#mycarousel > ul").jcarousel({
				start: sl,
				scroll: 1,
				buttonNextHTML: '',
				buttonPrevHTML: '',
				initCallback: mycarousel_initCallback,
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
			});
		}
	}
	$(".box-bookmarks").addClass("content-hidden");
	jQuery("ul.list-functions > li.item-03 > a").bind("click", function () {
		var handle = $(this);
		if ( ! handle.hasClass("active")) {
			$(handle.attr("href")).removeClass("content-hidden");
			handle.addClass("active");
		}
		else {
			$(handle.attr("href")).addClass("content-hidden");
			handle.removeClass("active");
		}
		return false;
	});
	jQuery(".information-link").bind("click", function () {
        $lightbox.openPopup({
            rel: '#popup-journaliste',
            closeBtnText: 'fermer',
            themeClass: 'journaliste-popup'
        });
        return false;
    });
	jQuery(".envoyer-link").bind("click", function () {
        $lightbox.openPopup({
            rel: '#popup-envoyer',
            closeBtnText: 'fermer',
            themeClass: 'envoyer-popup'
        });
        return false;
    });
    jQuery(".print_link").bind("click", function () {
        window.open (jQuery(this).attr('href'),'mywindow','status=1,toolbar=0,scrollbars=1,width=700,height=800');
        return false;
    });
    
	/*Call Popup Thank You*/
	jQuery(".thanks-link").bind("click", function () {
        $lightbox.openPopup({
            rel: '#popup-thanks',
            closeBtnText: 'fermer',
            themeClass: 'thanks-popup'
        });
        return false;
    });
	maxHeight(".main-sommaire div.list-detail div.item-som", "strong", 5, 0);
	maxHeightOuter(".main-sommaire div.list-detail div.item-som", 5, 0);
	maxHeight(".maincontent-rub > div.block02 > div", "div.item-content", 3, 0);
	maxHeightOuter(".maincontent-rub > div.block02 > div", 3, 0);
	setLeft(".maincontent-rub > div.block01 > div");
	setTop(".maincontent-rub > div.block01 > div");
	maxHeight(".free-push > ul.list-default > li > div.content", "div.item-content", 3, 0);
	maxHeightOuter(".free-push > ul.list-default > li > div.content", 3, 0);
	setTimeout(function () {
		setPadding("#menu > ul > li.current > ul", 2, "#content");
		maxHeight(".rubric-listing-wrapper > ul > li", "div", 4, 0);
		maxHeightOuter(".rubric-listing-wrapper > ul > li", 4, 0);
		maxHeightOuter(".maincontent-rub > div.block01 > div", 2, 1);
	}, 1);
	if( jQuery(".wrappergeneral").outerHeight(true) <= 13 ) {
		jQuery(".wrappergeneral").addClass("fix-general");
	}
	if( jQuery(".wrap-subnav").outerHeight(true) <= 37 ) {
		jQuery(".wrap-subnav").addClass("subnav-nocontent");
	}
	jQuery("#menu > ul > li > a").each(function () {
		var minWidth = 95;
		if( jQuery(this).outerWidth(true) <= 135 ) {
			minWidth += "px";
			jQuery(this).css("width",minWidth);
		}
	});

	// bind form using 'ajaxForm' 
	if( jQuery('#registerForm').length==1){
	    jQuery('#registerForm').ajaxForm(
			{ 
			    target:        '.error-message',
			    error:function(responseText, statusText){
					alert('An error occurred');
				}, 
		        success: function(responseText, statusText){
					if(responseText){
						jQuery(".error-message").html(responseText);
						jQuery(".error-message").show();
					}
				}
		    }
	    );
	}
	
	if(jQuery('#send_to_friend_form').length==1){
	    jQuery('#send_to_friend_form').ajaxForm({ 
				target:	'.LightBoxPopupContent',
		    success: function(responseText, statusText) {
					if(responseText) {
						if (responseText.substring(0,2) == 'OK') {
							jQuery("a.CloseBtn").click();
							jQuery("#popup-envoyer").html(responseText.substring(3, responseText.length));
							$lightbox.openPopup({
					            rel: '#popup-thanks',
					            closeBtnText: 'fermer',
					            themeClass: 'thanks-popup'
					        });
						} else {
							jQuery(".LightBoxPopupContent").html(responseText);
						}
					}
				}
			});
	}

});
Cufon.replace(".vagroundedstd-font", { fontFamily: 'VAGRoundedStd' });
Cufon.replace(".vagroundedstd-thin-font", { fontFamily: 'VAGRoundedStdThin' });
Cufon.replace('.vagroundedstd-gradient-font', { fontFamily: 'VAGRoundedStd', color: '-linear-gradient(#00a2d9, #018cc6)' });
Cufon.replace("#menu > ul > li > a", { fontFamily: 'VAGRoundedStd',hover: true});
Cufon.replace("#menu ul li ul.list-default li a", { fontFamily: 'VAGRoundedStdLight',hover: true });
Cufon.replace(".results-antidot h3 span.text-type-one", { fontFamily: 'VAGRoundedStd' });
function setPadding (jPar1, jPar2, jPar3 ) {
	var jHeight = jQuery(jPar1).outerHeight(true) + jPar2 + "px";
	jQuery(jPar3).each(function () {
		$(this).css({
			paddingTop: jHeight
		});
	});
}
function focusInput(jPar1, jPar2) {
	if(jPar2.val() == jPar1) {
		jPar2.val("");
	}
}
function blurInput(jPar1, jPar2) {
	if(jPar2.val() == '') {
		jPar2.val(jPar1);
	}
}
function validate () {
    var isValid = true;
    var email = $("#input-email");
    $(".error-message").hide();
  if (email.val() == 'Votre Email' || email.val() == '') {
  	$(".error-message").html($("#message_text1").html());
		$(".error-message").show();
		isValid = false;
  } else if( !isEmail(email) ) {
		$(".error-message").html($("#message_text").html());
		$(".error-message").show();
		isValid = false;
	}else{
		$(".error-message").hide();
	}	
    return isValid;
}

/* Validation function */
function is5Digit (el) {
    return (/^[a-zA-Z0-9]{0,5}$/).test(el.val());
}
function isEmail (el) {
    var strEmail = el.val().replace(/(^ *)|( *$)/g, "");
    //if (!strEmail) return true;
    var reEmail = /^[&\'*+\/=?^_{}~a-z0-9.-]+@([a-z0-9-]+\.)+[a-z0-9]+$/;
    return reEmail.test(strEmail);
}
function setLeft(jPar1) {
	jQuery(jPar1).each(function () {
		if( jQuery(this).find("img").length >= 1 ) {
			var disHeight = jQuery(this).find(".item-content").outerHeight(true) - jQuery(this).find("img").outerHeight(true);
		}
		if( jQuery(this).find("img").length >= 1 && disHeight < 0) {
			var widthImg = jQuery(this).find("img").outerWidth(true);
			position = parseInt(jQuery(this).find("p.link-text-five").css("left"));
			var left = position + widthImg + "px";
			jQuery(this).find("p.link-text-five").css("position","absolute");
			jQuery(this).find("p.link-text-five").css("left",left);
			jQuery(this).find("p.link-text-five").css("width","200px");
		}
		else {
			jQuery(this).find("p.link-text-five").css("width","350px");
		}
	});
}
function setTop(jPar1) {
	var maxHeight = 0;
	jQuery(jPar1).each(function () {
		if( jQuery(this).find(".item-content").outerHeight(true) > maxHeight ) {
            maxHeight = jQuery(this).find(".item-content").outerHeight(true);
        }
	});
	maxHeight = maxHeight - 7 + "px" ;
	jQuery(jPar1).each(function () {
		jQuery(this).find("p.link-text-five").css("position","absolute");
		jQuery(this).find("p.link-text-five").css("top",maxHeight);
	});
}
function maxHeightOuter(jPar1, jPar2, jPar3) {
	var maxHeightOuter = 0;
	var curIndex = 0;
	var pointer = 0;
  var count = jPar2;
	var heightLink = 0;
  var length = jQuery(jPar1).length;
    //count start with 5
  while ( count > 0 && pointer < length ) {
		if ( jPar3 == 1 ) {
			heightLink = jQuery(jPar1).eq(curIndex).outerHeight(true) + jQuery(jPar1).eq(curIndex).find("p.link-text-five").outerHeight(true);
			if( heightLink > maxHeightOuter ) {
				maxHeightOuter = heightLink;
			}
		}
		if( jQuery(jPar1).eq(curIndex).outerHeight(true) > maxHeightOuter ) {
      maxHeightOuter = jQuery(jPar1).eq(curIndex).outerHeight(true);
    }
    curIndex++;
    count--;
    if ( count == 0 ) {
			if( jPar3 == 1 ) {
				maxHeightOuter = maxHeightOuter - 20;
			}
			else 
				maxHeightOuter = maxHeightOuter;
			maxHeightOuter += "px";
      while ( pointer < curIndex ) {
       	if (!jQuery(jPar1).eq(pointer).hasClass("clear-items")) {
					jQuery(jPar1).eq(pointer).css({
            height: maxHeightOuter
          });
        }
        pointer++;
      }
      pointer = curIndex;
      count = jPar2;
			maxHeightOuter = 0;
    }
  }
}
function maxHeight(jPar1, jPar2, jPar3, jPar4) {
	
	var maxHeight = 0;
	var curIndex = 0;
	var pointer = 0;
    var count = jPar3;
    var length = jQuery(jPar1).length;
    //count start with 5
    while ( count > 0 && pointer < length ) {
	        if( jQuery(jPar1).eq(curIndex).find(jPar2).outerHeight(true) > maxHeight ) {
	            maxHeight = jQuery(jPar1).eq(curIndex).find(jPar2).outerHeight(true);
	        }
	        curIndex++;
	        count--;
        if ( count == 0 ) {
            maxHeight =  maxHeight + jPar4 + "px";
            while ( pointer < curIndex ) {
            	if (!jQuery(jPar1).eq(pointer).hasClass("clear-items")) {
								jQuery(jPar1).eq(pointer).find(jPar2).css({
									height: maxHeight
								});
							}
              pointer++;
            }
            pointer = curIndex;
            count = jPar3;
            maxHeight = 0;
        }
    }
}
function onChange() {
	alert("1");
}

/**
 * split a tring and add word
 * @param jQuery item 
 * @param integer width
 * @param integer height
 * @param string word
 */
function splitByPixel(item, /*width,*/ height, word, text) {
	item.css('visibility', 'hidden');
/*	item.html(htmlspecialchars(text)); */
	item.html(text); 
	while (item.outerHeight() > 0 && item.outerHeight() > height) {
		text = text.substring(0, strrpos(trim(text), " "));
		item.html(trim(text)+word);
	}
	item.css('visibility', '');
}
/**
 * Add text to the item 
 *
 */
 function AddText(item, text) {
    item.css('visibility', 'hidden');
    item.html(htmlspecialchars(text));
    item.css('visibility', '');
 }

