/* 
 * by: dvl
 * contato@xdvl.info
 * http://xdvl.info/
 */

var horizontal;

window.addEvent('domready',function(){	
	horizontal = new Fx.Scroll.Carousel('carousel',{
		mode: 'horizontal',
		onStart: function(){
			horizontal.getCurrent().tween('background-color','gray');
		},
		onComplete: function(){
			horizontal.getCurrent().highlight();
		}
	});	
	$('next').addEvent('click', function(){
		horizontal.toNext();
	});	
	$('previous').addEvent('click', function(){
		horizontal.toPrevious();
	});
});

function more() { 
	var i = $$('input[type=file]').length;

	if (i >= 5) { 
		return false; 
	}

	if ($chk($$('input[type=file]')[i-1].value) == false) { 
		return false;
	}

	var div = $('files');

	new Element('br').inject(div);

	new Element('input', {
		'type':'file',
		'name':'arquivo[]',
		'events': {
			'change':function() { more(); }
		}
	}).inject(div);

	var a = new Element('span', {
		'class':'err',
		'id':'filerr'+i
	}).inject(div);
}

function check() { 
	var files = $$('input[type=file]');

	var exts = /(.png|.jpg|.jpeg|.gif)$/i;

	var err = false;

	if (!$chk(files[0].value)) { 
		flash('filerr0','Vocę deve selecionar um arquivo');
		err = true;
	}

	files.each(function(item,index){
		if ($chk(item.value) && exts.test(item.value) == false) {
			flash('filerr'+index,'Formato de arquivo invalido');
			err = true;
		}
	});

	if ($chk($('user').value) == false || $chk($('senha').value) == false)  {
		flash('loginerr','Preencha os campos usuário e senha');		
		err = true;
	} else {
		var query = {'t':'1','u':$('user').value,'p':$('senha').value};
		new Request({
		url: 'ajax/check.php',
		async: false,
			onRequest: function() { 
				$('loginerr').set('html','<img src="img/loading.gif" />');
			},
			onComplete: function(resposta) {
				if (resposta == '001 - Login Error') { 
					flash('loginerr','Usuário ou senha invalidos');
					err = true;
				} 
				if (resposta == '002 - Login OK') { 
					$('loginerr').empty();
				}
			},
		}).get(query);
	}
	
	if (err) { return false; }
}

function flash(id,text) { 
	var a = $(id);
	a.set('html',text);
	a.set('tween', {duration: 'long'}).highlight('#E6EFC2','#353535');
}

