/** HOMEPAGE **/
  function homepage_fade($hp_visit) {

    if($hp_visit) {
      $(document).ready(function() {
        $("#homepage_intro").hide();
      });
      
    } else {
      $(window).load(function() {
        setTimeout(function() {
          $("#homepage_intro").fadeOut(2000);
        },2000);
      
        $("#homepage_intro").click(function() {
          $(this).fadeOut(500);
        });
      
      });
    }
  }
 
 
/** OUR ENVIRONMENT **/
    function our_environment_init() {
      $(document).ready(function() {
        
    /* generate supporting divs */
        $cheats = "";
        $cheat_loc = Array(
          Array(32,243),
          Array(329,446),
          Array(514,84),
          Array(563,316),
          Array(764,289),
          Array(694,537)
        );
     
     /* replace img with background */
       $s = $("#our_environment_image img").attr("src");
       $("#our_environment_image").css({
         "background-image":"url('"+$s+"')"
       });
       $("#our_environment_image img").hide();
       
     /* fade out welcome screen */  
       window.setTimeout(
         '$("#our_environment_welcome").fadeOut(1000)',
         14000
       );
       $("#our_environment_welcome").click(function() {
         $(this).fadeOut(1000);
       });
     
        $rollovers = "";
        $i = 0;
        
        $(".hot_area").each(function() {
          
          /* generate rollovers */
          $b = $(this).css("background-image");
          $h = $(this).height();
          $w = $(this).width();
          $t = $(this).css("top");
          $l = $(this).css("left");
          $rollovers += "<div class='rollover' style='position: absolute; z-index: 49; ";
          $rollovers += "background: "+$b+";";
          $rollovers += "width: "+$w+"px; height: "+$h+"px;";
          $rollovers += "top: "+$t+"; left: "+$l+";";
          $rollovers += "'>&nbsp;</div>";

          $(this).css({ "background-image":"url(../image/our_environment/hotspot_blank.png)" });
          
          /* generate cheats */
          $cheats += "<div class=\"cheat_tag\" ";
          $cheats += "style=\"left: "+$cheat_loc[$i][0]+"px; ";
          $cheats += "top: "+$cheat_loc[$i][1]+"px; ";
          $cheats += "\">"+$(this).html()+"</div>";
          $i++;
        });
        
        /* add supporting divs */
        $("#backgrounds").html($rollovers);
        $("#cheats").html($cheats);
        
        $(".rollover").hide();
        $(".cheat_tag").hide().fadeIn(500);
        
    /* add rollover for hot areas */
        $("#hot_areas div").mouseenter(function() {
          $i = $(this).index();
          $cheat = ".cheat_tag:eq("+$i+")";
          $rollover = ".rollover:eq("+$i+")";
          
          $($cheat).fadeIn(500);
          $($rollover).fadeIn(500);
        });
        
        $(".hot_area").mouseleave(function() {
          $i = $(this).index();
          $cheat = ".cheat_tag:eq("+$i+")";
          $rollover = ".rollover:eq("+$i+")";
          
          if (!$c) {
            $($cheat).fadeOut(500);
          }
          $($rollover).fadeOut(500);
        });
    
    /* add cheats on click */
        $cheat_on = "Turn cheat sheet off";
        $cheat_off = "Turn cheat sheet on";
        $c = 1;
        
        $("#cheats_on").click(function() {
          if($c) {
            $(this).html($cheat_off);
            $(".cheat_tag").fadeOut(500);
            $c = 0;
          } else {
            $(this).html($cheat_on);
            $(".cheat_tag").fadeIn(500);
            $c = 1;
          }
        });
      });
    }
    


/** IMAGES WE LOVE **/
    if(window.location.hash) {
      $current_slide = window.location.hash.substring(1);
    } else {
      $current_slide = 0;
    }
    $s = ".slide:eq("+$current_slide+")";
    $speed = 500;
    
    function images_we_love_init() {
      $(document).ready(function() {
      
        /* initiate */
        $(".slide").hide();
        $($s).show();        
        $(".slide_area").css({ "position":"relative" });      
        $(".slide").css({
          "position":"absolute",
          "top":"0px",
          "left":"0px",
          "z-index":"10",
          "background":"#FFFFFF"
        });
        
        $(".images_we_love_nav").hide();
        
        /* add indicator */
        $slide_nav = $(".slide_nav").html()+"<div id='indicator'></div>";
        $(".slide_nav").html($slide_nav);
        $(".slide_nav").css({ "position":"relative" });
        $("#indicator").css({ "width":$(".slide_nav li").width()+"px" });
        $total_width = parseInt($(".slide_nav li").css("margin-right").replace("px","")) + $(".slide_nav li").width();
        
        /* nav click */
        $(".slide_nav li").click(function(event) {
          event.preventDefault();
          images_we_love_fadein($(this).index());
        });
        
        /* prev click */
        $(".slide_next_prev li.prev").click(function(event) {
          event.preventDefault();
          images_we_love_nextprev($current_slide - 1);
        });
        
        /* next click */
        $(".slide_next_prev li.next, .image").click(function(event) {
          event.preventDefault();
          images_we_love_nextprev($current_slide + 1);
        });
        
      });
      
      $(window).load(function() { 
        $(".slide_area").height($($s).height()); 
        $(".images_we_love_nav").fadeIn(200);
      });
    } /* images_we_love_init() */
    
    function images_we_love_fadein($slide_index) {
      $this_slide = ".slide:eq(" + $slide_index + ")";
      $prev_slide = ".slide:eq(" + $current_slide + ")";
      //$("#feedback").html($this_slide);
      
      /* cross fade */
      $(".slide_area").animate({ "height":$($this_slide).height() }, $speed);
      $(".slide").css({ "z-index":"10" });
      $($this_slide).css({ "z-index":"50" });
      $($this_slide).fadeIn($speed,function() {
        $($prev_slide).hide();
      });
      
      /* move indicator */
      //alert($total_width * $slide_index);
      $("#indicator").animate({
        "left":$total_width * $slide_index
      },$speed);
      
      $current_slide = $slide_index;
    } /* images_we_love_fadein() */
    
    function images_we_love_nextprev($slide_index) {
      if($slide_index < 0) {
        $slide_index = $(".slide").length - 1;
      }
      
      if($slide_index >= $(".slide").length) {
        $slide_index = 0;
      }
      
      images_we_love_fadein($slide_index);
    } /* images_we_love_nextprev */

/** OUR CLIENTS **/
    function client_toggle_init() {
      $(document).ready(function() {

        /* initialize */
        $hash = window.location.hash;
        if ($hash == "") {
          $("#engagement_toggle input,#client_toggle input,#medium_toggle input,").attr({"checked":"checked"});
        } else {
          $("#client_toggle input,#medium_toggle input,").attr({"checked":"checked"});
          $("#engagement_toggle input").each(function() {
            if ($hash == "#"+$(this).attr("value")) {
              $(this).attr({"checked":"checked"});
            }
          });
          $(window).scrollTop(0);
        }
      
        /* toggle current/all clients */
        if($("#past_current")) {
          $("#past_current input").click(function() {
        
            if($(this).attr("value") == "all_clients") {
              $(".column_middle h2").html("All Clients (1972-present)");
              $("#engagement_toggle input").attr({"checked":"checked"});
              $("#engagement_toggle").slideUp($speed);
              
            } else {
              $(".column_middle h2").html("Current Clients");
              $("#engagement_toggle").slideDown($speed);
            }
            
          });
        }
        
        /* toggle options */
        $(".selector input").click(function() {
          client_show_hide();
        });
        
        client_show_hide();
        client_stick();
      });
    } /* client_toggle_init */
    
    function client_show_hide() {
      //alert("??");
      $speed = 500;
      
      $(".client").each(function() {
        $thisclass = $(this).attr("class");
        $e = 0;
        $c = 0;
        $m = 1;
        $msg = "";
        
        /* past/current */
        if ($("#past_current input:checked").val() == "current_clients" && $thisclass.search("current") == -1) {
          $(this).hide();
          return;
        }
        
        /* type of engagement */
        $("#engagement_toggle input:checked").each(function() {
          
          if ($thisclass.search($(this).val()) != -1) {
            $e++;
          }
        });
        
        /* type of client */
        $("#client_toggle input:checked").each(function() {
          
          if ($thisclass.search($(this).val()) != -1) {
            $c++;
          }
        });
        
        /* type of medium 
        $("#medium_toggle input:checked").each(function() {
          
          if ($thisclass.search($(this).val()) != -1) {
            $m++;
          }
        });*/
        
        if ($e == 0 || $c == 0 || $m == 0) {
          //$(this).fadeOut($speed, function() { fade_headers() });
          $(this).hide();
          fade_headers();
        } else {
          //$(this).fadeIn($speed, function() { fade_headers() });
          $(this).show();
          fade_headers;
        }
        
      });
    } /* client_show_hide */
    
    function fade_headers() {

      $(".column_middle h3").each(function() {
        //alert($(this).attr("class"));
        $l = $(".column_middle div."+$(this).attr("class")+":visible").length;
        if ($l == 0) {
          $(this).hide();
        } else {
          $(this).show();
        }
      });

    } /* fade_headers */
    
    function client_stick() {
      $("#status").html( $(window).height() );
      $("#selector_expand").hide();
      $(".selector").css({
        "position":"relative" });

      $(window).scroll(function() {
        if($(window).scrollTop() > 356) {
          $(".selector").css({
            "position":"fixed",
            "top":"10px"
          });
          
          if ( $(window).height() < 750 ) {
            $("#past_current,#engagement_toggle,#client_toggle,#medium_toggle").slideUp(200);
            $("#selector_expand").fadeIn(200);
          }
          
        } else {
          $(".selector").css({
            "position":"relative",
            "top":"0px"
          });
          
            $("#past_current,#client_toggle,#medium_toggle").fadeIn(200);
            if($("#past_current").length <= 0 || $("#past_current input:eq(0)").attr("checked") == false) {
              $("#engagement_toggle").fadeIn(200);
            }
            $("#selector_expand").fadeOut(200);
          
        }
      });
      
      $(".selector").hover(
        function() { 
          $("#past_current,#client_toggle,#medium_toggle").fadeIn(200);
    
          if($("#past_current").length <= 0 || $("#past_current input:eq(0)").attr("checked") == false) {
            $("#engagement_toggle").fadeIn(200);
          }
          
        },
        function() { 
          if ( $(window).height() < 750 && $(window).scrollTop() > 356 ) {
            $("#past_current,#engagement_toggle,#client_toggle,#medium_toggle").slideUp(200);
          }
      });
    }
    
/** CASE STUDIES **/
    function case_study_init($ajax_url) {
      $(document).ready(function() {
        
        $fade_speed = 750;
        $("#header_case_study a:eq(0)").addClass("active");
        $active_slide = 0;
        case_study_fadein($ajax_url);
        
        $("#header_case_study li a").click(function(event) {
          event.preventDefault();
          $new_url = $(this).attr("href");
          case_study_fadein($new_url);
          
          $("#header_case_study li a").removeClass("active");
          $(this).addClass("active");
          $active_slide = $(this).parent().index() - 1;
          
          $(".case_study_list").slideUp(500);
          
          //alert($active_slide);
        });
        
        /* next-prev rollover */
        $(".case_study_next,.case_study_prev").fadeTo(1,0.4);
        
        $(".case_study_next,.case_study_prev").mouseenter(function() {
          $(".case_study_next,.case_study_prev").fadeTo(250,0.7);
        });
        
        $(".case_study_next,.case_study_prev").mouseleave(function() {
          $(".case_study_next,.case_study_prev").fadeTo(250,0.4);;
        });
        
        /* next */
        $("#header_case_study li.next,.case_study_next a").click(function(event) {
          event.preventDefault();
          $active_slide++;
          $t = $("#header_case_study a").length;
          if ($active_slide >= $t) {
            $active_slide = 0;
          }
          
          $next_slide = "#header_case_study a:eq(" + $active_slide + ")";
          $new_url = $($next_slide).attr("href");
          case_study_fadein($new_url);
          
          $("#header_case_study a").removeClass("active");
          $($next_slide).addClass("active");
          
          $(".case_study_list").slideUp(500);
          
          //alert($active_slide);
        });
        
        /* previous */
        $("#header_case_study li.prev,.case_study_prev a").click(function(event) {
          event.preventDefault();
          $active_slide--;
          $t = $("#header_case_study a").length - 1;
          if ($active_slide < 0) {
            $active_slide = $t;
          }
          
          $next_slide = "#header_case_study a:eq(" + $active_slide + ")";
          $new_url = $($next_slide).attr("href");
          case_study_fadein($new_url);
          
          $("#header_case_study a").removeClass("active");
          $($next_slide).addClass("active");
          
          $(".case_study_list").slideUp(500);
          //alert($active_slide);
        });
        
        /* case study list toggle */
        $list = 0;
        $(".case_study_list").hide();
        $("#case_study_toggle").click(function() {
          $(".case_study_list").slideToggle(500);
          
          if ($list == 0) {
            $("#case_study_toggle").addClass("hide");
            $("#case_study_toggle p").html("Back to case study");
            $list = 1;
          } else {
            $("#case_study_toggle").removeClass("hide");
            $("#case_study_toggle p").html("View all case studies");
            $list = 0;
          }
          
        });
        
        //$(".case_study_next,.case_study_prev").fadeTo(20000,0.4);
        
        
      });
    }
    
    function case_study_fadein($url) {
    
      $("#content_case_study .slide").fadeOut($fade_speed,function() {
      
        $("#content_case_study .loading").show();
        $("#content_case_study .slide").load($url, function() {
          
          /* preload image if there is one */
          if ($("img",this).length > 0) {
            $("img",this).load(function(response, status, xhr) {
              if (status == "error") {
                alert("error!");
              }
              
              $("#content_case_study").animate({ "height":$("#content_case_study .slide").height() }, function() {
                $("#content_case_study .loading").hide();
                $("#content_case_study .slide").fadeIn($fade_speed);
              });
              //alert ($("#content_case_study .slide").height());
            });
          } else {
            $("#content_case_study").animate({ "height":$("#content_case_study .slide").height() }, function() {
              $("#content_case_study .loading").hide();
              $("#content_case_study .slide").fadeIn($fade_speed);
            });
          }
        
        }); /* loadUrl */
        
      }); /* fadeOut */
    
    } /* case_study_fadein */


/** CLIENT AWARDS **/
    function client_awards_init() {
      $(window).load(function() {
        
        /* wrap content in a div and add popup div */
        //$body = "<div id=\"body_content\" style=\"position: relative; z-index: 1\">";
        $body = "<div id=\"body_content\">";
        $body += $("body").html();
        $body += "</div><div id=\"award_popup\"></div>";
        $("body").html($body);
        
        $("#award_popup").hide();
        
        $(".award_list a").click(function(event) {
          event.preventDefault();
          $("#award_popup").load($(this).attr("href"), function() {
            
            $current_scroll = $(window).scrollTop();
            $("#body_content").addClass("fixed_body");
            $("#body_content").css({"top":$current_scroll*-1});
            
            $("#award_popup").fadeIn(500);
            
          }); /* award_popup.load */
        }); /* view winning work click */
        
        $("#award_popup").click(function() {
          $("#award_popup").fadeOut(500, function() {
            $("#body_content").removeClass("fixed_body");
            $(window).scrollTop($current_scroll);
          });
        });
        //alert("papapyayayaoaap");
      });
    }
    
/** OUR STAFF **/
    function ncsdo_staff($total_bg,$current_bg) {
      $(document).ready(function() {

        $(".staff_next,.staff_prev").fadeTo(0,0.5);
        $prev_text = $(".staff_prev a").html();
        $next_text = $(".staff_next a").html();
        
        /* create popup tags */
        $np = $("#staff_next_prev").html();
        //$np = "<div id=\"staff_reload\"></div>"+$np;
        $np += "<div id=\"popup_prev\" class=\"popup_next_prev\">"+$prev_text+"</div>";
        $np += "<div id=\"popup_next\" class=\"popup_next_prev\">"+$next_text+"</div>";
        if ($total_bg > 0) { $np += "<div id=\"staff_reload\" title=\"reload image\"></div>"; }
        $("#staff_next_prev").html($np);
        
        $(".popup_next_prev").hide().delay(500).each(function() {
          //$top = $(this).height();
          $top = 50;
          
          $(this).css({ "top":$top });
          $r = $(this).height();
          //$(this).html($r+ " | "+$top);
        });
        
        $(".staff_next,.staff_prev").mouseenter(function() {
          $(this).fadeTo(250,0.8);
          $(".popup_next_prev:eq("+$(this).index()+")").fadeIn(250);
        });
      
        $(".staff_next,.staff_prev").mouseleave(function() {
          $(this).fadeTo(500,0.5);
          $(".popup_next_prev").fadeOut(250);
        });
        
        /* image reload */
        $("#staff_reload").click(function() {
          $current_bg++;
          if ($current_bg > $total_bg) { $current_bg = 0; }
          $newclass = "bg"+$current_bg;
          $("#container_our_staff").attr("class",$newclass);
        });
        
      });
    }
    
/** CONTACT US **/
    function contact_validate(this_form) {
      var e = 0;
      var msg = "There were errors when submitting this form.\n\n";
      

      // entered a name?
      if (this_form.name.value == "") { 
        e++;
        msg += e + ". Please enter your name.\n";
      }
      
      // entered an email address?
      if (this_form.email.value == "") {
        e++;
        msg += e + ". Please enter your Email address.\n";      
      }
      
      // entered a valid email address?
      if (this_form.email.value.indexOf("@") <= 0 
         || this_form.email.value.lastIndexOf(".") <= this_form.email.value.indexOf("@")) 
      {
        e++;
        msg += e + ". Please enter a valid Email address.\n";      
      }
      
      // entered a message?
      if (this_form.message.value == "") { 
        e++;
        msg += e + ". Please enter your message.\n";
      }
      
      msg += "\nTotal "+e+" errors.\n";
      
      if (e) {
        alert(msg);
        return false;
      } else {
        return true;
      }
    }
