// JS By Abner Lus - EAY Design 2008
var redir = function(url){
	window.location = url;
}
var days = function(id, month, year, selected){
	month == 2 ? m=(year % 4 ? 28 : (year % 100 ? 29 : (year % 400 ? 28 : 29))) : m=((month - 1) % 7 % 2 ? 30 : 31);
	day = $('day'+id);
	day.innerHTML = '';
	day.options[0] = new Option('Todos',0);
	for(k=1;k<=m;k++){
		(selected == k) ? day.options[k] = new Option(k,k,true,true) : day.options[k] = new Option(k,k);
	}
}
var m_day = function(id){
	year = $('year'+id);
	month = $('month'+id);
	day = $('day'+id);
	var cal_dates = new Date();
	this_day = cal_dates.getDay();
	var yz = year.selectedIndex;
	var mz = month.selectedIndex;
	var dz = day.selectedIndex;
	days(id, month.options[mz].value, year.options[yz].text, day.options[dz].text);		
}
var iload = function(type,A,B,C){
	D='&id='+A+'&tid='+B;
	var request = new Request.JSON({
		url: 'getData.php',
		onRequest: function(){
			this.fireEvent('active',this);
		}.bind(C),
		onComplete: function(jsonObj) {
			showWin(jsonObj);
			this.fireEvent('success',this);
		}.bind(C)
	}).send('t='+type+D);
}
var ipost = function(obj,targ,type){
	objs = {0:obj, 1:targ, 2:type};
	obj.addEvent('submit',function(e){
		e.stop();
		R = new Request.HTML({
			url:'postData.php',
			onRequest: function(){
				ST = this[1][1].set('html','Enviando...');
				ST.tween('background-color','#FFFF00','#F7F7F7');
				ST.setStyle('color','blue');
			}.bind(this),
			onComplete: function(A,B,text){
				T = JSON.decode(text);
				ST = this[1][1];
				PS = this[1][0];
				if(T.error){
					ST.setStyles({color:'red',display:'block'});
					ST.tween('background-color','#FFFF00','#F7F7F7');
					ST.set('html',T.error);
				} else {
					ST = this[1][1].setStyles({color:'green',display:'block'});
					ST.tween('background-color','#FFFF00','#F7F7F7');
					ST.set('html',T.success);
					if(T.type==1){
						STE = T.message;
						PS.set('html','');
						A0 = new Element('h1',{'id':'head','html':STE.head}).inject(PS);
						STE.subject.length>0 ? A1 = new Element('h2',{'id':'subhead','html':STE.subject}).inject(PS) : '';
						B0 = new Element('div',{'id':'content'}).inject(PS);
						B1 = new Element('p',{'html':STE.message}).inject(B0);
						B2 = new Element('div',{'id':'author','html':'Por: <b>'+STE.name+'</b>'}).inject(PS);
						A0.tween('background-color','#FFFF00','#AA0000');
					}
				}
			}.bind(this)
		}).post(this[0]);
	}.bind(objs));
}
var showWin = function(data){
	
	ibody = $(document.body);
	ewindow = $(window);
	//Add Checker $chk
	$chk($('i_wback'))!=0 ? $('i_wback').destroy() : '';
	
	var wbck = new Element('div', {'id':'i_wback'});
	
	//Page Scroll Size
	bsizes = ibody.getScrollSize();
	by = bsizes.y;
	bx = bsizes.x;
	
	//Content Screen Size
	esizes = ewindow.getSize();
	ey = esizes.y;
	
	//Page Scroll
	ssizes = ibody.getScroll();
	sy=ssizes.y;
	if(sy<0){ sy=0 };//Firefox Fix
	
	wbck.setStyles({
		height: bsizes.y,
		opacity: 0
	});
	
	var my = Math.floor(by/2);
	var mx = Math.floor(bx/2);
	
	var win = new Element('div', {'id':'i_win'}).setStyle('overflow','visible');
	var tips = new Element('div',{'id':'i_tips','html':'Para <b>fechar</b> esta caixa <b>pressione ESC</b> ou <b>clique na área escurecida.</b>'}).inject(ibody,'top');

	if(data.type==1||data.type==0){
		var hd = new Element('h1',{'id':'hd','html':data.name}).inject(win,'top');
		if(data.img==1){var img = new Element('div',{'id':'evlogo'}).inject(win); var I = new Element('img',{'src':'images/events/avatar_'+data.id+'.jpg'}).inject(img)};

		var main = new Element('div',{'id':'main'}).inject(win);//Event data container
		
		data.ievent.each(function(me){//Parse event data
			var P = new Element('p',{'html':me.v}).inject(main);
			var H = new Element('h1',{'html':me.k}).inject(P,'top');
		});
		
		var dates = new Element('ul',{'id':'dates'}).inject(win);//Event dates container
		var B = new Element('li',{'html':'Data(s):','id':'hd'}).inject(dates);
		
		C=0;
		data.dates.each(function(me) {//Parse event dates
			var D = new Element('li',{'html':me.date,'class':(C==0?'a':'b')}).inject(dates);
			if(me.tid==data.tid){D.addClass('sel'); D.appendText(' *')}
			if(me.gid!=0){D.addClass('gid'); D.appendText(' '); E = new Element('img',{'src':'images/iwin_gal.gif'}).inject(D); D.addEvent('click',function(gid){redir('?area=galeria&s='+gid)}.pass(me.gid)); D.setStyle('cursor','pointer')}
			var F = new Element('span',{'html':'<br>('+me.hour+' H)'}).inject(D);
			C = C==0 ? 1 : 0;
		});
		var F = new Element('li',{'html':'<h1>Legenda:</h1><p><img src="images/iwin_gal.gif" alt="Ver Fotos do Evento" width="8" height="8" /> Ver galeria de fotos</p><p><b>* Data Selecionada</b></p>','class':'c'}).inject(dates);
		var mw = 500;
	} else if(data.type==2){
		var P = new Element('span',{'html':'<br>Você também pode <b>usar as setas</b> <- e -> <b>do seu teclado</b> para navegar entre as fotos.'}).inject(tips);
		tips.setStyle('height',36);
		var mw = 200;
		var mh = 25;
		win.setStyles({overflow:'hidden'});
		src = 'images/events/'+data.code+'/full/'+data.src;
		var load_img = new Asset.image(src,{id:'t_img',onload: function(){
			win = $('i_win');
			file = arguments[0];
			$('ld_st').destroy();

			var img = new Element('div',{'id':'c_img'}).inject(win,'top');
			var file = new Element('img',{'id':'c_pht','src':file}).inject(img);
			file.setStyle('zIndex',2000);
			
			var nav = new Element('div',{'id':'nav'}).inject(img);
			var gnext = new Element('img',{'id':'nav_nx','src':'images/dash_n.gif'}).inject(nav);
			var gback = new Element('img',{'id':'nav_bk','src':'images/dash_b.gif'}).inject(nav);
			var nav_pos = new Element('div',{'id':'nav_pos','html':'Código da foto: '+data.pid}).inject(nav);
			gback.setStyle('float','left');
			gnext.setStyles({float:'right',clear:'right'});

			//Navigation
			gnext.addEvent('click',function(){
				if(PI_POS>=PI_GAL.length-1){
					PI_POS = 0;
				} else {
					PI_POS++;
				}
				photo_load($('c_pht'),$('nav_pos'));
			});
			gback.addEvent('click',function(){
				if(PI_POS>0){
					PI_POS--;
				} else {
					PI_POS=PI_GAL.length-1;
				}
				photo_load($('c_pht'),$('nav_pos'));
			});

			nav.addEvents({
				'mouseenter':function(){this.set('tween',{duration:'short'});this.fade(1)},
				'mouseleave':function(){this.set('tween',{duration:'short'});this.fade(.001)}
			});
			
			imd = file.getSize();
			img.setStyle('opacity',0);

			mw = imd.x+4;//4 Due to Padding
			mh = imd.y+4;//20 Due to Nav
			
			ibody = $(document.body);
			
			A = ibody.getScroll();
			B = ibody.getScrollSize();
			
			pfx = new Fx.Morph($('i_win'));
			pfx.addEvent('complete',function(){
				this.fade(1);
			}.bind(img));
			pfx.start({
				'height': mh,
				'width': mw,
				'top': -180+Math.floor(A.y+B.y/2-mh/2),
				'left': Math.floor((-mw+B.x)/2)
			});
		}.pass(src)});
		if($chk('ld_st')==1) $$('#ld_st').destroy();//Remove All - Avoid Overlap
		var LD = new Element('center',{'id':'ld_st','html':'Aguarde<br>Carregando...'}).inject(win,'top');
	}//
	
	tips.set('tween', {duration: 'long'});
	tips.tween('background-color','#FFFF00','#000000');
	
	win.setStyles({width:mw,top:-5000,left:Math.floor(mx-mw/2),opacity:.001}).inject(ibody,'top');//Hide by negative high number and cacle height
	
	if(data.type==1||data.type==0){
		Z = {};
		OALI = sy+20;
		wsizes = main.getScrollSize();
		mh = wsizes.y+40;//Height of i_win
		if(mh<260) mh=260;
		Z.h = mh+5;//Bottom Padding
		ch = mh-40;
		OALI = OALI+(ey/2)-(mh/2); //Center to Screen
		OAL = by>mh+OALI ? (OALI>0 ? OALI : 20) : OALI-(OALI-by);
		OALTP = Math.floor(OAL);
	} else {
		OALI = sy-180;
		wsizes = win.getScrollSize();
		mh = wsizes.y;
		ch = 0;
		OALTP = Math.floor(OALI+my-(mh/2)); //Center to Whole Page
	}
	
	win.setStyles({height:mh-ch,top:OALTP,overflow:'hidden'});
	
	if(Browser.Engine.trident4==true){//IE6 Fix
		tsizes = tips.getSize();
		wbck.setStyle('top',tsizes.y);
	}
	
	wbck.inject(ibody,'top');
	wbck.addEvents({//Closer and Effects
		'click':function(){
			var fx0 = new Fx.Tween(win,{transition:Fx.Transitions.Sine.easeOut,duration:'short',fps:60});
			var fx1 = new Fx.Tween(wbck,{transition:Fx.Transitions.Sine.easeOut});
			fx0.addEvent('complete',function(){
				this.start('opacity',0);
				$('i_win').destroy();
			}.bind(fx1));
			fx1.addEvent('complete',function(){
				$('i_wback').destroy();
			});
			fx0.start('opacity',0);
			tips.destroy();
			A = $(document.body);
			A.removeEvents('mousewheel');
			document.removeEvents('keydown');
		}
	});
	
	//Auto-Align Functions
	ibody.addEvent('mousewheel',function(){
		if(arguments[0]==2){
			A = this.getScroll();
			B = this.getScrollSize();
			O = $('i_win');
			C = O.getSize();
			yPos = A.y<=(B.y-C.y) ? -180+A.y+B.y/2-(C.y/2) : B.y-C.y;
			O.tween('top',Math.floor(yPos));
		}
	}.bind(ibody,data.type));
	
	document.addEvent('keydown',function(event){//Navigation
		A=event.key;
		if(A=='up'||A=='down'){
			$(document.body).fireEvent('mousewheel');
		} else if(A=='esc'){
			$('i_wback').fireEvent('click');
		} else if(A=='right' && $chk($('nav'))==true){
			$('nav_nx').fireEvent('click');
		} else if(A=='left' && $chk($('nav'))==true){
			$('nav_bk').fireEvent('click');
		}
	});
	
	var fx2 = new Fx.Tween(wbck,{transition:Fx.Transitions.Sine.easeOut,duration:'short',fps:60});
	var fx3 = new Fx.Tween(win,{transition:Fx.Transitions.Sine.easeOut});
	if(data.type==1||data.type==0){
		fx3.addEvent('complete',function(){
			$('i_win').tween('height',this.h);
		}.bind(Z));
	}
	fx2.addEvent('complete',function(){
		this.start('opacity',1);
	}.bind(fx3));
	fx2.start('opacity',.7);
}

//Navigation Photo Loader - Gallery
var photo_load = function(obj,st){
	src = 'images/events/'+PI_GAL[PI_POS].img[1]+'/full/'+PI_GAL[PI_POS].img[2];

	PI_CTR = {obj:obj,st:st,pid:PI_GAL[PI_POS].res[2],src:src};//Control Object

	st.set('html','Carregando...');
	var load_img = new Asset.image(src,{
		id:'t_img',
		onload: function(){
			// IE Fix
			PT = new Element('img',{'id':'ptemp','src':this.src}).inject($(document.body),'top');
			PT.setStyles({
				position: 'absolute',
				top: -5000
			});
			imd = PT.getSize();
			PT.destroy();
			//
			
			this.obj.set('src',this.src);
			
			this.obj.setStyles({
				height: imd.y,
				width: imd.x
			});
			
			this.st.set('html','Código da foto: '+this.pid);
			
			this.obj.setStyle('opacity',0);

			mw = imd.x+4;//4 Due to Padding
			mh = imd.y+4;
			
			ibody = $(document.body);
			
			A = ibody.getScroll();
			B = ibody.getScrollSize();
			
			pfx = new Fx.Morph($('i_win'));
			pfx.addEvent('complete',function(){
				this.fade(1);
			}.bind(this.obj));
			pfx.start({
				'height': mh,
				'width': mw,
				'top': -180+Math.floor(A.y+B.y/2-mh/2),
				'left': Math.floor((-mw+B.x)/2)
			});
		}.bind(PI_CTR)
	});
}

//Get Cursor Position in Text Input and Insert Value
var cpos = function(S,V) {
	if (document.selection) {//IE
		S.focus();
		sel = document.selection.createRange();
		sel.text = V;
	} else if (S.selectionStart || S.selectionStart=='0') {//Gecko[Moz|Net|Saf]
		var startPos = S.selectionStart;
		var endPos = S.selectionEnd;
		S.value = S.value.substring(0, startPos)+V+S.value.substring(endPos, S.value.length);
	} else {
		S.value += V;
	}
}

//Top Events Behaviors
window.addEvent('domready', function(){
	elements = $$('.sch');
	for(i=0;i<elements.length;i++){
		elements[i].setStyle('cursor','pointer');
		elements[i].addEvents({
			'click': function(){
				reg = new RegExp(/([0-9]+)s([0-9]+)/);
				res = reg.exec(this.get("id"));
				iload(1,res[1],res[2],this);
			},
			'active':function(){
				K = 'p'+this.get('id');
				J=new Element('p',{'id':K,'html':'Carregando...'}).inject(this,'bottom');
			},
			'success':function(){
				$('p'+this.get('id')).destroy();
			}
		});
	}
});
var IAD_L = function(){
	var AD_L = new Swiff('banners/all_banner120x600.swf',{width: 120, height: 600}).inject($('right_ad'));
}
var IAD_M = function(){
	var AD_M = new Swiff('banners/banner_reveillon.gif',{width: 468, height: 60}).inject($('middle_ad'));
}