$(function(){
	if (navigator.appVersion.indexOf("Mac")!=-1){
		$('#content_nav li a').css({"line-height":"0,5"});
		$('#content_nav li').css({"margin-top":"9px"});
	}


	iter = 0;
	iterporto = 0;
	itermini = 0;
	action=false;
	paged = 0;
	
	function prev(){
		if(!action){
			paged--;
			action = true;
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getPortofolio&catName='+$('.current_show').text()+'&paged='+paged+'&nb='+$(this).attr('class'),
				success: function(text) {
					$('#portofolio_content').fadeOut();
					$('#portofolio_content').html(text);
					$('#portofolio_content').fadeIn();
					$('.showcase_projet').hover(function(){
						$(this).find('.showcase_projet_img').css({"display":'none'});
						$(this).find('.showcase_projet_detail').css({"display":'block'});
					},function(){
						$(this).find('.showcase_projet_img').css({"display":'block'});
						$(this).find('.showcase_projet_detail').css({"display":'none'});

					});
					$("a.details").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = this.href;
						$("body").fadeOut(1000, redirectPage);
					});
					
					$(".showcase_projet_detail").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = $(this).find('a.details').attr("href");
						$("body").fadeOut(1000, redirectPage);
					});
					
					$("a.details_projet").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = this.href;
						$("body").fadeOut(1000, redirectPage);
					});
					$('#next').click(next);
					action=false;
				}
			});
		}
	}
	
	function next(){
		if(!action){
			paged++;
			action = true;
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getPortofolio&catName='+$('.current_show').text()+'&paged='+paged+'&nb='+$(this).attr('class'),
				success: function(text) {
					$('#portofolio_content').fadeOut();
					$('#portofolio_content').html(text);
					$('#portofolio_content').fadeIn();
					$('.showcase_projet').hover(function(){
						$(this).find('.showcase_projet_img').css({"display":'none'});
						$(this).find('.showcase_projet_detail').css({"display":'block'});
					},function(){
						$(this).find('.showcase_projet_img').css({"display":'block'});
						$(this).find('.showcase_projet_detail').css({"display":'none'});

					});
					$("a.details").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = this.href;
						$("body").fadeOut(1000, redirectPage);
					});
					
					$(".showcase_projet_detail").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = $(this).find('a.details').attr("href");
						$("body").fadeOut(1000, redirectPage);
					});
					
					$("a.details_projet").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = this.href;
						$("body").fadeOut(1000, redirectPage);
					});
					$('#prev').click(prev);
					action=false;
				}
			});
		}
	}
	
	function updateNextShowcase(){
		if(!action){
			var tmpIter = iter + 1;
			if(tmpIter<($('#menu_content_showcase>li').length/2)){
				var text = $('.current_show').text();
				text = text.replace(" ", "");
				var nb = $('.'+text).length;
				var objet = $('.current_show').parent().next().next().children();
				var tmp = $('.current_show').parent().next().next().children().html();
				tmp = tmp.replace(" ", "");
				if($('.'+tmp).length>0){
					action = true;
					$('.current_show').removeClass('current_show');
					objet.addClass('current_show');
					res = parseInt($('#showcase_main_content').css('left')) - (parseInt(nb) * 291) -(parseInt(nb) * 13);
					$('#showcase_main_content').animate({left:res+'px'},500,function(){action=false;});
					iter++;
				}
				
			}
		}
	}
	
	function updatePrevShowcase(){
		if(!action){
			if(iter>0){		
				action = true;
				var objet = $('.current_show').parent().prev().prev().children();
				var tmp = $('.current_show').parent().prev().prev().children().html();
				tmp = tmp.replace(" ", "");
				var nb = $('.'+tmp).length;
				$('.current_show').removeClass('current_show');
				objet.addClass('current_show');
				res = parseInt($('#showcase_main_content').css('left')) + (parseInt(nb) * 291) +(parseInt(nb)* 13);
				iter--;
				$('#showcase_main_content').animate({left:res+'px'},500,function(){action=false;});
			}
		}
	}

	setTimeout(function(){
	var midMenu = parseInt($('#content_nav').innerWidth())/2;
	var tmp = (980/2) - midMenu;
	//$('#start_menu').css({'margin-left':tmp+'px'});
	},2000);
	
	var midMenuShowcase = $('#menu_content_showcase').innerWidth()/2;
	var tmp = (980/2) - midMenuShowcase - 19;
	$('#menu_showcase').css({"margin-left":tmp+'px'});
	
	$("#error").css("display", "none");
	$("body").css("display", "none");
    $("body").fadeIn(2000);
 
    $("a.details").click(function(event){
        event.preventDefault();
		$("html").css({"background-color":"black"});
        linkLocation = this.href;
        $("body").fadeOut(1000, redirectPage);
    });
	
	$(".showcase_projet_detail").click(function(event){
		event.preventDefault();
		$("html").css({"background-color":"black"});
		linkLocation = $(this).find('a.details').attr("href");
		$("body").fadeOut(1000, redirectPage);
	});
	
	$("a.details_projet").click(function(event){
        event.preventDefault();
		$("html").css({"background-color":"black"});
        linkLocation = this.href;
        $("body").fadeOut(1000, redirectPage);
    });
	
	$("#close_detail").click(function(event){
        event.preventDefault();
		$("html").css({"background-color":"white"});
        linkLocation = "portfolio";
        $("body").fadeOut(1000, redirectPage);
    });
	
	$("#button_close_detail").click(function(event){
        event.preventDefault();
		$("html").css({"background-color":"white"});
        linkLocation = "portfolio";
        $("body").fadeOut(1000, redirectPage);
    });
	
	function redirectPage() {
        window.location = linkLocation;
    }

	tmp = $('#contenttemoignage').innerHeight();
	if(tmp<242){
		var diff = 242 - tmp + 30;
		$('.client').css({"margin-top":diff+"px"});
		$('#contenttemoignage').height(242);
	}
	tmp = $('#content_services').innerHeight();
	if(tmp<151){
		$('#content_services').height(151);
	}
	tmp = $('#content_detail').innerHeight();
	if(tmp<170){
		var diff = 170 - (tmp - 4);
		$('#temoignage').css({"margin-top":diff+"px"});
		$('#content_detail').height(170);
	}
	tmp = $('#portofolio').innerHeight();
	if(tmp<267){
		$('#portofolio').height(267);
	}
	
	tmp = $('#contact').innerHeight();
	if(tmp<400){
		$('#contact').height(428);
	}
	
	tmp = $('.textarea').innerHeight();
	if(tmp<198){
		$('.textarea').height(198);
	}

	$('#allproject_button').hover(function(){
		$(this).addClass("hover");
		$('#allproject').addClass("hover");
	},function(){
		$(this).removeClass("hover");
		$('#allproject').removeClass("hover");
	});
	
	$('.showcase_projet').hover(function(){
		$(this).find('.showcase_projet_img').css({"display":'none'});
		$(this).find('.showcase_projet_detail').css({"display":'block'});
	},function(){
		$(this).find('.showcase_projet_img').css({"display":'block'});
		$(this).find('.showcase_projet_detail').css({"display":'none'});
	});
	
	$('#logo').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass("hover");
	});
	
	$('#logo').click(function(){
		document.location.href="Home";
	});
	
	$('.showcase').click(function(){
		if(!action){
			var text;
			action = true;
			text = $("#"+$(this).attr("id")).text();
			text = text.replace(" ", "");
			if($('.'+text).length>0){
				var res;
				
				var resNB = 0;
				if(iter<parseInt($(this).attr("id"))){
					var tmp = iter;

					for(tmp;tmp<parseInt($(this).attr("id"));tmp++){
						text = $('#'+tmp).text();
						text = text.replace(" ", "");
						resNB += $('.'+text).length;
					}
					res = - (resNB * 302) + parseInt($('#showcase_main_content').css('left'));
				}else{
					var tmp = iter-1;
					
					
					for(tmp;tmp>=parseInt($(this).attr("id"));tmp--){
						text = $('#'+tmp).text();
						text = text.replace(" ", "");
						resNB += $('.'+text).length;
					}
					res = parseInt($('#showcase_main_content').css('left')) + (resNB * 302);
				}
				$('#showcase_main_content').animate({left:res+'px'},500,function(){action=false;});
				iter = parseInt($(this).attr("id"));
				$('.current_show').removeClass('current_show');
				$(this).addClass('current_show');
			}	
		}
	});
	
	$('#next').click(next);
	
	$('#prev').click(prev);
	
	$(".portofolio").click(function(){
		if(!action){
			action = true;
			iterporto = $(this).attr("id");
			var text = $(this).text();
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getPortofolio&catName='+text,
				success: function(text) {
					$('#portofolio_content').fadeOut();
					$('#portofolio_content').html(text);
					$('#portofolio_content').fadeIn();
					$('.showcase_projet').hover(function(){
						$(this).find('.showcase_projet_img').css({"display":'none'});
						$(this).find('.showcase_projet_detail').css({"display":'block'});
					},function(){
						$(this).find('.showcase_projet_img').css({"display":'block'});
						$(this).find('.showcase_projet_detail').css({"display":'none'});

					});
					$("a.details").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = this.href;
						$("body").fadeOut(1000, redirectPage);
					});
					
					$(".showcase_projet_detail").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = $(this).find('a.details').attr("href");
						$("body").fadeOut(1000, redirectPage);
					});
					
					$("a.details_projet").click(function(event){
						event.preventDefault();
						$("html").css({"background-color":"black"});
						linkLocation = this.href;
						$("body").fadeOut(1000, redirectPage);
					});
					paged = 0;
					$('#next').click(next);
					$('#prev').click(prev);
					action=false;
				}
			});
			$('.current_show').removeClass('current_show');
			$(this).addClass('current_show');
		}
	});
	
	$('#menu_portofolio_prev').click(function(){
		if(!action){
			if(iterporto>0){
				action = true;
				iterporto--;
				var objet = $('.current_show').parent().prev().prev().children();
				var text = objet.text();
				$.ajax({url:ajax.ajaxurl,
					type:'POST',
					dataType: 'text', 
					data:'action=getPortofolio&catName='+text,
					success: function(text) {
						$('#portofolio_content').fadeOut();
						$('#portofolio_content').html(text);
						$('#portofolio_content').fadeIn();
						$('.showcase_projet').hover(function(){
							$(this).find('.showcase_projet_img').css({"display":'none'});
							$(this).find('.showcase_projet_detail').css({"display":'block'});
						},function(){
							$(this).find('.showcase_projet_img').css({"display":'block'});
							$(this).find('.showcase_projet_detail').css({"display":'none'});

						});
						$("a.details").click(function(event){
							event.preventDefault();
							$("html").css({"background-color":"black"});
							linkLocation = this.href;
							$("body").fadeOut(1000, redirectPage);
						});
						
						$(".showcase_projet_detail").click(function(event){
							event.preventDefault();
							$("html").css({"background-color":"black"});
							linkLocation = $(this).find('a.details').attr("href");
							$("body").fadeOut(1000, redirectPage);
						});
						
						$("a.details_projet").click(function(event){
							event.preventDefault();
							$("html").css({"background-color":"black"});
							linkLocation = this.href;
							$("body").fadeOut(1000, redirectPage);
						});
						paged = 0;
						$('#next').click(next);
						$('#prev').click(prev);
						action = false;
					}
				
				});
				$('.current_show').removeClass('current_show');
				objet.addClass('current_show');
			}
		}
	});
	
	$('#menu_portofolio_next').click(function(){
		if(!action){
			var tmpIter = iterporto + 1;
			if(tmpIter<($('#menu_content_showcase>li').length/2)){
				action = true;
				iterporto++;
				var objet = $('.current_show').parent().next().next().children();
				var text = objet.text();
				$.ajax({url:ajax.ajaxurl,
					type:'POST',
					dataType: 'text', 
					data:'action=getPortofolio&catName='+text,
					success: function(text) {
						$('#portofolio_content').fadeOut();
						$('#portofolio_content').html(text);
						$('#portofolio_content').fadeIn();
						$('.showcase_projet').hover(function(){
							$(this).find('.showcase_projet_img').css({"display":'none'});
							$(this).find('.showcase_projet_detail').css({"display":'block'});
						},function(){
							$(this).find('.showcase_projet_img').css({"display":'block'});
							$(this).find('.showcase_projet_detail').css({"display":'none'});

						});
						$("a.details").click(function(event){
							event.preventDefault();
							$("html").css({"background-color":"black"});
							linkLocation = this.href;
							$("body").fadeOut(1000, redirectPage);
						});
						
						$(".showcase_projet_detail").click(function(event){
							event.preventDefault();
							$("html").css({"background-color":"black"});
							linkLocation = $(this).find('a.details').attr("href");
							$("body").fadeOut(1000, redirectPage);
						});
						
						$("a.details_projet").click(function(event){
							event.preventDefault();
							$("html").css({"background-color":"black"});
							linkLocation = this.href;
							$("body").fadeOut(1000, redirectPage);
						});
						paged = 0;
						$('#next').click(next);
						$('#prev').click(prev);
						action = false;
						
					}
				
				});
				$('.current_show').removeClass('current_show');
				objet.addClass('current_show');
			}
		}
	});
		
	$('#menu_showcase_next').click(updateNextShowcase);
	$('#showcase_next').click(updateNextShowcase);
	$('#showcase_prev').click(updatePrevShowcase);
	$('#menu_showcase_prev').click(updatePrevShowcase);
	
	
	$('#content_nav li').each(function(){
		if($(this).text()!="Contact"){
			$(this).after('<li class="divider"></li>');
		}
	});
	
	$('#mini_showcase_next').click(function(){
		if(!action){
			if(itermini<($('#mini_showcase_list_content>li').length-1)){
				itermini++;
				action = true;
				var res = parseInt($('#mini_showcase_list_content').css('left')) - 129;
				$('#mini_showcase_list_content').animate({left:res+'px'},700,function(){
					action = false;
				});
			}
		}
	});
	
	$('#mini_showcase_prev').click(function(){
		if(!action){
			if(itermini>0){
				itermini--;
				action = true;
				res = parseInt($('#mini_showcase_list_content').css('left')) + 129;
				$('#mini_showcase_list_content').animate({left:res+'px'},700,function(){
					action = false;
				});
			}
		}
	});
	
	$("textarea").click(function(){
		if($(this).text()=="Message"){
			$(this).text("");
		}
	});
	
	$("textarea").focus(function(){
		if($(this).text()=="Message"){
			$(this).text("");
		}
	});
	
	$('input').click(function(){
		if($(this).attr("value")=="Nom*" || encodeURI($(this).attr("value"))=="Pr%C3%A9nom*" || $(this).attr("value")=="Adresse e-mail*"){
			$(this).attr("value","");
		}
	});
	
	$('input').focus(function(){
		if($(this).attr("value")=="Nom*" || encodeURI($(this).attr("value"))=="Pr%C3%A9nom*" || $(this).attr("value")=="Adresse e-mail*"){
			$(this).attr("value","");
		}
	});
	
	
	$('#mini_showcase_list_content').wslide({
		width: 120,
		height: 123,
		horiz: true,
		autolink: true
	});
	$('#mini_showcase_list_content-wrap').css({"width":"253px","margin-top":"5px"});
	
	$("#showcase_main_content").wslide({
		width: 293,
		height: 292,
		horiz: true,
		autolink: true
	});
	$('#showcase_main_content-wrap').css({"width":"911px"});
});
var tmp;
function verif_formulaire(name){
		var nom = $('#nom').attr("value");
		var prenom = encodeURI($('#prenom').attr("value"));
		var mail = $('#mail').attr("value");
		var content = $('#content_contact').val();
		if(nom =="" || prenom=="" || mail=="" ||content=="" || nom =="Nom*" || prenom=="Pr%C3%A9nom*" || mail=="Adresse e-mail*" ||content=="Message"){
			$('#error').fadeIn("slow");
			setTimeout(function(){$('#error').fadeOut("slow")},1500);
			return false;
		}
		$.ajax({url:ajax.ajaxurl,
			type:'POST',
			dataType: 'text', 
			data:'action=sendMail&nom='+nom+'&prenom='+prenom+'&mail='+mail+'&content='+content,
			success: function(text) {
			$('#nom').attr({"value":"Nom*"});
			$('#prenom').attr({"value":$('#prenom').attr("rel")});
			$('#mail').attr({"value":"Adresse e-mail*"});
			$('#content_contact').val("Message");
			$('#error p').text(text);
			$('#error').fadeIn("slow");
			setTimeout(function(){$('#error').fadeOut("slow")},1500);
			}
		});
		return false;
	}
