// Fx.Rotate
(function(){Fx.Rotate=new Class({Extends:Fx,options:{},regex:/rotate\((\-?[\d\.]+)deg\)/i,initialize:function(b,a){this.element=this.subject=$(b);this.parent(a)},set:function(a){var b=(Browser.Engine.webkit)?"WebkitTransform":"MozTransform";this.element.setStyle(b,"rotate("+a+"deg)");return this},start:function(b,a){if(arguments.length==1){a=b;b=this.getRotation()}return this.parent(b,a)},getRotation:function(){var a=(Browser.Engine.webkit)?this.element.getStyle("WebkitTransform"):this.element.getStyle("MozTransform");return(!$chk(a)||a=="none")?0:this.regex.exec(a)[1].toInt()},rotate:function(b,a){var c=this.getRotation(),d=90,a=$defined(a)?a:true;switch(b){case"ccw":d=(c%90==0)?c-90:(c/90).floor()*90;break;case"cw":default:d=(c%90==0)?c+90:(c/90).ceil()*90}if(a){this.start(d)}else{this.set(d)}},spin:function(a){this.toggleSpin(a)},startSpin:function(a){this.oldTransition=this.options.transition;this.options.transition=Fx.Transitions.linear;if($chk(a)){this.oldDuration=this.options.duration;this.options.duration=a}this.cycle();this.spinning=true},stopSpin:function(a){this.cancel();this.options.transition=this.oldTransition;if($chk(a)){this.options.duration=this.oldDuration}this.spinning=false},toggleSpin:function(a){if(this.spinning){this.stopSpin(a)}else{this.startSpin(a)}},cycle:function(){this.start(0,360).chain(function(){this.cycle()}.bind(this))}})})();
function logout(){
	if(confirm(logout_text)){
		document.cookie = 'login=0; expires=0; path=/';
		$('logged-out').setStyle('display','inline');
		$('logged-in').setStyle('display','none');
		openPage();
	}
}
function move(w){
	alert(w);
}
var notifications=0;
var reminders=0;
var logout_text="Are you sure you want to logout?";
var notification_text="Are you sure you want to delete this notification?";
var reminder_text="Are you sure you want to delete this reminder?";
var no_notification="You have no notifications";
var no_reminder="You have no reminders";
function Remove(w,n){
	new Request({
		url: 'index.php',
		data: 'delete='+n,
		method: 'post',
		onSuccess: function(){
	if(w=="n"){
		notifications--;
		if(notifications==0){
			setTimeout("$('notifications').set('html','<li><span>"+no_notification+"</span></li>');",1000);
		}
	}
	if(w=="r"){
		reminders--;
		if(reminders==0){
			setTimeout("$('reminders').set('html','<li><span>"+no_reminder+"</span></li>');",1000);
		}
	}
	var rotate = new Fx.Rotate(w+n+"_icon", {
	    duration: 500,
	    transition: Fx.Transitions.Back.easeOut
	});
	rotate.start(90);
	var tween = new Fx.Tween($(w+n), {
	    duration: 500,
	    transition: Fx.Transitions.Quart.easeOut,
	    link: 'chain'
	});
	tween.start('opacity','1','1').start('width','300px','0px').start('height','25px','0px');
	}
	}).send();
}
function Delete(w,n,x){
	if(w=="n"){
		var text=notification_text;
	}
	if(w=="r"){
		var text=reminder_text;
	}
	if(confirm(text)){
		Remove(w,n,x);
	}
}
function fav(icon) {
    var link = document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'shortcut icon';
    link.href = icon;
    document.getElementsByTagName('head')[0].appendChild(link);
}
function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
window.addEvent('domready', function(){
	var stepping=false;
	function stepper(){
		$$('input[type=numeric]').each(function(el){
			if(el.get('ajax')!='true'){
				var newElement = new Element('span', {class: 'numeric-stepper'});
				var maxlength=el.get('max').length;
				var size=maxlength-1;
				var value=el.get('value');
				var min=el.get('min');
				var dis=el.get('disabled');
				var name=el.get('name');
				if(value==""){value="0";} if(min==null){min="0";} if(dis==""){dis="DISABLED";}
				var inside='<div></div><input desc='+el.get('desc')+' '+dis+' name="'+name+'" size="'+size+'" maxlength="'+maxlength+'" min="'+min+'" max="'+el.get('max')+'" type="text" value="'+value+'" /><button value="+'+el.get('increase')+'" class="plus" tabindex="-1" onclick="return false;"></button><button value="-'+el.get('increase')+'" class="minus" tabindex="-1" onclick="return false;"></button>';
				newElement.set('html',inside);
				newElement.getElement('div').setStyle('width',maxlength*16+'px');
				newElement.inject(el,'after');
				el.setStyle('display','none');
				el.set('ajax','true');
			}
		});
		$$('.numeric-stepper').each(function(el){
			if(el.get('ajax')!='true'){
				el.getElements('button').addEvent('focus', function(){
					function step(){
						var newValue=eval($(el.getElement('input').get('name')).get('value')+this.get('value'));
						if(newValue<el.getElement('input').get('min')){
							newValue=el.getElement('input').get('min');
						}
						if(newValue>el.getElement('input').get('max')){
							newValue=el.getElement('input').get('max');
						}
						if(newValue>=el.getElement('input').get('min') && newValue<=el.getElement('input').get('max')){
							$(el.getElement('input').get('name')).set('value',newValue);
							el.getElement('input').set('value',newValue);
						}
					}
					stepping=step.periodical(50,this);
				});
				el.getElements('button').addEvent('blur', function(){
					$clear(stepping);
				});
				el.addEvent('contextmenu', function(e){
					e.stop();
					var p=prompt('Value of '+el.getElement('input').get('desc')+':',el.getElement('input').get('value'));
					if(p>=el.getElement('input').get('min') && p<=el.getElement('input').get('max') && p.toInt()==p){
						$(el.getElement('input').get('name')).set('value',p);
						el.getElement('input').set('value',p);
					}
				});
				el.set('ajax','true');
			}
		});
	}
	$('body').addEvent('mouseup', function(){
		$$('button').each(function(el){
			el.blur();
		});
	});
	stepper.periodical(100);
	function resize(){
		$('content').setStyle('min-height',$('side').getStyle('height'));
	}
	resize.periodical(100);
	if(!window.location.hash){
		window.location.hash="/dashboard";
	}
	var recentHash = "";
	var pageReq = new Request.HTML({});
	openPage=function(p) {
		fav('/assets/images/loading.gif');
		if(!p){
			p=recentHash;
		}
		pageReq.cancel();
		pageReq = new Request.HTML({
			url:'/index.php',
			method:'post',
			data:'page=' + p,
			onComplete: function(){
				fav('/assets/images/favicon.png');
			},
			onSuccess: function(HTML){
				$('content').set('text','');
				$('content').adopt(HTML);
			}
		}).send();
	}
	function pollHash() {
		if (window.location.hash==recentHash || window.location.hash=='') {
			return;
		}
		recentHash = window.location.hash;
		openPage(recentHash);
	}
	function forms(){
		$$('.form').each(function(el){
			if(el.get('ajax')!='true'){
				el.addEvent('submit', function(e) {
					fav('/assets/images/loading.gif');
					e.stop();
					new Request.HTML({
					url: 'index.php',
					data: this.toQueryString(),
					method: this.getProperty('method'),
					onComplete: function(){
						fav('/assets/images/favicon.png');
					},
					onSuccess: function(HTML){
						$('content').set('text','');
						$('content').adopt(HTML);
					}
					}).send();
				});
				el.set('ajax','true');
			}
		});
	}
	function setPage(){
		$$('.page').set('value',window.location.hash);
	}
	forms.periodical(100);
	pollHash.periodical(100);
	setPage.periodical(100);
	$(document.body).addEvent('click',function(e) {
		var id=e.target.getProperty('id');
		if(id=='tint'){
			closeLogin();
		}
	});
});
function shakeLogin(){
	alert('Incorrect username or password.');
}
function closeLogin(){
	new Fx.Tween($('login'), {duration: 500}).start('opacity','1','0');
	setTimeout("new Fx.Tween($('tint'), {duration: 500}).start('opacity','1','0')",400);
}
function login(){
	$$('#login input').set('value','');
	$('login').setStyle('background-image','url(/assets/images/login/bg.png)');
	$('login-password').setStyle('display','none');
	$('login-username').setStyle('display','none');
	$('login-login').setStyle('display','block');
}
function openLogin(){
	$$('#login input').set('value','');
	$('remember').set('value','1');
	$$('#login strong').setStyle('color','333');
	$('tint').setStyle('display','block');
	$('login').setStyle('display','block');
	new Fx.Tween($('tint'), {duration: 500}).start('opacity','0','1');
	setTimeout("new Fx.Tween($('login'), {duration: 500}).start('opacity','0','1')",400);
	login();
}
function forgot(w){
	$('login-login').setStyle('display','none');
	$('login-password').setStyle('display','none');
	$('login-username').setStyle('display','none');
	$('login-'+w).setStyle('display','block');
	$('login').setStyle('background-image','url(/assets/images/login/forgot-'+w+'.png)');
	$$('#login strong').setStyle('color','333');
	$$('#login input').set('value','');
	$('remember').set('value','1');
}