$(document).ready(function() {

        if(location.hash !="" && location.hash.split(':')[0] == '#!/noticia'){
            var self = this;
            self.content = new Noticias_Container();            
            var url = baseUrl + "/bandeja/index/format/json";
            $.getJSON(url,function(data){
                $.each(data.noticias.data, function(i,v){
                    if(location.hash.split(':')[1] == v.ID){
                        self.content.update(v);
                    }
                });
            })            
        }

        var _n = new Noticias();

});	

var Noticias = function(){
	var self = this;
	self.menu = new Noticias_Menu();
	self.content = new Noticias_Container();
	$("body").bind("atualiza-noticia",function(evt,param){
                self.content.update(param)
	})
}

var Noticias_Menu = function(){
	var self = this;
	self.data = null;
	self.current = "1";
	self.clear = function(){
		self.context.find(".bandeja").remove();
		self.paginacao.find("a").remove();
	};
	self.load = function(page){
		url = baseUrl + "/bandeja/index/format/json/pagina/" + page;
		$.getJSON(url,function(data){
			self.data = data;
			self.update();
		})
	}
	self.update = function(){
		self.clear();
		$(self.data.noticias.data).each(function(i){
			var noticia = this;
			var div = $(document.createElement("div")).addClass("bandeja");
			var h1 = $(document.createElement("h1"));
			var p = $(document.createElement("p"));
			
			if(this.FILES_IMAGE_THUMB != undefined){
				div.html("<img src=\"" + baseUrl + "/uploads" + this.FILES_IMAGE_THUMB + "\" />")
			}
			$(h1).html(this.TITULO)
			/*$(p).html(this.CHAMADA.substring(0,100))*/
			$(h1).click(function(){
				$("body").trigger("atualiza-noticia",noticia);
			})
			$(div).click(function(){
				$("body").trigger("atualiza-noticia",noticia);
			})
			div.append(h1)
			self.context.find(".paginacao").before(div);
		})
		$(self.data.noticias.pagination.lista).each(function(i){
			var a = document.createElement("a");
			$(a).html(this.toString())			
			self.paginacao.append(a)
			
			if(self.current == this.toString()){
				$(a).addClass("ativo")
			}	
			$(a).click(function(){
				self.current = $(this).html();
				self.change($(this).html());
			})
		})
	}
	self.change = function(page){
		self.load(page)
	}
	self.init = function(){
		self.paginacao = $(".paginacao");
		self.context = $(".noticias");
		self.container = $(".noticia-bandeja");
		self.clear();
		self.change(1);	
	}();
	
}

var Noticias_Container = function(){
	var self = this;
	self.state = "close";
	
	self.clear = function(){
		self.context.find("*").remove();	
	}
	self.open = function(){
		self.state = "transition";
		$(self.context).fadeIn("slow",function(){
			self.state = "open";
		})	
	}
	self.update = function(data){
		
		if(self.state == "transition"){
			return false;
		}else if(self.state == "close"){
			self.open();
		}
		
		self.clear()
		var top = $(document.createElement("div"))
		top.addClass("topo");
		var btClose = $(document.createElement("div"))
		btClose.addClass("fechar");
		btClose.html("fechar")
		btClose.click(function(){
			self.close();
		})
		top.append(btClose)
		self.context.append(top)
		
		self.context.append("<h1>" + data.TITULO + "</h1>")
		
		var div = $(document.createElement("div"))
		div.html("<img class='fleft margin-right margin-bottom' + src=\"" + baseUrl + "/uploads" + data.FILES_IMAGE + "\" />" + data.NOTICIA)
		div.addClass("miolo");
		
		self.context.append(div)
	}
	self.close = function(){
		self.state = "transition";
		$(self.context).fadeOut(1000,function(){
			self.state = "close";
		})
	}
	self.init = function(){
		self.context = $(".noticia-bandeja");
	}()
}
