/*

Common javascript functions

Author: Artem Timryakov
Copyright: 2011, Artem Timryakov. All rights resevered

-----------------------------------------------------------------------*/


jQuery(document).ready(function(){
    jQuery(document).bgStretcher({
        images: ['/files/theme/bg1.jpg'],
        imageWidth: 1400,
        imageHeight: 900
    });

    jQuery(document).slider({
        slideShow: true
    });

});

(function($){
	/*  Variables  */
	var container = null;
	var allLIs = '', containerStr = '', direction = '';

	var element = this;
	var _sliderPause = false;
	var _sliderTm = null;
	var isScrolling = false;

	$.fn.slider = function(settings){
		settings = $.extend({}, $.fn.slider.defaults, settings);
		$.fn.slider.settings = settings;

        direction = settings.direction;

		function _build(){
            containerStr = '.' + settings.imageContainer;
			container = $(containerStr);
            allLIs = '.' + settings.imageContainer + ' li';
			if(!allLIs.length){ return; }

			$(allLIs).hide();
			$(allLIs + ':first').show().addClass('S-cur');

			if(!container.length){ return; }



            $(".S-prev-slide").live("click",
                    function(){$.fn.slider.slideShow('prev')});
            $(".S-next-slide").live("click",
                    function(){$.fn.slider.slideShow('next')});
            $(".S-pause-slide").live("click",
                    function(){$.fn.slider.pause()});
            $(".S-play-slide").live("click",
                    function(){$.fn.slider.play()});

			if(settings.slideShow && $(allLIs).length > 1){
				_sliderTm = setTimeout(function(){$.fn.slider.slideShow(direction)}, settings.nextSlideDelay);
			}
		};



		/*  Start slider  */
		_build();
	};

	$.fn.slider.play = function(){
       _sliderPause = false;
       $.fn.slider._clearTimeout();
       $.fn.slider.slideShow(direction);
	};

	$.fn.slider._clearTimeout = function(){
       if(_sliderTm != null){
           clearTimeout(_sliderTm);
           _sliderTm = null;
       }
	};

	$.fn.slider.pause = function(){
	   _sliderPause = true;
	   $.fn.slider._clearTimeout();
	};

	$.fn.slider.slideShow = function(direction){

		var current = $(containerStr + ' li.S-cur');
        if(direction=='next')
        {
            direction = 'next';
            if(isScrolling){
                return;
            }
            isScrolling = true;
            var next = current.next();
            if(!next.length){
                next = $(containerStr + ' li:first');
            }
            var margST = '';
            margST = 450 * parseInt(next.index()) * (-1);

            next.addClass('S-cur').css('left','830px');
            current.animate({ 'left': "-=" + '830' + 'px' }, 1000, function()
            {
                $(this).hide();
                $(this).css('left','0');
                isScrolling = false;
            });
            next.animate({ 'left': "-=" + '830' + 'px' }, 1000, function()
            {
                isScrolling = false;
                current.removeClass('S-cur');

            });

            $('.S-slider-text ul').animate({ 'top': + margST + 'px' }, 1000);

        }
        else
        {
            direction = 'prev';
            if(isScrolling){
                return;
            }
            isScrolling = true;
            var prev = current.prev();
            if(!prev.length){
                prev = $(containerStr + ' li:last');
            }
            var margST = '';
            margST = 450 * parseInt(prev.index()) * (-1);

            prev.addClass('S-cur').css('left','-830px');
            current.animate({ 'left': "+=" + '830' + 'px' }, 1000, function()
            {
                $(this).hide();
                $(this).css('left','0');
                isScrolling = false;
            });
            prev.animate({ 'left': "+=" + '830' + 'px' }, 1000, function()
            {
                isScrolling = false;
                current.removeClass('S-cur');
            });
            $('.S-slider-text ul').animate({ 'top': + margST + 'px' }, 1000);

        }


		if(!_sliderPause && $.fn.slider.settings.slideShow){
          $.fn.slider._clearTimeout();
		  _sliderTm = setTimeout(function(){$.fn.slider.slideShow(direction)}, $.fn.slider.settings.nextSlideDelay);
		}
	};

	/*  Default Settings  */
	$.fn.slider.defaults = {
		imageContainer:             'S-slider',
		nextSlideDelay:             5000,
		slideShow:                  false,
        direction:                  'next'
	};
	$.fn.slider.settings = {};
})(jQuery);
