/* 
* News Li Scroll (1.0)
* by Stuart Gregg (www.civicasoft.com)
* sgregg at civicasoft dot com
*
* Copyright (c) 2011 Stuart Gregg (www.civicasoft.com)
*
* NOTE: This script requires jQuery
* Download jQuery @ www.jquery.com
* v 1.0   - Initial release
*
*/

function initSlideShow(mmid,target) {
  $(target).load('/custom/aja/hp-slider-101.asp', { id: mmid, cnt: -1 }, function () {
    slideShow();
  });
}


var galleryTimer = null;
var transition = false;
var galleryDir = 'fwd';
var mouseOver = false;
var slideOverHREF = '';

function slideShow() {

  //transition = true;
  $('a.next').click(function (e) {
    if (galleryTimer > 0) clearTimeout(galleryTimer);
    //alert('hello');
    e.preventDefault();
    e.stopPropagation();
    if (!transition) galeryNext();
  });

  $('a.prev').click(function (e) {
    if (galleryTimer > 0) clearTimeout(galleryTimer);
    //alert('hello');
    e.preventDefault();
    e.stopPropagation();
    if (!transition) galeryPrev();
  });

  //Set the opacity of all images to 0
  $('#gallery span').css({ opacity: 0.0 });

  //Get the first image and display it (set it to full opacity)
  $('#gallery span:first').css({ opacity: 1.0 });

  //Set the caption background to semi-transparent
  $('#slide-wrapper .caption').css({ opacity: 0.8 });

  //Resize the width of the caption according to the image width
  //  $('#slide-wrapper .caption').css({ width: $('#gallery a').find('img').css('width') });

  //Get the caption of the first image from REL attribute and display it
  $('#slide-wrapper .content').html($('#gallery span:first').find('img').attr('rel'))
  
  slideOverHREF = $('#gallery span:first').attr('href');
  if (slideOverHREF != '') {
    $('#slide-wrapper').css({ 'cursor': 'pointer' })
  }
  else {
    $('#slide-wrapper').css({ 'cursor': 'wait' })
  }


  //.animate({ opacity: 0.7 }, 400, "linear", function () { transition = false; });

  $("#slide-wrapper")
  .mouseenter(function () {
    //$('a', this).show();
    if (galleryTimer > 0) clearTimeout(galleryTimer);
    mouseOver = true;
    //$(this).css({ 'border': '1px solid red' })
  })
  .mouseleave(function () {
    //$('a', this).hide();
    mouseOver = false;
    //$(this).css({ 'border': '0px solid red' })
    if (galleryDir == 'fwd') {
      galleryTimer = setTimeout('galeryNext()', 3000);
    }
    else {
      galleryTimer = setTimeout('galeryPrev()', 3000);
    }
  })
  .click(function () {
    if (slideOverHREF != '') {
      window.location = slideOverHREF;
    };
  });

  //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
  if (!mouseOver) {
    galleryTimer = setTimeout('galeryNext()', 6000);
  }

}

function galeryNext() {
  transition = true;
  galleryDir = 'fwd';
  if (galleryTimer > 0) clearTimeout(galleryTimer);

  //if no IMGs have the show class, grab the first image
  var current = ($('#gallery span.show') ? $('#gallery span.show') : $('#gallery span:first'));

  //Get next image, if it reached the end of the slideshow, rotate it back to the first image
  var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $('#gallery span:first') : current.next()) : $('#gallery span:first'));

  //Get next image caption
  var caption = next.find('img').attr('rel');
  slideOverHREF = next.attr('href');

  if (slideOverHREF != '') {
    $('#slide-wrapper').css({ 'cursor': 'pointer' })
  }
  else {
    $('#slide-wrapper').css({ 'cursor': 'wait' })
  }


  //Set the fade in effect for the next image, show class has higher z-index
  next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1000);

  //Hide the current image
  current.animate({ opacity: 0.0 }, 1000, "linear", function () { transition = false; })
	.removeClass('show');

  //Display the content
  $('#slide-wrapper .content').html(caption);

  //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
  if (!mouseOver) {
    galleryTimer = setTimeout('galeryNext()', 6000);
  }

}

function galeryPrev() {
  transition = true;
  galleryDir = 'rev';

  if (galleryTimer > 0) clearTimeout(galleryTimer);

  //if no IMGs have the show class, grab the first image
  var current = ($('#gallery span.show') ? $('#gallery span.show') : $('#gallery span:first'));

  //Get next image, if it reached the end of the slideshow, rotate it back to the first image
  var next = ((current.prev().length) ? ((current.prev().hasClass('caption')) ? $('#gallery span:last') : current.prev()) : $('#gallery span:last'));

  //Get next image caption
  var caption = next.find('img').attr('rel');
  slideOverHREF = next.attr('href');
  if (slideOverHREF != '') {
    $('#slide-wrapper').css({ 'cursor': 'pointer' })
  }
  else {
    $('#slide-wrapper').css({ 'cursor': 'wait' })
  }

  //Set the fade in effect for the next image, show class has higher z-index
  next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1000);

  //Hide the current image
  current.animate({ opacity: 0.0 }, 1000, "linear", function () { transition = false; })
	.removeClass('show');

  //Display the content
  $('#slide-wrapper .content').html(caption);

  //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
  galleryTimer = setTimeout('galeryPrev()', 6000);

}

