jQuery(function($){
		$.fn.painel = function(opts){
				var defaults = {
						limit 					: 4,
						Speed						: 1000,
						timeout					: 5000,
						opacity 				: 0.5,
						width 					: 600,
						'height'				: 400,
						backgroundcolor : '#000000',
						titleFontColor 	: '#FFFFFF',
						btnColor 				: '#0080C0',
						fontSize				: '16px',
						color						: '#777777',
						fontFamily			: 'Verdana'
				},	
				nPAINEL = new PAINEL(),
				_titulo = $("ul").children('.titulo'),
				_imagem = $("ul").children('.imagem'),
				_link = $("ul").children('.link');

				opts = $.extend(defaults,opts);
				nPAINEL.globals.defaults = $.extend(opts);
				$.globalEval("var _speed = " + opts.Speed);
				
				_titulo.each(function(i){
							nPAINEL.globals._title.push($(this).html());
				});

				_imagem.each(function(i){
							nPAINEL.globals._image.push($(this).html());
				});
				
				_link.each(function(i){
							nPAINEL.globals._link.push($(this).html());
				});

				// INICIALIZA O PAINEL
				nPAINEL.globals.currentid = 0;
				nPAINEL.globals.previousid = (nPAINEL.globals._image.length)-1;
				nPAINEL.globals.nextid = nPAINEL.globals.currentid + 1;
				nPAINEL.globals._width = opts.width;
				nPAINEL.globals._position = ((opts.width * -1)-2);
				nPAINEL.globals._limit = opts.limit;
				nPAINEL.buildPainel(opts.width);

				$(".btn_proximo").live("click",function(){
						window.clearInterval(timeout);
						nPAINEL.moveBackward();
				});
				$(".btn_anterior").live("click",function(){
						window.clearInterval(timeout);
						nPAINEL.moveForward();
				});
				
				$("#div_html").live("click",function(){
						window.location.href = ''+nPAINEL.globals._link[nPAINEL.globals.currentid]+'';
						return false;
				});
	
				css_init(opts);
				
				$.callRebuild = function(){
						nPAINEL.rebuildPainel();
				}
				
				// GIRA PAINEL AUTOMATICAMENTE 
				$.callFn = function(){
						nPAINEL.moveBackward();
				}
				var timeout = window.setInterval("$.callFn();",opts.timeout);
	
		};

		// CSS SETTINGS 
		var css_init = function(opts){
				$("#painelrotativo").css({
						margin				: '0',
						position			:	'relative',
						width					: opts.width,
						height				: opts.height
				});
		
				$("#div_html_parent").css({
						width						: '100%',
						'height'				: '100%',
						overflow				: 'hidden',
						position				:	'absolute',
						'clip'					: 'rect(0px '+opts.width+'px '+opts.height+'px 0px)'
				});

				$("#div_html").css({
						position			:	'absolute',
						zIndex				: 1,
						margin					: '0px',
						cursor				: 'hand',
						cursor				: 'pointer',
						left					: ((opts.width * -1) - 4)
				});

				$(".fontTitle").css({
						fontSize			: opts.fontSize,
						color					: opts.fontColor,
						fontFamily		: opts.fontFamily,
						padding				: '8px 0 0 8px'
				});
		
				$(".tit_placeholder").css({
						opacity				: opts.opacity,
						position			:	'relative',
						zIndex				:	101,
						background		: opts.backgroundcolor,
						color					: opts.titleFontColor,
						top						: (opts.height-50),
						left					: 27.5,
						width					: (opts.width-54),
						height				: (opts.height-220),
						'font-weight'	: 'bolder'				
				});
		
				$(".img_placeholder").css({
						position			:	'absolute',
						zIndex				:	100
						
				});
		
				$(".board").css({
						position			:	'relative',
						background		: '#000000',
						width					: opts.width,
						height				: (opts.height)
				});
								
				// BUTTONS 
				$("<div class=btn_anterior>").appendTo("#div_html_parent");
				$("<div class=btn_proximo>").appendTo("#div_html_parent");
				
				
				$(".btn_anterior").css({
						opacity				: 0.7,
						position			: 'absolute',
						zIndex				: 102,
						top						: (opts.height-48),
						width					: 27,
						'height'			: opts.height-220,
						left					: 0,
						background		: opts.btnColor + ' url(imgs/btn_backward.png) no-repeat',
						cursor				: 'hand',
						cursor				: 'pointer'
				});
		
				$(".btn_proximo").css({
						opacity				: 0.7,
						position			: 'absolute',
						zIndex				: 102,
						top						: opts.height-48,
						width					: 27,
						'height'			: opts.height-220,
						left					: opts.width-27,
						background		: opts.btnColor + ' url(imgs/btn_forward.png) no-repeat',
						cursor				: 'hand',
						cursor				: 'pointer'
				});
				
		};
		
		// OBJETO PAINEL
		var PAINEL;
		PAINEL = function(){};
		PAINEL.prototype.globals = {
				currentid 		: 0,
				previousid 		: 0,
				nextid 				: 0,
				_limit				: 0,
				_index				: 0,
				_position			: 0,
				_width				: 0,
				_title				: [],
				_image				: [],
				_link					: [],
				defaults			: ''
		}
		
		PAINEL.prototype.moveForward = function(){
				var nLeft = (this.globals._position + this.globals._width)+2;
				
				this.globals.nextid 		= this.globals.currentid;
				this.globals.currentid 	= this.globals.previousid;
				this.globals.previousid = (this.globals._image[this.globals.currentid-1] == undefined) ? this.globals._limit-1 : this.globals.currentid-1;
				
				var w = this.globals._width;
				$("#div_html").animate({'left':nLeft},
						{
								queue			: false,
								duration	: _speed,
								easing		: '',
								complete	:	function(){
										$.callRebuild();
								}
				});
		}
		PAINEL.prototype.moveBackward = function(){
				var pos = this.globals._position;
				var nLeft = parseInt(pos - this.globals._width)-2;
				
				this.globals.previousid = this.globals.currentid;
				this.globals.currentid 	= this.globals.nextid;
				this.globals.nextid 		= (this.globals._image[this.globals.currentid+1] == undefined) ? 0 : this.globals.currentid+1;

				var w = this.globals._width;
				
				$("#div_html").animate({'left':nLeft},
						{
								queue			: false,
								duration	: _speed,
								easing		: '',
								complete	:	function(){
										$.callRebuild();
								}
				});
		}
		PAINEL.prototype.buildPainel = function(wdth){
				$("#painelrotativo").html(this.buildHtml(wdth));
		}
		PAINEL.prototype.rebuildPainel = function(){
				this.buildPainel(this.globals._width);
				css_init(this.globals.defaults);
		}
		PAINEL.prototype.buildHtml = function(wdth){
				var html='',i;
				var contentLength = this.globals._image.length;
				this.globals._index
				
				html+= "<div id=div_html_parent>";
				html+= "<div id=div_html>";
				html+= "<table border=0 cellpading=0 cellspacing=0 width=100%>";
				html+= "<tr>";
						html+= "<td valign=top width=800 id=td_parent>";
							html+= "<table border=0 cellpading=0 cellspacing=0 width=100%>";
								html+= "<tr>";
									// PREVIOUS 
									html+= "<td>";
										html+= "<div class=board>";
											html+= "<div id=img_placeholder class=img_placeholder>" + this.globals._image[this.globals.previousid] + "</div>";
											html+= "<div id=tit_placeholder class='tit_placeholder fontTitle'>" + this.globals._title[this.globals.previousid] + "</div>";
										html+= "</div>";
									html+= "</td>";
									// CURRENT 
									html+= "<td>";
										html+= "<div class=board>";
											html+= "<div id=img_placeholder class=img_placeholder>" + this.globals._image[this.globals.currentid] + "</div>";
											html+= "<div id=tit_placeholder class='tit_placeholder fontTitle'>" + this.globals._title[this.globals.currentid] + "</div>";
										html+= "</div>";
									html+= "</td>";
									// NEXT 
									html+= "<td>";
										html+= "<div class=board>";
											html+= "<div id=img_placeholder class=img_placeholder>" + this.globals._image[this.globals.nextid] + "</div>";
											html+= "<div id=tit_placeholder class='tit_placeholder fontTitle'>" + this.globals._title[this.globals.nextid] + "</div>";
										html+= "</div>";
									html+= "</td>";

								html+= "</tr>";
							html+= "</table>";
						html+= "</td>";
				html+= "</tr>";
				html+= "</table>";
				html+= "</div>";
				html+= "</div>";
				return html;

		}		

});


