$(document).ready(function() { kickstart(); });

function kickstart() {
  $(document).keyup(function(event) { key_handle(event.which); });

  if (typeof Touch == "object") {
    $('#move-left, #move-left-crossover').bind('touchstart', function(event) {scroll_buffer(-1); });
    $('#move-right').bind('touchstart', function(event) {scroll_buffer(1); });
    // $(window).bind('scroll', function(event) {fix_fixed(); });
    is_iphone = true;
  }
  else {
    $('#move-left, #move-left-crossover').click(function(event) {scroll_buffer(-1); });
    $('#move-right').click(function(event) {scroll_buffer(1); });
    $('#move-right').hover(
      function() {
        $(this).addClass('hover');
      },
      function() {
        $(this).removeClass('hover');
      }
    );
    
    $('#move-left, #move-left-crossover').hover(
      function() {
        $('#move-left, #move-left-crossover').addClass('hover');
      },
      function() {
        $('#move-left, #move-left-crossover').removeClass('hover');
      }
    );
    
  }  
  $('.article img.zoomable').click(function(event) {zoom_image(this)});
  $('#home-button, #home-floater, #iphone-home').click(function(event) {go_to(''); }); 
  $('#switcher-button, #switcher-floater, #iphone-switcher').click(function(event) {switch_mode();});

  relink_titles();
  
  $('#homepage .image-switcher').each(
    function() {
      $(this).attr('src', '/img/content/' + current_mode + '/' + $(this).attr('title'));
    }
  );
    
  $.History.bind(function(state) {hash_change(state)});
}

var is_iphone = false;

var current_col = 0;
var cols = 30;

var COL_WIDTH = 300;
var COL_GAP = 50;
 
var SCROLL_SPEED = 400;
var FADE_SPEED = 400;
 
var home_visible = true;
var current_article = null;
 
var cookie_set = false;

var allowed_articles;
var current_mode = 'risky';

var previous_article = null;

var buffer = new Array();

var url_fix = 'http://textcamp.nextwave.org.au/'

function add_article(id, cols) {
  // allowed_articles.push(id);
}

function load_article(id) {
  if (id == current_article) 
    return;  
  
  hide_current(id);
  home_visible = false;
  current_article = id;
  setTimeout(function() {show_article(id)}, FADE_SPEED);
}

function hide_current() {
  if (home_visible)
    $('#homepage').animate({opacity: 0}, FADE_SPEED, 'swing', function() { $('#homepage').hide(); });
  
  if (current_article)
    $('#article-loader').animate({opacity: 0}, FADE_SPEED, 'swing', function() { $('#article-loader').hide(); });
}

function show_article(id) {

  _timeout = (current_col == 0) ? 10 : 500;  
  scroll_zero();

  setTimeout(function() {
    $('#article-loader').empty();
    $('#article-' + id).clone().appendTo('#article-loader');
    $('#article-loader').css({opacity: 0, display: 'block'}).animate({opacity: 1}, FADE_SPEED);
    
    relink_images();
    
  }, _timeout);
  
}

function go_home() {

  previous_article = null;
  if (home_visible) {
    scroll_zero();
    return;
  }
    
  _timeout = (current_col == 0) ? 10 : 500;  
  scroll_zero();

  $('#homepage .next-article').text('Next article');
    
  setTimeout(function() {
    home_visible = true;
    current_article = null;
    $('#article-loader').animate({opacity: 0}, FADE_SPEED, 'swing', function() {
      $('#article-loader').hide();
      $('#homepage').css({opacity: 0, display: 'block'}).animate({opacity: 1}, FADE_SPEED);
    });

  }, _timeout);
    
}

var left_timer, right_timer;

function key_handle(key) {
  if (key == 37) {
    scroll_buffer(-1);

  }
  if (key == 39) {
    scroll_buffer(1);
  }
}

function scroll_buffer(dir) {
  buffer.push(dir);

  if (buffer.length < 2) {
    scroll(dir);
  }
  else {
  }
}

function scroll(dir) {

  // at the end
  if (dir == 1 && current_col >= cols - 1) finish_scroll();
  // at the start
  if (dir == -1 && current_col < 1) {
    if (!home_visible && previous_article) {
      buffer.length = 0;
      go_to(previous_article);
      previous_article = null;
    }
    else {
      finish_scroll();
    }
    return;
  };

  if (dir == '-1') {
    $('#move-left, #move-left-crossover').addClass('hover');
    clearTimeout(left_timer);
    left_timer = setTimeout(function() {$('#move-left, #move-left-crossover').removeClass('hover')}, 120);
  }
  else {
    $('#move-right').addClass('hover');
    clearTimeout(right_timer);
    right_timer = setTimeout(function() {$('#move-right').removeClass('hover')}, 120);
  }

  current_col += dir;
  new_left = current_col * (COL_WIDTH + COL_GAP) * -1;

  last_col = (!home_visible) ? $('#content #article-loader .lastcol') : $('#content #homepage .lastcol');

  if (last_col.offset().left < 400 && dir == 1) {
    last_col.find('.next-article').text('Loading next article...');
    // $('#content .lastcol .next-article').text('Loading next article...');
    previous_article = current_article;
    buffer.length = 0;
    // setTimeout(function() {$('#content .lastcol .advance').click();}, 400);
    setTimeout(function() {last_col.find('.advance').click();}, 400);

    return;
  }

  $('#content').animate({ left: new_left + 'px' }, 400);
  $('#page').animate({ backgroundPosition: new_left + 'px 0' }, 400);
  
  // in first col, scrolling to second
  if (current_col == 1 && dir == 1) {
    // 223
    $('#home-floater').animate({left: '710px'}, 400);
    $('#switcher-floater').animate({left: '1060px'}, 400);
    $('#move-left-crossover').animate({top: '-299px'}, 400);
    $('#move-left').animate({top: '258px'}, 400);
    $('#move-left .arrow').animate({marginTop: '51px'}, 400);
    $('#move-left-crossover .arrow').animate({paddingTop: '608px'}, 400);
  }
  
  // in second col, scrolling to third
  if (current_col == 2 && dir == 1) {
    $('#home-floater').animate({left: '360px'}, 400);
    $('#switcher-floater').animate({left: '710px'}, 400);
    $('#move-left-crossover').animate({top: '-557px'}, 400);
    $('#move-left').animate({top: '0'}, 400);
    $('#move-left .arrow').animate({marginTop: '309px'}, 400);
  }
  
  // in third col, scrolling to fourth 
  if (current_col == 3 && dir == 1) {
    $('#home-floater').animate({left: '10px'}, 400);
    $('#switcher-floater').animate({left: '360px'}, 400);
  }
  
  // in fourth col, scrolling to third
  if (current_col == 2 && dir == -1) {
    $('#home-floater').animate({left: '360px'}, 400);
    $('#switcher-floater').animate({left: '710px'}, 400);
  }
  
  // in third col, scrolling to second
  if (current_col == 1 && dir == -1) {
    $('#home-floater').animate({left: '710px'}, 400);
    $('#switcher-floater').animate({left: '1060px'}, 400);
    $('#move-left-crossover').animate({top: '-299px'}, 400);
    $('#move-left').animate({top: '258px'}, 400);
    $('#move-left .arrow').animate({marginTop: '51px'}, 400);
  }
  
  // in second col, scrolling to first
  if (current_col == 0 && dir == -1) {
    $('#home-floater').animate({left: '1060px'}, 400);
    $('#switcher-floater').animate({left: '1410px'}, 400);
    $('#move-left-crossover').animate({top: '0'}, 400);
    $('#move-left').animate({top: '557px'}, 400);
    $('#move-left .arrow').animate({marginTop: '-248px'}, 400);
    $('#move-left-crossover .arrow').animate({paddingTop: '309px'}, 400);
  }
    
  setTimeout(function() { finish_scroll(); }, 420);
  
}

function finish_scroll() {
  buffer.shift();
  if (buffer.length)
    scroll(buffer[0]);
}

function scroll_zero() {
  if (current_col == 0) 
    return;
  
  $('#content').animate({ left: '0px' }, 400);
  $('#page').animate({ backgroundPosition: '0 0' }, 400);

  $('#move-left-crossover').animate({top: '0'}, 400);
  $('#move-left').animate({top: '557px'}, 400);
  $('#move-left .arrow').animate({marginTop: '-248px'}, 400);
  $('#move-left-crossover .arrow').animate({paddingTop: '309px'}, 400);
  
  $('#home-floater').animate({left: '1060px'}, 400);
  $('#switcher-floater').animate({left: '1410px'}, 400);

  current_col = 0;
}

function jump(col) {
  current_col = col;
  if (is_iphone)
    current_col -= 1;
  new_left = current_col * (COL_WIDTH + COL_GAP) * -1;
  $('html, body').animate({scrollTop:0}, 'slow');
  $('#content').animate({ left: new_left + 'px' }, 400);
  $('#page').animate({ backgroundPosition: new_left + 'px 0' }, 400);
  // $('#home-floater').animate({left: '360px'}, 400);
  $('#move-left-crossover').animate({top: '-557px'}, 400);
  $('#move-left').animate({top: '0'}, 400);
  $('#move-left .arrow').animate({marginTop: '309px'}, 400);
  $('#home-floater').animate({left: '10px'}, 400);
  $('#switcher-floater').animate({left: '460px'}, 400);

}

function reset_scroll() {
  current_col = 0;
  // $('#content')
}

function go_to(hash) {

  buffer.length = 0;
  
  // compensate for going home when already home
  if ((window.location.hash == '' && hash == '') || (window.location.hash == '#' && hash == ''))
    go_home();
  
  $('html, body').animate({scrollTop:0}, 'slow');
  window.location = url_fix + '#' + hash;
}

function fix_fixed() {
  _top = $(document).scrollTop();
  // 200? yeah, it sucks. but so does iphones mum.
  _bottom = (_top * -1) - 200;
  $('.move-button').css({top: _top + 'px', bottom: _bottom + 'px'});
}

function relink_images() {
  $('#article-loader .image-switcher').each(
    function() {
      $(this).attr('src', '/img/content/' + current_mode + '/' + $(this).attr('title'));
    }
  );
}

function relink_titles() {
  // $('.image-switcher').each(
  //   function() {
  //     $(this).attr('src', '/img/content/' + current_mode + '/' + $(this).attr('title'));
  //   }
  // );
  
  $('.title-switcher').each(
    function() {
      $(this).attr('src', '/img/titles/' + current_mode + '/' + $(this).attr('title'));
    }
  );
}

function switch_mode() {
  
  _message = 'Are you sure you wish to disable Safe Mode? It\'s a dangerous internet out there.';
  
  if (!cookie_set) 
    if (current_mode == 'safe' && !confirm(_message))
      return;
  
  if (current_mode == 'safe' & !cookie_set)
    cookie_set = true;
  
  $('#content, #controls, #home-floater, #switcher-floater').animate({opacity: 0}, 400);

  if (current_mode == 'safe') {
    current_mode = 'risky';
  }
  else {
    current_mode = 'safe';
  }

  setTimeout(function() {
    $('body').removeClass().addClass(current_mode);
    relink_titles();
    relink_images();
    $('#homepage .image-switcher').each(
      function() {
        $(this).attr('src', '/img/content/' + current_mode + '/' + $(this).attr('title'));
      }
    );
  }, 700);
  
  setTimeout(function() {
    $('#content, #controls, #home-floater, #switcher-floater').animate({opacity: 1}, 400);
  }, 1000);
  
}

function hash_change(state) {
  // alert('state change');
  // alert(state);
  if (state == '' || state == 'home') {
    go_home();
  }
  else if (state in allowed_articles) {
    load_article(state);
  }

}

