(function($) {
	$.fn.randomize = function(childElem) {
	  return this.each(function() {
		  var $this = $(this);
		  var elems = $this.children(childElem);
		  elems.sort(function() { return (Math.round(Math.random())-0.5); });  
		  $this.empty();
		  for(var i=0; i < elems.length; i++){
			$this.append(elems[i]);
		  }
	  });    
	}
})(jQuery);

(function($) {
  $.arrRandom = function(arr) {
    for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
    return arr;
  };
})(jQuery);

$(function() {

	// Configurações padrões
	// ------------------------------------------------------------------------
	var quant = 0;
	var cur		= 0;
	var pageCurrent = 1;
	var speed = 15000;
	var mover = null;


	// Funções de inicilização
	// ------------------------------------------------------------------------
	function _IniciarSlide() {
		// ini: início automático -----------------------------------------------

		//$("div#fotos_ocultas").randomize("div.itens");
		var $li = $('div.itens', 'div#fotos_ocultas').get();    
		var random = $.arrRandom($li);    
		$('div#fotos_ocultas').empty();    
		$(random).appendTo('div#fotos_ocultas');  


		_exibir();
		$_thisItem = $("#fotos_ocultas .itens");
		quant = $_thisItem.size();
		if(quant > 1) _autoPlay();
	}// end : _IniciarSlide

	// Exibir o item atual
	// ------------------------------------------------------------------------
	function _exibir(){
		var $_obj = $('#fotos_ocultas .itens');
		var $next = $_obj.filter('.itens:eq('+ (cur) +')');
		$slide = $(".foto_random");
		$slide.html($next.html()).show();
		$slide.css({opacity: 0.8})
			.animate({opacity: 1.0}, 2000, function() {
				//$active.removeClass('active last-active');
			});
		return false;	
	}// _exibir

	// Auto Play - fica alternando automaticamente
	// ------------------------------------------------------------------------
	function _autoPlay(){
		if(quant<=1) { _stopPlay(); return false; }
		clearInterval(mover); 
		mover = setInterval(function(){ 
			if( cur < ( quant - 1) ) {
				cur++; 
			} else {
				cur = 0;
			}
			_exibir();
		},speed);
	}// _autoPlay

	// Stop Play - parar a animação atual
	// ------------------------------------------------------------------------
	function _stopPlay(){
		clearInterval(mover);
	}// _stopPlay

	// Chamando o início de tudo
	// ------------------------------------------------------------------------
	_IniciarSlide();
	
});
