// ALL
var communImages = "/commun/images/"
var IE6
var IE
var w // window
// MON_PLAN
var nb_plans = 6
var current_plan = 1

var current_size

IE = $.browser.msie
if ($.browser.msie && $.browser.version == "6.0") {
	var IE6 = true
}
w = $(window)

function spacer_ajuste() {

	if (current_size != w.height()) {
		current_size = w.height()
		$c = $("body").children(":visible").not("#spacer")
		$spacer = $("#spacer")
		var tot_height = 0
		for (var i = 0; i < $c.length; i++) {
			tot_height += $c.eq(i).height()
		}
		var sub = w.height() - tot_height
		if (sub <= 0) {
			sub = 0
		}
		$spacer.css({
			height: sub + "px"
		})
	}
}
$(document).ready(function() {
	$(".griser").css('opacity', '0.3');
	main()

	if ($("body.choix_projet").length > 0) {

		page_choix_projet()
	} else if ($("body.mon_plan").length > 0) {
		mon_plan()
	} else if ($("body.mon_compte").length > 0) {
		mon_compte()
		activeOutil()
	} else if ($("body.mon_installateur").length > 0) {
		mon_installateur()
		activeOutil()
	}

});


function activeOutil() {
	$("div.outils ul li a , td.send a").mouseover(function() {
		$(".outil_bulle").hide()
		var fonction = $(this).attr("class")

		if ($("#" + fonction).children('.elec_name').length > 0) {
			$("#" + fonction).children('.elec_name').removeClass("level1").removeClass("level2").removeClass("level3")
			$("#" + fonction).children('.elec_name').html("<br/>\"" + $(this).parent().parent().children(".entreprise").text() + "\"")

			var classs = $(this).parent().parent().children(".coordonnees").attr("class")
			$("#" + fonction).children('.elec_name').addClass(classs)

		}

		s = $(this).offset()

		var t = s.top - $("#" + fonction).height() - 25

		if (!IE) {
			var l = s.left - 10
		} else {
			var l = s.left
		}

		$("#" + fonction).css("top", t)
		$("#" + fonction).css("left", l)
		$("#" + fonction).fadeIn()

	}).mouseout(function() {
		var fonction = $(this).attr("class")
		$("#" + fonction).hide()
	})
}


function hide_connexion() {
	if ($("#connexion_rapide").css('display') != "none") {
		$("#connexion_rapide").slideUp()
	}
}

// JS POUR TOUS

function main() {
	if ($(".datepicker").length > 0) {
		$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });;
	}
	
	
	if ($(".error input").length > 0) {
		$(".error input").click(function(){
			$(this).parent().removeClass("error")
			$(this).val("")
		})
	}

	if ($("#spacer").length > 0) {
		var timer = setInterval("spacer_ajuste()", 300);
	}

	$("div.button").mouseover(function() {
		$(this).children().addClass("over")

	}).mouseout(function() {
		$(this).children().removeClass("over")
	}) 
	$("div.button_annuler").mouseover(function() {
		$(this).children().addClass("over")

	}).mouseout(function() {
		$(this).children().removeClass("over")
	})

	$(".not_logged").click(function() {
		if ($("#connexion_rapide").css('display') == "none") {
			$("#connexion_rapide").slideDown()
		} else {
			$("#connexion_rapide").slideUp()
		}
	})
	$(".logged").click(function() {
		if ($("#connected").css('display') == "none") {
			$("#connected").slideDown()
		} else {
			$("#connected").slideUp()
		}
	})
	
	$("#connexion_rapide").click(function(event) {
		event.stopPropagation();
	})
	$("#connected").click(function(event) {
		event.stopPropagation();
	})
	$(".not_logged").click(function(event) {
		event.stopPropagation();
	})
	$(".logged").click(function(event) {
		event.stopPropagation();
	})

	$('body').click(function() {
		hide_connexion()
	});
	alert_modal("erreur_connexion")

}



function alert_modal(value) {

	var $n = $("#" + value)
	if ($n.length > 0) {

		if (value == "check_envoi_installateur") {
			$n.dialog({
				resizable: false,
				width: 450,
				height: 450,
				draggable: false,
				modal: true,
				title: $("#" + value + " > code").html()
			})
		} else if(value == "force_connexion"){
			
			/*$("#tabs").tabs({
						event: 'click'
					});
			$(".ui-tabs-nav.ui-widget-header").css("background","white").css("border","none").css("border-bottom","1px solid silver")
			$("#tabs").css('border',"none")
			
			
			$n.dialog({
				resizable: false,
				width: 450,
				height: 470,
				draggable: false,
				closeOnEscape: false,
				modal: true,
				title: $("#" + value + " > code").html(),
				open: function(event, ui) { $(".ui-dialog-titlebar-close").hide()  }
				
			})*/
			window.location.href= '/solutions-domotique-personnalisee/avantages/';
			
		}
		else if (value == "confirmation_envoi_mail") {
			$n.dialog({
				resizable: false,
				width: 437,
				height: 490,
				draggable: false,
				modal: true,
				title: $("#" + value + " > code").html()
			})
		}
		else if (value == "ami_envoi_mail") {
			$n.dialog({
				resizable: false,
				width: 437,
				height: 490,
				draggable: false,
				modal: true,
				title: $("#" + value + " > code").html(),
				open: function(event, ui) { $(".ui-dialog-titlebar-close").hide()  }
			})
		}
		  else if (value == "duplicate_projet") {
				$n.dialog({
					resizable: false,
					width: 460,
					height: 420,
					draggable: false,
					modal: true,
					title: $("#" + value + " > code").html()
				})
			}  
			
			else if (value == "supp_mobilier") {
					$n.dialog({
						resizable: false,
						width: 250,
						height: 120,
						draggable: false,
						modal: true,  
						title: $("#" + value + " > code").html()                            
					})
				}
			else if (value == "connexion_popin") {
					$n.dialog({
						resizable: false,
						width: 428,
						height: 255,
						draggable: false,
						modal: true,  
						title: $("#" + value + " > code").html()                            
					})
				}
		
		else {
			$n.dialog({
				resizable: false,
				draggable: false,
				modal: true,
				title: $("#" + value + " > code").html()
			})
		}

		$n.children(".close_modal").click(function() {
			$("#" + $(this).parent().attr("id")).dialog("close")
		})
	}
}

// JS pour mon_installateur

function mon_installateur() {
	$("[name='mode']").click(function(){
		$("#form_valide_code_postal").submit()
	})
}

// JS POUR MON COMPTE

function mon_compte() {
	$("#annule").click(function(){
		window.location.href = "/solutions-domotique-personnalisee/mes-informations/?random="+Math.floor(Math.random()*100)
		})
	$("#close_ami_envoi_mail").click(function() {
		$("#ami_envoi_mail").dialog( "close" )
	})
	$("#change_mdp").click(function() {
		$(this).hide()
		$("#mdp").fadeIn()
	})
	var hsh = window.location.hash
	if(hsh != ""){
		if(hsh.substr(0,3) == "#id" || hsh.substr(0,2) == "id"){
			var  reg=new  RegExp("[^0-9]", "g");
			hsh = hsh.replace(reg,"")
			if($("div[idProjet="+hsh+"]").length > 0){
				//alert(hsh+" existe : "+$("div[idProjet="+hsh+"]").attr('nomProjet'))
				
				$("#champ_idProjet").val(hsh)
				$("#champ_nomProjet").html($("div[idProjet="+hsh+"]").attr('nomProjet'));
				alert_modal("check_envoi_installateur")
			}
			
		}
		
	}

}

// JS POUR MON_PLAN
function mon_plan() {
	$('#plan'+current_plan).addClass('actif');
	$('#module_choix_plan a').click(function()
	{
		if( !$(this).hasClass('actif') )
		{
			$('#module_choix_plan a').removeClass('actif');
			$(this).addClass('actif');
			current_plan =  parseInt( $(this).attr('id').replace('plan', '') );
		}
		return false;
	})
	/*
	if (document.images) {
		preload_image_object = new Image();
		for (var i = 0; i <= nb_plans; i++)
		preload_image_object.src = communImages + "mon-plan-plans" + i + ".png"
	}

	$("h1").html("")

	// CSS FIX pour IE6
	if (IE6) {
		$("#flecheG").css("margin-left", "10px")
		$("#flecheD").css("margin-left", "13px")
		$("#visionneuse").css("margin-left", "13px")
	}

	v_set(current_plan)

	$("#flecheG").mouseover(function() {
		$(this).css("background-position", "-25px 0px")

	}).mouseout(function() {

		$(this).css("background-position", "-25px -45px")
	}).click(function() {
		plan(-1)
		xt_click(this,'C','','M3_Mon-plan_Changement-Plan','A')
	})

	$("#flecheD").mouseover(function() {
		$(this).css("background-position", "0px 0px")

	}).mouseout(function() {

		$(this).css("background-position", "0px -45px")
	}).click(function() {
		plan(1)
		xt_click(this,'C','','M3_Mon-plan_Changement-Plan','A')

	})

	function plan(a) {
		sortie = a + current_plan
		if (sortie <= 0) {
			sortie = nb_plans
		} else if (sortie > nb_plans) {
			sortie = 1
		}
		current_plan = sortie
		v_set()

	}


	function v_set() {

		if (IE6) {
			$('#visionneuse').html("<div style='height:200px;width:280px;background:url(" + communImages + "mon-plan-plans" + current_plan + ".png) 0 0 no-repeat;display:block;' /></div>")
			$('#visionneuse').children("div").pngFix()
			$('#visionneuse').pngFix()
		} else {
			$('#visionneuse').html("<img height='200' width='280' src='" + communImages + "mon-plan-plans" + current_plan + ".png' alt='plan" + current_plan + "' style='display:none;' />")
		}
		if (IE) {
			$('#visionneuse').children().show("clip", {},
			500)
		} else {
			$('#visionneuse').children().show("puff", {},
			500)
		}

	}
	*/
}

// JS POUR CHOIX_PROJET
function page_choix_projet() {

	var flashvars = {};
	var params = {
		wmode: "transparent"
	};
	swfobject.embedSWF("/commun/video_niveaux/video_niveaux.swf", "aide_flash", "50", "50", "10.0.0", "/js/swfobject/expressInstall.swf", flashvars, params);

	$("h1").html("")

	$("div.choix").mouseover(function() {
		var d = ""
		$choix = $(this).parent().children(".choix")
		$this = $(this)
		$this_n = $choix.index($(this))

		for (i = 0; i < $choix.length; i++) {
			$current = $choix.eq(i)
			if (parseFloat($this.height()) < 250 && i == $this_n) {
				$current.addClass("over")
				$("#form_projet").appendTo($current).show()
				$("#project_finition").val(i+1);
				$current.stop().animate({
					height: '350px'
				},
				320, 'linear')

			} else if (i != $this_n) {
				$current.removeClass("over")
				$current.stop().animate({
					height: '187px'
				},
				100, 'linear')
			}

		}
	}).click(function(event) {
		event.stopPropagation();
	})

	$('body').click(function() {
		$("#form_projet").hide()
		$(".choix").removeClass("over")
		$(".choix").stop().animate({
			height: '187px'
		},
		100, 'linear')

		hide_connexion()
	});

}


$(document).ready(function()
{
	/*
	 *		:: ANIMATION :: BOUTON EDITION ::
	 */
	$("#retour_edition").hover(function(){
		$(this).addClass("hover")
	}, function(){
		$(this).removeClass("hover")
	})
	$("#retour_edition:not(.top_190)").css("top", "65px").css("display", "block")
	$("#nouveau_projet").css("top", "65px").css("display", "block").hover(function(){
		$(this).addClass("hover")
	}, function(){
		$(this).removeClass("hover")
	})
})

