$(function(){
    handleRelAttr();
	handleBubbles.attach();
	bannerRotator.rotate();
	handleServices.attach();
	handleCCRD.attach();
	handleFacts.attach2();
	handleform("http://local.averda.com/join-our-team/upload-cv.aspx/postForm");
})


function handleRelAttr(){

    $("a[rel='external']").bind("click", function(){
        this.target = "_blank";
    });
    
}


var bannerRotator = {
	
	elementIndex:0,
	totalElements:0,
	clickActive:true,
	rotate:function(){
			this.totalElements = $("#banner ul li").length;
			$("#banner ul").css({width:(990*this.totalElements)});

			$("#banner ul").eq(0).everyTime(8000,function(){
		  		 bannerRotator.clickActive = false;		  	
			  	 $("#banner ul").eq(0).animate({marginLeft:((bannerRotator.elementIndex+1)*-990)},1200,"easeOutCubic",function(){
			  	 	
			        $("#banner ul li").eq(bannerRotator.elementIndex).remove().appendTo("#banner ul");
			        $("#banner ul").eq(0).css({marginLeft:0});
			        if(bannerRotator.elementIndex == $("#banner ul li").length-1)
			        bannerRotator.elementIndex=0;
					bannerRotator.clickActive = true;
			  
			  	 });
		  	
		  	});		

			$("#scroll-right").bind("click",function(){
			  	 
				 if (bannerRotator.clickActive == true) {
				 	 bannerRotator.clickActive = false;
					 $("#banner ul").eq(0).animate({marginLeft:((bannerRotator.elementIndex+1)*-990)},1200,"easeOutCubic",function(){
				        $("#banner ul li").eq(bannerRotator.elementIndex).remove().appendTo("#banner ul");
				        $("#banner ul").eq(0).css({marginLeft:0});
				        if(bannerRotator.elementIndex == $("#banner ul li").length-1)
				        bannerRotator.elementIndex=0;
						bannerRotator.clickActive = true;
				  	 });
				 }
				 return false;
			});			

			$("#scroll-left").bind("click",function(){
			  	 
				if(bannerRotator.clickActive == true){
					 bannerRotator.clickActive = false;
					 $("#banner ul li").eq(bannerRotator.totalElements-1).remove().prependTo("#banner ul");
				     $("#banner ul").eq(0).css({marginLeft:-990});				 
					 $("#banner ul").eq(0).animate({marginLeft:((bannerRotator.elementIndex+1)*0)},1200,"easeOutCubic",function(){
				        if(bannerRotator.elementIndex == $("#banner ul li").length-1)
				        bannerRotator.elementIndex=0;
						bannerRotator.clickActive = true;
				  	 });					
				}
				return false;		
			});	

	}
	
}

var handleBubbles = {
		currentVisible:"",
		isMouseOver: false,
		attach:function(){
			
		    $("#whatlist a, #learnlist a").bind("mouseenter", function(){
				if($(".bubble:visible").attr("id") != $(this).attr("rel"))
				$(".bubble:visible").css({display:"none"});
				if($.browser.msie)
				$("#"+$(this).attr("rel")).css({display:"block"});
				else
				$("#"+$(this).attr("rel")).fadeIn("slow");
				return false;
		    });

		    $("#whatlist a, #learnlist a").bind("mouseleave", function(){
				handleBubbles.currentVisible = $(this).attr("rel");
				$(this).oneTime(500,function(){
					if(handleBubbles.isMouseOver == false)
					$("#"+handleBubbles.currentVisible).css({display:"none"});
				});
				return false;
		    });
			
			$(".bubble").bind("mouseenter",function(){
				handleBubbles.isMouseOver = true;
			});

			$(".bubble").bind("mouseleave",function(){
				$(this).css({display:"none"});
				handleBubbles.isMouseOver = false;
			});

			
			
			$(".bubble .close").bind("click",function(){
				$(this).parent().css({display:"none"});
				return false;
			});
			
		    $("a[rel='didyou']").bind("click", function(){
				$(".bubble").css({display:"none"});
				if($.browser.msie)
				$(this).parent().parent().parent().find(".didyou").css({display:"block"});
				else
				$(this).parent().parent().parent().find(".didyou").fadeIn("slow");
				return false;		        
		    });		
		    
		   $(".facts ul .orange a").bind("click",function(){
		   	$(".bubble").css({display:"none"});
		   	$(this).parent().parent().parent().find(".wallpapers").fadeIn("slow");
		   	return false;
		   	}); 				
			
		}
	
}


var handleServices = {
	
	attach:function(){

		if(location.hash =="#beirut" || location.hash =="#" || location.hash=="")
		{
			$(".services img").eq(0).css({display:"block"});
			$(".cities li").eq(0).find("a").addClass("active");
		}
		else if(location.hash =="#dubai")
		{
			$(".services img").eq(1).css({display:"block"});
			$(".cities li").eq(1).find("a").addClass("active");			
		}
		else if(location.hash =="#riyadh")
		{
			$(".services img").eq(2).css({display:"block"});
			$(".cities li").eq(2).find("a").addClass("active");			
		}
		else if(location.hash =="#sharjah")
		{
			$(".services img").eq(3).css({display:"block"});
			$(".cities li").eq(3).find("a").addClass("active");			
		}
		else if(location.hash =="#doha")
		{
			$(".services img").eq(4).css({display:"block"});
			$(".cities li").eq(4).find("a").addClass("active");			
		}
		else if(location.hash =="#muscat")
		{
			$(".services img").eq(5).css({display:"block"});
			$(".cities li").eq(5).find("a").addClass("active");			
		}
		else
		{
			$(".services img").eq(0).css({display:"block"});
			$(".cities li").eq(0).find("a").addClass("active");			
		}		

		
		$(".cities li a").bind("click",function(){
			
			$(this).parent().parent().find("a").removeClass("active");
			Cufon.refresh();
			$(".services img").fadeOut(300);
			$(".services img").eq($(this).parent().index()).fadeIn(700);
			$(this).addClass("active");
			return false;
			
		});
		
	}
	
}

var handleCCRD = {
	
	attach:function(){
		
		if(location.hash =="#clean" || location.hash =="#" || location.hash=="")
		{
			$(".ccrd li a").removeClass("active");
			$(".ccrd-details").css({display:"none"});
			$(".ccrd li").eq(0).find("a").addClass("active");
			$(".ccrd-details").eq(0).css({display:"block"});
		}
		else if(location.hash =="#collect")
		{
			$(".ccrd li a").removeClass("active");
			$(".ccrd-details").css({display:"none"});			
			$(".ccrd li").eq(1).find("a").addClass("active");
			$(".ccrd-details").eq(1).css({display:"block"});
		}
		else if(location.hash =="#recover")
		{
			$(".ccrd li a").removeClass("active");
			$(".ccrd-details").css({display:"none"});			
			$(".ccrd li").eq(2).find("a").addClass("active");
			$(".ccrd-details").eq(2).css({display:"block"});
		}
		else
		{
			$(".ccrd li a").removeClass("active");
			$(".ccrd-details").css({display:"none"});			
			$(".ccrd li").eq(3).find("a").addClass("active");
			$(".ccrd-details").eq(3).css({display:"block"});
		}		
		
		$(".ccrd li a").bind("click",function(){
			
			$(this).parent().parent().find("a").removeClass("active");
			Cufon.refresh();
			$(".ccrd-details").css({display:"none"});
			$(".ccrd-details").eq($(this).parent().index()).css({display:"block"});
			$(this).addClass("active");
			return false;
			
		});
		
	}
	
}



function pagescroll()
 {
 	
 	var $pagelist = $("#master-who > .panel");
 	var $firstchild = $("#master-who > div.panel:first-child");
 	var $lastchild = $("#master-who > div.panel:last-child");
 	var $currpage,$nextpage,$prevpage,$deactive;

 	if(location.hash == "" || location.hash == "#")
 	  location.hash ="home";


  $.scrollTo($("#master-who > div[id='"+location.hash.replace("#","")+"']"),800,{easing:"easeOutExpo",offset:{top:-90}});



   $('a[href*=#]').bind('click',function(){
   	
   	 if($(this).attr("href")!="#")
   	  {
   	   $.scrollTo($(this).attr("href"),1000,{easing:"easeOutExpo",offset:{top:-90}});
   	   location.hash = $(this).attr("href");
   	  }
   	 return false;

   	});
 	
 	
   $("#up-down a.down").bind("click",function(){
   	 
	 
	 if(location.hash == "" || location.hash == "#" || location.hash == "#home")
	 {	
   	   $nextpage = $("#master-who > div.panel").eq(1);
       $.scrollTo($nextpage,1000,{easing:"easeOutExpo",offset:{top:-90}});
       location.hash = $nextpage.attr("id");	 	
	 }
	 else
	 {
	   	 $currpage = $("#master-who > div[id='"+location.hash.replace("#","")+"']");
	   	 
	   	 if($currpage.attr("id") != $lastchild.attr("id"))
	   	  {
	   	   $nextpage = $currpage.next();
	       $.scrollTo($nextpage,1000,{easing:"easeOutExpo",offset:{top:-90}});
	       location.hash = $nextpage.attr("id");
	   	  }	 	
	 }
	 

   	 return false;
   	
   	});

   $("#up-down a.up").bind("click",function(){
   	 
   	 if(location.hash != "#who") 
	 {
	   	 $currpage = $("#master-who > div[id='"+location.hash.replace("#","")+"']");
	   	 
	   	 if($currpage.attr("id") != $firstchild.attr("id"))
	   	  {
	   	   $prevpage = $currpage.prev();
	       $.scrollTo($prevpage,800,{easing:"easeOutExpo",offset:{top:-90}});
	       location.hash = $prevpage.attr("id");
	   	  }	 	
	 }

   	 return false;
   	
   	});

  
 }
 
 
 var handleFacts = {
 	 total:0,
	 current:0,
	 activeNext:true,
	 attach:function(){

			this.total = $(".fact").length;
			this.current = Math.floor(Math.random()*this.total);
			$(".fact").eq(this.current).css({"display":"block"});
			
			$(".next").bind("click",function(){
				if(handleFacts.activeNext == true)
				  {
				  	alert("test");
				    handleFacts.activeNext = false;
				    if(handleFacts.current == handleFacts.total-1){
					    $(".fact").eq(handleFacts.current).slideUp("fast",function(){
						    handleFacts.current=0;
						    $(".fact").eq(handleFacts.current).slideDown("slow",function(){handleFacts.activeNext = true;});					
					    });
				    }
				    else{
					    $(".fact").eq(handleFacts.current).slideUp("fast",function(){
						    handleFacts.current+=1;
						    $(".fact").eq(handleFacts.current).slideDown("slow",function(){handleFacts.activeNext = true;});						
					    });
    				
				    }
                }
				return false;
				
			});
	 },
	 attach2:function(){

			$(".facts").each(function(index){
			handleFacts.total = $(this).find(".fact").length;
			handleFacts.current = Math.floor(Math.random()*handleFacts.total);
			$(this).find(".fact").eq(handleFacts.current).css({"display":"block"});
			
			$(this).find(".next").bind("click",function(){
			
				if (handleFacts.activeNext == true) {
					handleFacts.activeNext=false;
					if (handleFacts.current == handleFacts.total - 1) {
						$(this).parent().find(".fact:visible").slideUp("fast", function(){
							handleFacts.current = 0;
							$(this).parent().find(".fact").eq(handleFacts.current).slideDown("slow",function(){handleFacts.activeNext=true;});
						});
					}
					else {
						$(this).parent().find(".fact:visible").slideUp("fast", function(){
							handleFacts.current += 1;
							$(this).parent().find(".fact").eq(handleFacts.current).slideDown("slow",function(){handleFacts.activeNext=true;});
						});
						
					}
				}
				return false;
				
			});				
				
			});
			
	 }	 
	
 }
 
 
 
function handleform(path)
 {
  if($(".text1").length)	
  $(".text1").eq(2).mask("99/99/9999");	
  $("#submitter").bind("click",function(){
  	
  	 var validForm = true;
  	 
  	if($(".text1").eq(0).val() == "")
    {
    $(".text1").eq(0).css({borderColor:"red"});
    validForm=false;
    }
    else
    $(".text1").eq(0).css({borderColor:"#cccccc"}); 
    
    if($(".text1").eq(1).val() == "")
    {
    $(".text1").eq(1).css({borderColor:"red"});
    validForm=false;
    }
    else
    $(".text1").eq(1).css({borderColor:"#cccccc"});    

    if($(".text1").eq(2).val() == "")
    {
    $(".text1").eq(2).css({borderColor:"red"});
    validForm=false;
    }
    else
    $(".text1").eq(2).css({borderColor:"#cccccc"});

    if($(".text1").eq(3).val() == "")
    {
    $(".text1").eq(3).css({borderColor:"red"});
    validForm=false;    
    }
    else
    $(".text1").eq(3).css({borderColor:"#cccccc"});      

    if($(".text1").eq(4).val() == "")
    {
    $(".text1").eq(4).css({borderColor:"red"});
    validForm=false;    
    }
    else
    $(".text1").eq(4).css({borderColor:"#cccccc"});

    if($(".textarea1").eq(0).val() == "")
    {
    $(".textarea1").eq(0).css({borderColor:"red"});
    validForm=false;    
    }
    else
    $(".textarea1").eq(0).css({borderColor:"#cccccc"});

    if($(".textarea1").eq(1).val() == "")
    {
    $(".textarea1").eq(1).css({borderColor:"red"});
    validForm=false;    
    }
    else
    $(".textarea1").eq(1).css({borderColor:"#cccccc"});



    if(validForm == true)
    {
	     fullname = $(".text1").eq(0).val()+" "+$(".text1").eq(1).val();
	     dob = $(".text1").eq(2).val();
	     loc = $(".text1").eq(3).val();
	     phone =$(".text1").eq(4).val();
		 role = $(".textarea1").eq(0).val();
		 why = $(".textarea1").eq(1).val();
	     uploadedfile = $("#filepath").val();

    $.ajax({
      type:"POST",
      url:"http://local.averda.com/join-our-team/upload-cv.aspx/postForm",
      data:"{'fullname':'"+fullname+"','dob':'"+dob+"','loc':'"+loc+"','phone':'"+phone+"','role':'"+role+"','why':'"+why+"','uploadedfile':'"+uploadedfile+"'}",
      contentType:"application/json;charset=utf-8",
      dataType:"json",
      success: function(msg){

		$("#message").removeClass("error");
	  	$("#message").addClass("success");
		$("#message").fadeIn("slow");

	  	$("#message").text("Thank you "+$(".text1").eq(0).val()+", your details were submitted successfully.");

		$(".text1").eq(0).val("");
		$(".text1").eq(1).val("");
		$(".text1").eq(2).val("");
		$(".text1").eq(3).val("");
		$(".text1").eq(4).val("");
		$(".textarea1").eq(0).val("");
		$(".textarea1").eq(1).val("");
		$("#uploadedfile").text("");
		
		

	  },
      error:function(msg){
		$("#message").removeClass("success");
	  	$("#message").addClass("error");
		$("#message").fadeIn("slow");			  	
      	$("#message").text("We were not able to process your details, please try later.");     	
      }
      });
  	}

  	 return false;
  	 
  });
}


var tRotator = {

    elementIndex: 0,
    totalElements: 0,
    clickActive: true,
    rotate: function () {
        this.totalElements = $("ul.tlist>li").length;
        $("ul.tlist").css({ width: (360 * this.totalElements) });

        $("ul.tlist").eq(0).everyTime(8000, function () {
            if (tRotator.clickActive == true) {
                tRotator.clickActive = false;
                $("ul.tlist").eq(0).animate({ marginLeft: ((tRotator.elementIndex + 1) * -360) }, 1200, "easeOutCubic", function () {

                    $("ul.tlist>li").eq(tRotator.elementIndex).remove().appendTo("ul.tlist");
                    $("ul.tlist").eq(0).css({ marginLeft: 0 });
                    if (tRotator.elementIndex == $("ul.tlist>li").length - 1)
                        tRotator.elementIndex = 0;
                    tRotator.clickActive = true;

                });
            }

        });

        $(".tnav li").eq(1).find("a").bind("click", function () {

            if (tRotator.clickActive == true) {
                tRotator.clickActive = false;
                $("ul.tlist").eq(0).animate({ marginLeft: ((tRotator.elementIndex + 1) * -360) }, 1200, "easeOutCubic", function () {
                    $("ul.tlist>li").eq(tRotator.elementIndex).remove().appendTo("ul.tlist");
                    $("ul.tlist").eq(0).css({ marginLeft: 0 });
                    if (tRotator.elementIndex == $("ul.tlist>li").length - 1)
                        tRotator.elementIndex = 0;
                    tRotator.clickActive = true;
                });
            }
            return false;
        });

        $(".tnav li").eq(0).find("a").bind("click", function () {

            if (tRotator.clickActive == true) {
                tRotator.clickActive = false;
                $("ul.tlist>li").eq(tRotator.totalElements - 1).remove().prependTo("ul.tlist");
                $("ul.tlist").eq(0).css({ marginLeft: -360 });

                $("ul.tlist").eq(0).animate({ marginLeft: ((tRotator.elementIndex + 1) * 0) }, 1200, "easeOutCubic", function () {
                if (tRotator.elementIndex == $("ul.tlist>li").length - 1)
                tRotator.elementIndex = 0;
                tRotator.clickActive = true;
                });
            }
            return false;
        });

    }

}






