var Icelab = {};
Icelab.general = function () {
  
  // Called on instantiation
  function init() {
    build_logo_span();
    $("a[href^='#']").not(".no-scroll").anchorAnimate();
  }

  // Conventional name for the object that is returned by the constructor,
  // allowing access to the public methods and properties by external
  // code.
  var API = {
    constructor: Icelab.general
  };

  // A method that will be exposed via the API.
  function drawIcelabMap() {
    var position = new google.maps.LatLng(-35.282441,149.125022);
    var options = {
      zoom: 14,
      center: position,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("location-map"), options);
    var content = '<div id="map-marker-content"><strong>Icelab Secret Headquarters</strong>Unit 3, 1 Gordon St, City</div>';

    var infowindow = new google.maps.InfoWindow({
        content: content
    });
    var marker = new google.maps.Marker({
        position: position, 
        map:      map,
        title:    "Icelab"
    });
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map, marker);
    });
    
  }

  /* Replaces "Icelab" text in header with more appropriate link text from title=""
   */ 
  function build_logo_span() {
    var header = $("#header h1 a");
    var title = header.attr("title");
    var replacement = "<span>"+title+"</span>";
    if ($("body").attr("id") == "home") replacement = '<span class="heart">&#10084;</span>';
    header.html(replacement);
  }
  
  // Typically, public methods are attached to the API just before
  // returning, for easier scannability of the API.
  API.drawIcelabMap = drawIcelabMap;
  
  init();
  return API;
};

Icelab.home = function()
{
  function init() {
    make_fitted();
    create_project_cycle();
  };
  
  /* Apply fitted plugin to things <li>s
   */
  function make_fitted() {
    $("#intro-services li").fitted();
    $("#promo").fitted();
  };
  
  function create_project_cycle() {
    $("#projects").scrollable({
      size:       1,
      clickable:  false,
      keyboard:   false,
      easing:    "flick",
      speed: 850
    }).circular();
    
    $("#project-cycle-nav a").click(function(e) { e.preventDefault(); });
  }
  
  init();
};

Icelab.work = function()
{
  function init() {
    
    // Work - Index
    if($("#recent-work").length != 0) {
      activate_tabs();
      $("#promo").fitted();
    } else {
      create_image_cycle();
    }
    set_project_height();
  };
  
  /* Activate project switcher tabs 
   */
  function activate_tabs() {
    var selected = window.location.href;
    if (!selected) { select = "#recent-work"; }
    
    $("#content section").not($("selected")).hide();
    
    $("#project-selector a").click(function(e) { e.preventDefault(); });
    $("#project-selector").tabs("#content section", { effect: "default" }).history();
  };
  
  function create_image_cycle() {
    $("#project-media").scrollable({
      size:       1,
      clickable:  true,
      keyboard:   false,
      easing:    "flick",
      speed: 920
    })
    .circular()
    .navigator({
      navi: '.navi'
    });
  }
  
  
  function set_project_height() {
    var heights = [];
    var projects = $('#recent-work .project:not(#primary)');
    projects.each(function()
    {
      heights.push($(this).height());
    });
    heights.sort().reverse();
    var tallest = (heights[0] > 380) ? heights[0] : 380;
    projects.height(tallest);
  }
  init();
};


Icelab.articles = function()
{
  function init() {
    // Articles - Single
    if($("#article-single").length != 0) {
      prettyPrint();
    }
  };
  init();
};

Icelab.about = function()
{
  function init() {
    Icelab.general().drawIcelabMap();
  };
  init();
};

Icelab.contact = function()
{
  function init() {
    Icelab.general().drawIcelabMap();
  };
  init();
};


/** Custom easing equation */
$.easing.flick = function (x, t, b, c, d) { 
    var s = 1.70158;  
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
}

/** scrollTo for #anchors */
jQuery.fn.anchorAnimate = function(settings) {

   settings = jQuery.extend({
    speed : 800
  }, settings);  
  
  return this.each(function(){
    var caller = this;
    $(caller).click(function (event) {
      event.preventDefault();
      var locationHref = window.location.href;
      var elementClick = $(caller).attr("href");
      if (elementClick != "#") {
        var destination = $(elementClick).offset().top;
        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
          window.location.hash = elementClick
        });
        return false;
      }
    });
  });
}


// Inititialise
$(function() { 
  // Page object (based on first URL segment passed from template)
  Icelab.general();
  if (Icelab[Icelab.SECTION]) Icelab[Icelab.SECTION]();
});
