var rotatetimer, rotatetimerall;
function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 4000 = change to next image after 4 seconds
	rotatetimer=setInterval('rotate()',4000);
	
}

function rotate() {	
	clearTimeout(rotatetimer);
	rotatetimer=setInterval('rotate()',4000);
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1500);

	//Hide the current image
	current.animate({opacity: 0.0}, 1500)
	.removeClass('show');
	
};

function isValidEmail(email){
	// email address validation
    var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/
    if(RegExp.test(email)){
        return true;
    }else{
        return false;
    }
} 


function showError(msg){
	$('#error_msg').slideUp('normal', function callback(){
		$('#error_msg').text(msg);
		$('#error_msg').slideDown('normal');
	});
}
function contactUs(){
	var name=$('#name').val();
	var email=$('#email').val();
	var message=$('#message').val();
	var subject=$('#subject').val();
	
	if(name=='' || name==' '){
		showError('Please enter your name!');
		$('#name').focus();
	} else if(email=='' || email==' '){
		showError('Please enter your email!');
		$('#email').focus();
	} else if(!isValidEmail(email)){
		showError('Please enter a valid email!');
		$('#email').focus();
	} else if(subject=='' || subject==' '){
		showError('Please enter a subject!');
		$('#subject').focus();
	} else if(message=='' || message==' '){
		showError('Please enter a message!');
		$('#message').focus();
	} else {
		showError('Envoi...');
		var url='ajax/contactus.php';
		var pars='&name='+name;
		pars+='&email='+email;
		pars+='&subject='+subject;
		pars+='&message='+message;
		$.post(url, pars, contactResponse);
	}
	function contactResponse(data){
			if(data=='ok'){
				showError('Merci de votre message!');
				$('input').val('');
				$('textarea').val('');
				setTimeout("showError(' ');", 2000);
			} else {
				alert(data);
			}
		}
}
var mouseinside=false;
function openPopup(src,title,desc,obj){
        $('#popup img#theimage').hide();
	$('#popup img#loader-gif').show();
	$('#popup .text h2').text(title);
	$('#popup .text p').text(desc);
	$('#popup').fadeIn('slow', function callback(){
            $('#popup img#loader-gif').show();
            $('#popup img#theimage').attr('src', 'images/'+src).show();
            $('#popup img#loader-gif').hide();
            $('#nextbutton').unbind('click');
            $('#prevbutton').unbind('click');
            updatenav(obj);
        });
}
function updatenav(obj){
    var curitem=$(obj).attr('id');
    var curid=curitem.split('-');
    var nextid=parseInt(curid[1])+1;
    var previd=parseInt(curid[1])-1;
    var totalitems=$('#mycarousel li').length;
    if(nextid<=totalitems){
        $('#nextbutton').show();
        $('#nextbutton').bind('click', function(){
            $('#item-'+nextid).click();
        });
    } else {$('#nextbutton').hide();}
    if(previd>=1){
        $('#prevbutton').show();
        $('#prevbutton').bind('click', function(){
            $('#item-'+previd).click();
        });
    } else {$('#prevbutton').hide();}
}
function openPopupAll(){
	$('#popup-all').fadeIn('slow');
	theRotatorPpAll();
}

function theRotatorPpAll() {
	//Set the opacity of all images to 0
	$('div#rotator-all ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator-all ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 4000 = change to next image after 4 seconds
	rotatetimerall=setInterval('rotatePpAll()',4000);
	
}

function rotatePpAll() {
	clearTimeout(rotatetimerall);
	rotatetimerall=setInterval('rotatePpAll()',4000);
	//Get the first image
	var current = ($('div#rotator-all ul li.show')?  $('div#rotator-all ul li.show') : $('div#rotator-all ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator-all ul li:first') :current.next()) : $('div#rotator-all ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css('opacity', '1.0')
	.addClass('show');

	//Hide the current image
	current.css('opacity', '1.0')
	.removeClass('show');
	
};

function closePopup(){
	$('#popup').fadeOut('slow');
	$('#popup-all').fadeOut('slow');
}
$(document).ready(function(){
	// initialize the content slideshow
	theRotator();
        var credits;
	$('#mycarousel li img').mouseover(function(){ // portfolio hover functions
		$('#mycarousel li img').parents('li').removeClass('myhover');
		$(this).parents('li').addClass('myhover');
		var src=$(this).attr('src');
		var filename=src.split('.jpg');
		var mysrc=filename[0];
		$(this).attr('src', mysrc+'-on.jpg');
		mouseinside=true;
	});
	$('#mycarousel li img').mouseleave(function(){
		$(this).parents('li').removeClass('myhover');
		var src=$(this).attr('src');
		var filename=src.split('-on.jpg');
		var mysrc=filename[0];
		$(this).attr('src', mysrc+'.jpg');
		mouseinside=false;
	});
        $('#nextbutton').mouseover(function(){mouseinside=true;});
        $('#prevbutton').mouseover(function(){mouseinside=true;});
	$('body').click(function(){
		if(!mouseinside) { closePopup(); }
	});
	$('.sitelogo a img').mouseover(function(){
		if(!$(this).parents('.sitelogo').hasClass('home')){
			$('.ul-text').slideDown('slow');
		}
	});
	$('.sitelogo a img').mouseleave(function(){
		if(!$(this).parents('.sitelogo').hasClass('home')){
			$('.ul-text').slideUp('slow');
		}
	});
	$('.lb-menu, .ac-text').mouseover(function(){
                clearTimeout(credits);
		$('.ac-text').slideDown('slow');
	});
	$('.ac-text').mouseleave(function(){
		$('.ac-text').slideUp('slow');
	});
        $('.lb-menu').mouseleave(function(){
		credits=setTimeout("$('.ac-text').slideUp('slow')", 1000);
	});
	$('#rotator').mouseover(function(){
		mouseinside=true;
	});
	$('#rotator li a').click(function(){
		openPopupAll();
	});
	var preload = ['gimg20-on.jpg', 'gimg21-on.jpg', 'gimg22-on.jpg', 'gimg23-on.jpg', 'gimg24-on.jpg', 'gimg25-on.jpg', 'gimg26-on.jpg', 'gimg27-on.jpg', 'gimg28-on.jpg', 'gimg29-on.jpg', 'gimg30-on.jpg', 'gimg31-on.jpg', 'gimg32-on.jpg', 'gimg33-on.jpg', 'gimg34-on.jpg'];
	$.preloadImages(preload);
	$('div#rotator ul li').click(function(){rotate();});
});
jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i > -1; i--) {
		jQuery('<img>').attr("src", 'images/'+a[i]);
	}
}











