if(!window.Article){
	window.Article = {
			
		init: function(step){
			switch(step){
				case "cover":
					this.truncateCover();
				break;

				case "list":
					Consulte.setBackOptions({
						url:window.contextPath+"/article/list.action"
					});
					Consulte.clearPrompt();
				break;
				
				case "new":
					Consulte.toggleDimmer("dimmerCover", true);
					if(tinyMCE){
						tinyMCE.init({
							theme : "advanced",
							mode : "exact",
							elements : "article.body",
							plugins : "fullscreen,paste",
							theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent",
							theme_advanced_buttons2 : "formatselect,|,fullscreen",
							theme_advanced_buttons3 : "",
							theme_advanced_toolbar_location : "top", 
							theme_advanced_toolbar_align : "left", 
							theme_advanced_resizing : false,
							paste_auto_cleanup_on_paste : true,
							paste_strip_class_attributes : "all",
							apply_source_formatting : false,
							paste_block_drop : true,
					        paste_preprocess : function(pl, o) {
								o.content = o.content.unescapeHTML();
					        },
							debug : false
						});
					}
					
					if(categories && jQuery){
						this.bindCategory("article.category.name", categories);
					}
					
					//posiciona cursor no primeiro campo
					$("ArticleForm").focusFirstElement();
					Consulte.enableGlossary();
				break;

				case "detail":
					Consulte.setBackOptions({
						url:window.contextPath+"/article/detail.action"
					});
					Consulte.loadPrompt("article-relatedContent");
				break;
			}
		},

		showDetail: function(id){
			new Ajax.Request(window.contextPath+"/article/detail.action", {
				parameters:{ id:id },
				onComplete: function(response){
					if(!Consulte.hasError(response)){
						$("content").innerHTML = response.responseText;
					}
				}.bind(this)
			});
		},

		listByOwner: function(ownerId){
			this.listAll({
				ownerId: ownerId,
				listType:"2"
			});
		},

		listByCategory: function(categoryId){
			this.listAll({
				categoryId: categoryId,
				listType:"1"
			});
		},

		listByOrder: function(order){
			this.listAll({
				categoryId: $("articleFilterForm.categoryId").value,
				ownerId: $("articleFilterForm.ownerId").value,
				order: order,
				listType:"0"
			});
		},

		listAll: function(filters){
			filters = Object.extend({
				categoryId:"", ownerId:"", order:"", pageIndex:"1", listType:"0"
			}, filters||{});

			if($("articleFilterForm") != null){
				$("articleFilterForm.categoryId").value = filters.categoryId;
				$("articleFilterForm.ownerId").value = filters.ownerId;
				$("articleFilterForm.order").value = filters.order;
				$("filter.pageIndex").value = filters.pageIndex;
				$("listType").value = filters.listType;
			}
			this.list();
		},

		list: function(){
			var parameters = {};
			var form = $("articleFilterForm");
			if(form != null){
				parameters = form.serialize({hash:true});
			}

			new Ajax.Request(window.contextPath+"/article/list.action", {
				parameters:parameters,
				onComplete: function(response){
					if(!Consulte.hasError(response)){
						$("content").innerHTML = response.responseText;
					}
				}.bind(this)
			});
		},
		
		newArticle:function(){
			new Ajax.Request(contextPath + "/article/newArticle.action", {
				onComplete:function(response){
					if(Consulte.hasError(response) == false){
						$("content").innerHTML = response.responseText;
					}
				}
			});
		},
			
		save:function(){
			if(this.validateMandatoryFields()){
				if(Consulte.showConfirmMessage(Bundle.getMessage("message.save.warning.male", "txt.article"))){
					var parameters = Form.serialize($("ArticleForm"), {hash:true});
					parameters["article.body"] = tinyMCE.get("article.body").getContent();
					
					new Ajax.Request(contextPath + "/article/save.action", {
						parameters:parameters,
						onComplete:function(response){
							if(Consulte.hasError(response) == false){
								Consulte.toggleDimmer("dimmerCover", false);
								this.list.bind(this, true).defer();
							}
						}.bind(this)
					});
				}
			}
		},

		cancel:function(){
			if(Consulte.showConfirmMessage(Bundle.getMessage("message.cancel.process"))){
				Consulte.toggleDimmer("dimmerCover", false);
				this.list();
			}
		},
		
		validateMandatoryFields:function(){
			try{
				Consulte.mandatory("article.title", "label.title");
				Consulte.mandatory("article.category.id", "label.category");
				//verifica se foi digitado um texto
				tinyMCE.triggerSave();
				if(tinyMCE.get("article.body").getContent() == ""){
					Consulte.showInfoMessage(Bundle.getMessage("message.mandatory.info"));
					Consulte.showErrorMessage(Bundle.getMessage("message.mandatory.field", "label.content"));
					throw new Error("Erro!");
				}
				Consulte.mandatory("article.keywords", "label.keywords");
				return true;
				
			}catch(e){
				return false;
			}
		},
		
		bindCategory:function(field, data){
			jQuery($(field)).autocomplete(data, {
				minChars: 0,
				delay: 100,
				width: 200,
				max:data.length,
				selectFirst: false,
				matchContains: true,
				autoFill: false,
				formatItem: function(row, i, max) {
					return row.name;
				},
				formatMatch: function(row, i, max) {
					return row.name.replaceSpecialChars() +" "+ row.name;
				},
				formatResult: function(row) {
					return row.name;
				}
			}).result(function(event, item){
				$("article.category.id").value = "";
				if(item != null){
					if($("article.category.id").value != item.id){
						$("article.category.id").value = item.id;
						$("article.category.abbreviation").value = item.abbreviation;
					}
				}
			}.bind(this));
		},
		
		listCategories:function(){
			jQuery($("article.category.name")).showAll();
		},

		toggleListOrder: function(){
			var obj = $("ordenar_div");
			var arrow = $("img_ordenar");

			if(obj.visible()){
				obj.hide();
				arrow.className = "img_mostrar";
			}else{
				obj.show();
				arrow.className = "img_esconder";
			}
		},

		getContentEmail:function(){
			return Consulte.getContentEmail("txt.article.subject");
		},

		truncate: function(){
			Consulte.truncateText($$("div.trunca"), 400);
		},
		
		truncateCover: function(){
			Consulte.truncateText($$("p.trunca"), 250);
		}

	};
}

