$(document).ready(function(){
	//alert(document.location.hash);

	$('#ref_grid li').hover(function(){
		$(this).find('.hover').stop(true,true).animate({
			'opacity' : '1'
		}, 200);
		
		$(this).find('.visuel').stop(true,true).animate({
			'opacity' : '0.5'
		}, 200);
	}, function(){
		$(this).find('.hover').stop(true,true).animate({
			'opacity' : '0'
		}, 500);
		
		$(this).find('.visuel').stop(true,true).animate({
			'opacity' : '1'
		}, 500);
	});
	
	$('a[href*=#]').stop().click(function(event) {
    	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var target = $(this.hash);
			var myHash = this.hash;
      		target = target.length && target || $('[name=' + this.hash.slice(1) +']');
      		
      		if (target.length) {
        		var targetOffset = target.offset().top;
        		$('html,body').animate({scrollTop: targetOffset}, 1500, 'easeOutQuart', function(){	
					window.location.hash = myHash;
        		});
       			return false;
      		}
    	}
  	});
  	
  	/*if (document.location.hash.match(/references\w*g)){
		$('.btn_menu_ref').click();
	}*/
});

function openRefPanelFromUrl(){
	$('.btn_menup_ref').addClass('active');	
	
	var target = $('#quelques_references');
	var targetOffset = target.offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 1500, 'easeOutQuart');
	
	var id = "";
	currentHash = document.location.hash;
	id = currentHash.replace(/#quelques_references\/(\d*).*/g, '$1');
	if (id != ""){
		openReference(id);
	}
}

function openReference(referenceId){
	$('#ref_'+referenceId+' .hover').css('opacity', '0');
	$('#ref_'+referenceId+' .ajax_loader').css('display', 'block');
	$.ajax({
	  url: "index.php",
	  type: "GET",
	  data: {id : referenceId, 'ajax' : '1'},
	  dataType: "html",
	  success: function(data){
			$('#ajax_content').css('display', 'none');
			$('#ajax_content').empty().html(data);
			$('#ajax_content').css('top',$('#title_ref_content').height() + 60);
			$('#ajax_content').fadeIn('slow', function(){
				$('#ref_grid').css('display', 'none');
			});
			$('#quelques_references').animate({
				'height': $('#ajax_content').height() + $('#title_ref_content').height()
			}, 'slow');
			
			$('#ref_'+referenceId+' .ajax_loader').css('display', 'none');
			window.location.hash = '#quelques_references/'+referenceId+'/'+$('#ref_'+referenceId+' a').attr('href');
	  }
	});
}

function refBackWasClick(){
	$('#ref_grid').css('display', 'block');
	$('#ajax_content').fadeOut('slow', function(){
		$('#ajax_content').empty();
	});
	$('#quelques_references').animate({
		'height': $('#ref_grid_container').height() + $('#title_ref_content').height() + 60
	}, 'slow');
	$('.btn_menup_ref').click();
}

function refLoadNextProject(){
	var currentRef = $('#open_ref').attr('class');
	var nextRef = $('#ref_grid #'+currentRef+' + li').attr('id');
	
	if (nextRef == undefined){
		nextRef = $('#ref_grid li:first').attr('id');
	}
	
	$.ajax({
	  url: "index.php",
	  type: "GET",
	  data: {	id : nextRef.replace(/ref_/, ''), 
	  			'ajax' : '1'},
	  dataType: "html",
	  success: function(data){
			$('#ajax_content').animate({
				'opacity':'0'
			}, 100, 'linear', function(){
				$('#ajax_content').html(data);
				$('#ajax_content').animate({
					'opacity' : '1'
				});
			});
	  }
	});
}

function refLoadPreviousProject(){
	var currentRef = $('#open_ref').attr('class');
	var prevRef = $('#ref_grid #'+currentRef).prev().attr('id');
	
	if (prevRef == undefined){
		prevRef = $('#ref_grid li:last').attr('id');
	}
	
	$.ajax({
	  url: "index.php",
	  type: "GET",
	  data: {id : prevRef.replace(/ref_/, ''),'ajax' : '1'},
	  dataType: "html",
	  success: function(data){
			$('#ajax_content').animate({
				'opacity':'0'
			}, 100, 'linear', function(){
				$('#ajax_content').html(data);
				$('#ajax_content').animate({
					'opacity' : '1'
				});
			});
	  }
	});	
}

function printCountRef(currentRef){
	var currentRef = $('#open_ref').attr('class');
	var total = $('#ref_grid li').length;
	var indice = 0;
	var i = 0;
	
	$('#ref_grid li').each(function(){
		i++;
		if ($(this).attr('id') == currentRef){
			indice = i;
		}
	});
	
	$('.ref_slider_description_page').html(indice+'/'+total);
}

function selected(obj){
	/*var lilist = document.getElementById('menu');
	var alist = lilist.getElementsByTagName('a');
	for (i=0; i<alist.length; i++){
		alist[i].className="";
	}
	obj.className="active";*/
	$('#menu a').each(function(){
		$(this).removeClass('active');
	});
	$(obj).addClass('active');
}

function checkContactForm(){
	var name = $('#name');
	var email = $('#email');
	var message = $('#message');
	var error = false;
	
	name.removeClass('error');
	email.removeClass('error');
	message.removeClass('error');
	
	// name
	if (name.val() == ""){
		name.addClass('error');
		name.next().html('<img src="assets/templates/sk/images/check_ko.png" alt="ko" />');
		error = true;
	}else{
		name.addClass('ok');
		name.next().html('<img src="assets/templates/sk/images/check_ok.png" alt="ok" />');
	}
	
	// email
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');	
	if (!reg.test(email.val())){
		email.addClass('error');
		email.next().html('<img src="assets/templates/sk/images/check_ko.png" alt="ko" />');
		error = true;
	}else{
		email.addClass('ok');
		email.next().html('<img src="assets/templates/sk/images/check_ok.png" alt="ok" />');
	}
	
	// message
	if (message.val() == ""){
		message.addClass('error');
		message.next().html('<img src="assets/templates/sk/images/check_ko.png" alt="ko" />');
		error = true;
	}else{
		message.addClass('ok');
		message.next().html('<img src="assets/templates/sk/images/check_ok.png" alt="ok" />');
	}
	
	if (error == true){
		$('.message_error').css('display', 'block');
		return false;
	}
		
	
	return true;
}

function sendForm(){
	if (checkContactForm()){
		$.ajax({
			type: "POST",
		   	url: "index.php?id=5",
		   	data: 	"&name="+$('#name').val()+
		   			"&email="+$('#email').val()+
		   			"&company="+$('#company').val()+
		   			"&phone="+$('#phone').val()+
		   			"&message="+$('#message').val()+
		   			"&nobot="+$('#nobot').val(),
		   	success: function(message){
				if (message == "ok"){
					$('.submit_area input').css('display', 'none');
					$('.submit_area p').css('display', 'block');
				}
		   	}
		});
	}
}
