/* Declare a namespace for the site */
var Site = window.Site || {};

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

$.random_number = function(min, max, exclude) {
  if (min > 0) { max -= min }
  // var num = (Math.floor(Math.random()*max+1))+min;
  var num = Math.floor(Math.random()*max+1);
  num += min;
  
  if (typeof(exclude) != "undefined" && exclude.indexOf(num) != -1) {
  
    return $.random_number(min, max, exclude);
  } else {
    return num;
  }
};

(function($) {
  $(function() {
    /* HOME PAGE GALLERIES
    ----------------------*/
    $("#home div.item a").each(function() {
      $('img', this).css({position:'absolute',top:0,left:0,zIndex:1});
      if (over_img = $(this).attr('data-over')) {
        $(this).append( $("<img>").attr({ src:over_img, alt:'', border:0}).css({display:'none', position:'absolute', top:0, left:0, zIndex:2}) );
      }
    });
    $("#home div.item a").hover(function() {
      var fade = $('img', this).last();
      if (fade.is(':animated')) {
        // ...take it's current opacity back up to 1
        fade.stop().fadeTo(1, 1);
      } else {
        fade.show();
      }
    }, function() {
      // on hovering out, fade the element out
      var fade = $('img', this).last();
      if (fade.is(':animated')) {
        fade.stop().fadeOut(1);
      } else {
        fade.hide();
      }
    });
    $("#home div.item a").click(function() {
      // figure out placement
      item_id = $(this).parent('div.item').attr('id').replace(/gallery_/, '');
      switch (item_id%6) {
        case 1:
        case 2:
          pos = 0
          break;
        case 3:
          pos = 160
          break;
        case 4:
          pos = 320
          break;
        case 5:
        case 0:
          pos = 480
          break;
      }
      css = {left:pos, top:0}
      if ($('#overlay:visible').length > 0) {
        $('#overlay').animate(css, 400, 'swing');
      } else {
        $('#overlay').css(css).fadeIn();
      }
      // load the images
      images = [$(this).attr('href'), $(this).attr('data-img2'), $(this).attr('data-img3')];
      $('#overlay').gallery_nav({images : images, text_to_append: 'click image to close'})
      .fadeIn(400)
      .click(function(e) {
        // don't close for nav clicks
        if ($(e.target).parents('nav').length == 0) {
          $(this).fadeOut(400);
          $("#grid .item").fadeTo(400, 1);
        }
      });
      $("#grid .item").fadeTo(400, 0.5);

      return false;
    });
    // when the home header gets clicked, randomly activate a portfolio item
    $("#home #main h1").click(function() {
      $('#home div.item a').mouseout();
      nums = []
      nums.push($.random_number(1, 18, nums));
      nums.push($.random_number(1, 18, nums));
      nums.push($.random_number(1, 18, nums));
      nums.push($.random_number(1, 18, nums));
      $.each(nums, function(i, num) {
        wait = i * 400;
        var num = num.toString().length == 1 ? '0' + num.toString() : num.toString() ;
        window.setTimeout(function() {
          $('#home div.item#gallery_' + num + ' a').each(function() {
            var fade = $('img', this).last();
            // if the element is currently being animated (to a fadeOut)...
            if (fade.is(':animated')) {
              // ...take it's current opacity back up to 1
              fade.stop().fadeTo(1000, 1);
            } else {
              fade.fadeIn(1000);
            }
          });
        }, wait);
        window.setTimeout(function() {
          $('#home div.item#gallery_' + num + ' a').each(function(){
            var fade = $('img', this).last();
            if (fade.is(':animated')) {
              fade.stop().fadeTo(1000, 0);
            } else {
              // fade away slowly
              fade.fadeOut(1000);
            }
          });
        }, wait + 2000)
      });
    });
    
    /* TEAM
    -------*/
    $("#grid.team div.item.overlay").mouseenter(function() {
      $(this).hide();
    });
    $("#grid.team div.item").mouseleave(function() {
      overlay = $(this).prev(".overlay").length > 0 ? $(this).prev(".overlay") : $(this).prev().prev(".overlay");
      $(overlay).show();
    });

    /* PORTFOLIO
    ------------*/
    if ($("#grid.case_studies").length > 0) {
      $('.overlay-container')
      .append($('<nav/>').addClass('case-studies mod').append($('<div/>').addClass('content')).fadeTo(1, 0.8).delay(1750).fadeOut('fast'))
      .hover(function(){$('nav.case-studies').fadeIn('fast');}, function(){$('nav.case-studies').fadeOut('fast');});
      $('#overlay')
      .cycle({
        fx: 'scrollHorz',
        pager: 'nav.case-studies .content',
        timeout: 0,
        before: function(outgoing, incoming, options) {
          outgoing_content = "#" + $(outgoing).attr('data-id') + '-content';
          incoming_content = "#" + $(incoming).attr('data-id') + '-content';
          $(outgoing_content).hide();
          $(incoming_content).fadeIn();
        }
      });
      $('.case_study_thumbs img').each(function() {
        $(this).attr('data-out', this.src);
      });
      // hover on the items so there's no flicker between them
      $('.case_study_thumbs .item').hover(
        function() {
          page_id = $(this).attr('id').replace(/^portfolio-/, '');
          $('#link-' + page_id).addClass('current');
          $('img', this).attr('src', $('img', this).attr('data-over'));
        },
        function() {
          page_id = $(this).attr('id').replace(/^portfolio-/, '');
          $('#link-' + page_id).removeClass('current');
          $('img', this).attr('src', $('img', this).attr('data-out'));
        }
      );
      $('#areanav li').hover(
        function() {
          page_id = $(this).attr('id').replace(/^link-/, '');
          img = $('#portfolio-' + page_id + ' img');
          img.attr('src', img.attr('data-over'));
        },
        function() {
          page_id = $(this).attr('id').replace(/^link-/, '');
          img = $('#portfolio-' + page_id + ' img');
          img.attr('src', img.attr('data-out'));
        }
      );
      
      /*
      // this fades out all the other images - removed for now because it's too jumpy
      $('.case_study_thumbs .item').hover(
        function() {
          $('.case_study_thumbs img').not($('img', $(this))).each(function() {
            $(this).attr('src', $(this).attr('data-over'));
          });
        },
        function() {
          $('.case_study_thumbs img').not($('img', $(this))).each(function() {
            $(this).attr('src', $(this).attr('data-out'));
          });
        }
      );
      */
    }
    
    /* CONTACT MAP
    --------------*/
    $("#contact #map_container").contact_map();
    // 37.0625,-95.677068
  });

  // this get's triggered after all the images are loaded
  $(window).bind("load", function() {
    // load all the extra home page images
    $("#home div.item").each(function() {
      $('img', $(this)).each(function() {
        if ($(this).attr('data-over') != '')
          jQuery("<img>").attr("src", $(this).attr('data-over'));
      });
      $('a', $(this)).each(function() {
        if ($(this).attr('href') != '')
          jQuery("<img>").attr("src", $(this).attr('href'));
        if ($(this).attr('data-img2') != '')
          jQuery("<img>").attr("src", $(this).attr('data-img2'));
        if ($(this).attr('data-img2') != '')
          jQuery("<img>").attr("src", $(this).attr('data-img3'));
      });
    });
  });
})(jQuery);

