var pause = 0;  //GLOBAL
var stop_animate = 0; //GLOBAL
var window_height; //GLOBAL
var window_width; //GLOBAL

jQuery.fn.log = function (msg) {
    //console.log("%s: %o", msg, this);
    return this;
  };
  
function auto_cycle(){
  jQuery().log("AutoCycle");
  sim_item_hover(1);
  jQuery().log("oneTime set for 2");
  jQuery(document).oneTime("6s", "auto_cycle", function() {
    jQuery().log("Run item 2");
    sim_item_hover(2);
    jQuery().log("oneTime set for 3");
    jQuery(document).oneTime("6s", "auto_cycle", function() {
      jQuery().log("Run item 3");
      sim_item_hover(3);
      jQuery(document).oneTime("6s", "auto_cycle", function() {
        sim_item_hover(4);
        jQuery(document).oneTime("6s", "auto_cycle", function() {
          sim_item_hover(5);
          jQuery(document).oneTime("6s", "auto_cycle", function() {
            sim_item_hover(6);
            jQuery(document).oneTime("6s", "auto_cycle", function() {
              sim_item_hover(7);
              jQuery(document).oneTime("6s", "auto_cycle", function() {
                hide_all();
                init();
              });
            });
          });
        });
      });
    });
  });

}

function init(){
jQuery().log("init()");
  
  

  jQuery(document).oneTime("8s", "auto_cycle", function() {
    auto_cycle();
  });
  stop_animate = 0;
  bounce_arrows();

  
}

function first_init(){
jQuery().log("init()");
  
  jQuery("div#top_bar div.navigation").hide();
  jQuery("div#top_bar div.sm").hide();
  jQuery("img#logo").hide();
  jQuery("ul#articles").hide();
  jQuery("div#social_wrapper").hide();

  jQuery(document).oneTime("3s", "intro", function(){
    
    jQuery("div#splash").fadeOut(250);
    jQuery("img#logo").fadeIn(800);
    jQuery("div#top_bar .navigation").fadeIn(800);
    jQuery("div#top_bar .sm").fadeIn(800);
    jQuery("div#social_wrapper").fadeIn(800);
    jQuery("ul#articles").fadeIn(800, function(){
      jQuery(document).oneTime("8s", "auto_cycle", function() {
        auto_cycle();
      });
      stop_animate = 0;
      bounce_arrows();
    });
  });
  
  
}

function skip_splash_init(){
jQuery().log("skip_splash_init()");
  

  
    
    jQuery("div#splash").hide();
    jQuery("img#logo").show();
    jQuery("div#top_bar .navigation").show();
    jQuery("div#top_bar .sm").show();
    jQuery("div#social_wrapper").show();
    jQuery("ul#articles").fadeIn(800, function(){
      jQuery(document).oneTime("8s", "auto_cycle", function() {
        auto_cycle();
      });
      stop_animate = 0;
      bounce_arrows();
    });
  
  
  
}



function bounce_arrows(){
//jQuery().log("bounce_arrows()");   
//jQuery().log("Stop_Animate = " + stop_animate);
      if(stop_animate == 1){
        return;          
      }
      
      var amount = Math.floor( Math.random() * 50 + 85 );
      var arrow = Math.floor( Math.random() * 7 + 1 );
         switch(arrow){
          case 1:
            jQuery("img#arrow_1").animate({marginTop: "-" + amount + "px"}, function(){
              pause = 0;
              bounce_arrows();
            }).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
          break;
          case 2:
            jQuery("img#arrow_2").animate({marginTop: "-" + amount + "px"}, function(){
              pause = 0;
              bounce_arrows();
            }).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
          break;
          case 3:
            jQuery("img#arrow_3").animate({marginTop: "-" + amount + "px"}, function(){
              pause = 0;
              bounce_arrows();
            }).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
          break;
          case 4:
            jQuery("img#arrow_4").animate({marginTop: "-" + amount + "px"}, function(){
              pause = 0;
              bounce_arrows();
            }).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
          break;
          case 5:
            jQuery("img#arrow_5").animate({marginTop: "-" + amount + "px"}, function(){
              pause = 0;
              bounce_arrows();
            }).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
          break;
          case 6:
            jQuery("img#arrow_6").animate({marginTop: "-" + amount + "px"}, function(){
              pause = 0;
              bounce_arrows();
            }).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
          break;
          case 7:
            jQuery("img#arrow_7").animate({marginTop: "-" + amount + "px"}, function(){
              pause = 0;
              bounce_arrows();
            }).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
          break;
         }
   }

function item_hover(article_id){
    jQuery(document).stopTime("hide_articles").stopTime("auto_cycle");
    jQuery(document).log("Stop auto_cycle - item_hover");
    hide_all(article_id);
    hide_all(article_id);
    //alert("-" + (window_height * .4 ) + "px");
    stop_animate = 1;
    jQuery("img#arrow_" + article_id).stop(true).animate({marginTop: "-140px"}, function(){
      jQuery("div#article_" + article_id).fadeIn(1000);
    });
  }
  
function item_unhover(article_id){
  //alert("unhover");
  jQuery(document).oneTime("4s", "hide_articles", function() {
    jQuery(document).log("item_unhover_timer");
    stop_animate = 0;
    init();
    hide_all(0);
  });
}

function article_hover(article_id){
  jQuery(document).stopTime("hide_articles").stopTime("auto_cycle");
  jQuery(document).log("Stop auto_cycle - article_hover");
}

function article_unhover(article_id){
  jQuery(document).oneTime("4s", "hide_articles", function() {
    stop_animate = 0;
    init();
    hide_all(0);
  });
}
  
function hide_all(except){
  if(0 == arguments.length){
    except = 0;
  }
  //alert("except = " + except);
  var i = 1;
  while( i<=7 ){
    if( i != except){
      hide_article(i);
    }
    i += 1;
  }
  return;
}

function hide_article(article_id){
  jQuery("div#article_" + article_id).stop(true, true).fadeOut(250, function(){
    jQuery("div#article_" + article_id).css("opacity", '' );
    reset_arrow(article_id);
    //alert("img#arrow_" + i);
  });
}

function reset_arrow(arrow_id){
  jQuery("img#arrow_" + arrow_id).stop(true).animate({marginTop: "-68px"}).animate({marginTop: "-78px"}).animate({marginTop: "-70px"});
}

function sim_item_hover(article_id){
  jQuery().log("sim_item_hover()");
  hide_all(article_id);
  //alert("-" + (window_height * .4 ) + "px");
  stop_animate = 1;
  jQuery("img#arrow_" + article_id).stop(true).animate({marginTop: "-140px"}, function(){
    jQuery("div#article_" + article_id).css("margin-bottom", "140px").fadeIn(1000);
  });
}

jQuery(window).resize(function() {
  window_height = jQuery(window).height();
  window_width = jQuery(window).width();
  //alert(window_height);
  jQuery("div#wrapper").css("font-size", window_height * .02 + "px");
  //jQuery("div#timeline").css("font-size", window_height * .02 + "px");
  //jQuery("div#lower_wrapper").css("font-size", window_height * .02 + "px");
  //jQuery("div.image_wrapper").css("height", (  ( window_height * (.8*.35) ) - 40 ) + "px" );
});
