var TINY={};

var sl;

function $(i){return document.getElementById(i)}
function $$(e,p){p=p||document; return p.getElementsByTagName(e)}

TINY.scroll=function(){
	return{
		init:function(e,d,s){
			e=typeof e=='object'?e:$(e); clearInterval(e.si); var p=e.style.top||TINY.style.val(e,'top'); e.style.top=p;
			var l=d==1?parseInt(e.offsetHeight)-parseInt(e.parentNode.offsetHeight):0; e.si=setInterval(function(){TINY.scroll.mv(e,l,d,s)},20)
		},
		mv:function(e,l,d,s){
			var c=parseInt(e.style.top); if(Math.abs(c)==l){TINY.scroll.cl(e)}else{var i=Math.abs(l+c); i=i<s?i:s; var n=c-i*d; e.style.top=n+'px'}
		},
		cl:function(e){e=typeof e=='object'?e:$(e); clearInterval(e.si)}
	}
}();

TINY.style=function(){
	return{
		val:function(e,p){e=typeof e=='object'?e:$(e); return e.currentStyle?e.currentStyle[p]:document.defaultView.getComputedStyle(e,null).getPropertyValue(p)}
	}
}();

TINY.alpha=function(){
	return{
		set:function(e,a,s){
			e=typeof e=='object'?e:$(e); var o=e.style.opacity||TINY.style.val(e,'opacity'),
			d=a>o*100?1:-1; e.style.opacity=o; clearInterval(e.ai); e.ai=setInterval(function(){TINY.alpha.tw(e,a,d,s)},20)
		},
		tw:function(e,a,d,s){
			var o=Math.round(e.style.opacity*100);
			if(o==a){
				clearInterval(e.ai); if(o==0&&d==-1){e.style.display='none'}
			}else{
				var n=o+Math.ceil(Math.abs(a-o)/s)*d; e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'
			}
		}
	}
}();

TINY.ajax=function(){
	var x;
	return{
		sync:function(u){
  			x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
			x.open('GET',u,false);
			x.send(null);
			return x.responseText
		},
		async:function(u){
			x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
			x.onreadystatechange=rv;
			x.open('GET',u,true);
			x.send(null)
		},
		rv:function(){
			x.readyState==4?r=x.status==200?x.responseText:'Problem Retrieving XML Data':''
		}
	}
}();

TINY.page=function(){
	return{
		width:function(){
			return window.innerWidth!=null?window.innerWidth:document.documentElement&&document.documentElement.clientWidth?document.documentElement.clientWidth:document.body!=null?document.body.clientWidth:null
		},
		height:function(){
			return window.innerHeight!=null?window.innerHeight:document.documentElement&&document.documentElement.clientHeight?document.documentElement.clientHeight:document.body!=null? document.body.clientHeight:null
		},
		top:function(){
			return typeof window.pageYOffset!='undefined'?window.pageYOffset:document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop?document.body.scrollTop:0
		},
		left:function(){
			return typeof window.pageXOffset!='undefined'?window.pageXOffset:document.documentElement&&document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft?document.body.scrollLeft:0
		}
	}
}();

var SWITCH={};
var current=window.location.toString();

SWITCH.event=function(){
	var x,t;
	return{
		popup:function(u,z){
			t=z;
			x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
			x.onreadystatechange=SWITCH.event.rv;
			x.open('GET',u,true); x.send(null)
		},
		rv:function(){
			x.readyState==4&&x.status==200?SWITCH.event.sh(x.responseText):''
		},
		sh:function(j){
			var p,c,m;
			if(!$('popup')){
				p=document.createElement('div'); p.id='popup';
				c=document.createElement('div'); c.id='popupcontent';
				m=document.createElement('div'); m.id='popupmask';
				$('content').appendChild(m); $('content').appendChild(p); p.appendChild(c)
			}else{
				p=$('popup'); c=$('popupcontent'); m=$('popupmask')
			}
			var h=TINY.page.height(), x=TINY.page.left(),
			v=h>800?(h/3)-160:(h/2)-160, b=x+167; p.style.top=v>10?v+'px':10; p.style.left=b>10?b+$('content').offsetLeft+'px':10; c.innerHTML=j;
			if(t){
				sl=new TINY.slider();
    			sl.init("slider",568)
			}
			m.style.width='934px'; m.style.height=$('content').offsetHeight+'px'; p.style.display=m.style.display='block';
			p.style.opacity=0; p.style.filter='alpha(opacity=0)';
			TINY.alpha.set(m,80,2); TINY.alpha.set(p,100,2)
		},
		hd:function(i){
			var p=$('popup'); clearTimeout(p.tw); TINY.alpha.set(p,0,2);
			var m=$('popupmask'); clearTimeout(m.tw); TINY.alpha.set(m,0,2);
		},
		post:function(u,e){
			if($('title').value==''){
				alert('You must enter a title');
			}else{
				x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=SWITCH.event.pv;
				var p='title='+SWITCH.event.enc('title');
				p+='&location='+SWITCH.event.enc('location');
				p+='&description='+SWITCH.event.enc('desc');
				if($('allday').checked){
					p+='&allday=1';
				}
				var y=$('year').value, m=$('month').value, d=$('day').value,
				sh=$('starthour').value, eh=$('endhour').value;
				sh=$('startperiod').value=='pm'&&sh!='12'?parseInt(sh,10)+12:sh;
				eh=$('endperiod').value=='pm'&&eh!='12'?parseInt(eh,10)+12:eh;
				p+='&start='+y+'-'+m+'-'+d+' '+sh+':'+$('startminute').value+':00';
				p+='&end='+y+'-'+m+'-'+d+' '+eh+':'+$('endminute').value+':00';
				if($('cat1')&&$('cat1').value!=''){p+='&cat1='+$('cat1').value}
				if(e){p+='&event='+$('event_id').value}
				if($('view').checked){
					p+='&view=1';
				}
				x.open("POST",u,true);
				x.setRequestHeader("Content-type","application/x-www-form-urlencoded");
				x.send(p)
			}
		},
		pv:function(){
			if(x.readyState==4&&x.status==200){
				$('eventform').innerHTML=x.responseText;
				SWITCH.calendar.update($('currenturl').innerHTML.replace('&amp;','&'))
			}
		},
		enc:function(i){
			return encodeURIComponent(document.getElementById(i).value)
		},
		edit:function(u){
			x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
			x.onreadystatechange=SWITCH.event.ev;
			x.open('GET',u,true); x.send(null)
		},
		ev:function(){
			if(x.readyState==4&&x.status==200){
				$('popupcontent').innerHTML=x.responseText;
				sl=new TINY.slider();
    			sl.init("slider",568)
			}
		},
		del:function(u){
			x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
			x.onreadystatechange=SWITCH.event.dv;
			x.open('GET',u,true); x.send(null)
		},
		dv:function(){
			if(x.readyState==4&&x.status==200){
				$('description').innerHTML=x.responseText;
				SWITCH.calendar.update($('currenturl').innerHTML.replace('&amp;','&'))
			}
		}
	}
}();

SWITCH.calendar=function(){
	var x;
	return{
		update:function(u){
			x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
			x.onreadystatechange=SWITCH.calendar.cv;
			x.open('GET',u,true); x.send(null);
			current=u
		},
		cv:function(){
			x.readyState==4&&x.status==200?SWITCH.calendar.ch(x.responseText):''
		},
		ch:function(j){
			$('calendar').innerHTML=j
		},
		print:function(){
			var q=(current.indexOf("?")==-1)?'':current.substring(current.indexOf('?'), current.length);
			var u='print.php'+q;
			window.open(u,'Print','height=450,width=750,status=0,toolbar=0,scrollbars=1') 
		}
	}
}();

SWITCH.search=function(){
	return{
		submit:function(f,e){
			var k=window.event?window.event.keyCode:e.which;
			if(k==13){
				var v=$('query').value;
				if(v==''){
					alert('Your must enter a query.')
				}else if(v.length<3){
					alert('Your must enter at least 3 characters.')
				}else{
					SWITCH.calendar.update('calendar-results.php?q='+v)
				}
			}
		}
	}
}();

SWITCH.height=function(){
	return{
		set:function(e,h,s,d){
			e=typeof e=='object'?e:$(e); var oh=e.offsetHeight, ho=e.style.height||TINY.style.val(e,'height');
			ho=oh-parseInt(ho); var hd=oh-ho>h?-1:1; clearInterval(e.si); e.si=setInterval(function(){SWITCH.height.tw(e,h,ho,hd,s)},20)
		},
		tw:function(e,h,ho,hd,s){
			var oh=e.offsetHeight-ho;
			if(oh==h){clearInterval(e.si)}else{if(oh!=h){e.style.height=oh+(Math.ceil(Math.abs(h-oh)/s)*hd)+'px'}}
		}
	}
}();

function changeTab(p,x,s){
	var t=$('toggle'), o=$$('li',t), l=o.length, p=$(p);
	if(p.pos&&s==p.sel){
		SWITCH.height.set(p,0,5,-1); p.pos=p.sel=false;
	}else{
		$(x+'-'+s).style.display='block';
		var h=parseInt($(x+'-'+s).offsetHeight)+16;
		SWITCH.height.set(p,h,5,1);
		p.pos=true; p.sel=s
	}
	for(var i=0; i<l; i++){
		if(i==s&&p.pos){
			o[i].className='selectedtoggle';
		}else{
			o[i].className='toggle';
			$(x+'-'+i).style.display='none';
		}
	}
}

function loadEvents(){
	var q=$('query');
	q.onfocus=q.onmouseover=new Function("this.className='searchselected'");
	q.onclick=new Function("if(this.value=='Search Events'){this.value=''}");
	q.onblur=new Function("if(this.value==''){this.value='Search Events'}");
	q.onmouseout=new Function("this.className='search'");
	var t=$('toggle');
	t.pos=t.sel=false;
	var t=$('toggle'), o=$$('li',t), l=o.length;
	for(var i=0; i<l; i++){
		o[i].onclick=new Function("changeTab('tools','toolscontent',"+i+")")
	}
	t=$('toolscontent'); o=$$('div',t); l=o.length;
	for(i=0; i<l; i++){
		o[i].style.display='none';
	}
	changeTab('tools','toolscontent',0)
}

TINY.slider=function(){this.c=0};

TINY.slider.prototype={
  init:function(x,s){
    this.u=document.getElementById(x); this.z=s; var i=this.u.getElementsByTagName('li');
		this.l=i.length; document.getElementById(x).style.width=s*this.l+'px';
		this.u.style.left=this.u.style.top=0
	},
	pos:function(i){
		if(i!=this.c){
			clearInterval(this.u.int); var f=i>this.c?1:-1, o=i*this.z; this.c=i;
			this.u.int=setInterval(function(){sl.left(o*-1,f)},20)
		}
	},
	left:function(t,d){
		var o=parseInt(this.u.style.left);
		if(o==t){clearInterval(this.u.int)}else{this.u.style.left=o-Math.ceil(Math.abs(t-o)/10)*d+'px'}
	}
};

window.onload=loadEvents;
