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).addClass("fundo").css({height:altura+"px"});
        
		$("body").append(divLoader);
		$(divLoader).css({opacity:0}).animate({opacity:0.6},300);
    }else{
        $(".fundo").fadeOut(100,function(){
			$(this).remove();
		});
    }
}
Utils.fundoRedeSocial = function(status,msg) {
    if(status){
        var divLoader = document.createElement("div");
        $(divLoader).addClass("fundoRedeSocial");
		Utils.avisoRedeSocial({status:true,msg:msg,close:false});
		$("#jsBoxRedeSocial .acoes").append($(divLoader).clone());
		$("body").append(divLoader);
    }else{
		Utils.avisoRedeSocial({status:false});
		$(".fundoRedeSocial").remove();
    }
}
Utils.avisoRedeSocial = function(opt) {
    if(opt.status){
        var divMessage = document.createElement("div");
       	$(divMessage).html(opt.msg).addClass("mensagemRedeSocial").css({display:"none"});
		var divMessage2 = $(divMessage).clone();
		$(".mensagemRedeSocial").remove();	
		$("#intervencaoSocialAux").append(divMessage);
		$("#jsBoxRedeSocial").append(divMessage2);
		$(divMessage).slideToggle(150);
		$(divMessage2).slideToggle(150);
		if(opt.close == undefined || opt.close) setTimeout("$('.mensagemRedeSocial').slideUp(150,function(){$('.mensagemRedeSocial').remove()})",4500);
    }else{
        $(".mensagemRedeSocial").remove();
    }
}

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.executeAjax = function(options, object, encode){
	var params = {
		type: options.type,
		url: (options.url.indexOf("?") == -1) ? options.url + "?ram=" + Math.random() : options.url + "&ram=" + Math.random(),
		data: options.data,
		success: function(response){
			if (object) 
				object.responseAjax(response, options.args);
			else				
				if (options && options.call) 
					eval(options.call + "(response,options.args)");
		}
	}
	if (options.type && options.type.toLowerCase() == 'post') {
		params.contentType = 'application/x-www-form-urlencoded; charset=' + (encode || 'UTF-8');
	}
	$.ajax(params);
}
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.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.validateMultipleEmail = function(value){
	var value = value.split(";");
	var testEmail = new Array();
	
	for(i=0; value.length > i; i++){
		if(value[i]!="" && !(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value[i])))
    		testEmail.push(value[i]);
	}
	return testEmail;
}

Modal = function() {
	this.elemModal;
	this.elemContent;
	this.load;
	this.optModal;
	this.template = '<div id="modalMain" class="modal" style="height: auto; position: absolute; left: 50%">'
							+'<a id="btCloseModal" title="Excluir" href="javascript:void(0)" class="btFechar">[x] fechar</a>'
							+'<div class="modalAux">'
								+'<div id="modalContent" class="auxModalPadrao"></div>'
							+'</div>'
							+'<span class="bottom">&nbsp;</span>'
						+'</div>';
	
	this.init = function(optModal) {
		this.optModal = optModal;
		Utils.fundo(true);
		this.buildModal(optModal);
		
		if (optModal && optModal.ajax) this.callAjax(optModal.ajax);
		else $(this.elemContent).html(optModal.content);
	}
	this.buildModal = function(optModal) {
		var thisAux = this;
		var divModal = document.createElement("div");
		$(divModal).append(this.template);
		
		this.elemModal = $("#modalMain",divModal)[0];
		this.elemContent = $("#modalContent",divModal)[0];
		
		$(this.elemModal).css({top:$(document).scrollTop()+20}).click(function(e){
			Utils.stopPropagEvent(e);
		});
		$("#btCloseModal",divModal).click(this.close.bind(this));
		this.addLoad();
		
		$("body").append(divModal);
		
		$(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-branco.gif"});
		$(this.elemContent).html("").append(this.load);
	}
	this.callAjax = function(optAjax) {
		Utils.executeAjax(optAjax,this);
	}
	this.close = function(arg, event) {
		$(this.elemModal).fadeOut(250,function(){$(this).remove()});
		Utils.fundo(false);
		//if(this.optModal.callClose) this.optModal.callClose();
	}
	this.responseAjax = function(response) {		
		if(response.indexOf('{"redirect":')==0){
			var responseEval = eval("("+ response +")");
			
			if (responseEval.redirect) 
				window.location = responseEval.url;
			else {
				var opt = this.optModal.ajax;
				opt.url = responseEval.url;
				this.callAjax(opt);
			}
		}else{

			var thisObj = this;
			$(this.load).remove();
			$(this.elemContent).parent().css({overflow:"hidden",height:this.elemContent.offsetHeight});
			$(this.elemContent).animate({opacity:0},0).html(response);
			
			$(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("form").submit(function(){
				//thisObj.addLoad();
				//Utils.executeAjax({type:"post", url:$(this).attr("action"), data:$(this).serialize()},thisObj);
				//return false;
			})
			
			$(this.elemContent).find("a[target!='_blank']").click(function(){
				thisObj.addLoad();
				Utils.executeAjax({type:"get", url:$(this).attr("href")},thisObj);
				return false;
			})
			
			if(this.optModal.ajax.call) eval(this.optModal.ajax.call+"(this.elemContent,this.optModal)");
		}
	}
	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);
	}
	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")}, thisObj);
			//thisObj.loader();
			return false;
		})
	}
	this.responseAjax = function(response){
		//$(this.load).remove();
		$(this.elemContent).html(response);		
		
		this.bindAjax();
		if (this.opt && this.opt.ajax && this.opt.ajax.call) {
			eval(this.opt.ajax.call + "(this.elemContent)");
		}
	}
}


Comentario = function(){
	this.id;
	this.setReset=null;
	this.init = function(elem){
		var thisAux = this;
		this.id = $(elem).attr("href");
		this.elem = elem;
		
		this.execPosition();
		$(this.id).hide();
		
		$(elem).click(function(e){
			Utils.stopPropagEvent(e);
			if($(thisAux.id).data("status")=="close")thisAux.open();
			else thisAux.close();
			return false
		});
		$(this.id).find(".btFechar").click(function(e){
			thisAux.close();
			return false
		});
		$(document).keydown(function(e) {
			if(e.which == 27 || e.keyCode == 27) {
				thisAux.close();
			}
		});
	}
	this.execPosition = function(){
		$(this.id).show();
		$("body").append($(this.id));
		var top = $(this.elem).offset().top - $(this.id).height();
		var left = $(this.elem).offset().left;
		
		$(this.id).css({top:top, left:left}).data("status","close").click(function(e){Utils.stopPropagEvent(e)});
	}
	this.reset = function(){
		$(this.id).find("textarea").val("");
		$(this.id).find(".jsQtdCaracteres").html($(this.id).find("textarea").attr("maxlength"));
	}
	this.open = function(){
		if(this.setReset!=null) this.setReset();
		this.reset();
		this.execPosition();
		$(this.id).hide();
		$(this.id).slideDown(250);
		$(this.id).data("status", "open");
	}
	this.close = function(){
		$(this.id).data("status", "close");
		$(this.id).slideUp("fast", function(){
			$(this).find(".load").remove();
			$(this).find("*").show();
		});		
	}
}

BoxRedeSocial = function(){
	this.template = '<div id="jsBoxRedeSocial" class="boxRedeSocialAux">'+
						'<div class="boxRedeSocial">'+
							'<a class="btFechar" href="javascript:void(0)" title="Excluir" id="btCloseModal">[x] fechar</a>'+
							'<div id="contentBox"></div>'+
						'</div>'+
					'</div>';
	this.elem;
	this.elemContent;
	this.status = "close";
	this.ready = false;
	this.init = function(a){
		var thisAux = this;
		
		$(window).scroll(function(){
	    	var top = $(document).scrollTop() + 20;
	    	$(thisAux.elem).stop().animate({top:top}, 300);
	    });
		
		$(a).click(function(e){
			Utils.stopPropagEvent(e);
			if (thisAux.status == "close") thisAux.open(this);
			else thisAux.close();
			return false;
		});
		
		$(document).keydown(function(e) {
			if(e.which == 27 || e.keyCode == 27) {
				thisAux.close();
			}
		});
	}
	this.responseAjax = function(response,a){
		var thisAux = this;
		this.ready = true;
		$(this.elemContent).html(response);
		$(this.elem).find("textarea:eq(0)").val($("#recipe_sharing").val());
		$(this.elem).find(".btFechar").click(function(e){
			thisAux.close();
			return false
		});
		initRedeSocial();
		loginMsn();
	}
	this.execPosition = function(){
		$(this.elem).show();
		var top = $(document).scrollTop() + 20;
		$(this.elem).css({top:top}).hide().click(function(e){Utils.stopPropagEvent(e)});
	}
	this.build = function(a){
		var divBox = document.createElement("div");
		$(divBox).append(this.template);
		this.elemContent = $("#contentBox",divBox)[0];
		this.elem = $("#jsBoxRedeSocial",divBox)[0];
		$("body").append(divBox);
		Utils.executeAjax({type:"get", url:"/inc/boxRedeSocial.jsp"},this,a);
	}
	this.open = function(a){
		this.status = "open";
		if(!this.ready) this.build(a);
		this.execPosition();
		$(this.elem).slideDown(300);
	}
	this.close = function(){
		if (this.status == "open") {
			this.status = "close";
			$(this.elem).slideUp(300);
		}
	}
}

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;
}

Utils.locationSearch = function(term){
	var search = window.location.search.replace(/\?/g,"").split("&");
	
	for(i=0; search.length>i ;i++){
		var searchName = search[i].split("=")[0];
		var searchVal = search[i].split("=")[1];
		
		if(searchName == term){
			return searchVal;
		}
	}
	return "";
}

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)));
	}
}