var slide_current = 0;
var slide_next = 1;
var slide_count = 2;


$(document).ready(function(){

  // Fix swf objects
  $("embed").attr({"wmode": "opaque"});
  $("object").prepend('<param name="wmode" value="opaque" />');
  $("object").each(function(i,e){ 
    try {
      e.reload();
    } catch(err) {}
   });
  

  $("#slide_"+slide_current).hide();
  get_random_slide();
  setInterval(get_random_slide, 20000);

  // Slide Skaletor
  screen_resize();
  $(window).resize(function() {
    screen_resize();
  });
  

/*
  $("#menu").hoverIntent( {    
       sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
       interval: 200, // number = milliseconds for onMouseOver polling interval    
       over: menu_show, // function = onMouseOver callback (REQUIRED)    
       timeout: 500, // number = milliseconds delay before onMouseOut    
       out: menu_hide // function = onMouseOut callback (REQUIRED)    
  } ); 
*/ 
  
  // Video
//  var v = document.getElementsByTagName("video")[0];
//  v.play();

  // Move to hash
  //screenfull_move(window.location.hash, 0);  
  
  
  // Logo
  $("#logo").css({"top":"-300px"}).animate({"top":0}, 2000, function(){ 
    setTimeout(tab_hide, 5000);
  });  
  
  $("#logo").click(function(){
    menu_show();
    tab_show();
  });
  $("#logo_menu").click(function(){
    menu_hide();
  });
  
   $("#menu a").click(function(){
    menu_hide();
  });  
  
  
  // Image Zooms
  $('.zoom').fancyZoom({directory: '/javascripts/fancy-zoom_images'});
  
  
});



function screen_resize() {
  slide_resize($(window).width(), $(window).height());
  screenfull_update_size();
}







function get_random_slide(){
  $.ajax({
    type: "GET",
    url: "/api/slides/random",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
      slide_rotate(data);
    }
  });   
}



function slide_rotate(data) {
  if (data.content_type === "image/jpeg") {
   
    $("#slide_"+slide_current).fadeOut("slow", function(){
      //slide_resize($(window).width(), $(window).height());
      $("#slide_"+slide_current).empty().append("<img src='" + data.url + "' />").fadeIn("slow");
    });
// } else if VIDEO {
    
  
  }
}



function slide_resize(ww, wh) {  
    var o_w = 1600;
    var o_h = 1200;
    
    // Resize
    w = ww;
    h = o_h * (ww / o_h);
    
    if (wh > h) {
      h = wh;
      w = o_w * (wh / o_w);
    }
    
    // Reposition
    rw = (ww - w) / 2;
    rh = (wh - h) / 2;
      
    $(".slide video").width(w).height(h).offset({ top: rh, left: rw });
    $(".slide").width(ww).height(wh);
}



function bloat(){
  $(".slide img").effect("scale", { percent: 110, direction: 'both', origin: ['middle','center'] }, 10000);
}





function menu_show() {
  $("object").css({"visibility": "hidden"});
  //$("#weblog").css({"visibility": "hidden"});
  $("#menu").stop().fadeIn(1000);
}


function menu_hide() {
  $("#menu").stop().fadeOut(300, function(){
    $("object").css({"visibility": "visible"});
    //$("#weblog").css({"visibility": "visible"});
  });
  tab_hide();
}






var screenfull_move;
var screenfull_current_name = "home";

function screenfull_move(screen_name, speed){
  if (speed === undefined) {
    speed = 2000;
  }
    
  //document.location.hash = screen_name;  
  screenfull_current_name = screen_name;

  switch(screenfull_current_name) {
    case "projects": 
      x = -1; y = 0;
      break;
    case "about": 
      x = 0; y = -1;
      break;
    case "friends": 
      x = -1; y = -1;
      break;
    case "clients": 
      x = 0; y = -2;
      break;
    case "weblog": 
      x = -1; y = -2;
      break;
    case "crew": 
      x = -0; y = -3;
      break;
    case "capabilities": 
      x = -1; y = -3;
      break;
    default:  
      x = 0;  y = 0;
  }
  
  if ($(".screenfull_container").length > 0) {
    $("#party").animate({"opacity": 0}, speed);
  }

  $(".screenfull_container").stop().animate({
    left: screenfull_width() * x,
    top:  screenfull_height() * y
    }, speed, function() {
      $("#party").animate({"opacity": 1}, speed);
  });
  
   
}


function screenfull_width(){
  return $(window).width();
}


function screenfull_height(){
  return $(window).height();
}


function screenfull_update_size(){
  $(".screenfull_container").width(screenfull_width()*2).height(screenfull_height()*3);
  $(".screenfull").width(screenfull_width()).height(screenfull_height());
  screenfull_move(screenfull_current_name, 0);
}


function tab_show(){
  $("#menu_word_image").stop().animate({"opacity": 0}, 1000);
  $("#logo").stop().animate({"left": 0, "top": 0}, 2000, function(){
    $("#menu_word_tao").stop().animate({"opacity": 100}, 1000);
  });
}

function tab_hide(){
  $("#menu_word_tao").stop().animate({"opacity": 0}, 1000);
  $("#logo").stop().animate({"left": -140, "top": 0}, 2000, function(){
    $("#menu_word_image").stop().animate({"opacity": 100}, 1000);
  });
}




