    /******************/
    /* Configurations */
    /******************/
    
    /* Debug */
    var debug=false;
    
    /* Div Locations */
    var subnavLeadershipId = "leadershipList";
    var home_speed=1000;
    var home_easing = "easeOutExpo"
    
    /* Data Storage - do not modify */
    var leadersData;
    var currentHomeSlide=0;
    var totalHomeSlides;
    
    
    /**
     * Log data
     * @param string string the text to log
     *
     **/
    function log(string){
    
      /* Currently, Firebug is only supported for debugging */
      if (debug){ console.log(string); }
      
    }
    


// When the page is ready

$(document).ready(function(){

  $("a").click(function() {
    this.blur();
  });

  //fade in footer befor anything else
  $("#footer").fadeIn("slow");
  
  // before we do anything, reset all links and menus
  $(".nav.first > li a").click(function(){
    $("#footer").css("display","none");
    $(".nav.first li").removeClass("active");
    $(".subnav").css("display","none");
    $("#content").css("display","none");
  });
  
  // set class of active to main nav elements based on the body's id
  var elemId = document.getElementsByTagName("body")[0].id;
  $("#nav_"+elemId).addClass("active");
  $("#subnav_"+elemId).fadeIn("slow");
  
  $(".subnav li a").click(function(){
    $(".subnav li").removeClass("active");
   $("#content").css("display","none");
  });
  
  
  
  // onload of work page, activate first client      
  $("#subnav_work ul li:first").addClass("active");
  
  $("#nav_work").click(function(){
    $(this).addClass("active");
    //UPDATE HEADER
    document.title = "Crave, Inc : Work";
  }); 
  
  $("#subnav_work li").click(function(){
  $(this).addClass("active");
    $("#content").fadeIn("slow");
  });
  
  $("#nav_about").click(function(){
    //$(this).addClass("active");
    //$("#subnav_about").fadeIn("slow");
  });
   
  /* Leadership */
  $("#leadership").click(function(){
    loadLeaders('0');
    $(this).addClass("active");
    $("#subnav_leadership").fadeIn("slow");
  });
  
  $("#subnav_leadership li").click(function(){
  
    $("#leadership").addClass("active");
    $(this).addClass("active");
  });
  
  $("#nav_contact").click(function(){
    $(this).addClass("active");
  });
  
  $("#nav_blog").click(function(){
    $(this).addClass("active");
  });
  

 

    
    
});

 /*********** Contact ******************/
 
   //dynamically set heights of <p> on contact page to equal the tallest <p>
  function fixColumns(){  

    $("#content").fadeIn('slow');
    
    var tempH = 0;
    if ($(".col-1 > p").height()>$(".col-2 > p").height()) {
      tempH = $(".col-1 > p").height(); 
    } else {
      tempH = $(".col-2 > p").height()
    }
    if ($(".col-3 > p").height()>tempH) {
      tempH = $(".col-3 > p").height(); 
    }
    
    $("#content h2+p").each(function(i){
      $("#content h2+p").css("height",tempH+"px");
    })
    
    

  
  }
 
 
 /*********** Leaders *****************/
 
    function showLeaders(ulId,leaders){ 

       for(i=0;i<leaders.length;i++){

         //////////////////////////////////////
         // create a new ul for every 4 items
         //////////////////////////////////////
         if ( i % 4 == 0 ){

           // Create a new containter div
           $("#"+ulId).append('<div style="display:block" class="subnav" id="leaders_group_' + i +'"></div>');
           
           // Create a li containter div
           $("#leaders_group_"+i).append('<ul class="nav" id="leaders_list_' + i +'"></ul>');
           ulName='leaders_list_'+i;
           
         
         }
          //////////////////////////////////////
          // create an li and text
          //////////////////////////////////////
          link_text=leaders[i].first_name + " " + leaders[i].last_name;
          $("#"+ulName).append('<li class="nav" id="leader_' + i +'"><a href="javascript:void(' + i +');">' + link_text + '</a></li>');
          $("#leader_" + i).click( function(){ showLeader(this.id)} );

       }
       


    }
    
    function loadLeaders(id){
    
      log('loading leaders');
      jQuery.getJSON('/cms/leaders_json',{},
    
        function(leaders){
          showLeaders('subnav_leadership_wrapper',leaders);
          leadersData=leaders;
          if (id!=='') showLeader('leader_'+id);
          loaderOff();
        }
                     
      );


      $("#subnav_leadership").fadeIn("slow");
    
    
    }
    
    function showLeadershipNav(){
    
      $("#subnav_about" + " li").removeClass('active');
      $("#subnav_about #leadership").addClass('active')
      $("#nav_about").addClass('active');
      $("#subnav_about").fadeIn("slow")
    
    }
    

    function showLeader(id){
         
         $leaderInfo = id.split("_");
         
         /* Removal all active leaders */   
         $("#subnav_leadership_wrapper li").removeClass('active');
        // console.log('restting leaders');                               
         
         /* Set the new active leader */
         $("#"+id).addClass('active');
         
         // Empty the content
         $("#content").empty();
         $("#content").removeClass("purple");
         $("#content").css('display','none'); 
 
         // Set the title     
         document.title=leadersData[$leaderInfo[1]].page_title;
         
         // set variable for the long styles
         $("#content").css('backgroundImage','url(' + leadersData[$leaderInfo[1]].background_image_path +')');
         $("#content").css('background-color',leadersData[$leaderInfo[1]].background_color);
         $("#content").css('visibility','hidden');
         $("#content").css('display','block'); 
        // $("#content").css('float','left'); 
         
         // set the class
        if (leadersData[$leaderInfo[1]].alignment == 'right'){
           $("#content").addClass("leader_right");
           h1LeaderClass='hdr_organic-sifr-right';
           leaderSwf="/fl/din-right-align.swf";
           sifrFlashVars = "textalign=right";
         }else{ 
           h1LeaderClass='hdr_organic-sifr';
           leaderSwf="/fl/din.swf";
           $("#content").removeClass("leader_right");
            sifrFlashVars = "";
          }
         
         // Add the content
         leaderHtml='<div id="leader_content_'+ i + '">';
         leaderHtml+='<h1 class="'+ h1LeaderClass + '" id="leader_slide_' + $leaderInfo[1] + '">' + leadersData[$leaderInfo[1]].first_name + '</h1>';
         leaderHtml+='<h2 style="color:#' + leadersData[$leaderInfo[1]].role_color + '">' + leadersData[$leaderInfo[1]].role + '</h2>';
		     leaderHtml+='<p>' + leadersData[$leaderInfo[1]].copy + '</p>';
	       leaderHtml+='</div>';  
	         
	       $("#content").append(leaderHtml);
	         
	       //Sifr  
	       if(typeof sIFR == "function"){
	          log( leadersData[$leaderInfo[1]]);
            //sIFR.replaceElement(named({sSelector:"#leader_slide_"+$leaderInfo[1], sFlashSrc:leaderSwf, sColor:"#FFFFFF", sLinkColor:"#000000", sBgColor:leadersData[$leaderInfo[1]].background_color, sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0/*, sFlashVars:"textalign=center&offsetTop=6"*/}));
            sIFR.replaceElement(named({sSelector:"#leader_slide_"+$leaderInfo[1], sFlashSrc:leaderSwf, sColor:"#FFFFFF", sLinkColor:"#000000",  sWmode:"transparent", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars: sifrFlashVars }));
          };
      
      
          $("#content").css('display','none');
          $("#content").css('visibility','visible'); 
	        $("#content").fadeIn("slow");
       
       }
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
 /*********** Home Slides *****************/
       var homeSlides;
       function startHomeSlides(){
       
        $("#slide_0").animate({"left": "0px"}, home_speed,home_easing);
       
         log('loading home slides');                           
         jQuery.getJSON('/cms/home_slides_json',{},
         
    
          function(slides){
          
            //console.log(slides)
            totalHomeSlides=slides.length;
            homeSlides=slides;
            
            // Load the slides
            slide_count=0;
            for(i=0;i<slides.length;i++){
            
              if (slide_count==0){ 
                slide_class='first_slide';
              }else{
                slide_class='slide';
              }
             
              if (slides[i].link.length!=0){ 
                html="<a href='" + slides[i].link + "'>";         
                html+="<div class='" + slide_class + "' id='slide_" + slide_count + "'>";
                html+="<img id='home_slide_image_"+ slide_count +"' src='" + slides[i].image_path + "'/>";
                html+="</div>";
                html+="</a>";
                //console.log('link provided:' + slides[i].link )
              }else{
                html="<div class='" + slide_class + "' id='slide_" + slide_count + "'>";
                html+="<img id='home_slide_image_"+ slide_count +"' src='" + slides[i].image_path + "'/>";
                html+="</div>";
                //console.log('link not provided:' + slides[i].link )
              }

              $("#slide_wrapper").append(html)
              slide_count++;     
            }
            
            // start the sliding
            
            setInterval('showHomeSlides()',10000)
  
           }
                     
          );
       
       
       }
    
    

       function allHomeSlidesLoaded(){

         slide_count=0; 
         for(i=1;i<=homeSlides.length;i++){
         
           if (document.getElementById("home_slide_image_" + slide_count).complete == false){
             return false;
           }

           slide_count++;         
         }
         
         return true;
       
       
       }
       
       function showHomeSlides(){
       
         // get the next slide (tmp) to see if the next slide is loaded
         tmpNextSlide=currentHomeSlide+1; 
         if (tmpNextSlide>totalHomeSlides-1) { tmpNextSlide=0; }        
          
         if ( $("#home_slide_image_" + tmpNextSlide).length > 0 && 
                document.getElementById("home_slide_image_" + tmpNextSlide).complete != true  ){
            // the next slide is not loaded      
            return;
          }
          
          
         // see if all the slides have been loaded
         // if so, turn off the loader
         if (allHomeSlidesLoaded()){
         
           $('#loader').css('visibility','hidden');
         
         }

         // get the next slide
          nextSlide=currentHomeSlide+1;
          
          // get the prev slide
          prevSlide =  currentHomeSlide - 1;
          if (prevSlide<0) { prevSlide=totalHomeSlides-1 }
           $("#slide_" + prevSlide).css("left","870px");
     
          // get the next slide
          if (nextSlide>totalHomeSlides-1) { nextSlide=0; }
     
          // Move slides to the left
        
            $("#slide_" + currentHomeSlide).animate({"left": "-=870px"}, home_speed,home_easing);
            $("#slide_" + nextSlide).animate({"left": "-=870px"}, home_speed, home_easing);
          
        
          currentHomeSlide++;
          if (currentHomeSlide>totalHomeSlides-1) currentHomeSlide=0;
          log('cur:' + currentHomeSlide + ' ' + totalHomeSlides);
    
        }
    

  /********* Misc *******************/
      function loaderOff(){
    
      $('#loader').css('visibility','hidden');
    
    }
    
    function loaderOn(){
    
      $('#loader').css('visibility','visisble');
    
    }  
    
  /********* To be deleted **********/ 
    
    
    
    
        
    /**
     * image Loader
     * Loads images based on clientData, currentClientIndex, and imageArray
     *
     **/
    function imageLoader_DELETEME(direction){

      // Local Vars
      log("current client index: " + currentClientIndex);
      nextAction = "no action";
      images = clientData[currentClientIndex].images;

      //debug
      log("\n");
      log("Image loader count:" + images.length);

    
      for(i=0;i<images.length;i++){
      
        // initialize images
        if (images[i].loaded == undefined) images[i].loaded=false;

        // FF debug
        if (images[i].type=='photo'){
         log(i + " " + images[i].url + "(" + images[i].loaded + ")" );
        }else{
         log(i + " project slide (" + images[i].loaded + ")" );
         
        }

      
        // Check to see if an image is downloading 
        if (images[i].loaded == 'downloading'){
        
           nextAction = "wait";
        
           // see if the item is done downlaoding
           if ( imageArray[i].complete == true ){
             nextAction = "no action";
             images[i].loaded = "completed";
             if(i==0 && direction!="prev") {
             
               setSlideTitle(images[i].description);
               $("#slide_"+i).fadeIn("slow");
               $("#content").fadeIn("slow");
               $("#total_slides").text(clientData[currentClientIndex].images.length);
               //console.log('show 1st image');
               
             }
           }
        }
        
       
        if( (images[i].loaded == false ) && nextAction == 'no action' ){
       
          //console.log('  action: starting download');
          nextAction = "downloading";
          images[i].loaded = "downloading";
        
          // Create containing div  
          tmpDiv = document.createElement('div');
          // tmpDiv.setAttribute("class","slide")can not setAttribute of class in IE5/6/7.
          elemClassTmpDiv = document.createAttribute("class");
          elemClassTmpDiv.nodeValue="slide";
          tmpDiv.setAttributeNode(elemClassTmpDiv);
          tmpDiv.id="slide_" + i;
          
          // set directional info
          if(i==0 && direction!="prev") tmpDiv.style.display="none";
          if(i==0 && direction=="prev") tmpDiv.style.display="none";
          $("#"+divId).append(tmpDiv);
        
          if (images[i].type=='photo'){
        
            // Load the next image
            imageArray[i] = new Image(870,385);
            imageArray[i].src = images[i].url;
            imageArray[i].id = "img_" + i;
            $("#slide_"+i).append(imageArray[i]);
        
          }else{
          
           // populate the project slide
           //console.log("at projec #slider_" + i);
           //console.log(images[i]);
           
           
           if (images[i].project_slide_background_color.indexOf('#') == -1){
                  images[i].project_slide_background_color='#'+images[i].project_slide_background_color;
           }
           
            if (images[i].project_slide_title_color.indexOf('#') == -1){
                  images[i].project_slide_title_color='#'+images[i].project_slide_title_color;
           }
          
                   
           html='<div style="background-color: ' + images[i].project_slide_background_color + '"  id="project_slide_' + i + '">';
           html+='<h1 style="visibility:hidden" class="hdr_organic-sifr" id="project_slide_header_' + i + '">'+ images[i].project_slide_intro_title +'</h1>';
           html+='<h2 style="color:'+ images[i].project_slide_title_color +'">'+images[i].project_slide_title+'</h2>';
           html+='<p style="color:black">'+images[i].project_slide_copy+'</p>';
           html+='</div>';
           
           $("#slide_"+i).append(html);
           
            if(typeof sIFR == "function"){
               sIFR.replaceElement(named({sSelector:"#project_slide_header_"+i, sFlashSrc:"/fl/din.swf", sColor:"#FFFFFF", sLinkColor:"#000000", sBgColor:images[i].project_slide_background_color, sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0/*, sFlashVars:"textalign=center&offsetTop=6"*/}));
           };
           
            
           $("#project_slide_"+i).css({display:"block"})
           $("#project_slide_"+i).css({padding:"52px 166px 0 46px",width:"658px",height:"385px"});
           $("#project_slide_header_"+i).css({visibility:'visible'});
           $(".slide").fadeIn("slow");
          
            
           //$(".slide").html($("#client_tmp").html());
           
           // For project slides - no need to cycle again
           imageArray[i] = {complete:true}
           
           
          }
        }
      }
      
     
      if ( nextAction == "no action" ){

        clearInterval(timerID);
        timerID = -1;  /* reset the timer ( -1 = off ) */
        for(i=0;i<images.length;i++){
        images[i].loaded = false; 
        
        }
        
        if (direction == 'prev'){
        
          $("#slide_wrapper .slide").css("left", "-870px");
          $("#slide_wrapper .slide").css("display", "none");
          $("#slide_wrapper #slide_"+parseInt(clientData[currentClientIndex].images.length-1)+".slide").css("left", "0px");
          $("#slide_wrapper #slide_"+parseInt(clientData[currentClientIndex].images.length-1)+".slide").fadeIn("slow");
        }
      }
    }
    