var pages = ['about', 'projects', 'todo']; //spacial order of pages
var currentHash; //monitor currently loaded page

if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
    for(var i=0; i<this.length; i++){
      if(this[i]==obj){
        return i;
      }
    }
    return -1;
  }
}

//clear active state on navigation items
function clearNavActive(){
  $('#navigation>li').removeClass('active');
  return false;
}

//content loader
function loadSection(page){
  $('body').css('overflow-x','hidden');
  
  var dWidth = $(document).width();
  var cWidth = $('#content').width();
  var animationA;
  var animationB;
  var navUnderlineMargin = 98;
  
  if(pages.indexOf(currentHash.substring(1)) <= pages.indexOf(page)){ //this line throws error in IE at character 3
    animationA = {marginLeft: -((dWidth / 2) + cWidth)};
    animationB = dWidth;
  } else {
    animationA = {marginLeft: ((dWidth / 2) + cWidth)};
    animationB = -dWidth;
  }
  
  for(var i = 0;i < pages.indexOf(page);i++){
    navUnderlineMargin += 12 + $('#' + pages[i] + 'Link').width() + 13.5;
  }
  navUnderlineMargin += ($('#' + pages[i] + 'Link').width()-50)/2;
  
  $('#content').animate(animationA, 200, function(){
    $('#content').css('margin-left', animationB + 'px');
    
    $('#navUnderline').animate({'margin-left': navUnderlineMargin},200);
    
    $.ajax({
      url:'./' + page + '.html',
      success: function(data){
        $('#content').html(data);        
        if(page == 'projects'){
          indexProjects();
        } else if (page == 'about'){
          //load svg portrait on webkit, opera, gecko
          if($.browser.webkit || $.browser.opera){ 
            $('#portraitPhoto').attr('src', './img/evan.svg');
          } else if ($.browser.mozilla){
            $('#portraitPhoto').replaceWith('<object alt="Evan Long" id="portraitPhoto" data="./img/evan.svg" type="image/svg+xml" />');
          }
        }
        $('#content').animate({marginLeft: 100}, 200, function(){
          $('body').css('overflow-x','auto');      
        });
      }
    });
    
  });
}

//page switcher
function switchTo(page){
  if(!$('#' + page + 'Link').hasClass('active')){
    clearNavActive();
    $('#' + page + 'Link').addClass('active');
    loadSection(page);
    if(window.location.hash.split(',')[0].substring(1) != page){
      window.location.hash = page;
    }
    currentHash = window.location.hash;
  }
  
}

//check for hash change
function checkHash(){
  if(window.location.hash.split(',')[0] != currentHash){
    switchTo(window.location.hash.split(',')[0].substring(1));
    currentHash = window.location.hash.split(',')[0];
  }
}

//generate table of contents for projects page
function indexProjects(){
  $('#projectsList').before($('<ul id="tableOfContents"></ul>'));
  
  $('#projectsList li a:first-child').each(function(index, element){
    var title = $(element).text();
    var newLink = $('<li><a href="">' + title + '</a></li>');
    $('#tableOfContents').append(newLink);
  })
  
  $('#tableOfContents li').each(function(index, element){
    var target = $('#projectsList li')[index].offsetTop;
    $(element).click(function(event){
      event.preventDefault();
      $('html, body').animate({scrollTop: target},1000);
      if(window.location.hash.split(',')[1] != undefined){
        window.location.hash = window.location.hash.substring(0,window.location.hash.length - window.location.hash.split(',')[1].length - 1);
      }
      window.location.hash += ',' + index;
      return false;
    });
  })
  
  if(window.location.hash.split(',')[1] != undefined && parseInt(window.location.hash.split(',')[1])){
    var ind = $('#tableOfContents li')[window.location.hash.split(',')[1]];
    $(ind).click();
  }
  
}

//MAIN
jQuery(function(){
  
  hashCheck = setInterval('checkHash()', 50);
  
  //load svg logo on webkit, opera, gecko
  if($.browser.webkit || $.browser.opera){ 
    $('#logoImg').attr('src', './img/logo.svg');
  } else if ($.browser.mozilla){
    $('#logoImg').replaceWith('<object alt="Evan Long dot info" id="logoImg" data="./img/logo.svg" type="image/svg+xml" />');
  }
  
  //attach navigation actions
  $('#aboutLink>a').click(function(e){
    e.preventDefault();
    switchTo('about');
    return false;
  })

  $('#projectsLink>a').click(function(e){
    e.preventDefault();
    switchTo('projects');
    return false;
  })
  
  $('#todoLink>a').click(function(e){
    e.preventDefault();
    switchTo('todo');
    return false;
  })

  //fix remote deep links, load about page by default
  if(window.location.hash != ''){
    currentHash = window.location.hash;
    switchTo(window.location.hash.substring(1).split(',')[0]);
  } else {
    currentHash = '#about';
    switchTo('about');
  }

  //livestatic logo
  $('#livestatic').animate({"opacity":"0"}, 2000).mouseenter(function(){
    $(this).animate({"opacity":"100"},200);
  }).mouseleave(function(){
    $(this).animate({"opacity":"0"},200);
  });
  
});
