Utils = function(){}

Utils.$A = function(object) {
	var array = [];
	for(var i = 0; i < object.length; i++)
		array[i] = object[i];
	
	return array;
}	
Utils.fundo = function(status) {	
    if(status){
        var divLoader = document.createElement("div");
		var altura = $("body").get(0).offsetHeight;
        $(divLoader).attr("id","mask").css({height:altura});
        
		$("body").append(divLoader);
		$(divLoader).fadeIn(1000).fadeTo("slow", 0.8);
    }else{
        $("#mask").fadeOut(100,function(){
			$(this).remove();
		});
    }
}

Tabs = function(elem){
	this.elem = elem;
	this.elemTabs;
	this.elemItems;
	
	this.init = function(opt){
		this.elemTabs = opt.elemTabs;
		this.elemItems = opt.elemItems;
		
		$(this.elemTabs).find("a").unbind("click").click(this.eventClick.bind(this));
	}
	this.eventClick = function(e){	
		var target = e.currentTarget;
		var index = $(this.elemTabs).find("a").index(target);
			
		$(this.elemTabs).find("li").removeClass("ativo");
		$(target).parents("li").addClass("ativo");
		
		$(this.elemItems).hide();
		$(this.elemItems).eq(index).show();
		
		return false;
	}
}

Colapse = function(elem){
	this.elem = elem;
	this.handle;
	this.classColapse;
	this.callOpen;
	this.callClose;
	
	this.init = function(opt){
		this.handle = opt.handle;
		this.callOpen = opt.callOpen;
		this.callClose = opt.callClose;
		this.classColapse = opt.classColapse;

		$(this.elem).find(this.handle).unbind("click").click(this.eventClick.bind(this));
	}
	this.eventClick = function(e){
		var target = e.target;
		var thisAux = this;
		var parent = $(target).parents("ul").eq(0);
		var ulCurrent = $(target).parent().find(" > "+this.classColapse);
		
		$(parent).find(".open > "+this.classColapse).not(ulCurrent).hide();
		
		$(ulCurrent).slideToggle(300,function(){
			var parent = $(this).parent();
			parent.toggleClass("open");
		
			if(parent.hasClass("open")){
				if(thisAux.callOpen) thisAux.callOpen(e);
			}else{
				if(thisAux.callClose) thisAux.callClose(e);
			}
			$(thisAux.elem).find("li").not(parent).removeClass("open");
		});
		return false;
	}
	this.open = function(){
	}
	this.close = function(){
	}
}

Utils.loader = function(objLoader) {
	var divImg = document.createElement("div");
	var img = new Image();
	$(divImg).addClass("_loader").css({position:"absolute", top:"50%",left:"50%"});
	$(divImg).append(img);
	
	$(img).load(function(){
		var width = $(this).attr("width");
		var height = $(this).attr("height");
		$(divImg).css({width:width, height:height,marginTop:-(height/2)+"px",marginLeft:-(width/2)+"px"});
	});
	$(img).attr({src: objLoader.src});
	
	return divImg;
}

Utils.boxAlert = function(opt){	
	var HTMLCode =	'<div class="boxAlert">'
						+'<a title="fechar" href="#" class="btCloseAlert">X</a>'
						+'<h2>'+ opt.title +'</h2>'
						+'<p>'+ opt.message +'</p>'
					+'</div>';

	$("body").append(HTMLCode);
	$(".boxAlert").css({top:$(document).scrollTop()+20});
	$(".boxAlert .btCloseAlert").click(function(){
		$(this).parents(".boxAlert").remove();
		return false;
	});
	setTimeout('$(".boxAlert").remove()',10000);
}

Utils.stopPropagEvent = function(e){
	var event = e || window.event;
	if (event.stopPropagation) {event.stopPropagation();
	} else {event.cancelBubble = true}
}

Utils.pop = function(page,name,h,w){
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += 'location=no,';
	settings += 'status=no,';
	settings += 'scrollbars=1,';
	settings += 'titlebar=no';
	
	var win = window.open(page,name,settings);
}
Utils.fieldFocus = function(field,value){
	$(field).parents("form").submit(function(){		
		if ($(field).val() == $(field).data("valueIni")) {
			$(field).val("");
		}
	});
	$(field).focus(function(){
		if ($(this).val() == $(this).data("valueIni")) {
			$(this).val("");
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).val($(this).data("valueIni"));
		}
	}).data("valueIni",(typeof value== "undefined")?$(field).val():value);
}

Utils.fieldUppercase = function(field){
	$(field).keyup(function(){
		$(this).val($(this).val().toUpperCase());
		return false;
	});
}

Utils.quantityCharacter = function(textarea,span){
	var max = ($(this).attr("maxlength")) ? $(this).attr("maxlength") : $(span).html();
	
	$(textarea).keyup(function(e){
		var value = max - $(this).val().length;
		$(span).html(value);
	})
	$(textarea).keypress(function(e){		
		var value = max - $(this).val().length;
		if(value == 0 && (e.charCode!=0 || e.which==13)){
            return false;
        }
	});
}

Utils.cookie = {
	"set": function(name, data, expires, path) {
		var date = new Date();
		var expires = expires || new Date(date.getFullYear()+1, date.getMonth(), date.getDate());
		var path = path || "/";
		document.cookie = [name, "=", escape(data), ";path=", path, ";expires=", expires.toUTCString()].join("");
		return true;
	},
	"get": function(name) {
		var cookies = document.cookie.split(";");
		for(var i = 0, cookie; cookie = cookies[i]; i++) {
	  	while(cookie.charAt(0) == " ")
	  		cookie = cookie.substring(1, cookie.length);
		  if(cookie.indexOf(name + "=") == 0)
	  		return unescape(cookie.substring(name.length + 1, cookie.length));
		}
		return false;
	},
	"unset": function(name) {
		this.set(name, "", new Date(0));
		return true;
	}
}

Utils.executeAjax = function(options, encode){
	var params = {
		type: (options.type)?options.type:"get",
		url: (options.url.indexOf("?") == -1) ? options.url + "?ram=" + Math.random() : options.url + "&ram=" + Math.random(),
		data: options.data,
		dataType:(options.dataType)?options.dataType:"html",
		success: function(response){
			if(options.call) options.call(response);
		},
		error: function(XMLHttpRequest, textStatus) {
			Utils.boxAlert({title:"Erro", message:"Não foi possível concluir a requisição!"});
		}
	}
	if (options.type && options.type.toLowerCase() == 'post') {
		params.contentType = 'application/x-www-form-urlencoded; charset=' + (encode || 'UTF-8');
	}
	$.ajax(params);
}


Modal = function() {
	this.elemModal;
	this.elemContent;
	this.load;
	this.optModal;
	this.template = '<div id="modalMain" class="modal" style="height: auto; position: absolute">'						
						+'<a id="btCloseModal" class="btFechar" href="#" title="Fechar">Fechar(x)</a>'
						+'<div class="modalAux">'
							+'<div id="modalContent" class="auxModalPadrao"></div>'
						+'</div>'							
					+'</div>';
	
	this.init = function(optModal) {
		this.optModal = optModal;
		Utils.fundo(true);
		this.buildModal(optModal);
		
		if (optModal && optModal.ajax) 
			this.callAjax(optModal.ajax);
		else {
			this.appendContent(optModal.content);
		}
	}
	this.buildModal = function(optModal) {
		var thisAux = this;
		var divAux = $("<div/>");
		$(divAux).append(this.template);
		
		this.elemModal = $("#modalMain",divAux)[0];
		this.elemContent = $("#modalContent",divAux)[0];

		if(this.optModal.cssClass){
			$(this.elemModal).addClass(this.optModal.cssClass);
		}		
		$(this.elemModal).css({top:$(document).scrollTop()+20}).click(function(e){
			Utils.stopPropagEvent(e);
		});
		$("#btCloseModal",divAux).click(this.close.bind(this));
		this.addLoad();

		$("body").append(this.elemModal);
		
		$(document).keydown(function(e) {
			if(e.which == 27 || e.keyCode == 27) {
				thisAux.close();
			}
		});
	}
	this.addLoad = function(optAjax) {
		this.load = Utils.loader({src:"/img/ajax-loader.gif"});
		$(this.elemContent).html("").append(this.load);
	}
	this.callAjax = function(optAjax) {
		var opt = {url:optAjax.url,data:optAjax.data,type:"get",call: this.responseAjax.bind(this)};
		Utils.executeAjax(opt);
	}
	this.close = function(event) {		
		$(this.elemModal).fadeOut(250,function(){$(this).remove()});
		Utils.fundo(false);
		if(this.optModal.callClose) this.optModal.callClose();
		return false;
	}
	this.appendContent = function(content){
		var thisObj = this;
		$(this.load).remove();
		$(this.elemContent).parent().css({overflow:"hidden",height:this.elemContent.offsetHeight});
		$(this.elemContent).animate({opacity:0},0).html("").append(content);
		
		$(this.elemContent).parent().animate({height:this.elemContent.offsetHeight},350,function(){
			$(this).css({height:"auto"});
			$(thisObj.elemContent).animate({opacity:1},350,function(){
				this.style.filter = "";
			});
		});
		
		$(this.elemContent).find(".jsCloseModal").unbind("click").click(this.close.bind(this));
	}
	this.responseAjax = function(response){
		var thisObj = this;
		this.appendContent(response);
		
		$(this.elemContent).find("form").submit(function(){
			thisObj.addLoad();
			Utils.executeAjax({type:"post", url:$(this).attr("action"), data:$(this).serialize() ,call: thisObj.responseAjax.bind(thisObj)});
			return false;
		})
		
		$(this.elemContent).find("a[target!='_blank']").click(function(){
			thisObj.addLoad();
			var opt = {url:$(this).attr("href"),type:"get",call: thisObj.responseAjax.bind(thisObj)};
			Utils.executeAjax(opt);	
			return false;
		})
		
		if(this.optModal.ajax && this.optModal.ajax.call) this.optModal.ajax.call(this.elemContent);
		if(this.optModal && this.optModal.call) this.optModal.call(this.elemContent);
		
		$(this.elemContent).find(".jsCloseModal").unbind("click").click(this.close.bind(this));
	}
	this.getElemModal = function() {
		return this.elemModal;
	}
}

ElementoAjax = function(){
	this.elemContent;
	this.load;
	this.opt = null;
	
	this.init = function(elem,opt){
		this.elemContent = elem;
		this.opt = opt;
		this.bindAjax();
	}
	this.loader = function(){
		var load = Utils.loader({src:"/img/ajax-loader.gif"});
		this.load = load;
		$(this.elemContent).append(load);
	}
	this.refresh = function(optAjax){
		Utils.executeAjax(optAjax);
	}
	this.bindAjax = function(){
		var thisObj = this;
		
		$(this.elemContent).find("form").submit(function(){
			//Utils.executeAjax({type:"post", url:$(this).attr("action"), data:$(this).serialize()},thisObj);
			//thisObj.loader();
			return false;
		})
		
		var linksAjax = (this.opt && this.opt.links)?this.opt.links +" a":"a";
		$(this.elemContent).find(linksAjax).click(function(){
			Utils.executeAjax({type:"get", url:$(this).attr("href"),call:thisObj.responseAjax.bind(thisObj)});
			//thisObj.loader();
			return false;
		})
		if(this.opt.call && this.opt.call) this.opt.call(this.elemContent);
	}
	this.responseAjax = function(response){
		//$(this.load).remove();
		$(this.elemContent).html(response);
		this.bindAjax();
	}
}

Dropdown = function(opt){
	this.elem = (opt && opt.elem)?opt.elem:"";
	this.load;
	this.position = (opt && opt.position)?opt.position:"left";
	this.opt = opt;
	
	this.init = function(){
		var thisAux = this;
		this.load = this.buildLoad();
		this.execPosition();
		
		$("body").click(function(){
			thisAux.remove();
		});
		
		return this;
	}
	this.buildLoad = function(){		
		var thisAux = this;
		var div = $("<div/>");
		var ul = $("<ul/>");
		
		$(opt.itens).each(function(index,item){
			var a = $("<a/>");
			$(a).attr({href:"#",title:item.text}).text(item.text).click(function(){
				thisAux.remove();
			}).click(item.call);
			$(ul).append($("<li/>").append(a));
		});
		
		$(div).addClass("dropdown").append(ul).hide();
		$("body").append(div);
		
		return div;
	}
	this.execPosition = function(){
		this.position = this.position.toLocaleUpperCase();
		var left;
		var top;
		
		switch(this.position) {
			case "TOP":
				top = $(this.elem).offset().top-$(this.load).outerHeight();
				left = $(this.elem).offset().left-$(this.load).outerWidth()+$(this.elem).outerWidth();
			break;
			case "BOTTOM":
				top = $(this.elem).offset().top+$(this.elem).outerHeight();
				left = $(this.elem).offset().left-$(this.load).outerWidth()+$(this.elem).outerWidth();
			break;
			case "LEFT":
				top = $(this.elem).offset().top;
				left = $(this.elem).offset().left-$(this.load).outerWidth();
			break;
			case "RIGHT":
				top = $(this.elem).offset().top;
				left = $(this.elem).offset().left+$(this.elem).outerWidth();
			break;
		}
		
		$(this.load).css({top:top, left:left}).show();
	}
	this.show = function(){
		this.execPosition();
	}
	this.hide = function(){
		$(this.load).animate({opacity:0},200,function(){
			$(this).hide();
		});
	}
	this.remove = function(){
		$(this.load).animate({opacity:0},200,function(){
			$(this).remove();
		});
	}
	this.removeAll = function(){
		$(".dropdown").animate({opacity:0},200,function(){
			$(this).remove();
		});
	}
}

Load = function(opt){
	this.elem = (opt && opt.elem)?opt.elem:"";
	this.load;
	this.position = (opt && opt.position)?opt.position:"left";
	
	this.init = function(elem){
		this.elem = (elem)?elem:this.elem;
		this.load = this.buildLoad();
		
		if(elem){
			this.execPosition();
		}
		return this;
	}
	this.buildLoad = function(){
		var load = $("<div/>");
		$(load).addClass("loadElement").append("<img src='/img/ajax-loader.gif'/>").hide();
		$("body").append(load);
		
		return load;
	}
	this.execPosition = function(){
		this.position = this.position.toLocaleUpperCase();
		var left;
		var top;
		
		switch(this.position) {
			case "TOP":
				//top = $(this.elem).offset().top-$(this.load).height() : $(this.elem).offset().top+($(this.elem).outerHeight()-$(this.load).height())/2;
				//left = $(this.elem).offset().left-$(this.load).width()-5 : $(this.elem).offset().left+$(this.elem).outerWidth()+5;
			break;
			case "BOTTOM":
				
			break;
			case "LEFT":
				top = $(this.elem).offset().top+($(this.elem).outerHeight()-$(this.load).height())/2;
				left = $(this.elem).offset().left+$(this.elem).outerWidth()+5;
			break;
			case "RIGHT":
				
			break;
		}

		$(this.load).css({top:top, left:left}).show();
	}
	this.show = function(){
		this.load = this.buildLoad();
		this.execPosition();
	}
	this.hide = function(){
		$(this.load).animate({opacity:0},200,function(){
			$(this).hide();
		});
	}
	this.remove = function(){
		$(this.load).animate({opacity:0},200,function(){
			$(this).remove();
		});
	}
}

Galeria = function(){
	this.elem = null; //jsGaleriaContainer
	this.elemGal = null; //jsGaleriaMask
	this.elemGalAux = null; //jsGaleriaMaskAux
	this.elemAux = null; //jsGaleriaAux
	this.widthAdd = 0;
	this.width = 0;
	this.controle = true;
	this.total;
	this.index;
	this.page = 0;
	this.optionAjax = null;
	
	this.init = function(elem,optionAjax){
		this.elem = elem; //jsGaleriaContainer
		this.elemGal = $(elem).find(".jsGaleriaMask"); //jsGaleriaMask
		this.elemGalAux = $(elem).find(".jsGaleriaMaskAux"); //jsGaleriaMaskAux
		this.elemAux = $(elem).find(".jsGaleriaAux"); //jsGaleriaAux
		this.widthAdd = $(this.elemGal).get(0).offsetWidth;
		this.width = $(this.elemAux).get(0).offsetWidth;
		this.optionAjax = optionAjax;
		this.callAjax = (this.optionAjax && this.optionAjax.call)?this.optionAjax.call:null;
	
		var thisAux = this;
		if(this.callAjax) this.callAjax();
		
		this.verifyQuantity();
		$(this.elem).find(".jsPrevious").unbind("click").click(function(){
			thisAux.setWidth();
			thisAux.previus();
			return false;
		});
		$(this.elem).find(".jsNext").unbind("click").click(function(){
			thisAux.setWidth();
			thisAux.next();
			return false;
		});
	}
	this.next = function(){
		if(this.width - this.widthAdd > (this.getLeft() *(-1)))
			this.anima(this.getLeft() - this.widthAdd);
	}
	this.previus = function(){
		if(this.getLeft() != 0)
			this.anima(this.getLeft() + this.widthAdd);
	}
	this.goTo = function(index){
		this.setWidth();
		this.anima(-index * this.widthAdd);
	}
	this.getLeft = function(){        
		var left = $(this.elemGalAux).css("left");
		left = parseInt(left.substring(0,left.length-2));
		return left;
	}
	this.setWidth = function(){
		this.widthAdd = $(this.elemGal).get(0).offsetWidth;
		this.width = $(this.elemAux).get(0).offsetWidth;
	}
	this.verifyQuantity = function(){
		this.width = $(this.elemAux).get(0).offsetWidth;
		this.total = this.width/this.widthAdd;
		this.index = this.getLeft()*(-1)/this.widthAdd;
	}
	this.reset = function(){
		this.anima(0);
	}
	this.anima = function(range){
		var thisAux = this;
		if(thisAux.controle){
			thisAux.controle = false;
			$(this.elemGalAux).animate({
				left: range
				},'250',function(){
					thisAux.verifyQuantity();
			    	thisAux.controle = true;
					if(thisAux.optionAjax != null) thisAux.addMoreItemByAjax();
			    }
			)
		}
	}
	this.addMoreItemByAjax = function(){
		var thisAux = this;
		if (this.total - this.index < 4) {
			this.page = this.page + 1;
			var opt = this.optionAjax;
			opt.data = (this.optionAjax.data)?this.optionAjax.data:{};
			opt.data.page = this.page;
			opt.call = function(response){
				$(thisAux.elemAux).html($(thisAux.elemAux).html() + response);
				if(thisAux.callAjax) thisAux.callAjax();
			}
			Utils.executeAjax(opt);
		}		
	}
}

Utils.arrayUnique = function(array){
	var newArray = new Array();
	$(array).each(function(index,value){
		var status = true;
		$(newArray).each(function(index2,value2){
			if(value == value2) status = false;
		});
		if(status) newArray.push(value);
	});	
	return newArray;
}

Function.prototype.bind = function() {
	var self = this, args = Utils.$A(arguments), scope = args.shift();
	return function() {
 		return self.apply(scope, args.concat(Utils.$A(arguments)));
	}
}
