/* Initialisation des module JS */

var src=''; 
var BaseUrl = '/idara/';
var Dossier = 'softart';
$(function(){ 

	$(".bgcontour2").click(function(){
		$(this).parent().find("img").click();
	     return false;
	});
	$(".bgcontour").click(function(){
	     $(this).parent().find("img").click();
	     return false;
	});
	$(".bgcontour3").click(function(){
		var link = $(this).parent().find("a");
			if (link.length > 0) window.open(link.attr('href'));
	     return false;
	});

	//Yoxview
	$(".yoxview").yoxview({
		lang:'fr'
	});
	$(".yoxview2").yoxview({
		allowInternalLinks: true,
		defaultDimensions: { iframe: { width: 770,height: 450 }},
		skin: "top_menu",
		lang:'fr'
	});
	$(".yoxview3").yoxview({
		allowInternalLinks: true,
		defaultDimensions: { iframe: { width: 770,height: 450 }},
		lang:'fr',
		skin: "top_menu"
	});
	
	
	// Evenement rotator
	var index_next = 0;
	var index_prev = 1;
	var i = 0;
	rotate = function(){
		
		var index_prev = $active.attr('rel');
	 	$(".realisation").fadeOut().removeClass('active');//Remove all active class
	 	$(".navi_elem").each(function(i) {
	 		$(this).removeClass('n'+i+'_active').addClass('n'+i);
			if ($(this).attr('rel') == index_next){
				$(this).fadeIn().addClass('n'+index_next+'_active').removeClass('n'+index_next);
			}
		})
	 	
	    $active.fadeIn().addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	}; 

	//Rotation  and Timing Event
	rotateSwitch = function(){
	    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds 
	        if ( $('.active').next('div').length === 0) { //If paging reaches the end...
	            $active = $('.realisation:first'); //go back to first
	            index_next = 0;
	        }else {
	        	$active = $('.active').next(); //Move to the next paging
	        	index_next++;
	        }
	        rotate(); //Trigger the paging and slider function
	    }, 6000); //Timer speed in milliseconds (7 seconds)
	};
	rotateSwitch();
	
	//On Click
	$(".navi_elem").live("click",function(){
		console.log(index_next);
		index_next = $(this).attr('rel');
		$(".navi_elem").each(function(i) {
	 		$(this).removeClass('n'+i+'_active').addClass('n'+i);
		})
		$(this).fadeIn().addClass('n'+index_next+'_active').removeClass('n'+index_next);
		$(".realisation").fadeOut().removeClass('active');//Remove all active class
		$(".realisation").each(function() {
			if ($(this).attr('rel') == index_next) $active = $(this); //Activate the clicked paging
		})
		
	    //Reset Timer
	    clearInterval(play); //Stop the rotation
	    rotate(); //Trigger rotation immediately
	    rotateSwitch(); // Resume rotation timer
	    return false; //Prevent browser jump to link anchor
	});
	
	//Slider 
	$('#slider').nivoSlider({
		controlNav:true,
		captionOpacity:0,
		pauseTime:6000
	});

	//List services
	$(".btn_top").live("click",function(){
		var list = $(this).parent().parent().find('.list_service');
		var head = $(this).parent();
		head.css("margin-bottom", "15px");
		list.slideDown('fast').show();
		$(this).hide();
	});
	$(".btn_bottom").live("click",function(){
		var list = $(this).parent().parent().find('.list_service');
		list.slideUp('slow');
		var btn_t = $(this).parent().parent().find('.btn_top');
		btn_t.show();
		var head = $(this).parent().parent().find('.left_head');
		head.css("margin-bottom", "5px");
	});

	$(".service").click(function(){
	     window.location=$(this).find("a").attr("href");
	     return false;
	});
	
	$(".left_head_int").click(function(){
	     window.location=$(this).find("a").attr("href");
	     return false;
	});
	
	/* Kwicks */ 
	$(".domaine_desc").jKwick({min: 50, max: 349, mid: 50,speed:50}); 
	
	/* Questions */
	$(".question").click(function () {
		var resp = $(this).parent().find('.reponse');
		resp.toggle('fast');
		});

	//contact
	src =$('#captcha').attr('src');
	$('#captcha').remove();
	$('#contcap').html('<img src="'+src+'" id="captcha" name="captcha" width="200" height="50" />');
	$('.formcaptch label').append ('<br /><span class="code_msg">(Si vous n\'arrivez pas à saisir correctement le mot, <a href="" onClick="this.blur(); new_phpcap();return false;" class="txt_red"> cliquer ici</a> )</span>'); 
	
	$('#contcap_connect').html('<img src="'+src+'" id="captcha" name="captcha" width="150" height="50" />');
	$('.code_connect').append ('<br /><span class="code_msg1">(Si vous n\'arrivez pas à saisir correctement le mot, <a href="" onClick="this.blur(); new_phpcap();return false;" class="txt_red"> cliquer ici</a> )</span>'); 
	
	//$("#formcommander").attr('novalidate', 'novalidate');
	$("#domaine_input").keypress(function(event) {
		  if (event.keyCode == '13') {
			  $("#btn_verifier").click(); 
			  return false;
		  }
	});	  
	$("#btn_verifier").click(function(){
		$('#text_exist_dom').html('<img src="/public/images/large-loading.gif"  />');
		$('#text_exist_dom1').empty();
		$(".heb_commande").css({"display":"none"});
		$(".domaine_suggest").css({"display":"none"});
		var formVerifier = $("#domaine_form");
		$.ajax({ url:formVerifier.attr("action"), data:formVerifier.serialize(), dataType:"json", type: "POST",
			success: function(json) {
				if (!json["valider"]){
					$('#text_exist_dom').html('<span class="txt_red"> '+json["msg"]+' </span>');
				}
				else{
					if (json["disponible"]) {
						$('#text_exist_dom').html('le domaine<span class="txt_blue"> "'+json["res"][json["type"]]["domaine"]+'" </span>est bien <span class="txt_green">disponible</span>')
						$(".heb_commande").css({"display":"block"});
						$('.heb_commande').attr('href','/commander/vuecommander/Domaine/'+json["res"][json["type"]]["domaine"]);
					}
					else {
						propose = json["propose"];
						domain_mult = '';
						for (i=0; i<propose.length;i++){
							domain_mult = domain_mult+'<tr><td class="span-7"> '+propose[i]["domaine"]+' </td><td class="span-4"><span class="txt_green">disponible</span></td><td class="span-2"><input type="radio" name="group1" class="radio_btn" id="radio'+i+'" value="'+propose[i]["domaine"]+'"></td></tr>';
						}
						$('#text_exist_dom').empty();
						$('#text_exist_dom1').html('le domaine<span class="txt_blue"> "'+json["res"][json["type"]]["domaine"]+'" </span>est malheureusement <span class="txt_red">indisponible</span>');
						$('.domaine_name_table').html(domain_mult);
						$('#radio0').attr('checked', 'checked');
						first_dom = $('#radio0').attr('value');
						$('#suggest_comm').attr('href','/commander/vuecommander/Domaine/'+first_dom);
						$(".domaine_suggest").css({"display":"block"});
					}
					$(".yoxview4").yoxview({
						allowInternalLinks: true,
						defaultDimensions: { iframe: { width: 770,height: 450 }},
						renderButtons : false,
						skin: "top_menu",
						lang:'fr'
					});
				}
			},
			error: function(data, status, e) {	
			}
		});
	});

	
	$(".radio_btn").live("change", function(){
		if ( $(this).is(':checked')) {
        	suggest_dom = $(this).attr('value');
    		$('#suggest_comm').attr('href','/commander/vuecommander/Domaine/'+suggest_dom);
    		$(".yoxview4").yoxview({
				allowInternalLinks: true,
				defaultDimensions: { iframe: { width: 770,height: 450 }},
				renderButtons : false,
				lang:'fr'
			});
        } 
	  });
	
	$marg_left = $("#slider").attr('rel');
	if ($marg_left < 300)  { 
		$(".nivo-controlNav").css({"margin-left":$marg_left+"px"});
	}
});

function new_phpcap(){ 

	$('#captcha').attr('src',src+'?'+Math.round(Math.random()*100000));

}		
