document.write('<style type="text/css">body #content .more{display:none;};</style>');

preloadImage("images/menu/biomasse.png");
preloadImage("images/menu/eolien.png");
preloadImage("images/menu/marine.png");
preloadImage("images/menu/solaire.png");
preloadImage("images/menu/biomasse_on.png");
preloadImage("images/menu/eolien_on.png");
preloadImage("images/menu/marine_on.png");
preloadImage("images/menu/solaire_on.png");

function getFlashVersion(){ 
  // ie 
  try { 
	try { 
	  // avoid fp6 minor version lookup issues 
	  // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
	  var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
	  try { axo.AllowScriptAccess = 'always'; } 
	  catch(e) { return '6,0,0'; } 
	} catch(e) {} 
	return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
  // other browsers 
  } catch(e) { 
	try { 
	  if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){ 
		return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
	  } 
	} catch(e) {} 
  } 
  return '0,0,0'; 
} 

finBannerHP = function()
{
	//nextBan();
}

startHome = function() {
	$('#neoen').show();
		
	var cssObj = {
      'background' : '#FFFFFF url(../images/background.png) fixed repeat-x bottom left',
      'overflow' : 'auto'
    }
    $('#home').css(cssObj);
	
	
	$(".diaporama ul").diaporama({
		delay: 2,
		keepSense: false,
		showTitle:true
	});
	updateBan();
}
startVoeux2012 = function() {
	var flashvars = {
		urlLib: "lib_voeux2012.swf",
		urlSkip: "http://www.neoen.fr"
	};
	var params = {
	  menu: "false",
	  allowfullscreen:"false"
	};
	var attributes = {
	  id: "swf_2012",
	  name: "swf_2012"
	};
	swfobject.embedSWF("neoen_voeux2012.swf", "swf_2012", "100%", "100%", "9.0.0.0", false, flashvars, params, attributes);
}
function closeModule() {
	//window.location = "http://www.neoen.fr";
	$('#swf_voeux2012').html('');
	$('#swf_voeux2012').hide();
	startHome();
}
	

$(document).ready(function(){

	var version = getFlashVersion().split(',').shift(); 	
	if(version > 0) {
		$("#animate_img").html("");
		$('#neoen').hide();
		
		if (ref != "" && ref.indexOf("neoen.fr/") != -1 && ref.indexOf(".php") != -1) {
			closeModule();
			startHome();
		} else {
			startVoeux2012();
		}
	} else {
		closeModule();
		startHome();
	}
	/*
	$(".diaporama ul").diaporama({
		delay: 2,
		keepSense: false,
		showTitle:true
	});
	*/
	
	//Ascenseur
	iniScroll();
	setTimeout("iniScroll()", 500);
	
	/* $('#neoen #page #content .content .text .show a').each(function(){
		//On cache le bloc
		$(this).parent().prev().prev().prev().css('overflow', 'auto');
		$(this).parent().prev().prev().removeClass('active').slideUp(function(){iniScroll();});
		$(this).attr('title', 'En savoir +');
		$(this).html("En savoir +");
	}); */
	$('#neoen #page #content .content .text .show a').click(function(){
		if($(this).html().indexOf("+") >= 0){
			//On affiche le bloc
			$(this).parent().prev().prev().prev().css('overflow', 'visible');
			$(this).parent().prev().prev().addClass('active').slideDown(function(){iniScroll();});
			$(this).attr('title', 'masquer');
			$(this).html("- masquer");
		}else{
			//On cache le bloc
			$(this).parent().prev().prev().prev().css('overflow', 'auto');
			$(this).parent().prev().prev().removeClass('active').slideUp(function(){iniScroll();});
			$(this).attr('title', 'En savoir +');
			$(this).html("En savoir +");
		}
		return false;
	});
	$('#neoen #page #content .content .text h2').hover(function(){
		if($(this).next().next().hasClass("more"))
			$(this).css("cursor", "pointer");
	});
	$('#neoen #page #content .content .text h2').click(function(){
		if($(this).next().next().hasClass("more")){
			var a = $(this).next().next().next().next().find("a");
			if($(a).html().indexOf("+") >= 0){
				//On affiche le bloc
				$(a).parent().prev().prev().prev().css('overflow', 'visible');
				$(a).parent().prev().prev().addClass('active').slideDown(function(){iniScroll();});
				$(a).attr('title', 'masquer');
				$(a).html("- masquer");
			}else{
				//On cache le bloc
				$(a).parent().prev().prev().prev().css('overflow', 'auto');
				$(a).parent().prev().prev().removeClass('active').slideUp(function(){iniScroll();});
				$(a).attr('title', 'En savoir +');
				$(a).html("En savoir +");
			}
		return false;
		}
	});
	
	$('.selectAllID').click(function(){
		if($(this).attr('checked'))
			$(".selected_id_"+$(this).val()).attr('checked',true);
		else
			$(".selected_id_"+$(this).val()).attr('checked',false);
	});
	
	$('.publishAllSelectedItems').click(function(){
		var ids = "";
		$(".selected_id_"+$(this).attr('rel')+":checked").each(function(){
			ids += ","+$(this).val();
		});
		ids = ids.substring(1, ids.length);
		location.href += "&publier="+ids;
		return false;
	});
	$('.unpublishAllSelectedItems').click(function(){
		var ids = "";
		$(".selected_id_"+$(this).attr('rel')+":checked").each(function(){
			ids += ","+$(this).val();
		});
		ids = ids.substring(1, ids.length);
		location.href += "&depublier="+ids;
		return false;
	});
	$('.deleteAllSelectedItems').click(function(){
		var ids = "";
		$(".selected_id_"+$(this).attr('rel')+":checked").each(function(){
			ids += ","+$(this).val();
		});
		ids = ids.substring(1, ids.length);
		location.href += "&del="+ids;
		return false;
	});
	
	$('#content .content .intro table, #content .box .offres li, #content .box .realisations').click(function(){
		var base = $('base').attr('href');
		window.location = base+$(this).find("a").attr("href");
	});
	
	$('#content .box .potentiel').hover(function(){
			var img = $(this).find("img.picto");
			adr = $(img).attr("src").split(".")[0];
			$(img).attr("src",adr+"_on.jpg");
		},
		function(){
			var img = $(this).find("img.picto");
			adr = $(img).attr("src").split("_on")[0];
			$(img).attr("src",adr+".jpg");
		}
	);
	
	//ouverture des liens externes
	$('a._blank').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	$('a._player').click(function(){
		var width  = "100%";
		var height = "100%";
		var top = 0;
		var left = 0;
		var w = "";
		var h = "";
		var margin = "0 0 0 0";
		if($(this).attr('name')){
			dim = $(this).attr('name').split("x");
			width  = dim[0]+"px";
			height = (parseInt(dim[1]) + 100)+"px";
			w  = dim[0];
			h = dim[1];
			top="50%";
			left="50%";
			margin="-"+((parseInt(dim[1]) + 100) / 2)+"px 0 0 -"+(dim[0] / 2)+"px";
		}
		$("body").append('<div id="nyroModalFull" style="display: none; z-index: 100; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%;">'
			+'	<div id="nyroModalBg" style="overflow: hidden; background-color: rgb(0, 0, 0); position: absolute; top: 0px; left: 0px; height: 100%; width: 100%;"/>'
			+'	<div id="nyroModalWrapper" style="position: absolute; top: '+top+'; left: '+left+'; margin: '+margin+'; display: block; width: '+width+'; height: '+height+';">'
			+'		<div id="wrapperSwf" style="width: '+width+'; height: '+height+'"></div>'
			+'	</div>'
			+'</div>');
		
		swfobject.embedSWF("player-neoen/player_neoen.swf", "wrapperSwf", "100%", "100%", "8.0.0", false, {file: $(this).attr("href"), displaywidth: w?w:null, displayheight: h?h:null, image:$(this).attr("href").replace(/\.flv$/, ".jpg")}, { wmode: "transparent", allowfullscreen:"true" }, { id: "wrapperSwf", name: "wrapperSwf" });
		$("#nyroModalWrapper").hide();
		$("#nyroModalFull").show();
		$("#nyroModalBg").fadeTo("slow", 0.75, function(){
			$("#nyroModalWrapper").fadeIn();
		});
		return false;
    });
	
	if($.browser.msie && $.browser.version=="6.0")
		correctPNG();
	
	//Formulaire de contact
	sowContactFields();
	$('#contact_form #type').change(function(){
		sowContactFields();
	});
	
	/*
	//Pagination sur la home
	if($("#content .banniere").hasClass("banniere") && version > 0)
		updateBan();
	*/
		
	$("#content .banniere .pagination li a").click(function(){
		if($(this).parent().hasClass("prev")) prevBan();
		else if($(this).parent().hasClass("next")) nextBan();
		else goToBan(this);
		return false;
	});
	$('#content .banniere .pagination li').click(function(){
		if($(this).hasClass("prev")) prevBan();
		else if($(this).hasClass("next")) nextBan();
		else goToBan($(this).find('a'));
		return false;
	})
	
	//Box potentiel dans un popin
	
	$('#content .box .potentiel').click(function(){
		var base = $('base').attr('href');
		openPopin(base+$(this).find("a").attr("href"),780,540);
		return false;
	});
	$('#content .box .potentiel a').click(function(){
		var base = $('base').attr('href');
		openPopin(base+$(this).attr("href"),780,540);
		return false;
	});
	
	
	$('.masque, #content .preview img, #content .more img').each(function(){
		addRoundedCornerToImages($(this));
	});
	
	
	incrementeCompteur();
});
closePlayer = function(){
	$("#nyroModalFull").remove();
}

var incrementeConteurTimeout = null;
function incrementeCompteur(){
	clearTimeout(incrementeConteurTimeout);
	//Incrémentation du compteur
	$('.compteur .montant span').each(function(){
		var valeurTotale = $(this).attr("title");
		var valeurAffichee = $(this).text();
		var valeurSpeed = $(this).parent().attr("title");
		
		var precision = parseInt(valeurTotale.length - valeurAffichee.length);
		
		valeurTotale = parseInt(valeurTotale) + parseInt(valeurSpeed);
		$(this).attr("title", valeurTotale);
		
		
		valeurAffichee = (valeurTotale+"").substr(0, (valeurTotale+"").length - precision)
		if($(this).hasClass("space")){
			i = (valeurAffichee+"").length;
			tempValue = "";
			for(i; i >= 3; i -= 3)
				tempValue = (valeurAffichee+"").substring(i-3, i)+" "+tempValue;
			valeurAffichee = (valeurAffichee+"").substring(0, i)+" "+tempValue;
				
		}
		$(this).text(valeurAffichee);
	});
	incrementeConteurTimeout = setTimeout("incrementeCompteur()",1000);
}

var numRoundedImage = 0;
addRoundedCornerToImages = function(obj){
	var id = $(obj).attr("id");
	if(!id)
		$(obj).attr("id", "rounded"+numRoundedImage);
	numRoundedImage ++;
	
	var img = new Image();
	img.id = obj.attr('id');
	$(img).load(function(){
		var src = this.src;
		var alt = this.alt;
		var title = $('#'+this.id).attr('title');
		var css = $('#'+this.id).attr('class');
		var h = $('#'+this.id).css('height')?$('#'+this.id).css('height').split("px")[0]:this.height;
		var w = $('#'+this.id).css('width')?$('#'+this.id).css('width').split("px")[0]:this.width;
		//alert(w+"x"+h);
		var html = '<div class="masque '+css+'" style="width:'+w+'px;height:'+h+'px; display:inline-block" >'+
			'<div class="tl"></div>'+
			'<div class="tr"></div>'+
			'<div class="bl"></div>'+
			'<div class="br"></div>'+
			'<img src="'+src+'" alt="'+alt+'"'+(title?' title="'+title+'"':'')+' style="width:'+w+'px;height:'+h+'px;" />'+
		'</div>';
		//alert('#'+this.id);
		$('#'+this.id).replaceWith(html);
	});
	img.src = $(obj).attr("src");
	//alert($(obj).attr("src"));
};
openPopin = function(url, w, h){
	$.nyroModalManual({
		url: url,
		width: w?w:null,
		height: h?h:null,
		windowResize: false,
		modal:true,
		endFillContent:function(){
			$('#nyroModalWrapper > div').prepend('<a href="#" class="nyroModalClose" id="closeBut" title="Fermer">Fermer</a>');
			$('.nyroModalClose').click(function(){
				$.nyroModalRemove();
				return false;
			});
			
		}
    });
	
};
goToBan = function(obj){
	if(! $(obj).parent().hasClass("active")){
		$("#content .banniere .pagination li.active").addClass('num').removeClass("active");
		$(obj).parent().addClass("active").removeClass("num");
		updateBan();
	}
}
nextBan = function(){
	var old = $("#content .banniere .pagination li.active");
	$(old).addClass('num').removeClass("active");
	if($(old).next().hasClass("next"))
		$("#content .banniere .pagination li:first").next().addClass('active').removeClass("num");
	else
		$(old).next().addClass('active').removeClass("num");
	updateBan();
}
prevBan = function(){
	var old = $("#content .banniere .pagination li.active");
	$(old).addClass('num').removeClass("active");
	if($(old).prev().hasClass("prev"))
		$("#content .banniere .pagination li:last").prev().addClass('active').removeClass("num");
	else
		$(old).prev().addClass('active').removeClass("num");
	updateBan();
}
var timeoutBan = null;
updateBan = function(){
	clearTimeout(timeoutBan);
	//On cache le flash
	$("#animate_text").hide();
	$('#animate').fadeOut('slow', function(){
	
		var href = "#ban0";
		if($("#content .banniere .pagination li.active a").attr("href"))
			href = $("#content .banniere .pagination li.active a").attr("href");
		var data = dataBan[href.split("#ban")[1]];
		
		//banniere, url, texte, texte_x, texte_y, texte_w, texte_h
		var type = data[0].split(".")[1];
		if(type == "swf"){
			$("#ban").html('<div id="animate" style="display:none">' +
								'<div class="image">' +
									'<span id="animate_img"></span>' +
									'<div id="animate_text" class="text"></div>' +
								'</div>' +
							'</div>');
			swfobject.embedSWF(data[0], "animate_img", 746, 355, "8.0.0", false, { }, { wmode: "transparent", allowfullscreen:"false" }, { id: "animate_img", name: "animate_img" });
		}else{
			$("#ban").html('<div id="animate" style="display:none">' +
								'<div class="image">' +
									'<img id="animate_img" class="animate_img" src="'+data[0]+'" alt="" />' +
									'<div id="animate_text" class="text" style="position:absolute;top:'+data[4]+'px;left:'+data[3]+'px;width:'+data[5]+'px;height:'+data[6]+'px">'+data[2]+'</div>' +
								'</div>' +
							'</div>');
		}
		
		$('#ban').css('cursor', 'pointer').attr('rel', data[1]).click(function(){
			window.location = $(this).attr('rel');
		});
		
		var l = parseInt(data[3]);
		var r = 745 - (parseInt(data[5])+parseInt(l));
		horSens = ((r>l)?"left":"right");
		
		if(horSens == "left")
			$("#animate_text").hide().css("marginLeft",-200).css("opacity", 0);
		else
			$("#animate_text").hide().css("marginLeft",200).css("opacity", 0);
		
		$("#animate").fadeIn("slow",
			function()
			{
				if(horSens == "left")
				{
					$("#animate_text").fadeIn().animate({
						marginLeft: 15,
						opacity: 1
					}, 1000).animate({
						marginLeft: 0
					}, 120)
				}else
				{
					$("#animate_text").fadeIn().animate({
						marginLeft: -15,
						opacity: 1
					}, 1000).animate({
						marginLeft: 0
					}, 120)
				}
				$("#previewText").css('filter','none');
			}
		);
	
	
	});
	if(dataBan.length > 1)
		timeoutBan = setTimeout("nextBan()", 35000);
};


iniScroll = function(){
	$('#content .scroll').jScrollPane({showArrows:false, scrollbarWidth: 5, arrowSize: 5, scrollbarMargin: 10, wheelSpeed:50});
};

sowContactFields = function(){
	var val = $('#contact_form #type').attr('value');
	$('#contact_form .secteur, #contact_form .represente, #contact_form .commune').hide();
	if(val == "collectivite") $('#contact_form .represente').fadeIn();
	if(val == "entreprise") $('#contact_form .secteur').fadeIn();
	if(val == "riverain") $('#contact_form .commune').fadeIn();
}

function preloadImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])

	for(var i=0; i<document.images.length; i++)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText
			if (img.align == "left") imgStyle = "float:left;" + imgStyle
			if (img.align == "right") imgStyle = "float:right;" + imgStyle
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle      
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}

