/**
*
* autocomplete plugin
*
* @package jQuery
* 
**/
(function($) {
	
	$.fn.flCarousel = function(o) {
		return this.each(function() {
			new $flc(this, o);
		});
	};
	
	options = {
		speed: 'normal',
		scroll_by: 1,
		delay: 5,
		vertical: false
	}
	
	$.flCarousel = function(e, o) {
		this.obj = e;
		this.options = $.extend({}, options, o || {});

		this.setup();
	};
	
	// Create shortcut for internal use
    var $flc = $.flCarousel;

    $flc.fn = $flc.prototype = {
        flCarousel: '0.2.3'
    };

    $flc.fn.extend = $flc.extend = $.extend;

    $flc.fn.extend({

		block_width_margin: 70,
		min_margin: 8,
		block_id: false,
		block_key: false,
		block_width: false,
		block_height: false,
		work_width: false,
		items_count: false,
		item_work_side: 0,
		current_pos: 0,
		current_item: 0,
		items_per_slide: 1,
		items_per_slide_tmp: false,
		slider_width: 0,
		slider_height: 0,
		time_out: 0,
		visible_items: 0,
		new_margin: 0,
		back: true,
		items: new Array(),
    	
    	setup: function() {
    		
    		/* adapt configurations */
    		this.options.scroll_by = this.options.scroll_by < 1 ? 1 : this.options.scroll_by;
			this.options.speed = this.isInteger(this.options.speed) ? this.options.speed * 1000 : this.options.speed;
			this.options.delay = this.isInteger(this.options.delay) ? this.options.delay * 1000 : 5*1000;
			
			/* adapt variables' values */
			this.block_id = $(this.obj).attr('id');
			this.block_key = this.block_id.split('lc_block_')[1];
			this.block_width = $('#lc_width_'+this.block_key).width();
			this.work_width = this.block_width-this.block_width_margin;
			this.items_count = $(this.obj).find('ul li').length;
			this.items_per_slide = this.options.scroll_by;
			
			var self = this;
			
			/* set current block width */
			$(this.obj).width(this.work_width);
			$(this.obj).children('div.lc_border').width(this.work_width);
			
			/* show content */
			$(this.obj).find('ul').show();
			
			/* build items array */
			var tmp_items = new Array();
			var tmp_max_side = 0;
			var tmp_work_width = 0;
			var tmp_work_height = 0;
			var tmp_vertical = this.options.vertical;
			
			$(this.obj).find('ul li').each(function(){
				var item = new Object({
					width: $(this).width(),
					height: $(this).height()
				});
				tmp_items.push(item);
				tmp_max_side += tmp_vertical ? $(this).height() : $(this).width();
				tmp_work_width = $(this).width() > tmp_work_width ? $(this).width() : tmp_work_width;
				tmp_work_height = $(this).height() > tmp_work_height ? $(this).height() : tmp_work_height;
			});
			
			//this.item_work_side = tmp_item_work_side;
			this.work_height = tmp_work_height;
			
			/* tmp slider width/height */
			if ( this.options.vertical )
			{
				this.slider_width = this.work_width;
				this.slider_height = tmp_max_side;
			}
			else
			{
				this.slider_width = tmp_max_side;
				this.slider_height = this.work_height;	
			}
			
			this.item_work_side = tmp_max_side / this.items_count;
			this.item_work_side = Math.floor(this.item_work_side);

			if ( (this.item_work_side > this.work_width && !this.options.vertical) || (this.item_work_side > this.work_height && this.options.vertical) )
			{
				$(this.obj).find('ul').remove();
				$(this.obj).html('Listing image is bigger then block area, please move this block to wider place.');
				return;
			}
			
			/* detect visible items per block */
			if ( !this.options.vertical )
			{
				this.visible_items = Math.floor(this.work_width/this.item_work_side);
				
				/* correct height for nav arrows */
				$(this.obj).find('div.lc_nav_prev').css('top', (this.work_height/2)-11);
				$(this.obj).find('div.lc_nav_next').css('top', (this.work_height/2)-11);
			}
			else
			{
				this.visible_items = this.items_per_slide;
				
				/* correct height for nav arrows */
				$(this.obj).find('div.lc_nav_prev').css('left', (this.work_width/2)+(this.block_width_margin/2)-11);
				$(this.obj).find('div.lc_nav_next').css('left', (this.work_width/2)+(this.block_width_margin/2)-11);
			}
			
			var work_side = this.options.vertical ? this.work_height : this.work_width;
		
			var margin_option = this.options.vertical ? 'Bottom' : 'Right';
			var margin_diff = 0;
			var index = 0;
			
			$(this.obj).find('ul li:not(:last)').each(function(){
				if ( tmp_vertical )
				{
					var margin = self.work_height - $(this).height();
				}
				else
				{
					var margin = ((self.work_width / self.visible_items) - $(this).width());
					margin += (margin / (self.visible_items == 1 ? 1 : self.visible_items - 1));
					
					if ( margin < self.min_margin )
					{
						self.visible_items -= 1;
						margin = ((self.work_width / self.visible_items) - $(this).width());
						
						margin += (margin / (self.visible_items == 1 ? 1 : self.visible_items - 1));
					}
				}
				
				margin = Math.floor(margin);
				
				tmp_items[index].margin = margin;
				margin_diff += tmp_vertical ? 0 : margin;
				$(this).css('margin'+margin_option, margin);
				index++;
			});
			
			this.visible_items = self.visible_items;
			
			this.items = tmp_items;
			
			var c = this.options.vertical ? this.visible_items : 1;
			
			/* set slider height/width */
			$(this.obj).height(this.work_height * c);
			$(this.obj).children('div.lc_border').height(this.work_height * c);
			$(this.obj).find('ul').height(this.slider_height * c);
			$(this.obj).find('ul').width(this.slider_width + margin_diff);
			
			/* set center */
			if ( this.work_width > this.item_work_side && ( this.options.vertical || ( !this.options.vertical && this.visible_items == 1 ) ) )
			{
				var cor = Math.floor( (this.work_width - tmp_work_width) / 2 );
				var cur = parseInt($(this.obj).css('paddingLeft'));
				
				if ( this.options.vertical )
				{
					$(this.obj).find('ul').width(this.work_width - (cor * 2));
				}
				$(this.obj).children('div.lc_border').width(this.work_width - cor);
				$(this.obj).width(this.work_width - cor);
				
				$(this.obj).css('paddingLeft', cur+cor);
				if ( cur-cor > cur )
				{
					$(this.obj).css('paddingRight', cur-cor)
				}
			}
			
			/* sliding */
			var self = this;
			
			$(this.obj).find('div.lc_nav_prev').click(function(){
				self.stop();
				self.slide('prev');
			});
			
			$(this.obj).find('div.lc_nav_next').click(function(){
				self.stop();
				self.slide('next');
			});
			
			/* automatical start sliding */
			if ( this.options.delay )
			{
				this.time_out = setTimeout( function() { self.slide('next'); }, self.options.delay );
			}
			else
			{
				this.back = flase;
			}
    	},
    	
    	slide: function( dir ){
			if ( this.items_count <= this.visible_items )
			{
				return;
			}
			
			this.items_per_slide = this.items_per_slide > this.visible_items ? this.visible_items : this.items_per_slide;
			
			var axes = this.options.vertical ? 'height' : 'width';
			
			if ( dir == 'next' )
			{
				// stop if the item is the latest
				if ( this.current_item == this.items_count - this.visible_items )
				{
					this.reset();
					return;
				}
				
				// calculate next item possition
				var diff = this.items_count - (this.current_item + this.items_per_slide + (this.visible_items - this.items_per_slide));
				//console.log( 'dif: '+diff )
				//console.log( 'vis: '+this.visible_items )
				if ( diff < this.visible_items )
				{
					//console.log('enter')
					
					if ( diff < 0 )
					{
						this.items_per_slide_tmp = this.items_per_slide + diff;
					}
					else if ( diff == 0 )
					{
						this.items_per_slide_tmp = this.items_per_slide;
					}
					else if ( diff <= this.items_per_slide )
					{
						this.items_per_slide_tmp = diff;
					}
					else if ( diff > this.items_per_slide )
					{
						this.items_per_slide_tmp = this.items_per_slide;
					}
					
					//console.log('stp: '+this.items_per_slide_tmp)
					this.current_pos -= (this.items[this.current_item][axes] + this.items[this.current_item].margin) * this.items_per_slide_tmp;
					
					// set current item
					this.current_item += this.items_per_slide_tmp;
				}
				else
				{
					this.current_pos -= (this.items[this.current_item][axes] + this.items[this.current_item].margin) * this.items_per_slide;
					
					// set current item
					this.current_item += this.items_per_slide;
				}
			}
			else if ( dir == 'prev' )
			{
				// stop if we are in beggining
				if ( this.current_item == 0 )
				{
					this.forward();
					return;
				}
				
				// calculate next item possition
				if ( this.items_per_slide_tmp )
				{
					this.current_pos += (this.items[this.current_item][axes] + this.items[this.current_item].margin) * this.items_per_slide_tmp;
					
					// set current item
					this.current_item -= this.items_per_slide_tmp;
					this.items_per_slide_tmp = false;
				}
				else
				{
					this.current_pos += (this.items[this.current_item][axes] + this.items[this.current_item].margin) * this.items_per_slide;
					
					// set current item
					this.current_item -= this.items_per_slide;
				}
			}
			
			/* do slide */
			var o = this.options.vertical ? {'top': this.current_pos} : {'left': this.current_pos};
			
			$(this.obj).find('ul').animate(o, this.options.speed);

			if ( this.back )
			{
				var self = this;
				this.time_out = setTimeout( function() { self.slide('next'); }, self.options.delay );
			}
    	},
    	
    	stop: function(){
    		clearTimeout(this.time_out);
    		$(this.obj).find('ul').stop();
    	},
    	
    	reset: function(){
    		this.current_pos = 0;
    		this.current_item = 0;
    		
    		var o = this.options.vertical ? {'top': this.current_pos} : {'left': this.current_pos};
			$(this.obj).find('ul').animate(o, this.options.speed);
    		
    		if ( this.back )
    		{
	    		var self = this;
				this.time_out = setTimeout( function() { self.slide('next'); }, self.options.delay );
    		}
    	},
    	
    	forward: function(){
    		var axes = this.options.vertical ? 'height' : 'width';
    		this.current_pos = 0;
    		
    		var to = this.items_count - this.visible_items;
    		for ( var i = 0; i < to; i++ )
    		{
    			this.current_pos -= this.items[i][axes] + this.items[i].margin;
    		}
    		
    		this.current_item = i;
    		this.items_per_slide_tmp = this.detectTmp(this.items_count, this.current_item, this.visible_items, this.items_per_slide);
    		
    		var o = this.options.vertical ? {'top': this.current_pos} : {'left': this.current_pos};
			$(this.obj).find('ul').animate(o, this.options.speed);
    		
    		if ( this.back )
    		{
	    		var self = this;
				this.time_out = setTimeout( function() { self.slide('next'); }, self.options.delay );
    		}
    	},
    	
    	detectTmp: function(total, current, visible, perSlide, tmp){
    		tmp = tmp ? tmp : visible;
    		tmp += perSlide;
    		
			if ( tmp == total )
    		{
    			return perSlide
    		}
    		else if ( total - tmp > perSlide)
    		{
    			return this.detectTmp(total, current, visible, perSlide, tmp);
    		}
    		else if ( total - tmp < 0)
    		{
    			return perSlide + (total - tmp);
    		}
    		else
    		{
    			return total - tmp;
    		}
    	},
    	
    	isInteger: function(s)
    	{
    		return (s.toString().search(/^-?[0-9]+$/) == 0);
    	}
    	
    });
	
})(jQuery);
