/*
* JQuery controller script
*

*
*/
var postcardPairs = new Array();

$(document).ready(function(){

var bannerInterval=0;
var isAnimating=false;
var stopAnimation=false;

//$("#slideshow .card").css("top", "-22px");
//$("#slideshow .left").css("left", "0px");
//$("#slideshow .right").css("right", "0px");

postcardSlide = function() {

  if(isAnimating) return;

  clearInterval(bannerInterval);

  var topCards = $(".postcardPair:last");

  isAnimating=true;

  $(".postcardPair:last .right").animate({top: "-400px"}, 400, "swing", function(){
    $(".postcardPair:last .left").animate({top: "-400px"}, 400, "swing", function(){
      $(topCards).hide();
      $(".postcardPair:first").before(topCards);
      $(".postcardPair:first .card").css("top","-22px");
      $(".postcardPair:first").fadeIn('fast');

      isAnimating=false;
      if(!stopAnimation) bannerInterval = setInterval("postcardSlide()",5000);

    });
  });

}


loadPostcardPair = function(num) {
  var imgLeft = new Image();
  var imgRight = new Image();

  var postcardPair = document.createElement('div');
  var cardLeft = document.createElement('div');
  var cardRight = document.createElement('div');

  $(postcardPair).addClass("postcardPair");

  $(cardLeft).addClass("card left");
  $(cardRight).addClass("card right");


  $('#slideshow').prepend(postcardPair);
  $(postcardPair).append(cardLeft);
  $(postcardPair).append(cardRight);

  $(imgLeft).load(function () { // set the image hidden by default
      $(this).hide();
      $(cardLeft).append(this);
      //$(this).fadeIn('fast');
      $(this).css("display","block");
    })
    .error(function () {
      // notify the user that the image could not be loaded
    })
    .attr('src', 'cm_sitedata/page_images/'+postcardPairs[num][0])
    .attr('alt', 'Postcards from our clients making the most of their financial freedom')
    .attr('title', 'Postcards from our clients making the most of their financial freedom');

    $(imgRight).load(function () { // set the image hidden by default
      $(this).hide();
      $(cardRight).append(this);
      //$(this).fadeIn('fast');
      $(this).css("display","block");
    })
    .error(function () {
      // notify the user that the image could not be loaded
    })
    .attr('src', 'cm_sitedata/page_images/'+postcardPairs[num][1])
    .attr('alt', 'Postcards from our clients making the most of their financial freedom')
    .attr('title', 'Postcards from our clients making the most of their financial freedom')

    if (num < (postcardPairs.length-1)) {
      num++;
      loadPostcardPair(num);
    } else {
      bannerInterval = setInterval("postcardSlide()",5000);
    }
}

showPostcards = function(num) {

    $("#slideshow .postcardPair .card").fadeIn();

}


var pause = document.createElement('div');
$(pause).addClass("pause");
$(pause).hide();
$("#slideshow").append(pause);

/*$("#slideshow").bind("mouseover", function(){
  clearInterval(bannerInterval);
  $(pause).show();
});

$("#slideshow").bind("mouseout", function() {
    postcardSlide();
    //bannerInterval = setInterval("postcardSlide()",5000);
    $(pause).hide();
});*/

$("#slideshow").hover(function(){
  clearInterval(bannerInterval);
  stopAnimation=true;
  $(pause).show();
}, function(){
   postcardSlide();
   stopAnimation=false;
   $(pause).hide();
 });

 /*
 *  Make links with a rel attribute of 'external' open in a new window
 */

 $("a[rel='external']").attr("target","_blank");



});