/* Simple Alert */
var SA = {
	'created': 0,
	'opened': 0,
	'size': null,
	'docSize': null,
	'scroll': null,
	'shakeEnabled': false,
	'open': function(tit,txt,bts) {
		this.create();
		var sBts = bts.split("|");
		var htmlBts = "";
		for(var i in sBts){
			htmlBts += '<img alt="" src="imagens/alerta'+sBts[i]+'.gif" onclick="SA.click(\''+sBts[i]+'\');" style="margin:2px;cursor:pointer;" />';
		}		
		$('SATitulo').innerHTML = tit;
		$('SAButtons').innerHTML = htmlBts;
		$('SAText').innerHTML = txt;
		
		if (this.opened == 0) {
			shakeEnabled=false;
			this.reset();
			$('SA').style.top = (this.scroll.y-this.size.y)+'px';
			$('block').style.height = getDocSize().y+13+'px';
			$('block').style.display = 'block';
			var x = (this.docSize.x-this.size.x)/2+this.scroll.x;
			var y = (this.docSize.y-this.size.y)/2+this.scroll.y;
			//$('SA').tweenTo(y,x,'easeoutexpo',0.35,function(){shakeEnabled=true;});
			$('SA').style.top = y+'px';
			$('SA').style.left = x+'px';			
			this.opened = 1;
		} else {
			this.refresh(true);
			this.shake();
		}
	},
	'close': function() {
		if (this.opened == 1) {
			this.opened = 0;
			shakeEnabled=false;
			var x = (this.docSize.x-this.size.x)/2;
			var y = this.docSize.y+this.scroll.y;
			//$('SA').tweenTo(y,x,'easeinexpo',0.35,function(){SA.reset();SA.onClose();});
			SA.reset();
			//$('SA').style.top = y+'px';
			//$('SA').style.left = x+'px';			
			$('block').style.display = 'none';
		}
	},
	'reset': function() {
		this.size = getSize('SA');
		this.docSize = getDocVisibleSize();
		this.scroll = getScroll();
		var x = (this.docSize.x-this.size.x)/2;
		var y = (this.size.y)*(-1);
		if (this.opened == 0) {
			$('SA').style.top = y+'px';
			$('SA').style.left = x+'px';
		}
	},
	'refresh': function(type) {
		type = type || false;
		if (this.opened == 1) {
			shakeEnabled=false;
			this.size = getSize('SA');
			this.docSize = getDocVisibleSize();
			this.scroll = getScroll();
			var x = (this.docSize.x-this.size.x)/2+this.scroll.x;
			var y = (this.docSize.y-this.size.y)/2+this.scroll.y;
			if (type == false) {
				//$('SA').tweenTo(y,x,'easeoutexpo',0.25,function(){shakeEnabled=true;});
				$('SA').style.top = y+'px';
				$('SA').style.left = x+'px';				
			} else {
				setPosition($('SA'),y,x);
			}
		}
	},
	'create': function() {
		if (this.created == 0) {
			new tween('SA');
			new shake('SA');
			$('SA').className = 'SA';
			this.created = 1;
		}
	},
	'shake': function() {
		if (this.opened == 1 && shakeEnabled == true) {
			shakeEnabled = false;
			$('SA').shakeNow(0.5,function(){shakeEnabled=true;});
		}
	},
	'click': function(a) {
		this.close();
		this.onClick(a);
	},
	'onClose': function(){},
	'onClick': function(){}
};
/* Shake */
shake = function () {
	var obj = $(arguments[0]);
	var dist = 4;
	var moving = false;
	obj.numStepShake = 25;
	obj.thisIntervalShake = null;
	obj.onEnterFrameShake = function() {
		obj.numTimeShake += obj.numStepShake;
		var posX = Math.round((Math.random()*dist*2-dist)+obj.numInicioXShake);
		var posY = Math.round((Math.random()*dist*2-dist)+obj.numInicioYShake);
		setPosition(obj, posX, posY);
		if (obj.numTimeShake>=obj.numDurationShake) {
			setPosition(obj, obj.numInicioXShake, obj.numInicioYShake);
			clearInterval(obj.thisIntervalShake);
			obj.callbackShake();
			moving = false;
		}
	};
	obj.shakeNow = function() {
		if (moving == true) {
			obj.stopShake();
		}
		var pos = getPos(obj);
		this.numTimeShake = 0;
		this.numInicioXShake = pos.y;
		this.numInicioYShake = pos.x;
		this.numDurationShake = (arguments[0]) ? arguments[0]*1000 : 1000;
		this.callbackShake = arguments[1] || function () {
		};
		obj.thisIntervalShake = setInterval(obj.onEnterFrameShake, obj.numStepShake);
		moving = true;
	};
	obj.stopShake = function() {
		clearInterval(obj.thisIntervalShake);
		setPosition(obj, obj.numInicioXShake, obj.numInicioYShake);
	};
};