/**
 * Teleuco Web Framework
 * /home/js/components/sliders.js
 * 
 * @copyright	Copyright (C) 2010 Teleuco s.r.l. (http://teleuco.com)
 * @license	Commercial
 * @version	1.0.0
 * @package	twaf
 * 
 */

jQuery.fn.sliders = function(settings) {
	return this.each(function(){
		var __e = $(this);
		var options = $.extend({
			'selector': 'div'
		}, settings);

		__e.addClass('twaf-sliders').children().hide();

		var d = new Date(), _uid = 'twaf_sliders_' + d.getTime();
		var _index = 0, _total = __e.find(options.selector).size();

		__e.find(options.selector).hide().each(function(i) {
			_index = i + 1;
			$(this).attr('data-sliders', _uid + '_' + _index);
			
			var _next = (_index == _total) ? 1 : (_index + 1);
			$(this).attr('data-sliders-next', _uid + '_' + _next);
			
			var _box = $(this).parent();
			
			if (! _box.hasClass('twaf-sliders')) {
				_box.attr('data-sliders-box', _uid + '_' + _index);
			}
			
			__e.prepend($(this));
		});

		if (_total > 0) twaf_sliders(_uid + '_1');
	});
};

function twaf_sliders (_id) {
	var __e = $('[data-sliders='+_id+']');

	(__e.is(':visible')) ? _out(__e) : _in(__e);

	function _out (e) {
		var _next = $('[data-sliders='+ e.attr('data-sliders-next') +']');
		
		_outBox(e);
		e.fadeOut(500, function(){_in(_next);});
	}

	function _in (e) {
		var _id = e.attr('data-sliders');
		
		e.fadeIn(500, function(){
			_inBox(e);
			setTimeout("twaf_sliders('"+ _id +"')", 10000);
		});
	}

	function _outBox (e) {
		var _box = $('[data-sliders-box='+ e.attr('data-sliders') +']');
		_box.hide();
	}

	function _inBox (e) {
		var _box = $('[data-sliders-box='+ e.attr('data-sliders') +']'),
		position = e.offset(),
		top = position.top,
		left = position.left, 
		width = e.width(),
		height = e.height();
		
		_box.hide().css({'position':'absolute', 'padding': '20px'});
		var _p = _box.attr('data-sliders-position') || 'bottom';
		
		switch (_p) {
		case 'top':
			_box.css({'top':top,'left':left,'width':width-40,'z-index':'-100'}).show();
			
			var h = _box.height();
			_box.hide().css({'z-index':'auto','height':0});
			
			_box.show().animate({height:'+='+h}, 1000);
			
			break;
		case 'right':
			_box.css({'top':top,'left':left,'width':160,'height':'auto','z-index':'-100'}).show();
			
			var h = _box.outerHeight(true), w = 160;
			if (height < h) {
				var diff = h - height, area = diff * 160;
				w += (area / (height-40));
			}
			
			_box.hide().css({'z-index':'auto','top':top+height-40,'left':left+(width-w-40),'width':w,'height':0,'overflow': 'hidden'});
			
			_box.show().animate({height:'+='+(height-40),top:top}, 1000);
			break;
		case 'left':
			_box.css({'top':top,'left':left,'width':160,'height':'auto','z-index':'-100'}).show();
			
			var h = _box.outerHeight(true), w = 160;
			if (height < h) {
				var diff = h - height, area = diff * 160;
				w += (area / (height-40));
			}
			
			_box.hide().css({'z-index':'auto','height':0,'width':w});
			
			_box.show().animate({height:'+='+(height-40)}, 1000);
			break;
		default: 
			_box.css({'top':top+height-40,'left':left,'width':width-40,'z-index':'-100'});
		
			var h = _box.height();
			_box.hide().css({'z-index':'auto','height':0});
		
			_box.show().animate({height:'+='+h,top:'-='+h}, 1000);				
		} 
	}
}
