$(document).ready(function() {
	
	var options = { path: '/'};//, expires: 365 
	
	$("#scrollist a").click(function(){
		
		//event.preventDefault();
		
		var target = $(this).attr("href");
		var parts = target.split("#");
		var trgt = parts[1];
		//alert(trgt);
		scrollToo("a[name='"+trgt+"']");
		return false;	
	});
	
	$("#suscribe input").focus(function(){
		
		var val = $(this).val();
		
		if(val == '')
			$(this).prev('label').hide();
		
		$(this).addClass("active");
				
	}).blur(function(){
		
		var val = $(this).val();
		
		if(val == '')
			$(this).prev('label').show();
			
		$(this).removeClass("active");
	});
	
	$(".slideDown").click(function() {
		
		var target  = $(this).attr("href").substr(1);
		$("#"+target).slideDown("fast");
		return false;	
	});
	
	$("#main .listing:odd, #main ul.comments li:odd").addClass("even");
	
	$("button.button").mouseover(function(){
		$(this).addClass("hover");
			
	}).mouseout(function(){
		$(this).removeClass("hover");	
	});
	
	$(".change-image").click(function() {
		
		var urlsrc  = url+'inc/captcha.php?'+Math.random();
		$(this).prev("img").attr("src",urlsrc);
		return false;
		
	});
	
	$("a.request[href^=#]").click(function(){
		
		var form = $(this).parent("form");
		form.trigger('submit');
		
		return false;
	});
	
	$("form").submit(function(){
		
		
		$(".request").hide();
		$(this).children("button").hide();
		$(".min-request").hide();
		
		var form = true;
		var id = $(this).attr("id");
		
		$("#"+id+" input,#"+id+" textarea,#"+id+" select").each(function(){
			
			if($(this).hasClass("boxcheck") && $(this).getRequisCheck() != 'succes')
			{ 
				var targetOffset = $(this).offset().top;
      			$('html,body').animate({scrollTop: targetOffset}, 1000);
				
				form = false; 
				return false;
			}
			else if($(this).hasClass("required") && $(this).getRequis() != 'succes')
			{ 
				var targetOffset = $(this).offset().top;
      			$('html,body').animate({scrollTop: targetOffset}, 1000);
				$(this).focus();
				
				form = false; 
				return false;
			}
			else if($(this).hasClass("reg") && $(this).getReg() != 'succes')
			{
				var targetOffset = $(this).offset().top;
      			$('html,body').animate({scrollTop: targetOffset}, 1000);
				$(this).focus();
				
				form = false; 
				return false;
			}
			else if($(this).hasClass("compar") && $(this).getCompar() != 'succes')
			{
				var targetOffset = $(this).offset().top;
      			$('html,body').animate({scrollTop: targetOffset}, 1000);
				$(this).focus();
				
				form = false; 
				return false;
			}
			else if($(this).hasClass("dbl") && $(this).getDbl() !='succes')
			{
				var targetOffset = $(this).offset().top;
      			$('html,body').animate({scrollTop: targetOffset}, 1000);
				$(this).focus();
				
				form = false; 
				return false;
			}
			
				
		});	
		
		$(".request").show();
		$(this).children("button").show();
		$(".min-request").show();
		
		if(form==false)
			return false;
			
		
	
	});
	
	
	
	
});

function scrollToo(target)
{
	var targetOffset = $(target).offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 1000);
}

function noSpam(vName,vDomain){
	location.href = ("mailto:" + vName + "@" + vDomain);
};
// VALIDATION FILTRE REGEX
	
jQuery.fn.extend({
		getRequisCheck: function() {
			
			
			var result = null;
			
			if($(this).attr("checked")==false)
			{
				$(this).nextAll("span.error").show();
				$(this).next("label").addClass("bold");
				
				result = 'error';
			}
			else
			{
				$(this).nextAll("span.error").hide();
				$(this).next("label").removeClass("bold");
				
				result = 'succes';
			}
			
			return result;
		}
	});
		
jQuery.fn.extend({
		getRequis: function() {
			
			//var id = $(this).attr("id");
			var result = null;
			
			if($(this).val() =='')
			{
				$(this).next("span.error").text("* Requis").show();
				$(this).addClass("textuelError");
				
				result = 'error';
			}
			else
			{
				$(this).next("span.error").text("* Requis").hide();
				$(this).removeClass("textuelError");
				
				result = 'succes';
			}
			
			return result;
		}
	});
	
jQuery.fn.extend({
	getReg: function() {
	
	tab_reg = {'mail':/^[0-9a-z\._-]+@[0-9a-z\._-]+(\.)[a-z]{2,}$/, 
		'alphanum':/^[\w]{4,20}$/, 
		'cp': /^[0-9]{3,6}$/,
		'tel': /^[0-9]{10}$/,
		'price': /^([0-9]{1,})(([.]?[0-9]{1,})?)$/,
		'www': /^(www\.)?[0-9a-z\/\._-]+(\.)[a-z]{2,}$/,
		'http': /^(http:\/\/)(www\.)?[0-9a-z\/\._-]+(\.)[a-z]{2,}[\/]?$/,
		'numeric': /^[0-9]+$/};
		
		var alt = $(this).attr("alt");
		var result = null;
		var reg = tab_reg[alt];
		
		if($(this).val() !='')
		{
			if ($(this).val().match(reg)) 
			{
				$(this).next("span.error").text("* Incorrect").hide();
				$(this).removeClass("textuelError");
				
				result = 'succes';
			}
			else
			{
				$(this).next("span.error").text("* Incorrect").show();
				$(this).addClass("textuelError");
				
				result = 'error';
			}
		}
		else
		{
			$(this).next("span.error").text("* Incorrect").hide();
			$(this).removeClass("textuelError");
			result = 'succes';
		}
		
		return result;
	}
});

jQuery.fn.extend({
	getCompar: function() {
		
		var id = $(this).attr("id");
		
		var result = null;
		
		var id2 = id.substring(5);
	
		if($(this).val() != $("#"+id2).val())
		{
			$(this).next("span.error").show();
			$(this).addClass("textuelError");
			
			result = 'error';
		}
		else
		{
			$(this).next("span.error").hide();
			$(this).removeClass("textuelError");
			
			result = 'succes';
		}
		
		return result;
	}
});

jQuery.fn.extend({
	getDbl: function() {
		
		$.ajax().abort();
		$(this).nextAll(".minispiner").show();
		
		var valeur = $(this).val();
		var id = $(this).attr("id");
		
		var result = null;
		
		$.ajax({
				type: "GET",
				cache:   false,
				async:   false,
				dataType: 'json',
				url: url+"ajax/verif.php",
				data: {champ: id, valeur: valeur},						
				success: function(data)
				{
					
					$(".minispiner").hide();
					result = data.response;
					
					if(data.response == 'outline')
						window.location.reload();
					else if(data.response!='succes')
					{
						$("#"+id).next("span.error").html('* Indisponible').show();
						$("#"+id).addClass("textuelError");
						
						//return false;
					}
					else
					{
						$("#"+id).next("span.error").html('* Incorrect').hide();
						$("#"+id).removeClass("textuelError");
						
						//return true;
					}
					
					
				}
			
			});
		return result;
	}
});
jQuery.cookie = function (key, value, options) {
    
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        
        value = String(value);
        
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
