(function ($) {

    $.fn.ciuSlider = function (options) {

        var opts = $.extend({}, $.fn.ciuTab.set, options);

        return this.each(function () {
            var o = $.meta ? $.extend({}, opts, $(this).data()) : opts;
            
            slider = this;
            slidertype = o.type;
            thumbnailtype = o.thumbnails;
            activeCL = o.activeclass;
            rightelement = o.rightclass;
            leftelement = o.leftclass;
            thumbclasses = o.thumbclass;
            method = o.method;
            time = o.time;
            auto = o.auto;
            autotime = o.autotime;
            
        /* Values */
		elementClass = $(this).attr('class');
		elementsList = $(this).children('ul');
		elementsHtml = $(this).html();
		elements = $(this).children('ul').children('li');
		elementWidth = $(this).children('ul').children('li').width();
		elementsCount = elements.size();
		thumbnailClass = thumbclasses;
		thumbnails = $("<div/>").addClass(thumbnailClass).html(elementsHtml);
		thumbnailelements = $('.'+ thumbnailClass).children('ul').children('li');
		allWidth = parseInt(elementsCount * elementWidth);
		elementsList.width(allWidth);
        
		if (thumbnailtype != 'image') {
			thumbnailelements.each(function() {	
				index = $(this).index() +1;
				$(this).children('a').html(index);
				$('.'+ thumbnailClass).addClass('numeric');
			});
		}
			
		elements.first().addClass(activeCL);
		thumbnailelements.first().addClass(activeCL);

		$(document).bind("slideNext", function(e, myName, myValue) {
            $.fn.ciuSlider.change(activeCL, elementWidth, elementsCount, elementClass, thumbnailClass, "right");
		});
		
		$('.'+ rightelement).click(function() { $(document).trigger('slideNext'); });
		$('.'+ leftelement).click(function() { $.fn.ciuSlider.change(activeCL, elementWidth, elementsCount, elementClass, thumbnailClass, "left"); });
		
		if (auto == true) { setInterval("$(document).trigger('slideNext')", autotime); }           

        });
    };

    $.fn.ciuSlider.change = function (index, width, size, contClass, thumbclass, type) {
        sliderList = $('.'+ contClass).children('ul');
        
        if (index == size) {
			index = 0;
		} 
		
		if (index < 0) {
			index = size -1;
		}
		
		if (type == "list") {
			marginLeft = parseInt(index * width);
            title = sliderList.children('li').eq(index).find('a').attr('title');
			href = sliderList.children('li').eq(index).find('a').attr('href');
			sliderList.animate({ "right": "" + marginLeft + ""}, time);
			sliderList.children('li').removeClass(activeCL);
			$('.'+ thumbclass).children('ul').children('li').removeClass(activeCL);
			sliderList.children('li').eq(index).addClass(activeCL);
			$('.detailSlider').find('h4').html(title);
			$('.detailSlider').find('.description a').attr('href', href);
			$('.'+ thumbclass).children('ul').children('li').eq(index).addClass(activeCL);
		} else {
                sliderList.find('li').each(function() {
					if ( $(this).hasClass(index) ) { indexVal1 = $(this).index(); }
				});
				
			if (type == "right") {
				indexVal = indexVal1+1;
			} else {
				indexVal = indexVal1-1;
			}
			type = "list";		
            $.fn.ciuSlider.change(indexVal, width, size, contClass, thumbclass, type);
		}
        
    }

    $.fn.ciuSlider.set = {
      'type' : 'classic',
	  'thumbnails' : 'image',
	  'activeclass' : 'active',
	  'rightclass' : 'sliderRightButton',
	  'leftclass' : 'sliderLeftButton',
	  'thumbclass' : 'sliderThumbnails',
	  'method' : 'click',
	  'time' : 500,
	  'auto' : true,
	  'autotime' : 12000
    };

})(jQuery);
