$(function(){
	$('div[id="rotativo"] div[id="descricao"]').fadeTo(0,0.8);
	window.i = 0;
	var pastaImgs = 'admin/images/';
	//var items = new Array('descrição 1|||0.jpg|||http://www.teste.com.br','descrição2|||1.jpg');
	var items = window.items;
	window.count = (items.length-1);
	window.esquerda = false;
	window.inEvent = false;
	
	var item = items[0].split('|||');
	var link = pastaImgs+item[1];
	var href = '';
	if( typeof item[2] != "undefined" ){
		if( item[2].length != '' && $.trim(item[2]) != '' ){
			if( item[2].substr(0,7) != 'http://' ){
				item[2] = 'http://'+item[2];
			}
			href = item[2];
		}
	}
	var imgLoader = new Image();
	$(imgLoader).load(function(){
		var img = $(this);
		var img_this = this;
		$(this).hide();
		$('div[id="rotativo"] div[id="imagem"]').append(img_this);
		$('div[id="rotativo"] div[id="descricao"] div[class="texto"]').fadeOut('normal',function(){
			// mesmo tirando css e return false ele está redirecionando, verificar se remove evento anterior.
			if( href != '' ){
				eval('$(\'div[id="rotativo"] div[id="clear"]\').css("cursor","pointer").click(function(){document.location.href="'+href+'";});');
			}else{
				eval('$(\'div[id="rotativo"] div[id="clear"]\').css("cursor","default").click(function(){return false;});');
			}
			$(this).text(item[0]).fadeIn();
			img.css({
				'width':'530px',
				'height':'150px',
				'border':'0px'
			}).fadeIn('normal');
		});
		window.i++;
		window.interval = setInterval(rotate,5000);
	}).attr('src',link);
	
	function rotate(){
		var item = items[window.i].split('|||');
		var link = pastaImgs+item[1];
		var href = '';
		if( typeof item[2] != "undefined" ){
			if( item[2].length != '' && $.trim(item[2]) != '' ){
				if( item[2].substr(0,7) != 'http://' ){
					item[2] = 'http://'+item[2];
				}
				href = item[2];
			}
		}
		var imgLoader = new Image();
		$(imgLoader).load(function(){
			var img = $(this);
			var img_this = this;
			img.hide();
			if( href != '' ){
				eval('$(\'div[id="rotativo"] div[id="clear"]\').css("cursor","pointer").click(function(){document.location.href="'+href+'";});');
			}else{
				eval('$(\'div[id="rotativo"] div[id="clear"]\').css("cursor","default").click(function(){return false;});');
			}
			$('div[id="rotativo"] div[id="descricao"] div[class="texto"]').fadeOut('normal',function(){
				$(this).text(item[0]).fadeIn('normal',function(){
					window.inEvent = false;
				});
			});
			$('div[id="rotativo"] div[id="imagem"]').children('img').fadeOut('normal',function(){
				$(this).remove();
				$('div[id="rotativo"] div[id="imagem"]').children('img').remove();
				$('div[id="rotativo"] div[id="imagem"]').append(img_this);
				img.css({
					'width':'530px',
					'height':'150px',
					'border':'0px'
				}).fadeIn('normal');
			});
			if( window.count == window.i && window.esquerda !== true ){
				window.i = 0;
			}else{
				if( window.esquerda == true ){
					if( window.i == 0 ){
						window.i = window.count;
					}else{
						window.i--;
					}
					window.esquerda = false;
				}else{
					window.i++;
				}
			}
		}).attr('src',link);
	}
	
	function rotateEsquerda(){
		if( window.inEvent === false ){
			clearInterval(window.interval);
			window.esquerda = true;
			rotate();
			window.inEvent = true;
		}
	}
	function rotateDireita(){
		if( window.inEvent === false ){
			clearInterval(window.interval);
			rotate();
			window.inEvent = true;
		}
	}
	
	$('div[id="rotativo"] div[id="descricao"] img[id="seta_esquerda"]').click(function(){
		rotateEsquerda();
	});
	
	$('div[id="rotativo"] div[id="descricao"] img[id="seta_direita"]').click(function(){
		rotateDireita();
	});
	
	$('div[id="rotativo"]').hover(function(){
		clearInterval(window.interval);
	},function(){
		window.interval = setInterval(rotate,5000);
	});
});