var count;
var current;
var last; 
var $active; 
var $next;


function slideForward(){
  $("#controlls > *").unbind();
  $active = $('#slideshow IMG.active');
  last = current;
 
  if(count > current){
    $next = $active.next();
    current++;
  }else{
    $next = $('#slideshow IMG:first');
    current =1;
  }

  $('#controlls #'+last).css({
    'background':'#000000',
    'color':'#FFFFFF'
  });
  $('#controlls #'+current).css({
    'background':'#FFFFFF',
    'color':'#000000'
  });
  
  $active.addClass('last-active');
  $next.css({
    opacity: 0.0
  })
  .addClass('active')
  .animate({
    opacity: 1.0
  }, 1000, function() {
    $active.removeClass('active last-active');
    $("#backwards").bind('click',backwards);
    $("#forward").bind('click',slideForward);
    $("#controlls > .selector").bind('click',SlideControlls);
  });


};
function SlideControlls(){
  $("#controlls > *").unbind();
  $active = $('#slideshow IMG.active');
  last = current;
  current = $(this)[0].id
  $next = $('#slideshow img').eq(current-1);

  $('#controlls #'+last).css({
    'background':'#000000',
    'color':'#FFFFFF'
  });
  $('#controlls #'+current).css({
    'background':'#FFFFFF',
    'color':'#000000'
  });
  $active.addClass('last-active');
  $next.css({
    opacity: 0.0
  })
  .addClass('active')
  .animate({
    opacity: 1.0
  }, 1000, function() {
    $active.removeClass('active last-active');
    $("#backwards").bind('click',backwards);
    $("#forward").bind('click',slideForward);
    $("#controlls > .selector").bind('click',SlideControlls);
  });
};

function backwards(){
  $("#controlls > *").unbind();
  $active = $('#slideshow IMG.active');
  last = current;
 
  if(current == 1){
    $next = $active.next();
    current = count;
  }else{
    $next = $('#slideshow IMG:first');
    current--;
  }
  $('#controlls #'+last).css({
    'background':'#000000',
    'color':'#FFFFFF'
  });
  $('#controlls #'+current).css({
    'background':'#FFFFFF',
    'color':'#000000'
  });

  $active.addClass('last-active');
  $next.css({
    opacity: 0.0
  })
  .addClass('active')
  .animate({
    opacity: 1.0
  }, 1000, function() {
    $active.removeClass('active last-active');
    $("#backwards").bind('click',backwards);
    $("#forward").bind('click',slideForward);
    $("#controlls > .selector").bind('click',SlideControlls);
  });
};



$(document).ready(function() {
  //slideSwitch();
  current =1;
  count =$('#slideshow')[0].children.length - 1;

  $("#backwards").bind('click',backwards);
  $("#forward").bind('click',slideForward);
  $("#controlls > .selector").bind('click',SlideControlls);
  setInterval( "slideForward()", 8000 );
  
  $('#slideshow .linked').click(function(){
    if ($(this).attr('target') == '_new' || $(this).attr('target') == '_blank'){
      window.open($(this).attr('rel'));
    } else {
      window.location.assign($(this).attr('rel'));
    }
    
  });


});
