var App = {
	domReady : function() {
		
		// if ($('divError')) $('divError').setStyle('display', 'none');
		
		$$('a._blank').each(function(item){
			item.setAttribute('target', '_blank');
		});
		
		if (window.ie6) {
			$$('div.bg-box').each(function(el) {
				el.setStyle('opacity', 0.4);
			})
		}
		
		Vitrine.ready();
		
	}
}

var Vitrine = {
	ready : function() {
		$$('ul#cases-home li').each(function(el) {
			el.addEvent('click', function(e) {
				if (el.getAttribute('id') != 'principal') {
					new Event(e).stop();
					Vitrine.change(el);
				}
			});
		});
	},
	timer : '',
	vitrine : 1,
	init : function() {
		Vitrine.timer = Vitrine.row.delay(10000);
	},
	row : function() {
		$$('li.case-' + Vitrine.vitrine).each(function(item) {
			Vitrine.change(item);
			Vitrine.init();
		});
		Vitrine.vitrine = (Vitrine.vitrine == 3) ? 1 : Vitrine.vitrine + 1;
	},
	change : function(el) {
		var i = 1;
		$$('ul#cases-home li').each(function(li) {
			li.removeClass('case-1');
			li.removeClass('case-2');
			li.removeClass('case-3');
			if (li != el) {
				li.setAttribute('id', '');
				li.addClass('case-' + i);
				i++;
			}
		});
		el.setOpacity(0);
		el.setAttribute('id', 'principal');			
		el.effect('opacity', {
			'duration': 500,
			'wait': false
		}).start(0, 1);
		$clear(Vitrine.timer);
	}
}

var Clientes = {
	qtd : 0,
	list : '',
	showList : '',
	init : function(qtd) {
		Clientes.qtd = qtd;
		Clientes.list = $$('ul#clientes li');
		Clientes.change();
	},
	change : function() {
		Clientes.showList = new Array();
		Clientes.list.each(function(li) {
			li.setStyle('display', 'none');
		});
		var size = (Clientes.list.length < Clientes.qtd) ? Clientes.list.length : Clientes.qtd;
		for(var i = 0; i < Clientes.list.length; i++) {
			Clientes.showList.push(Clientes.list[i]);
		}
		Clientes.show(size);
	},
	show : function(size) {
		for(var i = 0; i < size; i++) {
			var k = Math.ceil(Math.random()*Clientes.showList.length) - 1;
			var li = Clientes.showList[k];
			li.inject($('clientes'));
			li.setStyle('opacity', 0);
			li.setStyle('display', '');
			li.effect('opacity', {
				'duration': 1000,
				'wait': false
			}).start(0, 1);
			Clientes.remove(Clientes.showList, k);
		}
		Clientes.change.delay(5000);
	},
	remove : function(array, from, to) {
		var rest = array.slice((to || from) + 1 || array.length);
		array.length = from < 0 ? array.length + from : from;
		return array.push.apply(array, rest);
	}
}

var Oportunidades = {
	Busca : {
		texto : 'Buscar por palavra-chave...',
		id : 'palavra',
		form_id : 'form-opo-vitrine',
		init : function() {
			if (!$(Oportunidades.Busca.id).value) {
				$(Oportunidades.Busca.id).value = Oportunidades.Busca.texto;
				$(Oportunidades.Busca.id).setStyle('color', '#666');
			}
			$(Oportunidades.Busca.id).addEvent('focus', function() {
				if ($(Oportunidades.Busca.id).value == Oportunidades.Busca.texto) {
					$(Oportunidades.Busca.id).value = '';
					$(Oportunidades.Busca.id).setStyle('color', '');
				}
			});
			$(Oportunidades.Busca.id).addEvent('blur', function() {
				if ($(Oportunidades.Busca.id).value == '') {
					$(Oportunidades.Busca.id).value = Oportunidades.Busca.texto;
					$(Oportunidades.Busca.id).setStyle('color', '#666');
				}
			});
			$(Oportunidades.Busca.form_id).addEvent('submit', function(event) {
				new Event(event).stop();
				if ($(Oportunidades.Busca.id).value == Oportunidades.Busca.texto) $(Oportunidades.Busca.id).value = '';
				$(Oportunidades.Busca.form_id).submit();
			});
		}
	},
	Interesse : {
		texto_login : 'E-mail',
		form_id : 'form-opo-login',
		id_login : 'login',
		init : function() {
			if (!$(Oportunidades.Interesse.id_login).value) {
				$(Oportunidades.Interesse.id_login).value = Oportunidades.Interesse.texto_login;
				$(Oportunidades.Interesse.id_login).setStyle('color', '#666');
			}
			$(Oportunidades.Interesse.id_login).addEvent('focus', function() {
				if ($(Oportunidades.Interesse.id_login).value == Oportunidades.Interesse.texto_login) {
					$(Oportunidades.Interesse.id_login).value = '';
					$(Oportunidades.Interesse.id_login).setStyle('color', '');
				}
			});
			$(Oportunidades.Interesse.id_login).addEvent('blur', function() {
				if ($(Oportunidades.Interesse.id_login).value == '') {
					$(Oportunidades.Interesse.id_login).value = Oportunidades.Interesse.texto_login;
					$(Oportunidades.Interesse.id_login).setStyle('color', '#666');
				}
			});
			$(Oportunidades.Interesse.form_id).addEvent('submit', function(event) {
				new Event(event).stop();
				if ($(Oportunidades.Interesse.id_login).value == Oportunidades.Interesse.texto_login) $(Oportunidades.Interesse.id_login).value = '';
				$(Oportunidades.Interesse.form_id).submit();
			});
		}
	}
}

var Sistema = {
	Login : {
		login : '',
		senha : '',
		init : function() {
			this.login = new Fx.Slide('oportunidades-login');
			this.senha = new Fx.Slide('oportunidades-senha');
			this.senha.slideOut().chain(function() {
				$('oportunidades-senha').setStyle('display', '');
			});
			$('form-senha').addEvent('submit', function(event) {
				new Event(event).stop();
				if ($('usu_email').value) {
					this.send({
						onComplete : function(response) {
							var r = response.split('::');
							alert(r[1]);
							if (r[0] == 1) Sistema.Login.show_login();
						}
					});
				}
			});
		},
		show_senha : function() {
			Sistema.Login.login.slideOut().chain(function() {
				Sistema.Login.senha.slideIn();
			});
		},
		show_login : function() {
			Sistema.Login.senha.slideOut().chain(function() {
				Sistema.Login.login.slideIn();
			});
		}
	},
	
	HeaderLogin : {
		login : 'header-login-email',
		senha : 'header-login-senha',
		init : function() {
			$(this.senha).setStyle('opacity', 0);
			$(this.senha).setStyle('display', '');
			$('header-form-senha').addEvent('submit', function(event) {
				new Event(event).stop();
				if ($('header_usu_email').value) {
					this.send({
						onComplete : function(response) {
							var r = response.split('::');
							alert(r[1]);
							if (r[0] == 1) Sistema.HeaderLogin.show_login();
						}
					});
				}
			});
		},
		show_senha : function() {
			$(this.login).effect('opacity', {
				'duration': 500,
				'wait': false
			}).start(1, 0);
			$(this.senha).effect('opacity', {
				'duration': 500,
				'wait': false
			}).start(0, 1);
		},
		show_login : function() {
			$(this.senha).effect('opacity', {
				'duration': 500,
				'wait': false
			}).start(1, 0);
			$(this.login).effect('opacity', {
				'duration': 500,
				'wait': false
			}).start(0, 1);
		}
	}
}

window.addEvent('domready', function() {
	App.domReady();
});
window.addEvent('load', function() {
	Vitrine.init();
});