$(document).ready(function(){
	$('#illustration img').hide();
})

$(window).load(
	function(){
	$('#illustration img').show();

		slideshow('casestudy',500,1000);
		slideshow('slide-references',1000,2000);
	
		$('.categorie').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		});
		
		
		$('.off').hover(
		function(){
			$(this).addClass('on');
		},
		function(){
			$(this).removeClass('on');
		}
		);
		
		
		$('#actus .actu-home').hover(
		function(){
			$(this).removeClass('actuoff');
		},
		function(){
			$(this).addClass('actuoff');
		}
		);
		
		
		form();
		
		$('#references').hide();
		references();
		
		
		$('.illus_case').click(function(){
				var id=$(this).attr('id').substring(5);
				popupCase(id);
				return false;
			});
		
		
		});


$('.ref').live('hover',
		function(){
			var id=$(this).attr('id').substring(4);
			if(!isIE6())
			{
				$(this).prepend('<a href="#" id="loupe_'+id+'" class="loupe"><img src="/wp-content/themes/wrs/images/loupe.png" alt="" /></a>');
			}
			else
			{
				$(this).prepend('<a href="#" id="loupe_'+id+'" class="loupe"><img src="/wp-content/themes/wrs/images/loupe.gif" alt="" /></a>');
			}
		}
);
$('.ref').live('mouseleave',
		function(){
			$(this).find('.loupe').remove();
		}
);

$('.loupe').live('click',function(){
	var id=$(this).attr('id').substring(6);
	popup(id);
	return false;
});


var oldH;
var oldS;
$('#popup-fermer').live('click',function(){
	//$('body').css('height',oldH).css('overflow','auto');
	/*$('#wrapper-content').show();
	$('#wrapper-header').show();*/
	$('#popup-container').remove();
	$(window).scrollTop(oldS);
	return false;
});


function popup(id)
{
	var popup_img;
	if(!isIE6())
		popup_img='popup-croix.png';
	else
		popup_img='popup-croix.gif';
		
	$.get('/wp-content/themes/wrs/ajax_references.php?action=detail&id='+id,function(data){
	
		var obj=$.parseJSON(data);
		var img='';
		var titre='';
		var desc='';
		
		for(var i = 0; i < obj.length; i++) 
		{
			img=obj[i]['img'];
			titre=obj[i]['titre'];
			desc=obj[i]['desc'];
			if(desc!='')
			{
				img='<a target="_blank" href="'+desc+'">'+img+'</a>';
				desc='<p><a target="_blank" id="lien_ref" href="'+desc+'">'+desc+'</a></p>';
			}
		}

		$('body').append('<div id="popup-container">'+
						'<div id="popup">'+
							'<a id="popup-fermer" href="#"><img src="/wp-content/themes/wrs/images/'+popup_img+'" alt="" /></a>'+
							'<div id="popup-content">'+
								img+desc+
							'</div>'+
						'</div>'+
					'</div>');
		
		oldH=$('body').css('height');
		oldS=$(window).scrollTop();
		
		if($(window).height()>$('#popup').height())
		{
			newH=$(window).height();
			var top=((newH-$('#popup').height())/2)+'px';
		}
		else
		{
			newH=$('#popup').height();
			var top='15px';
		}
		
		
		$('#popup-container').css('height',oldH);
		$('#popup').css('margin-top',top);
		$(window).scrollTop(0);

	});
		
	return false;
}


function popupCase(id)
{
	var popup_img;
	if(!isIE6())
		popup_img='popup-croix.png';
	else
		popup_img='popup-croix.gif';
		
	$.get('/wp-content/themes/wrs/ajax_casestudies.php?action=getPhoto&id='+id,function(data){
	
		var obj=$.parseJSON(data);
		var img='';
		var titre='';
		var desc='';
		
		for(var i = 0; i < obj.length; i++) 
		{
			img=obj[i]['img'];
			titre=obj[i]['titre'];
			desc=obj[i]['desc'];
		}

		
					
		$('body').append('<div id="popup-container">'+
						'<div id="popup">'+
							'<a id="popup-fermer" href="#"><img src="/wp-content/themes/wrs/images/'+popup_img+'" alt="" /></a>'+
							'<div id="popup-content">'+
								img+
							'</div>'+
						'</div>'+
					'</div>');
		
		oldH=$('body').css('height');
		oldS=$(window).scrollTop();
		
		if($(window).height()>$('#popup').height())
		{
			newH=$(window).height();
			var top=((newH-$('#popup').height())/2)+'px';
		}
		else
		{
			newH=$('#popup').height();
			var top='15px';
		}
		
		
		$('#popup-container').css('height',oldH);
		$('#popup').css('margin-top',top);
		$(window).scrollTop(0);
		

	});
		
	return false;
}



function form()
{
	$('#nom').click(function(){
		if($(this).val()=='Nom')
			$(this).val('');
	});
	
	$('#prenom').click(function(){
		if($(this).val()=='Prénom')
			$(this).val('');
	});
	
	$('#email').click(function(){
		if($(this).val()=='E-mail')
			$(this).val('');
	});
	
	$('#message').click(function(){
		if($(this).text()=='Message')
			$(this).text('');
	});
}

function slideshow(id,transition,pause)
{
	var total=$('#'+id+' .slideshow-item').length;
	$('#'+id+' .slideshow-item').each(
		function(i){
			$(this).css('z-index',(total-i));
		}
	);
	setTimeout(function() {slide(id,total,transition,pause)},transition+pause);
}

function slide(id,total,transition,pause)
{
	$('#'+id+' .slideshow-item').each(
		function(i){
			if($(this).css('z-index')==total)
			{
				if(!isIE6())
				{
					$(this).animate({opacity:0},transition, function(){
					
						$('#'+id+' .slideshow-item').each(
							function(){
								if($(this).css('z-index')==total)
								{
									$(this).css('z-index',1);
									$(this).animate({opacity:1},0);
								}
								else
									$(this).css('z-index',parseInt($(this).css('z-index'))+1);
							}
						);
					});
				}
				else
				{
					$(this).animate({left:0},transition, function(){
					$('#'+id+' .slideshow-item').each(
						function(){
							if($(this).css('z-index')==total)
							{
								$(this).css('z-index',1);
							}
							else
								$(this).css('z-index',parseInt($(this).css('z-index'))+1);
							/*alert($(this).css('z-index'));*/
						}
					);
					});
				}
			}
		}
	);
	setTimeout(function() {slide(id,total,transition,pause)},transition+pause);
}

function references()
{
	$('.aref').click(function(){
		var id=$(this).attr('id').substring(5);
		
		$('.categorie').removeClass('selected');
		$('.categorie').addClass('off');
		$(this).parent().addClass('categorie-image-on');


		$(this).parent().parent().addClass('selected').removeClass('off');
		$('#references').slideUp('slow', function(){
			$('#references').load('/wp-content/themes/wrs/ajax_references.php?action=liste&cat='+id, function(){
				$('#references').slideDown('slow');
			});
			$('html, body').animate({scrollTop:'450px'}, 'slow')
		});
		return false;
	});
}

function isIE6()
{
	var IE6 = false;
	var strChUserAgent = navigator.userAgent;
	var intSplitStart = strChUserAgent.indexOf('(',0);
	var intSplitEnd = strChUserAgent.indexOf(')',0);
	var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
	if(strChMid.indexOf('MSIE 6') != -1) IE6 = true;
	return IE6;
}
