(function($){
	$.tiny = $.tiny || { version: '0.1.0'};
	
	$.tiny.accordion = {
		conf: {	
			interval: false 
		} 
	};
			
	function Accordion(root, conf) {  	
		var self = this; 
		var oHeads = $(root).children( '.head' );
		var oFirst = $(oHeads[0]);
		var oCurrent = oFirst;
		var oTimer, bPause;
		
		$.extend(self, {
			initialize: function() {	
				oFirst.addClass('active').next().css({ 'margin-right': 5, 'width': 709 });
				oHeads.click(self.control);	
				oTimer = setTimeout(self.setTimer, 4000);
				$(root).mouseover(function(){ clearTimeout(oTimer); bPause = true; });
				$(root).mouseleave(function(){ bPause = false; self.setTimer(); });
			},
			control: function(oEvent, oTargetCustom){
				var oTarget = (this == self ? oTargetCustom : $(this));
				var oTcontent = oTarget.next(); 
				var	oCcontent = oCurrent.next();
				
				if(oCurrent[0] != oTarget[0] ) {
					oCurrent.removeClass('active');
					oTarget.addClass('active');
					
					oCcontent.animate({ 'width': 0 },{queue: false, duration: 1000}).css({ 'margin': 0 });
					oTcontent.animate({ 'width': 709 },{queue: false, duration: 1000}).css({ 'margin-right': 5 });
				}
				
				oCurrent = oTarget;
				return false;
			},
			setTimer: function(){
				if(conf.interval && $(root).hasClass('active') && !bPause){
					clearTimeout(oTimer);
					oTimer = setTimeout(function(){
						var index = oHeads.index(oCurrent);
						index = index+1 < oHeads.length ? index+1 : 0;
						self.control(null,$(oHeads[index]));
						self.setTimer();
					}, 4000);
				};
			}
		});
		
		
		self.initialize();
	};
	
	$.fn.tinyaccordion = function(conf) { 
		var oElement = null;
		
		conf = $.extend({}, $.tiny.accordion.conf, conf); 
		
		this.each(function() {			
			oElement = new Accordion($(this), conf);
		});
		
		return oElement; 
	};

	
	
})(jQuery);
