﻿function EmptyObject(){}
var _extend=new Function('var destination=arguments[0], source=arguments[1];for(var property in source)destination[property]=source[property]; return destination;');
if(typeof RDA == 'undefined') {
  var RDA = {}  
}
RDA.baseURL='/js/';
RDA.debug = false;
RDA.dump = function(data) {
  if(typeof data == 'object') {
	var res = [];
	for (var i in data)
	  res.push(i+': '+data[i]);
	res = 'OBJECT\n\n'+res.join(' || ');
  } else
  if(typeof data == 'String') {
	var res = 'STRING:\n\n'+data;
  } else {
    var res = (typeof data);
    res = res.toUpperCase() + ':\n\n'+data;
  }
  
  return res;
}
RDA.addEvent=function(func,evnt,object) {
  if(!object) object = window;
  var oldonload = object[evnt];
  if (typeof object[evnt] != 'function') {
    object[evnt] = func;
  } else {
	object[evnt] = function() { oldonload(); func(); }
  }
}
RDA.getEventSource = function(ev) {
  ev=ev||window.event;var res=(ev)?((ev.target)?ev.target:ev.srcElement):window.event.srcElement;
  return res;
}
function Class(data){
  var ret = new EmptyObject();
  ret.$extend=new Function('var destination=arguments[0], source=arguments[1];for(var property in source)destination[property]=source[property]; return destination;');
  ret.setProperty=function(param,value) {
    if(value!=null && typeof value!='undefined')
	  this[param]=value;
  };
  return ret.$extend(ret,data||{});
}
Class.$extend=Function('var destination=arguments[0], source=arguments[1];for(var property in source)destination[property]=source[property]; return destination;');
Class.require = function() {
  for(var i=0; i<arguments.length;i++) {
    var library = arguments[i];
	if(!RDA.LIBS.exists(library)) {
      document.write('<script type="text/javascript" src="'+RDA.baseURL+library+'.js"></script>\n');
	  RDA.LIBS.push(library);
	}
  }
}

var $ = function() {
  var elements = new Array();
  for (var i=0;i<arguments.length;i++) {
    var element=arguments[i];
    if (typeof element == 'string') element=document.getElementById(element);
    if (arguments.length == 1) return element;
    elements.push(element);
  }
  return elements;
}

var $$ = function(tag){return document.createElement(tag);}

Function.prototype.foreach = function(object, block, context) {
  for (var key in object) {
    if (typeof this.prototype[key] == "undefined") {
      block.call(context, object[key], key, object);
    }
  }
};

/* from http://dean.edwards.name/weblog/2006/07/enum/ */
foreach = function(object, block, context) {
  if (object) {
    var resolve = Object; // default
    if (object instanceof Function) {
      resolve = Function;
    } else if (object.foreach instanceof Function) {
      object.foreach(block, context);
      return;
    } else if (typeof object.length == "number") {
      resolve = Array;
    }
    resolve.foreach(object, block, context);
  }
};
String.prototype.trim = function(){var str = this;if(str == null || str == 'undefined' || typeof str == 'undefined') return ''; return str.replace(/(^\s*)|(\s*$)/g,'');}
Class.$extend(Array.prototype,{
  popValue: function(value) {
	if(this.length==0) return [];
	var newList = new Array();
	for(var i=0;i<this.length;i++) {
		if(this[i] != value) {
			newList.push(this[i]);
		}
	}
	this.length=0;
	for(var i=0;i<newList.length;i++) {this.push(newList[i]);}
	return newList;
  },
  popIndex: function(index) {
	if(this.length==0) return [];
	var newList = new Array();
	for(var i=0;i<this.length;i++) {
	  if(i != index) newList.push(this[i]);
	}
	this.length=0;
	for(var i=0;i<newList.length;i++) {this.push(newList[i]);}
	return newList;
  },
  clean: function() {
	if(this.length==0) return [];
	var newList = new Array();
	for(var i=0;i<this.length;i++) {
		if(this[i] != null || this[i] != '') {
	 		newList.push(this[i]);
		}
	}
	this.length=0;
	for(var i=0;i<newList.length;i++) {this.push(newList[i]);}
	return newList;
  },
  exists: function(value) {
	if(this.length==0) return false;
	var exists = false;
	for(var i=0;i<this.length;i++) {
	  if(this[i] == value) {exists=true;break;}
	}
	return exists;
  },
  first:function(){
	return this.length>0?this[0]:null;
  },
  last:function(){
	return this.length>0?this[this.length-1]:null;
  },
  'index_of': function(item) {
	for(var i=0;i<this.length;i++) {
	  if(this[i]==item) {break; return i;}
	}
	return null;
  },
  'foreach':function(processor) {
	for(var i=0;i<this.length;i++)
	  processor(this[i]);
	return this;
  },
  map: function(iterator) {
    var results = [];
	for(var i=0;i<this.length;i++) {
	  results.push( iterator(this[i],i) );
	}
    return results;
  }
});

RDA.LIBS = new Array();

/*

SCRIPTLOADER: Loads a group of instructions after the page loads.

*/
RDA.scriptLoader = function() {}
RDA.scriptLoader.list = new Array();
RDA.scriptLoader.run = function () {
	var list = RDA.scriptLoader.list;
	list.foreach(function(f){f();});
}
RDA.scriptLoader.init = function() {
	if(document.all) {
	  setTimeout("RDA.scriptLoader.run();",1500);
	} else {
	  RDA.scriptLoader.run();
	}
}
window.onload = RDA.scriptLoader.init;
RDA.scriptLoader.add = function(f){if(typeof f=='function') RDA.scriptLoader.list.push(f);}

var Cookies = {
	setCookie:function(pA,pB,days) {
	  var dt=new Date();
	  var cookie="";
	  if(days>0){
		dt.setTime(dt.getTime()+days*24*360000);
		cookie="; expires="+dt.toGMTString();
	  }
	  document.cookie=pA+"="+pB+cookie;
	},
	getCookie:function(cookie){
	  if(document.cookie){
		var st=document.cookie.indexOf(cookie);
		if(st>-1){
		  st=st+cookie.length+1;
		  var end=document.cookie.indexOf(";",st);
		  if(end==-1) end=document.cookie.length;
		  return unescape(document.cookie.substring(st,end));
	    }
	  }
	  return null;
	}
}

function Assigned(property) {
  return (typeof property != 'undefined' && property != null);
}

Number.random = function() {
  return Math.ceil(Math.random()*999999);
}
String.cut = function(str,size) {
  if(str.length>size) return str.substr(0,size-3)+'...'; else return str;
}

function Styler() {}
Styler.prototype = {
  tag:null,
  apply:function(data) {
	if(data && this.tag && this.tag.style) {
	  for(var k in data)
		this.tag.style[k] = data[k];
    } else return false;
  },
  applyCSS:function(text) {
	if(text == '') return false;
	var style = document.createElement('style');
	style.type='text/css';
	var k = document.all?'innerText':'innerHTML';
	if(this.tag != null) {	  
	  if(this.tag.id=='') this.tag.id = 'randID_' + Number.random();
	  style[k] = '#'+this.tag.id+' {'+text+'}';
	} else {
	  style[k] = text;
	}
	document.body.appendChild(style);
	
  },
  addClass:function(name) {
	if(!this.tag) return false;
	var list = this.tag.className;
	if(list.indexOf(' ',0)>0) list = list.split(' '); else list = [list];
	if(!list.exists(name)) list.push(name);
	this.tag.className = list.join(' ');
  },
  removeClass:function(name) {
	if(!this.tag) return false;
	var list = this.tag.className;
	if(list.indexOf(' ',0)>0) list = list.split(' '); else list = [list];
	while(list.exists(name)) list = list.popValue(name);
	this.tag.className = list.join(' ');
  },
  haveClass:function(name) {
	if(!this.tag) return false;
	var list = this.tag.className || '';
	if(list.indexOf(' ',0)>0) list = list.split(' '); else list = [list];
	return list.exists(name);
  }
}

var Style = function(element) {
  element = $(element);
  var s = new Styler();
  s.tag = element;
  return s;
}

var Messages = {
  FClose: function(){
	if(Messages.last!=null && Messages.list.length>0) {
	  window.clearTimeout(Messages.last.tAutoClose);	  
	  Messages.list.shift();
	}
  },
  infoproto:{
	object:null,
	isRunning:false,	
	text:'',
	autoClose:false,
	delay:4000,
	tAutoClose: null,
	background:'#FFC',
	foreground:'#600',
	border:'#666',
	icon:'',
	exec:function() {
	  this.object = document.createElement('div');
	  this.object.id = 'line-message'+Math.ceil(Number.random());
	  this.object.onclick = new Function('var v=$("'+this.object.id+'");v.parentNode.removeChild(v);Messages.FClose();');
	  var anc = document.createElement('a');
	  var txtobj = document.createElement('div');
	  txtobj.innerHTML=this.text;
	  Style(txtobj).apply({float:'left'});
	  var closefunc = new Function('var v=$("'+this.object.id+'");v.parentNode.removeChild(v);Messages.FClose();');
/*	  var btnclose = document.createElement('button');
	  Style(btnclose).apply({background:'none',color:'#999',fontWeight:'bold',fontSize:'12px',border:0,float:'right',display:'block'});
	  btnclose.innerHTML = '&times;';
	  this.object.appendChild(btnclose);*/
	  this.object.appendChild(txtobj);
	  var auto = function(width,sample) {
		var nsz = Math.ceil((sample-width)/2);
		return nsz>0?nsz:0;
	  }
	  var ww = Objects.getPageviewSize().pageWidth;
	  var top = Objects.getPageScroll().yScroll;
	  if(top>0) top+=20; else top=20;
	  Style(this.object).apply({width:(this.text.length*10+30)+'px',top:top+'px',background:this.background,padding:'10px',color:this.foreground,fontSize:'8pt',fontWeight:'bold',position:'absolute',left:(auto(300,ww)+'px'),border:'1px solid '+this.border});
	  if(this.icon!='') Style(this.object).apply({background:this.background+' url('+this.icon+') no-repeat 5px center',paddingLeft:'30px'});
	  
	  if(this.autoClose==true) this.tAutoClose = window.setTimeout(closefunc,this.delay);
	  document.body.insertBefore(this.object,document.body.firstChild);
	  this.isRunning = true;
	  Messages.last=this;
	}
  },
  msgproto: {
	object:null,
	isRunning:false,
	icon:'',
	text:'',
	winID:null,
	autoClose:false,
	delay:3000,
	tAutoClose: null,
	background:'#FFFFCC',
	foreground:'#000000',
	exec:function() {
	  var win = WindowManager.create({W:550,H:200,T:'auto',L:'auto',title:'Mensagem',onDestroy:Messages.FClose,closeable:true});
	  this.winID = win.ID;
	  this.object = document.createElement('div');
	  var txt = document.createElement('div');
	  txt.innerHTML = this.text;
	  Style(txt).apply({background:this.background,height:'70px',padding:'10px',margin:'0',color:this.foreground,fontSize:'8pt'});
	  if(this.icon!='') Style(txt).apply({background:this.background+' url('+this.icon+') no-repeat 10px center',paddingLeft:'50px'});
	  this.object.appendChild(txt);
	  win.client.appendChild(this.object);
	  var top = Objects.getPageScroll().yScroll || 50;
	  if(top>0) top+=50;
	  Style(win.client).apply({height:'auto',top:top+'px',background:'#FFFFFF',color:this.foreground,padding:'0',fontSize:'9pt',fontFamily:'Verdana,Arial,Helvetica'});
	  if(this.autoClose==true) this.tAutoClose = window.setTimeout(function(){Windows.destroy(Messages.last.winID);Messages.FClose();},this.delay);
	  var cmdbar = document.createElement('div');
	  Style(cmdbar).apply({padding:'10px'});
	  cmdbar.style.textAlign='center';
	  var but = document.createElement('input');
	  but.className = 'cmdbutton';
	  but.type = 'button';
	  but.value = ' OK ';
	  but.onclick = function(){Windows.destroy(Messages.last.winID);Messages.FClose;}
	  cmdbar.appendChild(but);
	  win.client.appendChild(cmdbar);
	  this.isRunning = true;
	  Messages.last=this;	  
	}
  },
  last:null,
  list:[],
  verify:function(){
	if(Messages.list.length>0 && Messages.list[0].isRunning==false) {
	  Messages.list[0].exec();	  
    }
  },
  _message:function(data) {
	var m = Class(Messages.msgproto);
	Class.$extend(m,data);
	if(Messages.verifier==null) Messages.verifier = setInterval(Messages.verify,250);
    Messages.list.push(m);
  },
  infobox:function(value) {
	var m = Class(Messages.infoproto);
	var data = {text:value,autoClose:true,foreground:'#235',background:'#FFF',border:'#F60'};
	Class.$extend(m,data);
	if(Messages.verifier==null) Messages.verifier = setInterval(Messages.verify,250);
    Messages.list.push(m);
  },
  warnbox:function(value) {
	var m = Class(Messages.infoproto);
	var data = {text:value,icon:'/img/warning16.gif',autoClose:true,foreground:'#FF6600',background:'#FFFFCC'};
	Class.$extend(m,data);
	if(Messages.verifier==null) Messages.verifier = setInterval(Messages.verify,250);
    Messages.list.push(m);
  },
  warn:function(value) {
	Messages._message({text:value,icon:'/img/message-warning32.gif',autoClose:true,foreground:'#FF6600',background:'#FFFFCC'});
  },
  error:function(value) {
	Messages._message({text:value,icon:'/img/message-error32.gif',autoClose:true,foreground:'#000000',background:'#FFCCCC'});
  },
  info:function(value) {
	Messages._message({text:value,icon:'/img/message-info32.gif',autoClose:true,foreground:'#FF6600',background:'#FFFFFF'});
  }
}

