$(document).ready(function() {									 
	//faq 
	$('.ul-faq h4 a').click(function(){
		var $content = $(this).parent().next();
		$content.is(':hidden') ? $content.fadeIn() : $content.hide();
		return false;
	});
		
	$(".tooltip").tooltip({
		bodyHandler: function() { 
			return $('#'+$(this).attr("href").split('#')[1]).html(); 
		}, 
		showURL: false 
	});

	/* epand content with id */									 
	$('.jExpandOn, .jExpandOff').click(function(){
		var id = $(this).attr('href');
		var showed = $(id).is(':visible');
		if(showed){
			$(id).hide();
			$(this).removeClass('jExpandOn').addClass('jExpandOff');
		} else {
			$(id).show();
			$(this).removeClass('jExpandOff').addClass('jExpandOn');
		}
		return false;
	}).each(function(){
		if($(this).hasClass('jExpandOff')){
			var id = $(this).attr('href');
			$(id).addClass('jExpandContent');
		}
	});	
													 
	/* cleaning des champs input on focus/blur */
	$.fn.search = function(type) {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$('.jClean').search();
 
													
	/* form addebito directo (step3) : focus next element */
	$('.jNext').keyup(function(){
		length = $(this).val().length;
		maxlength = $(this).attr('maxlength');
		next = $(this).parent().next().find('input');
		if(length == maxlength)	next.focus();
	});
	
	//left menu 
	$('#nav02 > li > a').click(function(){
		$this = $(this);
		ul = $this.parents('li').find('ul');
		if(ul.length){
			showedClass = 'selected';
			if($this.hasClass(showedClass)){
				$this.removeClass(showedClass);	
				ul.hide();
			} else {
				currentSelectted = $('#nav02 > li > a.selected');
				currentUl = $('#nav02 > li > ul:visible');
				if(currentSelectted.length){ currentSelectted.removeClass(showedClass);currentUl.hide(); }
				$this.addClass(showedClass);
				ul.show();
			}
			return false;
		}										
	});
	
	
	//expand next
	$('.jExpandMenu-on, .jExpandMenu-off').click(function(){
		if($(this).hasClass('jExpand-on')){
			$(this).attr('class', 'jExpand-off').next().hide();
		}	else {
			$(this).attr('class', 'jExpand-on').next().show();
		}																
	});
	
	//icontent (overlayer)
	$('.icontent').icontent();
	
	
	/* check option sms */
	$('.csOptionsSms li').click(function(){
		$(this).find('input').attr('checked', true);
	});
	
	
	/* check phones */
	csPhones();
	
	
	/* popup link */
	$('.jPopup').click(function(){
		var w = getParam(this.href, 'w') || 600;
		var h = getParam(this.href, 'h') || 400;
		var l = ($(window).width())/2 - w/2;
		var t = ($(window).height())/2 - h/2;
		var s = getParam(this.href, 's') || 1;
		window.open(this.href, '','top='+t+', left='+l+', width='+w+',height='+h+', scrollbars='+s+', resizable=1');
		return false;
	});
});

function csPhones(){
	$('.csPhones .picture, .csPhones .btn-choisir').click(function(){
		$(this).parent().find('input[type=radio]').click();
		return false;
	});	
}


/* parsing of url parameters */
function getParam(url, name){
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if( results == null )
		return "";
	else 
		return results[1];
}


function envoyer() {
  page  = escape(this.location.href);
  title = escape(this.document.title);
  url = '/scripts/envoyer.html?page=' + page + '&titre=' + title;
  envoyer_a_un_ami = window.open(url, 'envoyer', 'status=no,scrollbars=no,width=350,height=220'); 
  envoyer_a_un_ami.focus();
}

function bookmarksite(){
	if (document.all)
		window.external.AddFavorite(location.href, document.title);
	else if (window.sidebar)
		window.sidebar.addPanel(document.title, location.href, "");
}

(function($){ 
  $.fn.showHideOpt = function(){
		var showContent = function(){ 
			$(title_id)[this.checked ? 'fadeIn' : 'hide']();
		}

		this.each(function(){
			var id = this.title;
			$(id).hide();
			$(this).bind('click', showContent);
		});
		
		this.hover(function(){						
			title_id = this.title;
			this.title = '';
		}, function(){
			this.title = title_id;
		});
	}
	
	//Auto-run 
  $(function(){ $(".jsh-opt").showHideOpt(); }); 	
})(jQuery);

//validate method length
jQuery.validator.addMethod("_length", function(value, element, params) { 
	return this.optional(element) || value.length == params; 
}, $.format("Votre N° de téléphone fixe doit comporter {0} chiffres."));

//validate method length
jQuery.validator.addMethod("goodPassword", function(value, element) { 
	function in_array(needle, haystack, argStrict) {
    var found = false, key, strict = !!argStrict;
    for (key in haystack) {
			if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
				found = true;
				break;
			}
    }
    return found;
	}
	
	var excluded = ['0000', '1111', '2222', '3333', '4444', '5555', '6666', '7777', '8888', '9999', '1234'];
	
	return this.optional(element) || !in_array(value, excluded); 
}, $.format("Votre mot de passe est trop simple."));

function conf_by_sms(message, numero_mobile) {
  document.getElementById('conf_by_sms').src = "/img/sms/loading.gif";
  if (confirm("Désirez-vous envoyer ce message au "+numero_mobile+" :\n\n"+message)) {
    rand = Math.random();
    document.getElementById('conf_by_sms').src = "/espaceclient/sms-do.php?message="+message+"&numero_mobile="+numero_mobile+"&rand="+rand;
  } else {
    document.getElementById('conf_by_sms').src = "/img/sms/sms.gif";
  }
}