// validate news
function checkComments(thisform){
	
	var errorField = document.getElementById('errors');
	errorField.innerHTML = '';
	var errors = "";
	
	
	with (thisform){
	 
	errors += validateLength(name,100);
	errors += validate_email(email);
	errors += validateLength(message,1000);
	
	
	}	
	
	if(errors==""){
		showLoading();
		return true;
	}else{
		errorField.innerHTML += errors;
		return false;
	}
}



// validate news
function checkNews(thisform){
	
	var errorField = document.getElementById('errors');
	errorField.innerHTML = '';
	var errors = "";
	
	
	with (thisform){
	 
	errors += validateLength(titre,20);
	//errors += validateLength(texte,100000);
	
	}	
	
	if(errors==""){
		showLoading();
		return true;
	}else{
		errorField.innerHTML += errors;
		return false;
	}
}


// edit image check form
function checkEditForm(thisform){
	
	var errorField = document.getElementById('errors');
	errorField.innerHTML = '';
	var errors = "";
	
	
	with (thisform){
	 
	errors += validateLength(titre,20);
	errors += validateLength(titre_en,20);
	errors += validateLength(texte,300);
	errors += validateLength(texte_en,300);
	
	}	
	
	if(errors==""){
		showLoading();
		return true;
	}else{
		errorField.innerHTML += errors;
		return false;
	}
}

// add image check form
function checkForm(thisform){
	
	var errorField = document.getElementById('errors');
	errorField.innerHTML = '';
	var errors = "";
	
	
	with (thisform){
	 
	errors += validateLength(titre,20);
	errors += validateLength(titre_en,20);
	errors += validateLength(Filedata,600);
	errors += validate_fileType(Filedata);
	errors += validateLength(texte,300);
	errors += validateLength(texte_en,300);
	
	}	
	
	if(errors==""){
		showLoading();
		return true;
	}else{
		errorField.innerHTML += errors;
		return false;
	}
}

// Validate field length
function validateLength(field,size){
	var error="";
	var maxSize = size;
	with(field){
		if(value.length > maxSize){	
			//alert(value.length+' max size: '+maxSize);
			error += "* Field "+field.name+" has to be less than "+maxSize+" characters <br />";
		}
		if(value==""){
			error += "* "+field.name+" must not be empty <br />";
		}
	}

	return(error);	
}

//Validate fileType	 
function validate_fileType(field){
	var error="";
	with (field){
		if((value.lastIndexOf(".jpg")==-1)){
		   error += "* File must be a .jpg file <br />";
		}
		
	}
  return (error);
}

//Validate email	 
function validate_email(field){
	var error="";
	with (field){
	  apos=value.indexOf("@");
	  dotpos=value.lastIndexOf(".");
	  if (apos<1||dotpos-apos<2){
		  error += "* Invalid eMail <br />";
	  }
	 }
  return (error);
}


//Affiche une animation lors de l'upload d'image
function showLoading(){
	
	var loadZone = document.getElementById('main');
	div = document.createElement('div')
	div.setAttribute('class','loader');
	div.innerHTML = 'Processing...';

	loadZone.appendChild(div);  
	
}
// Confirm delete url
function confirmDelete(delUrl,type) {
  if (confirm("Etes vous sure de vouloir supprimer cette "+type+"?")) {
    document.location = delUrl;
  }
}
//=======================================
// animate boxes
// --------------------------------------
// Require:
// jquery-ui.min.js
// jquery.js
//========================================

var hover = "#333";
var initialState = "#111";

$(document).ready(function(){
	$(".work,.news").hover(function() {
	$(this).stop().animate({ backgroundColor: hover}, 500);
	},function() {
	$(this).stop().animate({ backgroundColor: initialState }, 500);
	});

}); 


// ======================================
// HOVER EFFECT ON CATEGORIES ITEMS
// ======================================

var hoverCat = "#30beff";
var hoverText = "#333";
var initialStateCat = "#080808";
var initialText = "#dcdcdc";
// le Background
$(document).ready(function(){
	$("li.catHover").hover(function() {
		$(this).stop().animate({ backgroundColor: hoverCat}, 500); // Le Background Hover
		$(this).children("a").stop().animate({ color: hoverText}, 500); // Le Texte Hover
		$(this).children("a").css('background-image',"url('images/puceHover.png')"); // swap d'image
	},function() {
		$(this).stop().animate({ backgroundColor: initialStateCat }, 500); // Le Background Initial
		$(this).children("a").stop().animate({ color: initialText }, 500); // Le Text Initial
		$(this).children("a").css('background-image',"url('images/puce.png')"); // swap d'image
	});
/*
var thumb = "#EEE";
// les Border
	$(".gallery a img").hover(function() {
	$(this).stop().animate({ borderTopColor: hoverCat}, 500);
	},function() {
	$(this).stop().animate({ borderTopColor: initialText }, 500);
	});
*/
}); 


//=======================================
// animate boutons
// --------------------------------------
// Require:
// jquery-color.js
// jquery.js
//========================================

var hoverColour = "#FFF";

$(function(){
	$("a.hoverBtn").show("fast", function() {
		$(this).wrap("<div class=\"hoverBtn\">");
		$(this).attr("class", "");
	});
	
	//display the hover div
	$("div.hoverBtn").show("fast", function() {
		//append the background div
		$(this).append("<div></div>");
		
		//get link's size
		var wid = $(this).children("a").width();
		var hei = $(this).children("a").height();
		
		//set div's size
		$(this).width(wid);
		$(this).height(hei);
		$(this).children("div").width(wid);
		$(this).children("div").height(hei);
		
		//on link hover
		$(this).children("a").hover(function(){
			//store initial link colour
			if ($(this).attr("rel") == "") {
				$(this).attr("rel", $(this).css("color"));
			}
			//fade in the background
			$(this).parent().children("div")
				.stop()
				.css({"display": "none", "opacity": "1"})
				.fadeIn("fast");
			//fade the colour
			$(this)	.stop()
				.css({"color": $(this).attr("rel")})
				.animate({"color": hoverColour}, 350);
		},function(){
			//fade out the background
			$(this).parent().children("div")
				.stop()
				.fadeOut("slow");
			//fade the colour
			$(this)	.stop()
				.animate({"color": $(this).attr("rel")}, 250);
		});
	});
});

