/***
 * JavaScript SlideShow Script v1.0
 * Author: Jeroen Mandersloot
 * Date: 30 Aug, 2010
 *
 * This script creates a lightweight and easily usable slideshow for your website.
 *
 * To use it effectively, just follow the below steps:
 *   - Include jquery.js
 *   - Include this file
 *   - Put all the images you wish to be in the slideshow in a single <div> wrapper with a unique ID
 *   - Create a new SlideShow object
 *       ex: slideShowObject = new SlideShow();
 *   - Call the SlideShow method start() on the object and pass the <div> wrapper as a parameter
 *       ex: slideShowObject.start($('div#slideshow_wrapper'));
 *   - You can specify a few other optional parameters for customizability. These should all be passed as a single object. The following parameters are available:
 *       - speed
 *       - freq
 *       - click
 *       - nav
 *       - navID
 *            ex: slideShowObject.start(
 *                  $('div#slideshow_wrapper'), 
 *                  {
 *                    speed: 1000, 
 *                    freq: 1000,
 *                    click: true, 
 *                    nav: true, 
 *                    navID: 'slideshow_navigation'
 *                  }
 *                );  
 *
***/

var SS_sliding = false;
var SS_kenBurnsEffect = null;

jQuery.fn.kenBurns = function(params) {
 if (typeof(params) == 'undefined') {
    params = {};
  }
   
  //Determine the desired ken burns effect, taking into account the previous one  
  if (this.height() > this.parent().height() && this.width() <= this.parent().width()) {
    if (SS_kenBurnsEffect == 'ttb') {
      SS_kenBurnsEffect = 'btt';
      this.css('margin-top', (this.parent().height()) - this.height() + 'px');
    } else {
      SS_kenBurnsEffect = 'ttb';
    }
  }
  
  else if (this.height() <= this.parent().height() && this.width() > this.parent().width()) {
    if (SS_kenBurnsEffect == 'ltr') {
      SS_kenBurnsEffect = 'rtl';
      this.css('margin-left', (this.parent().width() - this.width()) + 'px');
    } else {
      SS_kenBurnsEffect = 'ltr';
    }
  }
  
  else {
    SS_kenBurnsEffect = null;
  }
  
  applyKenBurns = function(obj, oheight, owidth, fx) {
    if (obj.height() > obj.parent().height() && obj.width() > obj.parent().width()) {
      obj.css({
        height: Math.floor(obj.height() * 0.999) + 'px',
        width:  Math.floor(obj.width()  * 0.999) + 'px'
      });
    }
    
    else if (obj.height() > obj.parent().height() && obj.width() <= obj.parent().width()) {
      if (fx == 'ttb' && parseInt(obj.css('margin-top').replace('px', '')) + obj.height() > obj.parent().height()) {
        obj.css('margin-top', parseInt(obj.css('margin-top').replace('px', '')) - 1 + 'px');
      } else if (parseInt(obj.css('margin-top').replace('px', '')) < 0) {
        obj.css('margin-top', parseInt(obj.css('margin-top').replace('px', '')) + 1 + 'px');
      }
    }
    
    else if (obj.height() <= obj.parent().height() && obj.width() > obj.parent().width()) {
      if (fx == 'ltr' && parseInt(obj.css('margin-left').replace('px', '')) + obj.width() > obj.parent().width()) {
        obj.css('margin-left', parseInt(obj.css('margin-left').replace('px', '')) - 1 + 'px');
      } else if (parseInt(obj.css('margin-left').replace('px', '')) < 0) {
        obj.css('margin-left', parseInt(obj.css('margin-left').replace('px', '')) + 1 + 'px');
      }
    }
    
    if (obj.css('display') != 'none') {
      setTimeout(
        function() {
          applyKenBurns(obj, oheight, owidth, fx);
        },
        50
      );
    } else {
      obj.css({
        height: oheight +  'px',
        width:  owidth  +  'px',
        margin: '0px'
      });
    }
    
    return false;
  }
  
  var parent = this.parent(), height = this.height(), width = this.width(), o = this;
  
  parent.css({
    position: 'relative'
  });
  
  this.css({
    position: 'absolute',
    top: '0px',
    left: '0px'
  });
  
  applyKenBurns(o, height, width, SS_kenBurnsEffect);
};

function SlideShow(w, img, p) {
  if (typeof(w) == 'undefined' || typeof(img) == 'undefined') return;
  this.Defaults = {
    speed: 3000,
    freq: 3000,
    click: true,
    nav: false,
    order: 'random',
    navID: '',
    kenBurns: false,
    loadingImg: null
  };
  
  this.imgDefaults = {
    src: '',
    link: null
  }
  
  this.Data = jQuery.extend(this.Defaults, p);
  this.wrapper = w;
  this.intervalID = null;
  
  this.wrapper.children('img').each(function() {
    jQuery(this).remove();
  });
  
  var imgElem = null, x = null;
  for (i = 0; i < img.length; i++) {
    img[i] = jQuery.extend({}, this.imgDefaults, img[i]);
    imgElem = jQuery('<img/>').attr('src', img[i].src);
    this.wrapper.append(imgElem);
  }
  
  this.wrapper.children('img').each(function(i) {
    if (img[i].link) {
      jQuery(this).click(function() {
        window.location = img[i].link;
      });
      jQuery(this).css('cursor', 'pointer');
    }
  });
    
  if (typeof(p) == 'undefined') {
    p = {};
  }
  
  this.resetInterval = function() {
    var o = this, a = this.wrapper.children('img').size();
    if (this.intervalID) {
      clearInterval(this.intervalID);
    }
    
    if (a > 1) {
      switch (this.Data.order) {
        case 'random':
          this.intervalID = setInterval(
            function() {
              var c = o.wrapper.children('img.active'), a =  o.wrapper.children('img').size();
              do {
                i = Math.floor(Math.random() * a) + 1;
              } while (c.attr('src') == o.wrapper.children('img:nth-child(' + i + ')').attr('src'));
              o.load(i);
            }, 
            this.Data.freq
          );
        break;
        
        case 'reverse':
          this.intervalID = setInterval(
            function() { 
              o.prev();
            }, 
            this.Data.freq
          );
        break;
        
        default:
          this.intervalID = setInterval(
            function() { 
              o.next();
            }, 
            this.Data.freq
          );
        break;
      }
    }
  };
  
  this.prepare = function() {
    var first = this.wrapper.children('img:first-child'), o = this;      
    
    if (first.height()) {
      if (this.Data.loadingImg) {
        jQuery('img#slideShowLoadingImg').remove();
      }
      first.show();
      if (this.Data.kenBurns) {
        first.kenBurns();
      }
      this.resetInterval();
    } else {
      setTimeout(
        function() {
          o.prepare();
        },
        100
      );
    }
  };

  this.start = function() {
    var a = this.wrapper.children('img').size(), first = this.wrapper.children('img:first-child');
    if (a > 1) {
      var o = this;
      this.wrapper.css('position', 'relative');
      this.wrapper.children('img').each(function() {
        jQuery(this).css({
          position: 'absolute',
          top: '0px',
          left: '0px'
        });
        if (o.Data.click) {
          jQuery(this).click(function() {
            o.next();
          });
        }
      });
      first.addClass('active').hide();
      if (this.Data.loadingImg) {
        first.parent().append(jQuery('<img>').css({
          display: 'inline-block',
          position: 'absolute',
          top: parseInt(first.parent().height() / 2) + 'px',
          left: parseInt(first.parent().width() / 2) + 'px'
        }).attr({
          id: 'slideShowLoadingImg',
          src: this.Data.loadingImg
        }));
      }
      jQuery(window).load(function() {
        if (o.Data.loadingImg) {
          jQuery('img#slideShowLoadingImg').remove();
        }
        first.show();
        if (o.Data.kenBurns) {
          first.kenBurns();
        }
        o.resetInterval();
      });
      if (this.Data.nav) {
        this.wrapper.after(jQuery('<div></div>').attr('id', this.Data.navID));
        for (i = 1; i <= a; i++) {
          jQuery('div#' + this.Data.navID).append(jQuery('<a></a>').html(i));
        }
        jQuery('div#' + this.Data.navID + ' a').attr('href', this.wrapper.attr('id')).each(function(i) {
          jQuery(this).bind('click', function() {
            o.load(i+1);
            return false;
          });
        });
      }
    }
  };
  
  /***
   * Loads the next image
  ***/
  this.next = function() {
    if (SS_sliding) return;
    SS_sliding = true;
    var c = this.wrapper.children('img.active'), n = c.next('img');
    if (!n.get(0)) {
      this.change(this.wrapper.children('img:first-child'));
    } else {
      this.change(n);
    }
    setTimeout("SS_sliding = false", this.Data.speed);
  };
  
  /***
   * Loads the previous image
  ***/
  this.prev = function() {
    if (SS_sliding) return;
    SS_sliding = true;
    var c = this.wrapper.children('img.active'), n = c.prev('img');
    if (!n.get(0)) {
      this.change(this.wrapper.children(':last-child'));
    } else {
      this.change(n);
    }
    setTimeout("SS_sliding = false", this.Data.speed);
  };
  
  /***
   * Loads image #i by calling this.change()
  ***/
  this.load = function(i) {
    if (SS_sliding) return;
    SS_sliding = true;
    var c = this.wrapper.children('img.active'), n = this.wrapper.children('img:nth-child('+i+')');
    if (c.attr('src') != n.attr('src')) {
      this.change(n);
    }
    setTimeout("SS_sliding = false", this.Data.speed);
  };

  /***
   * Replaces the current image with a new one, which should be parameter n
  ***/
  this.change = function(n) {
    var o = this, c = this.wrapper.children('img.active'), a = this.wrapper.children('img').size();
    o.wrapper.children('img:not(.active)').hide();
    n.show();
    if (this.Data.kenBurns) {
      n.kenBurns();
    }
    c.fadeOut(o.Data.speed, function() {
      o.wrapper.children('img.active').each(function() {
        jQuery(this).removeClass('active');
      });
      if (!o.wrapper.children('img.active').size()) {
        n.addClass('active');
      }
      if (o.Data.nav) {
        jQuery('#' + o.Data.navID + ' a').removeClass('active');
        jQuery('#' + o.Data.navID + ' a:nth-child(' + (a - jQuery('img.active ~ img').size()) + ')').addClass('active');
      }
    });
    this.resetInterval();
  };
}

var slideShowObject = new SlideShow(
  jQuery('div#main_page_image_wrapper'),
  [
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6488.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6515.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6533.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6562.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6589.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6632.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6670.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6690.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6735.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6778.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6793.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6838.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6846.jpg'
    },
    {
      src: 'skin/frontend/default/Orchid/images/home/_MG_6867.jpg'
    }
  ],
  {
    speed: 1000, 
    freq: 6000,
    order: 'random',
    nav: true, 
    navID: 'main_page_image_navigation', 
    kenBurns: true,
    loadingImg: 'http://79.170.93.4/~hnpnl001/skin/frontend/default/Orchid/images/loading_icon.gif'
  }
);
slideShowObject.start();
