var urlsite = "http://www.ausy.fr/";
$(document).ready(function(){
	//$(document).pngFix(); 
	$("#portrait").css("z-index","-1");
	$("#banner").css("z-index","-1");
	$("div.hidden").css("display","none");
	
	var color = "green";
	var bodyclass = $("body").attr("class");
	bodyclass = bodyclass.split("-");
	if (bodyclass[1]!="") color=bodyclass[1];
	// Plus table 2
	$("table[summary=plus] > tbody").each(function() {
		var size = $(this).children().size();
		if (size<=2) {
			$("tr", this).eq(1).children("td:first").addClass("text").hide();
			$(this).children("tr:eq(1)").prepend("<td class='plus clickable'><img src='"+urlsite+"img/plus-"+color+".png' width='20' height='20' alt='plus' /> Lire la suite...</td>");
		}
		if (size==2) {
			var caption = $("tr:first td", this).html();
			$("tr:first", this).hide();
			$(this).parent().prepend("<caption class='clickable'>"+caption+"</caption>");
		}
	});
	$("table[summary=plus] tbody tr td.clickable").click(function() {
		if ($(this).parent("tr").parent("tbody").find("td.text").css("display")=="none") {
			$(this).parent("tr").parent("tbody").find("td.text").show();
			$(this).parent("tr").parent("tbody").find("td.plus").html("<img src='"+urlsite+"img/moins-"+color+".png' width='20' height='20' alt='minus' />&nbsp;");
		} else {
			$(this).parent("tr").parent("tbody").find("td.text").hide();
			$(this).parent("tr").parent("tbody").find("td.plus").html("<img src='"+urlsite+"img/plus-"+color+".png' width='20' height='20' alt='plus' /> Lire la suite...");
		}
	});
	$("table[summary=plus] caption.clickable").click(function() {
		if ($(this).parent("table").find("td.text").css("display")=="none") {
			$(this).parent("table").find("td.text").show();
			$(this).parent("table").find("td.plus").html("<img src='"+urlsite+"img/moins-"+color+".png' width='20' height='20' alt='minus' />&nbsp;");
		} else {
			$(this).parent("table").find("td.text").hide();
			$(this).parent("table").find("td.plus").html("<img src='"+urlsite+"img/plus-"+color+".png' width='20' height='20' alt='plus' /> Lire la suite...");
		}
	});
	$("table[summary=plus] .clickable").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});

	
	// Divers
	$("#hide_form_recherche").click(function(){ $("#form_recherche").slideUp();});
	$(".toggle_recherche").css("cursor", "pointer").click(function(){ $("#form_recherche").toggle("slow");});
	$("#menu_archives").click(function(){ $("#menu_archives .ul").slideDown("slow");});
	$("#menu_actuelles").click(function(){ $("#menu_actuelles .ul").slideDown("slow");});
	$("#tablehomepage tr td").eq(0).hover(function() {
		$(this).css("background-color", "#D1E8EF");
	}, function() {
		$(this).css("background-color", "transparent");
	});	
	$("#tablehomepage tr td").eq(1).hover(function() {
		$(this).css("background-color", "#D1E8EF");
	}, function() {
		$(this).css("background-color", "transparent");
	});	
	$("#tablehomepage tr td").eq(2).hover(function() {
		$(this).css("background-color", "#DFC3CB");
	}, function() {
		$(this).css("background-color", "transparent");
	});	
	$("#tablehomepage tr td").eq(3).hover(function() {
		$(this).css("background-color", "#D1E8EF");
	}, function() {
		$(this).css("background-color", "transparent");
	});	
	
	$('a.popup').nyroModal({bgColor: '#8F8F8F',width:'590',height:'500',contentLoading:''});
	$('area.popup').nyroModal({bgColor: '#8F8F8F',width:'590',height:'500',contentLoading:''});
	$('a[href*=/embed/]').nyroModal({bgColor: '#8F8F8F',width:'590',height:'500',contentLoading:''});
	
	// Formulaire Recrutement - Minimap
	$("#select_region option:selected").each(function() {
			var id = $(this).val();
			$("#minimap_"+id).show();
	});
	$("#select_region").change(function() {
		$(".minimap img").hide();
		$("#select_region option:selected").each(function() {
				var id = $(this).val();
				$("#minimap_"+id).show();
				if (id=="") n=n-1;
		});
		//$("#minimap_"+id).show();
	});
	$(".areas div").click(function() {
		var id = $(this).attr("class");
		var valeur = id.replace("minimap_", "");
		if ($('img#'+id).css("display")=="none") {
			$('img#'+id).show();
			$("#select_region option[value="+valeur+"]").attr("selected", "selected");
		} else {
			$('img#'+id).hide();
			$("#select_region option[value="+valeur+"]").removeAttr("selected");
		}
	});	
	
	// Effet de fond allant de droite à gauche
	$("#leftmenu ul li div.ul div.li:not(.selected) a")
		.css( {backgroundPosition: "-173px -11px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-360px -11px)",color:"#e1e2e3"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-173px -11px)",color:"#0093BD"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-173px -11px",color:"#0093BD"})
			}})
		})
	$("#leftmenu .menu_investisseurs ul li div.ul div.li:not(.selected) a")
		.css( {backgroundPosition: "-174px -11px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-360px -11px)",color:"#000"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-174px -11px)",color:"#000"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-174px -11px",color:"#000"})
			}})
		})
	$("#rightmenu .bloc div.ul div.li:not(.selected) a")
		.css( {backgroundPosition: "-173px -11px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-360px -11px)",color:"#e1e2e3"}, {duration:400})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-100px -11px)",color:"#9e1e5c"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-173px -11px"})
			}})
		})
	$("#rightmenu .menu_investisseurs .bloc div.ul div.li:not(.selected) a")
		.css( {backgroundPosition: "-173px -11px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-360px -11px)",color:"#3e3d40"}, {duration:400})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-100px -11px)",color:"#3e3d40"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-173px -11px"})
			}})
		})
	  
	  // Planisphere
	$('a.gotooff').click(function() {
		$('#paneimplantations').scrollTo( {top:'0px',left:'0px'}, 500 );
		$("div.menu ul li a").removeClass("selected");
		$("div.menu ul li a.gotooff").addClass("selected");
	});
	$('a.gotonear').click(function() {
		$('#paneimplantations').scrollTo( {top:'0px',left:'528px'}, 500 );
		$("div.menu ul li a").removeClass("selected");
		$("div.menu ul li a.gotonear").addClass("selected");
	});
	$('a.gotoproximite').click(function() {
		$('#paneimplantations').scrollTo( {top:'0px',left:'1056px'}, 500 );
		$("div.menu ul li a").removeClass("selected");
		$("div.menu ul li a.gotoproximite").addClass("selected");
	});
	
	// top navigation
    $('#topnavigation > li')
      .each(function(){
        var title = $('h2:first', this);
        var href = $('a:first', this).clone();
        href.text(title.text()).wrapInner('<span></span>');
        title.html(href);
       })
      .find('h2 > a').bind('mouseenter', function(){
        $(this).parents('li.column').find('> ul').slideDown("fast");
      }).end()
      .bind('mouseleave', function(){
        $('> ul', this).slideUp();
      });
	  
	  // Témoignages
	  $(".temoignages .bloc").hide().fadeIn(3000);
	  $(".temoignages .bloc .img img").hover(function(){
		  $(this).fadeTo("slow", 0.2); // This should set the opacity to 100% on hover
	    },function(){
		  $(this).fadeTo("slow", 1); // This should set the opacity back to 30% on mouseout
		});
	  
	  /* Retraicissement de la fenetre pour petit ecran */
	  var height = $('body').height();
	  if (height<800) {
		  $("#leftmenu .container").height("450px");
		  $("#leftmenu #implantations").css("top","140px");
		  $("#leftmenu #secteurs").css("top","280px");
		  $("#leftmenu .menu_investisseurs ul li:not(.info)").height("40px");
		  $("#leftmenu .menu_investisseurs #action_ausy").css("position", "relative").css("top", "30px");
		  $("#rightmenu .ad").height("80px").css("font-size", "12px").css("line-height", "12px");
		  $("#rightmenu .ad a").css("padding-top", "15px");
		  $("#rightmenu").height("450px");
		  $("#rightmenu .bloc#events").css("margin-bottom", "0px");
		  $("#rightmenu #universites").css("margin-bottom", "0px");
		  $("#rightmenu .bloc.contact_news").css("margin-bottom", "0px");
		  $("#rightmenu .menu_investisseurs .bloc").css("margin-bottom", "0px");
		  $("#rightmenu .bloc#news").css("position", "relative").css("top", "0px");
		  $("#rightmenu .menu_investisseurs .bottombloc").css("position", "relative").css("top", "0px");
		  $("#rightmenu .logo").hide();
	  }
	  var width = $('body').width();
	  if (width<1100) {
		  $("#header #mots").width((width-550)+"px").css("left", (((615-(width-550))/2))+"px");
	  }
});

offset = new Array();
function toFloat(elt,d) {
	if (!($.browser.msie && $.browser.version.substr(0,1)<7)) {

		offset[elt]= $(elt).offset().top;
		$(window).scroll(function() {
			$(elt).stop();
			var topToWindow = ($(window).scrollTop()>offset[elt])?$(window).scrollTop()-offset[elt]:0;
			if (topToWindow>160) {
				$(elt).animate({marginTop:topToWindow-160},{duration:d,queue:false});
			} else {
				$(elt).animate({marginTop:0},{duration:d,queue:false});
			}
		});
	}
}
toFloat('#rightmenu',500);
toFloat('#leftmenu',500);

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
	cntfield.value = maxlimit - field.value.length;
}