/* -------------------------------------------------------------
	s3Slider

	Developped By: Boban Karišik  -> http://www.serie3.info/
      CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.0

	Copyright: Feel free to redistribute the script/modify it, as
			       long as you leave my infos at the top.

	Modified by: Zoltan Hajdu, DolcerHolding Ltd.
  ------------------------------------------------------------- */

( function ( $ ) {

  $.fn.s3Slider = function ( vars ) {

    /* Variables */

    var element = this;

    var timeOut = (
      vars.timeOut != undefined
    ) ? vars.timeOut : 4000;

    var current    = null;
    var direction  = 'next';
    var timeOutFn  = null;
    var faderState = 'in';

    var itemsLeftLower  = $('#' + element[0].id + 'ContentLeftLower .'  + element[0].id + 'ImageMainLower');
    var itemsLeftLow    = $('#' + element[0].id + 'ContentLeftLow .'    + element[0].id + 'ImageMainLow');
    var items           = $('#' + element[0].id + 'Content .'           + element[0].id + 'ImageMain');
    var itemsRightLow   = $('#' + element[0].id + 'ContentRightLow .'   + element[0].id + 'ImageMainLow');
    var itemsRightLower = $('#' + element[0].id + 'ContentRightLower .' + element[0].id + 'ImageMainLower');

    var itemsSpan = $('#' + element[0].id + 'Content .' + element[0].id + 'ImageMain span');

    /* Mouse Overs */

    $('#sliderPrevious').mouseover( function () {
      $('#sliderPrevious').attr('src', '/static/dh_slides_previous_main_on.gif');
    } );

    $('#sliderPrevious').mouseout( function () {
      $('#sliderPrevious').attr('src', '/static/dh_slides_previous_main_off.gif');
    } );

    $('#sliderNext').mouseover( function () {
      $('#sliderNext').attr('src', '/static/dh_slides_next_main_on.gif');
    } );

    $('#sliderNext').mouseout( function () {
      $('#sliderNext').attr('src', '/static/dh_slides_next_main_off.gif');
    } );

    /* Previous Slide */

    $('#sliderPrevious').click( function () {
      if (
        faderState == 'out'
      ) {
        direction  = 'previous';
        clearTimeout(timeOutFn);
        makeSlider();
      }
    } );

    /* Next Slide */

    $('#sliderNext').click( function () {
      if (
        faderState == 'out'
      ) {
        direction  = 'next';
        clearTimeout(timeOutFn);
        makeSlider();
      }
    } );

    /* Fade Element */

    var fadeElement = function () {
      if (
        faderState == 'in'
      ) {
        makeSlider();
      } else {
        timeOutFn = setTimeout(makeSlider, timeOut);
      }
    }

    /* Slider */

    var makeSlider = function () {

      current = (
        current != null
      ) ? current : items[( items.length - 1 )];

      var currNo = ( jQuery.inArray(current, items) + 1 );

      currNo = (
        direction == 'previous' &&
        currNo    == 11
      ) ? 10 : currNo;

      currNo = (
        currNo == items.length
      ) ? 0 : ( currNo - 1 );

      /* Fade In */

      if (
        faderState == 'in'
      ) {

        current = items[currNo];

        $(itemsLeftLower[currNo]).delay((  direction == 'next' ? 225 : 150 )).fadeIn(300);
        $(itemsLeftLow[currNo]).delay((    direction == 'next' ? 300 :  75 )).fadeIn(300);
        $(itemsRightLow[currNo]).delay((   direction == 'next' ?  75 : 300 )).fadeIn(300);
        $(itemsRightLower[currNo]).delay(( direction == 'next' ? 150 : 225 )).fadeIn(300);

        $(items[currNo]).fadeIn(
          300,
          function () {
            if (
              $(itemsSpan[currNo]).css('bottom') == 0
            ) {
              $(itemsSpan[currNo]).slideUp(
                600,
                function () {
                  faderState = 'out';
                  fadeElement();
                }
              );
            } else {
              $(itemsSpan[currNo]).slideDown(
                600,
                function () {
                  faderState = 'out';
                  fadeElement();
                }
              );
            }
          }
        );

      /* Fade Out */

      } else {

        faderState = 'in';

        current = (
          direction == 'next'
        ) ? items[( currNo + 1 )] : items[( currNo - 1 )]; 

        $(itemsSpan[currNo]).slideUp(
          600,
          function () {

            $(itemsLeftLower[currNo]).delay((  direction == 'next' ? 225 : 150 )).fadeOut(300);
            $(itemsLeftLow[currNo]).delay((    direction == 'next' ? 300 :  75 )).fadeOut(300);
            $(itemsRightLow[currNo]).delay((   direction == 'next' ?  75 : 300 )).fadeOut(300);
            $(itemsRightLower[currNo]).delay(( direction == 'next' ? 150 : 225 )).fadeOut(300);

            $(items[currNo]).fadeOut(
              300,
              function () {
                fadeElement();
              }
            );
          }
        );

      }

    }

    /* Initialize */

    makeSlider();

  };

} ) (
  jQuery
);
