// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var current_tab = 'ror';
var slideshow_count = 5;
var current_slide = 0;

document.observe("dom:loaded", function() {
  if($('prime')) {
    $('tab_link_ror').observe('click', did_click_ror_link, false);
    $('tab_link_iphone').observe('click', did_click_iphone_link, false);
    $('slideshow_previous').observe('click', did_click_previous_link, false);
    $('slideshow_next').observe('click', did_click_next_link, false);
    
    // new PeriodicalExecuter(did_click_next_link, 5);
  }
  
  $('menu_contact_link').observe('click', did_click_contact_link, false);
  
  $$('.screen_link').each(function(link) {
    link.hover(
      function(e) {
        element = Event.element(e);
        var desc = element.down('.description');
        new Effect.Appear(desc, { duration: 0.2, to: 0.9 });
      },
      function(e){
        element = Event.element(e);
        var desc = element.down('.description');
        new Effect.Fade(desc, { duration: 0.2, to: 0 });
      }
    );
  });
  
  $$('.small_screen_link').each(function(link) {
    link.hover(
      function(e) {
        element = Event.element(e);
        var desc = element.up('.screen').down('.tooltip');
        desc.morph({ top: '-110px' }, { duration: 0.2 });
        new Effect.Appear(desc, { duration: 0.2, to: 0.9 });
      },
      function(e){
        element = Event.element(e);
        var desc = element.up('.screen').down('.tooltip');
        desc.morph({ top: '-120px' }, { duration: 0.2 });
        new Effect.Fade(desc, { duration: 0.2, to: 0 });
      }
    );
  });
  

});

function cancel_current_animation(){
  Effect.Queues.get('global').each(function(effect) { effect.cancel(); });
}

function slide_in(e) {
  var duration = 0.5;
  new Effect.Move($(e), { x: 50, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, duration: 0 });
  new Effect.Move($(e), { x: 0, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, duration: duration });
  new Effect.Appear($(e), { duration: duration });
}

function slide_out(e) {
  var duration = 0.5;
  new Effect.Move($(e), { x: -50, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, duration: duration});
  new Effect.Fade($(e), { duration: duration});
}

function did_click_iphone_link(e) {
  if (current_tab != 'iphone') {
    cancel_current_animation();
    slide_in('prime_iphone');
    slide_out('prime_ror');

    $('tab_ror').removeClassName('active');
    $('tab_iphone').addClassName('active');
    
    current_tab = 'iphone';
  }
  Event.stop(e);
}

function did_click_ror_link(e) {
  if (current_tab != 'ror') {
    cancel_current_animation();
    slide_in('prime_ror');
    slide_out('prime_iphone');

    $('tab_iphone').removeClassName('active');
    $('tab_ror').addClassName('active');
    
    current_tab = 'ror';
  }
  Event.stop(e);
}

function slide_to_position(e) {
  var offsets = $(e).positionedOffset();
  var total_width = $(e).getWidth();
  var slideshow_width = total_width / slideshow_count;
  
  var slide_to_position = -(current_slide * slideshow_width);
  new Effect.Move($(e), { x: slide_to_position, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, duration: 0.5});
}

function slide_to_previous() {
  cancel_current_animation();
  
  current_slide -= 1;
  if (current_slide < 0){ 
    current_slide = slideshow_count - 1;
  }
  
  slide_to_position('slideshow');
}

function slide_to_next() {
  cancel_current_animation();
  
  current_slide += 1;
  if (current_slide >= slideshow_count){ 
    current_slide = 0;
  }
  
  slide_to_position('slideshow');
}

function did_click_previous_link(e) {
  slide_to_previous();
  Event.stop(e);
}

function did_click_next_link(e){
  slide_to_next();
  Event.stop(e);
}

function did_click_contact_link(e){
  new Effect.ScrollTo('contact', {queue: 'front' });
  new Effect.Pulsate('contact', {pulses: 2, duration: 1, queue: 'end' });
  Event.stop(e);
}