
// JQ Slide Widget - jq.slidewidget.js
// author: Danail Chanev - dido@actsols.co.cc
// Date: 02th June, 2011
// Version: 1.0 {date: 02 June 2011}
// Revision: 1
// web: www.actsols.co.cc
/*
// JQ Slide Widget is free jQuery Plugin: you can redistribute it and/or modify
// it under the terms of the either the MIT License or the Gnu General Public License (GPL) Version 2
*/


;(function($) {

$.fn.extend({
	slideWidget: function(options) {

              var data = ''
              options = $.extend({}, $.slideWidget.defaults, options);
              return this.each(function() {
                      new $.slideWidget(this, options);
              });
	}
});


$.slideWidget = function(input, options) {

    var stepSpeed = options.stepSpeed;    // seconds
    var transSpeed = options.transSpeed;  // milliseconds

    // do not set up
    var MaxItems = 0;
    var idx = 0;
    var forcedIdx = 0;
    var stepTimer;
    var doneOnce = false;

    startSlide();

    function startSlide() {

      MaxItems = $('.swImage').length;

      // set invisible
      $('.swImage').hide();
      $('#swImage0').show();
      $('.rpt').css('display', 'none');
      $('#swThumb0').addClass('active');
      $('#rpt0').show();
      $('.tit').hide();
      $('#tit0').show();

      $('.swThumb').bind("click", function(event) {
        var evnTarget = event.currentTarget;
        if (evnTarget) {
          forceStep(evnTarget);
        }
      });

      stepTimer = setTimeout(step, stepSpeed * 1000);

    }

    function forceStep (item) {
       if (stepTimer) window.clearTimeout(stepTimer);
       forcedIdx = item.getAttribute("idx");
       forcedIdx++;
       step();
    }

    function step() {
      showOff();
      stepTimer = setTimeout(step, stepSpeed * 1000);
    }

    function showOff() {

      if (doneOnce && !forcedIdx) {
        stopShow();
        return 0;
      }

      $('#swImage'+parseInt(idx)).fadeOut(transSpeed);
      $('#rpt'+parseInt(idx)).fadeOut(transSpeed);
      $('#tit'+parseInt(idx)).fadeOut(transSpeed);
      $('#swThumb'+parseInt(idx)).removeClass('active');

      if (forcedIdx) {
        idx = forcedIdx -1;
        forcedIdx = 0;
        showOn();
      } else {
        idx++;
        if (idx < MaxItems) {
          showOn();
        } else {
          idx = 0;
          showOn();
          doneOnce = true;
        }
      }

    }

    function showOn() {
      $('#swThumb'+parseInt(idx)).addClass('active');
      $('#swImage'+parseInt(idx)).fadeIn(transSpeed);
      $('#rpt'+parseInt(idx)).fadeIn(transSpeed);
      $('#tit'+parseInt(idx)).fadeIn(transSpeed);
    }

    function stopShow() {
      try {
        if (stepTimer) window.clearTimeout(stepTimer);
      } catch (e) {alert(e)}
    }


}

$.slideWidget.defaults = {
        stepSpeed: 6,
        transSpeed: 500
};

})(jQuery);





