/* Promo */
$(function(){
	/* init */
	$cursor=0;
	$nbrPromo=0;
	$showHidePromoNavigSpeed=500;
	$promoNavigVisible=false;
	$firstImgIni=false;

	/* initial interface */
	$("#promo-div-img").show();
	$("#promo-div-img a img").css({opacity:"1"});
	$("#unclickedSurface").css({opacity:"0.6"});
	$("#promo-div-img > a").each(function(){
		$(this).hide();
		$nbrPromo++;
	});

    margePourCentrer=($("#pointerSurface").parent().width()-$("#pointerSurface").width())/2;
    $("#pointerSurface").css({marginLeft:margePourCentrer});

    $("#promo .on,#promo .over").hide();
	showPromoImg($cursor);

	/* interactivity */
	$("#promo-navig").animate({bottom:"0px"},$showHidePromoNavigSpeed);
	$("#promo").mouseover(showPromoNavig);
	$("#promo").mouseout(hidePromoNavig);

	$(".promo-navig-btn img.off").mouseover(promoBtnOver);
	$("#promo-arrow-left img.out").mouseover(promoArrowOver);
	$("#promo-arrow-right img.out").mouseover(promoArrowOver);

	$(".promo-navig-btn img.over").mouseout(promoBtnOut);
	$("#promo-arrow-left img.over").mouseout(promoArrowOut);
	$("#promo-arrow-right img.over").mouseout(promoArrowOut);

	$(".promo-navig-btn img.over").click(promoBtnClick);
	$("#promo-arrow-left img.over").click(promoLeftArrowClick);
	$("#promo-arrow-right img.over").click(promoRightArrowClick);

	/* determine focus */
	$focusShadow=false;
	$(".promo-shadow-left, .promo-shadow-right, .promo-shadow-bottom").mouseover(focusShadowOn);
	$(".promo-shadow-left, .promo-shadow-right, .promo-shadow-bottom").mouseout(focusShadowOff);
})
function focusShadowOn(){$focusShadow=true;}
function focusShadowOff(){$focusShadow=false;}
function focusNavigOn(){$focusNavig=true;}
function focusNavigOff(){$focusNavig=false;}

function showPromoNavig(){
	if($focusShadow==false && $promoNavigVisible==false){
		$promoNavigVisible=true;
		$(this).find("#promo-navig").stop();
		$(this).find("#promo-navig").animate({bottom:"36px"},$showHidePromoNavigSpeed);
        stopPromoTimer();
	}
}
function hidePromoNavig(){
	if($promoNavigVisible==true || $focusShadow==true){
		$(this).find("#promo-navig").stop();
		$promoNavigVisible=false;
		$(this).find("#promo-navig").animate({bottom:"0px"},$showHidePromoNavigSpeed);
        startPromoTimer();
	}
}
function showPromoImg(newCursor){
	$("#promo-div-img a:eq("+$cursor+")").find("img").fadeOut(250);
	$("#promo-div-img a:eq("+$cursor+")").fadeOut(250);
	$(".promo-navig-btn img.on:eq("+$cursor+")").fadeOut(250);
	$cursor=newCursor;
	$(".promo-navig-btn img.on:eq("+$cursor+")").fadeIn(500,function(){
		$("#promo-div-img a:eq("+$cursor+")").fadeIn(100,function(){
    		$("#promo-div-img a:eq("+$cursor+")").find("img").fadeIn(250);
			hideUnclickableSurface();
		});
	});
}
function promoBtnClick(){
	othis=$(this);
	showUnclickableSurface();
	showPromoImg((othis.parent().attr("title"))-1);
}
function promoLeftArrowClick(){
    showUnclickableSurface();
    newCursor=$cursor-1;
    if(newCursor<0){
        newCursor=$nbrPromo-1;
    }
    showPromoImg(newCursor);
}
function promoRightArrowClick(){
	showUnclickableSurface();
	$newCursor=$cursor+1;
	if($newCursor>=$nbrPromo){
		$newCursor=0;
	}
	showPromoImg($newCursor);
}
function showUnclickableSurface(){
	$("#unclickedSurface").fadeIn(100);
}
function hideUnclickableSurface(){
	$("#unclickedSurface").fadeOut(100);
}
function promoBtnOver(){
	$(this).parent().parent().find("img.over").hide();
	$(this).parent().children("img.over").fadeIn(250);
}
function promoArrowOver(){
	$(this).parent().parent().find("img.over").hide();
	$(this).parent().children("img.over").fadeIn(100);
}
function promoBtnOut(){
	$(this).parent().parent().find("img.over").fadeOut(100);
}
function promoArrowOut(){
	$(this).parent().parent().find("img.over").fadeOut(100);
}

/* side navig */
$(function(){
	$inChange=false;
	$subtabId=0;
	if($("#switch a:eq(0)").hasClass("on")){
		$position="type";
	}else{
		$position="customer";
	}
	$("#switch-bg").click(animSwitch);
	$("#switch a:eq(0)").click(mkSwitchToType);
	$("#switch a:eq(1)").click(mkSwitchToCustomer);
	$("a.side-navig-mask").click(changeProjectTypeList);

	$("a.side-navig-mask").each(function(){
		$(this).attr("href",$(this).attr("title"));
		$(this).attr("title","");
	});
});
function animSwitch(){
	if($inChange==false){
		$inChange=true;
		$obj_img=$(this).children("img");
		$obj_type=$(this).parent().children("a:eq(0)");
		$obj_client=$(this).parent().children("a:eq(1)");
		if($obj_img.css("left")=="3px"){
			$obj_client.addClass("on");
			$obj_type.removeClass("on");
			$obj_img.animate({left:"12px"},100);
			$position="customer";
			getSideNavigAjax("customer");
		}else{
			$obj_type.addClass("on");
			$obj_client.removeClass("on");
			$obj_img.animate({left:"3px"},100);
			$position="type";
			getSideNavigAjax("tag");
		}
	}
}
function mkSwitchToType(){
	if($position!="type"){
		$(this).parent().children("#switch-bg").each(animSwitch);
	}
	return false;
}
function mkSwitchToCustomer(){
	if($position!="customer"){
		$(this).parent().children("#switch-bg").each(animSwitch);
	}
	return false;
}
function hideSwitch(){
	$("#side-navig ul").hide("clip");
	$("#listProjects").children(".project").fadeOut();

	if(arguments[0]) {
		eval(arguments[0]);
		arguments[0]();
	}
}
function getSideNavigAjax(tab){
	hideShowAllProjectBtn();
    indiceNouveaute="no";
    $(".newProjectAvailable").each(function(){
        indiceNouveaute="yes";
    });

	hideSwitch(function(){
		showAnimatedGifLoader(function(){
			$.ajax({
				type: "POST",
				data: "tab="+tab+"&lang="+$str_lang+"&newProject="+indiceNouveaute,
				url: "../../../lib/scripts/get-side-navig-projects.ajax.php",
				success: function(result1){
					if(result==""){
						alert("Une erreur s'est produite, veuillez contacter l'administrateur Web.");
					}else{
						listHeight=0;
						nbrProject=0;
						$cpt=0;
						$(".project").each(function(){
							$cpt++;
						});
						for($i=0;$i<=$cpt;$i++){
							if($i%3==1){
								nbrProject++;
							}
						}
						listHeight=nbrProject*180;

						if($("#switch a:eq(0)").hasClass("on")==true){
							tab="type";
						}else{
							if($("#switch a:eq(1)").hasClass("on")==true){
								tab="clients";
							}
						}
						subtab="";
						$("#listProjects").height(listHeight);
						$.ajax({
							type: "POST",
							data: "tab="+tab+"&subtab="+subtab+"&lang="+$str_lang,
							url: "../../../lib/scripts/get-projects-list.ajax.php",
							success: function(result){
								if(result==""){
									alert("Une erreur s'est produite, veuillez contacter l'administrateur Web.");
								}else{
									$fadeOutOK=false;
									if($fadeOutOK==false){
										$fadeOutOK=true;

										$("#listProjects").html("");
										$("#listProjects").html(result);
										iniProjectList();
										listHeight=0;
										nbrProject=0;
										$cpt=0;
										$(".project").each(function(){
											$cpt++;
										});
										for($i=0;$i<=$cpt;$i++){
											if($i%3==1){
												nbrProject++;
											}
										}
										listHeight=nbrProject*180;

										$("#listProjects").animate({height:listHeight},500,function(){
											hideAnimatedGifLoader(function(){
												$(".project").css({display:"block",opacity:0});
												$(".project").animate({opacity:1},250);

												$("#side-navig ul").html(result1);
												$("#side-navig ul").show("clip");
												$("a.side-navig-mask").click(changeProjectTypeList);
												$("a.side-navig-mask").each(function(){
													$(this).attr("href",$(this).attr("title"));
													$(this).attr("title","");
													$inChange=false;
													showShowAllProjectBtnIfNeed("tab="+tab+"&subtab="+subtab+"&getNumber=getIt");
												});
											});
										});
									}
								}
							}
						});
					}
				}
			});
		});
	});
}
function changeProjectTypeList(){
	if($(this).parent().hasClass("on")==false && $inChange==false){
		$inChange=true;
		hideSpeed=100;
		showSpeed=100;
		$linkToClose=$(this).parent().parent().children("li.on");
		$linkToOpen=$(this).parent();

		$subtabId=$(this).attr("href");
		
		hideShowAllProjectBtn();

		$linkToClose.find(".side-navig-arrow").animate({opacity:0,right:"0px"},hideSpeed);
		
		$linkToClose.find(".side-navig-btn-bg").animate({left:"-140px"},hideSpeed,function(){
			$linkToClose.find(".side-navig-mask span").animate({
				//"color":"#706D67",
				left:"0px"
			},hideSpeed);

			jQuery.fx.off = true;
			$linkToClose.find(".side-navig-hole").fadeOut(hideSpeed,function(){
				jQuery.fx.off = false;

				$linkToClose.removeClass("on");

				listHeight=0;
				nbrProject=0;
				$cpt=0;
				$(".project").each(function(){
					$cpt++;
				});
				for($i=0;$i<=$cpt;$i++){
					if($i%3==1){
						nbrProject++;
					}
				}
				listHeight=nbrProject*180;

				if($("#switch a:eq(0)").hasClass("on")==true){
					tab="type";
				}else{
					if($("#switch a:eq(1)").hasClass("on")==true){
						tab="clients";
					}
				}
				subtab="";
				if($subtabId!=0){
					subtab=""+$subtabId+"";
				}
				$("#listProjects").height(listHeight);
				$fadeOutOK=false;

				jQuery.fx.off = true;
				$linkToOpen.find(".side-navig-hole").fadeIn(showSpeed,function(){
					jQuery.fx.off = false;

					$linkToOpen.find(".side-navig-btn-bg").css({left:"-140px",display:"block"});
					$linkToOpen.find(".side-navig-btn-bg").animate({left:"0px"},showSpeed);
					$linkToOpen.find(".side-navig-arrow").css({opacity:0,right:"0px",display:"block"});
					$linkToOpen.find(".side-navig-arrow").animate({opacity:1,right:"8px"},showSpeed);
					$linkToOpen.find(".side-navig-mask span").animate({
						left:"12px"
					},showSpeed,function(){
						$linkToOpen.addClass("on");
					});
				});

				$("#listProjects").children(".project").fadeOut(function(){
					showAnimatedGifLoader(function(){
						if($fadeOutOK==false){
							$fadeOutOK=true;
							$.ajax({
								type: "POST",
								data: "tab="+tab+"&subtab="+subtab+"&lang="+$str_lang,
								url: "../../../lib/scripts/get-projects-list.ajax.php",
								success: function(result){
									if(result==""){
										//alert("Une erreur s'est produite, veuillez contacter l'administrateur Web.");
									}else{
										$("#listProjects").html("");
										$("#listProjects").html(result);
										iniProjectList();
										listHeight=0;
										nbrProject=0;
										$cpt=0;
										$(".project").each(function(){
											$cpt++;
										});
										for($i=0;$i<=$cpt;$i++){
											if($i%3==1){
												nbrProject++;
											}
										}
										listHeight=nbrProject*180;
										$("#listProjects").animate({height:listHeight},500,function(){
													$inChange=false;
													showShowAllProjectBtnIfNeed("tab="+tab+"&subtab="+subtab+"&getNumber=getIt");
													hideAnimatedGifLoader(function(){
														$(".project").css({display:"block",opacity:0});
														$(".project").animate({opacity:1},250);
													});
												
											
										});
									}
								}
							});
						}
					});
				});
			});
		});
	}
	return false;
}

/* project presentation */
$(function(){
	iniProjectList();
});
function iniProjectList(){
	$(".project").each(function(){
		/* dom rebuild */
		/*h3=$(this).children("h3");
		span=$(this).children("span");*/
        heightText=$(this).find("h3").height()+$(this).find("span").height();
		/*$(this).children(".text h3").remove();
		$(this).children(".text span").remove();
		$(this).children("a").prepend(h3);
		$(this).children("a").append(span);*/
		/* stylising */
		$(this).children("a").css({
			width:"220px",
			height:"160px"
		});
		$(this).css({
            position:"relative"
		});
		$(this).find("img.overShadow").css({
			position:"absolute",
			zIndex:"20",
			display:"block",
			opacity:0
		});
		$(this).find("img.thumbnail").css({
			position:"absolute",
			zIndex:"10"
		});

        $(this).find(".text").css({
			position:"absolute",
			bottom:"10px",
            left:"10px",
			zIndex:"20"
		});
		$(this).find("h3").css({
			zIndex:"40",
			color:"#fff",
			fontWeight:"bold",
			left:"15px",
			opacity:0
		});
		$(this).find("span.teaser").css({
			zIndex:"40",
			color:"#5bbed8",
			fontWeight:"bold",
			opacity:0,
            display:"block"
		});
        
		$(this).find("img.new-project").css({
			zIndex:"50"
		});
		$(this).mouseover(projetOver);
		$(this).mouseout(projetOut);
	});
    // determine the opacity depend on the browser
    $opacity=1;
    if($.browser.msie == true){
        $opacity=0.8;
    }
}
function projetOver(){
	$(this).find("img.overShadow").stop();

	$(this).find("img.overShadow").animate({opacity:$opacity},250);

	$(this).find("h3").stop();
	$(this).find("h3").animate({opacity:1},250);
	$(this).find("span.teaser").stop();
	$(this).find("span.teaser").animate({opacity:1},250);
}
function projetOut(){
	$(this).find("img.overShadow").stop();
	$(this).find("img.overShadow").animate({opacity:"0"},250);
	$(this).find("h3").stop();
	$(this).find("h3").animate({opacity:"0"},250);
	$(this).find("span.teaser").stop();
	$(this).find("span.teaser").animate({opacity:"0"},250);
}

// hide project in over
$(function(){
	$showedProject=$("#listProjects").children(".project").length;
	if($showedProject>9){
		$i=0;
		$("#listProjects").children(".project").each(function(){
			$i++;
			if($i>9){
				$(this).remove();
			}
		});
	}
});

// show all projects btn
$(function(){
	
	$("#showAllProjects").html("<div class=\"round-btn-left-bg\"></div>"
		+"<div class=\"round-btn-middle-bg\"><a>"
		+$("#showAllProjects").attr("title")+"</a></div>"
		+"<div class=\"round-btn-right-bg\"></div>");

	hideShowAllProjectBtn();
	if($("#switch a:eq(0)").hasClass("on")==true){
		tab="type";
	}else{
		if($("#switch a:eq(1)").hasClass("on")==true){
			tab="clients";
		}
	}
	subtab=$(".side-navig-link.on a").attr("href");
	showShowAllProjectBtnIfNeed("tab="+tab+"&subtab="+subtab+"&getNumber=getIt");
	
	$("#showAllProjects div a").click(function(){
		hideShowAllProjectBtn();
		if($("#switch a:eq(0)").hasClass("on")==true){
			tab="type";
		}else{
			if($("#switch a:eq(1)").hasClass("on")==true){
				tab="clients";
			}
		}

		subtab=$(".side-navig-link.on a").attr("href");

		$data="tab="+tab+"&subtab="+subtab+"&limit=nolimit",

		//alert($data);
		$.ajax({
			type: "POST",
			data: $data+"&lang="+$str_lang,
			url: "../../../lib/scripts/get-projects-list.ajax.php",
			success: function(result){
				if(result==""){
					//alert("Une erreur s'est produite, veuillez contacter l'administrateur Web.");
				}else{
					html=$("#listProjects").html();
					$("#listProjects").html(html+result);
					iniProjectList();
					listHeight=0;
					nbrProject=0;
					$cpt=0;
					$(".project").each(function(){
						$cpt++;
					});
					for($i=0;$i<=$cpt;$i++){
						if($i%3==1){
							nbrProject++;
						}
					}
					listHeight=nbrProject*180;

					$("#listProjects").animate({height:listHeight},500,function(){
						$(".project").css({display:"block"});
						$(".project").animate({opacity:1},250);
					});
				}
			}
		})
	});
});


//show btn showAllproject after loading new list of project
function showShowAllProjectBtnIfNeed(theData){
	$.ajax({
		type: "POST",
		data: theData+"&lang="+$str_lang,
		url: "../../../lib/scripts/get-projects-list.ajax.php",
		success: function(result){
			result=parseInt(result);
			//alert(result);
			if(result==""){
				//alert("Une erreur s'est produite, veuillez contacter l'administrateur Web.");
			}else{
				if(result>8){
					//alert(result);
					showShowAllProjectBtn();
				}
			}
		}
	})
}
function hideShowAllProjectBtn(){
		$("#showAllProjects").animate({height:"30px"},150);
		$("#showAllProjects").children("div").fadeOut("fast");
}
function showShowAllProjectBtn(){
		$("#showAllProjects").children("div").fadeIn("fast");
}




/* animated gif for loader */
$(function(){
	$("#side-navig").prepend("<div id=\"project-loader\"><img src=\"../../medias/img/ajax-loader.gif\" alt=\"\" /></div>");
	$("#project-loader").css({
		height:"24px",
		width:"24px",

		position:"absolute",
		left:"573px",
		top:"70px"
	});
	$("#project-loader").css("opacity","0");
});
function showAnimatedGifLoader(){
    $("#project-loader").animate({opacity:1},250);

	if(arguments[0]) {
		eval(arguments[0]);
		arguments[0]();
	}
}
function hideAnimatedGifLoader(){
    $("#project-loader").animate({opacity:0},250);

	if(arguments[0]) {
		eval(arguments[0]);
		arguments[0]();
	}
}

/* timers */
$(function(){
    $interval="5s";//milisecond
    $promoTimerActive=false;
    startPromoTimer();
});
function startPromoTimer(){
    if($promoTimerActive==false){
        $promoTimerActive=true;
        $(document).everyTime($interval, "promoTimer",promoRightArrowClick);
    }
}
function stopPromoTimer(){
    if($promoTimerActive==true){
        $promoTimerActive=false;
        $(document).stopTime("promoTimer");
    }
}
