
function flashLink(href) {
  $.address.value(href);
}

var scrollTarget = false;
var scrolling = false;
var offsets = new Array();
var prevId = false;


$(window).load( function() {
  // Setup page offsets
  $('#container .page').each(function() {
    offsets.push({
      id: $(this).attr('id'),
      offset: $(this).offset().top,
      height: $(this).height()
    });
  });

});

$(document).ready(function() {
  if (!$.address) {
    return;
  }

  // Setup page offsets
  $('#container .page').each(function() {
    offsets.push({
      id: $(this).attr('id'),
      offset: $(this).offset().top,
      height: $(this).height()
    });
  });
  

  // jQuery Address callback
  $.address.change(urlChanged);
  
  // Fix footer for IE6
  if ($.browser.msie && parseInt($.browser.version) == 6) {
    new IE6Footer();
  }


  // Update navigation on scroll & resize
  $(window).scroll(positionChanged);
  $(window).resize(positionChanged);

  // Setup polaroids
  new Polaroids();

  // Setup fancybox
  $('#_caset .thumbnail a').fancybox({
    overlayOpacity: 1,
    overlayColor: '#fff',
    // centerOnScroll: true,
    titleFormat: caseTitleFormat,
    showCloseButton: false,
    showNavArrows: false,
    width: 838,
    height: 522,
    onComplete: function(img, i) {
      $('#fancy').css('margin-left', Math.round((838 - $('#fancy').width())/2)+'px' );
      var link = $($(img).get(i)).next('input[type=hidden]').val();
      if (link) {
        $('#fancybox-inner').wrapInner($('<a href="' + link + '" target="_blank"></a>'));
      }
    }
  });
});

function urlChanged(event) {
  var path = event.path.substring(1);
  
  // Forward non-existing pages to frontpage
  if (!$('#_' + path).length) {
    var target = $('#_family').offset().top;

  // Existing pages
  } else {
    var target = $('#_' + path).offset().top;

    // Special case for news
    if (path == 'paivakirja') {
      var id = event.parameters.id ? event.parameters.id : 0;
    
    // Special case for cases
    } else if (path == 'caset') {
      if (!$('#case-' + event.parameters.id).length) {
        target = $('#_caset').offset().top;
      } else {
        target = $('#case-' + event.parameters.id).offset().top;
        target -= 20;
      }
    }
  }

  scrollTarget = target;
  scrolling = true;

  $('html,body').animate({ scrollTop: target }, 1000, function() {
    scrolling = false;
  });
}

function positionChanged() {
  var offset = $(window).scrollTop();
  var id = '';
  
  // Find current page
  $.each(offsets, function(i, u) {
    if (u.offset <= offset) {
      id = u.id;
      return;
    }
  });
  if (id == '_caset') {
    id = '_tyomme';
  }
  
  if (id != prevId) {
    // Update navigation images
//    var navi = $('#navi img');
    $('UL#navi LI').each(function() {
      if (id && $(this).attr('data-navi') == id.substring(1)) {
        $(this).find("A").css("color", "#62bb46");
      } else {
        $(this).find("A").css("color", "#bfbfbf");
      }
    });
  }

  // Tracking
  if (scrollTarget == offset || (!scrolling && id != prevId)) {
    if (id == "_family" || !id) icamo.track(6275);
    if (id == "_me") icamo.track(6274);
    if (id == "_tyomme") icamo.track(6276);
    if (id == "_caset") icamo.track(6277);
    if (id == "_paivakirja") icamo.track(6278);
    if (id == "_yhteydet") icamo.track(6279);
  }

  prevId = id;
}

function caseTitleFormat(title, currentArray, currentIndex, currentOpts) {
  var html = '<div id="fancy" style="float: left;">';
  html += '<div class="cases" onclick="$.fancybox.close(); window.location.hash = \'tyomme\';"></div>';
  if (currentIndex > 0) {
    html += '<div class="prev" onclick="$.fancybox.prev();"></div>';
  }
  html += '<div class="title">' + title + '</div>';
  if (currentIndex < currentArray.length - 1) {
    html += '<div class="next" onclick="$.fancybox.next();"></div>';
  }
  html += '<div class="close" onclick="$.fancybox.close();"></div>';
  html += '</div>';

  return html;
}

/**
 * Class for handling polaroids on people page
 */
function Polaroids() {
  this.current = false;
  this.list = $('#_me .persons .person');
  this.events();
}

Polaroids.prototype.open = function(index) {
  var bg = $(this.list.get(index)).children('.bg');
  var photo = $(this.list.get(index)).children('.photo');
  var info = $(this.list.get(index)).children('.info');
  var full = $(this.list.get(index)).children('.full');

  this.list.each(function(i) {
    if (i != index) {
      $(this).css('z-index', 1);
    } else {
      $(this).css('z-index', 2);
    }
  });

  info.fadeOut(250, function() {
    bg.animate({
      width: 300,
      height: 400,
      left: -50,
      top: -80
    }, 250, function() { full.fadeIn(250)});
    photo.animate({
      width: 260,
      left: -30,
      top: -60
    }, 250);
  });

  return false;
}

Polaroids.prototype.close = function(index) {
  var bg = $(this.list.get(index)).children('.bg');
  var photo = $(this.list.get(index)).children('.photo');
  var info = $(this.list.get(index)).children('.info');
  var full = $(this.list.get(index)).children('.full');

  full.fadeOut(250, function() {
    bg.animate({
      width: 200,
      height: 240,
      left: 0,
      top: 0
    }, 250, function() { info.fadeIn(250); });
    photo.animate({
      width: 170,
      left: 15,
      top: 15
    }, 250);
  });

  return false;
}

Polaroids.prototype.events = function() {
  var self = this;
  this.list.click(function() {
    var index = self.list.index(this);

    // Only open
    if (self.current === false) {
      self.open(index);
      self.current = index;
    // Close current
    } else if (self.current == index) {
      self.close(self.current);
      self.current = false;
    // Open new & close current
    } else {
      self.open(index);
      self.close(self.current);
      self.current = index;
    }
  });
}


/**
 * Footer fix for IE6
 */
function IE6Footer() {
  var self = this;
  $(window).scroll(function() { self.update() });
  $(window).resize(function() { self.update() });
  $('#footer').css('position', 'absolute');
  this.update(false);
}

IE6Footer.prototype.update = function() {
  var offset = $(window).scrollTop();
  var wHeight = $(window).height();
  var fHeight = $('#footer').height();
  $('#footer').css('top', offset + wHeight - fHeight);
}

