/**
 * Slideshow animations
 *
 * Variable number boxes with faded backgrounds
 * and two sliding elements, one for text and one
 * for images.
 *
 * Copyright 2011 rb design + interactive (http://rb-d.com)
 *
 **/

$(document).ready(function(){

  // A few globals for Automating slideshow
  TIMER_XLONG = 10000;
  TIMER_LONG  = 5000;
  TIMER_SHORT = 3000;
  TIMER_MAX_C = 1 * $('#count li').length;
  TIMER_C_CNT = 0;
  TIMER_RESET =  setTimeout(function(){timer(0)}, TIMER_LONG);

  function timer(current) {
    var max =  $('#count li').length - 1;
    current += 1;
    if (current > max) {
      current = 0;
    }
    swapTxt(current);
    swapImg(current);
    swapNP(current);
    fadeNumber(current);
    TIMER_RESET = setTimeout(function(){timer(current)}, TIMER_SHORT);
    TIMER_C_CNT += 1;
    if (TIMER_C_CNT >= TIMER_MAX_C) {
      clearInterval(TIMER_RESET);
    }
  }

  // Swaps text out on left side. 
  function swapTxt (imgNum) {
    var imgDir = $('#slideshow #slide-img').attr('alt');
    $('#slideshow #txt-b').stop().css('top','365px');
    $('#slideshow #txt-b img').attr('src','/img/site/slideshow/'+imgDir+'/img-slide-txt-'+imgNum+'.png');
    $('#slideshow #txt-b').attr('href',$('#count li:eq('+imgNum+') a').attr('href'));
    $('#slideshow #txt-b').animate({top:'0px'},900, function(){
      $('#slideshow #txt-a img').attr('src','/img/site/slideshow/'+imgDir+'/img-slide-txt-'+imgNum+'.png');
      $('#slideshow #txt-a').attr('href',$('#slideshow #txt-b').attr('href'));
    });
  }

  //Swaps out large image
  function swapImg (imgNum) {
    var imgDir = $('#slideshow #slide-img').attr('alt');
    $('#slideshow #img-b').stop().css('left','697px');
    $('#slideshow #img-b').fadeIn(0).attr('src','/img/site/slideshow/'+imgDir+'/img-slide-'+imgNum+'.png').css('z-index','100');
    $('#slideshow #img-b').animate({left:'0px'},900, function(){
      $('#slideshow #img-a').attr('src','/img/site/slideshow/'+imgDir+'/img-slide-'+imgNum+'.png');
    });
  }

  // Does graduated transparent backgrounds
  function fadeNumber (current) {
    var max =  $('#count li').length - 1;
    var fadeStep = parseInt(100/(max+5))/100;
    $('#count li').removeClass('dark');
    $('#count li .fade-block').stop();
    for(i=current-1, j = max+1; i>=0; i-=1, j-=1) {
      $('#count li:eq('+i+') .fade-block').fadeTo(900,fadeStep * j);
    }
    for(i=current+1, j = 3; i<=max; i+=1, j+=1) {
      $('#count li:eq('+i+') .fade-block').fadeTo(900,fadeStep * j);
    }
    $('#count li:eq('+current+') ').addClass('dark');
    Cufon.replace('#slideshow #count li a',{ fontFamily: 'bauhaus', hover:true });
    $('#count li:eq('+current+') .fade-block').stop().fadeTo(0,1);
  }

  // Changes the count for the next and previous arrows
  function swapNP(current) {
    var max =  $('#count li').length - 1, next = current + 1, previous = current -1;
    if(next > max) { next = 0;}
    if(previous < 0) { previous = max;}
    $('#slideshow #arrow-r').attr('alt',next);
    $('#slideshow #arrow-l').attr('alt',previous);
  }


  // Add image for doing graduated transparent backgrounds
  $('#count li').each(function(index){
    $(this).append('<img class="fade-block" src="/img/site/slideshow/img-white.gif" alt="'+index+'" />');
  });

  // Check click events to trigger animation
  $('#count li a, #arrow-r, #arrow-l').click(function(){
    var current = parseInt($(this).siblings('.fade-block').attr('alt')); // if li click
    if(isNaN(current)) { current=parseInt($(this).attr('alt'));} //if arrow click.
    swapTxt(current);
    swapImg(current);
    swapNP(current);
    fadeNumber(current);
    clearTimeout(TIMER_RESET);
    return false;
  });


  // Bind keyevents
  document.onkeydown = function(ev) {
    var key;
    var focusedInputs = $("input:focus");
    // Ignore arrow keys if an input is focused
    if (focusedInputs == null || focusedInputs.length <= 0) {
      ev = ev || event;
      key = ev.keyCode;

      if (key == '37') { $('#arrow-l').click();}
      if (key == '39') { $('#arrow-r').click();}
    }
  }


  // Initialize to the first slide
  swapTxt(0);
  swapImg(0);
  swapNP(0);
  fadeNumber(0);
});

