
var ninetyfour = {

  initted: false,

  init: function(){

    if(ninetyfour.initted) return;
    ninetyfour.initted = true;

    ninetyfour.showRandomChild("#home .staff_portraits");
    ninetyfour.insertReplaceElements( $(".replace") );
    ninetyfour.setContactFormProperties();
    ninetyfour.initPersonalMenuGroups();

    DD_roundies.addRule('.ui-corner-all', 5);

    //are we in IE (do we have to PNG fix)
    if($.browser.msie){
      //fix normal, static background pngs in <IE7
      if($.browser.version < 7) DD_belatedPNG.fix(".png, #main-logo");
      //fix no-repeat png images in ALL IE's that will need to animate opacity (uses AlphaLoader instead of belated PNGs VML)
      //NOTE - a hacked version of jquery.pngFix is used, the original is stupid in its overwriting of the replaced elements position/display
      $(".fadeable-png").pngFix({ maxVersion: 8 });
    }
    
    //do cross fades for non IE browsers
    ninetyfour.initCrossFades(".cross-fade");
    //do rollovers for team
    ninetyfour.initRollovers("#team li", { itemsToAnimate: ".bullet" } );
    //do rollovers for home
    ninetyfour.initRollovers("#home .columns .column");

    
    //setup typekit fonts
    try {
      Typekit.load({
        active: function() {
        //reinit scrollpane after fonts load
        $('.scroll-pane').jScrollPane({ scrollbarWidth: 12});
      }});
    } catch(e) { }

  },

  insertReplaceElements: function(targets){

    targets.each(function(){

      var item = $(this);
      if(item.data("ninetyfour.replacer")) return;

      var replacer = $('<div class="replacer"></div>');
      
      replacer.css({ 
        backgroundImage: item.css('backgroundImage'), 
        backgroundPosition:item.css('backgroundPosition'), 
        backgroundRepeat: item.css('backgroundRepeat') 
      });
      
      if( item.css('backgroundImage').indexOf(".png") > -1 ){
        replacer.addClass("png");
      }

      item.css({background: 'none'});
      item.append(replacer);
      
      item.data("ninetyfour.replacer", replacer);
      
    });

  },

  setContactFormProperties: function(){

    //set default class and active effects for inputs
    var element = $('input, textarea, select')
    element.addClass('ui-state-default ui-corner-all-small')
    element.focus(function(){
      $(this).addClass('ui-state-hover');
    });
    element.blur(function(){
      $(this).removeClass('ui-state-hover');
    });


    $("#contact_form").validate({
      submitHandler: function(form) {
      
        //_gaq.push(['_trackPageview', 'contact-form-submitted']);
        
        $.post("mailer.php",
          $(form).serialize(),
          function(return_data){
            if(return_data == 1)
            {
              $('#form_title').html($('#mail_success_header').html());
              $('#contact_form').html($('#mail_success_body').html());
              $('#contact_form_box').stop().animate({height: '180px'}, 1000);
              //_gaq.push(['_trackPageview', 'contact-form-sent']);
            }
            else
            {
              $('#form_title').html($('#mail_fail_header').html());
              $('#contact_form_message_container').html($('#mail_fail_body').html())
              $('#contact_form_message_container').stop().animate({height: '15px', opacity: 1}, 1000);
              $('#contact_form_box').stop().animate({height: '470px'}, 1000);
            }
          }
        );
        return false;
      }
    });

  },

  initPersonalMenuGroups: function(){
   $(".menu_group").mouseenter(function(){
      $(this).find(".logo").css({backgroundPosition:"bottom left"});
   });

   $(".menu_group").mouseleave(function(){
      $(this).find(".logo").css({backgroundPosition:"top left"});
   });
  },

  initRollovers: function(targets, settings){

    var itemsToAnimate = ".rollover";
    if(settings){
      if(settings.itemsToAnimate) itemsToAnimate += ", "+settings.itemsToAnimate;
    }
    
    $(targets).each(function(){

      var target = $(this);
      var hit = target;
      var hitSearch = target.find(".hit");
      if(hitSearch.length > 0) {
        hit = hitSearch
      } else {
        hitSearch = target.next(".hit");
        if(hitSearch.length > 0) hit = hitSearch;
      }
      target = target.add(hit);
      
      target.css({zoom: 1});
      target.find(itemsToAnimate).css({opacity:0, zoom: 1});
      
      hit.mouseenter(function(){
        target.find(itemsToAnimate).stop().animate({opacity:1});
      });

      hit.mouseleave(function(){
        target.find(itemsToAnimate).stop().animate({opacity:0});
      });
        
    });
    
  },

  showRandomChild: function( targets ){
    $(targets).each(function(){
      var children = $(this).children();
      if(children.length < 2) return;
      var rand = $(children[Math.floor(Math.random()*children.length)]);
      children = children.not(rand);
      children.remove();
    });
  },

  initCrossFades: function( targets ){
    
    var duration = 5000;
    var fadeSpeed = 1000;
    
    $(targets).each(function(){
      
      var children = $(this).children();
      if(children.length < 2) return;
      
      var currentChildIndex = Math.floor(Math.random()*children.length);
      var currentChild = $(children[currentChildIndex]);
      children.not(currentChild).css({opacity: 0, display: "none"});
      
      setInterval(
        function(){
          
          currentChildIndex++;
          if(currentChildIndex > children.length - 1) currentChildIndex = 0;
          currentChild.stop().animate({opacity: 0}, fadeSpeed, function(){  
            $(this).css({display:"none"});
          });
          
          currentChild = $(children[currentChildIndex]);
          currentChild.css({display:"block", opacity: 0 });
          currentChild.find(".bullet").css({filter:"alpha(opacity=100)"});
          currentChild.stop().animate({opacity: 1}, fadeSpeed, function(){
            $(this).css({ filter: "none" });
            $(this).find(".bullet").css({filter:"none"});
          });
          
        }, 
        duration
      );
      
    });
    
  }
}

$(document).ready(function(){
  ninetyfour.init();
});


