var Ontir = {};

$(function(){
	$('html').addClass('js');
	if (jQuery.browser.msie){
		$('html').addClass('ie');
	}
	Cufon.replace('#nav-servicos a span', {
		textShadow: '#000 1px 1px'
	});
	Cufon.replace('#header h2', {
		textShadow: '#000 1px 1px'
	});
	Cufon.replace('.content-title h2, .content-title h3, .content-title h4, #newsletter-subscricao h4', {
		textShadow: '#FFF 2px 2px'
	});
	//Cufon.replace('#column h4');

	/* Navigation *bling*bling* */
	/*(function(){
		var items = {};
		$('#nav li').each(function(){
			var prev = $(this).prev();
			if (prev.size() == 0){
				prev = $(this).parent();
			}
			items[$(this).attr('id')] = $([prev.get(0), this]);
		});

		$('#nav a').hover(function(){
			items[$(this).parent().attr('id')].addClass('hover');
		}, function(){
			items[$(this).parent().attr('id')].removeClass('hover');
		});
	}());*/

	Ontir.Nav.init();

	// Linguagens
	$('#language-selector ul').kwicks({
		selector: 'a',
		active: 'a.active',
		duration: 200,
		min: 22,
		max: 40
	}).find('a.active').width(42);
	/*$('#language-selector li[id!=language-pt] a').css({
		'opacity': .3,
		'cursor': 'default'
	}).click(function(){
		return false;
	});*/

	// Login
	$('#login-el-username, #login-el-password').addClass('idle').focus(function(){
		$(this).addClass('focus');
		if (this.value == this.defaultValue){
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	}).blur(function() {
		$(this).removeClass('focus');
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});

	$('#nav-servicos li ul').each(function(){
		if ($(this).find('li a.current').size() == 0){
			$(this).hide();
		}
	});
	//$('#nav-servicos li a[class!=active] ~ ul').hide();

	//$('#nav-servicos ul a[class!=active] + ul a[class!=current]').css('padding-left', 0);
	$('#nav-servicos .nav-servico-tipo > a').click(function(){
		$('#nav-servicos .nav-servico-tipo > a').not(this).removeClass('active');
		var ul = $(this).addClass('active').parent().children('ul').get(0);
		$('#nav-servicos li ul').not(ul).slideUp(300);
		$(ul).slideDown(300);
		//$(ul).find('a').animate({'paddingLeft': 20});
		return false;
	});

	var section = $('body').attr('id');
	if (Ontir.Section[section]){
		Ontir.Section[section]();
	}

	// Servicos
	if ($('#form-adesao').size() > 0){
		$('<a href="#" title="Enviar adesao">Enviar adesao</a>').appendTo('#form-el-enviar-wrap').click(function(){
			$('#form-adesao').submit();
			return false;
		});
	}

	// Contactos
	if ($('#form-contacto').size() > 0){
		$('<a href="#" title="Enviar contactos">Enviar contacto</a>').appendTo('#form-el-enviar-wrap').click(function(){
			$('#form-contacto').submit();
			return false;
		});
	}


	Ontir.Ofertas.init();

	$('a[rel=lightbox]').lightBox({
		overlayOpacity: 0.6,
		imageLoading: '../img/ico-loading.gif',
		imageBtnClose: '../img/btn-close.gif',
		imageBtnPrev: '../img/btn-prev.gif',
		imageBtnNext: '../img/btn-next.gif',
		imageBlank: '../img/blank.gif',
		txtImage: 'Imagem',
		txtOf: 'de'
	});

});

Ontir.Section = {
	inicio : function(){
		/*var ontir = $('#hd-ontir').css('top', 230),
			texto = $('#hd-texto').css('left', 740);
		ontir.animate({
			'top': 30
		}, 800, 'linear', function(){
			texto.animate({
				'left': 370
			}, 800)
		});*/


		/*var altri = $('#ad-logo').css('left', 210),
			bg = $('#ad-logo-bg').css('opacity', 0),
			grupo = $('#ad-grupo').css('top', 210);
		var adAnim = function(){
			bg.animate({
				'opacity': .7
			}, 500, 'linear', function(){
				altri.animate({
					'left': 78
				}, 800, 'linear', function(){
					grupo.animate({
						'top': 170
					}, 800);
				});
			});
		}
		adAnim();
		setInterval(adAnim, 5000);*/

		$("#ads-200x200 .ads").carousel({
			pagination: false,
			direction: 'vertical',
			prevBtnInsert: 'appendTo',
			autoSlideInterval: 5000,
			//effect: 'fade',
			autoSlide: true
		});
	},

	emprego : function(){
		if ($('#form-candidatura').size() > 0){
			$('<a href="#" title="Enviar candidatura">Enviar candidatura</a>').appendTo('#form-el-enviar-wrap').click(function(){
				$('#form-candidatura').submit();
				return false;
			});
		}
	},

	noticias : function(){
		Cufon.replace('#noticia h3');
	}
};

Ontir.Ofertas = (function(){
	var self,
		ofertas, original, clone, interval;

	return {
		init : function(){
			self = this;

			ofertas = $('#ofertas-info');

			if (ofertas.find('tr').size() <= 3){
				return;
			}

			original = ofertas.find('table');
			clone = original.clone();

			$('#ofertas-info').append(clone);

			interval = setInterval(self.scroll, 4000);
		},

		scroll : function(){
			ofertas.animate({
				'top': ofertas.position().top - 46
			}, 500, 'linear', function(){
				if ($(this).position().top <= (0 - original.height())){
					ofertas.find('table:first').appendTo(ofertas)
					ofertas.css('top', 0);
				}
			});
		}
	}
})();

Ontir.Nav = (function(){
	var self, items = {};

	return {
		init : function(){
			self = this;

			$('#nav li').each(function(){
				var prev = $(this).prev();
				if (prev.size() == 0){
					prev = $(this).parent();
				}
				items[$(this).attr('id')] = $([prev.get(0), this]);
			});

			this.el = $('#nav ul');
			this.current = this.el.find("li.current a").get(0);

			this.el.find('a').hover(function(){
				items[$(this).parent().attr('id')].addClass('hover');
				self.slide(this);
			}, function(){
				items[$(this).parent().attr('id')].removeClass('hover');
				self.slide(self.current);
			}).click(function(){
				return self.select(this);
			});

			this.el.append('<li class="bullet"><div class="left">&nbsp;</div></li>');
			this.back = this.el.find("li.bullet").css('opacity', 0);
			if(this.current) {
				this.setCurrent(this.current);
			}
		},

		slide : function(to){
			if(this.current){
				this.back.stop(true).animate({
					'left': to.offsetLeft,
					'width': to.offsetWidth
				}, 'slow', 'swing');
			}
		},

		setCurrent : function(el){
			this.back.css({
				'left': el.offsetLeft,
				'width': el.offsetWidth,
				'opacity': 1
			});
		},

		select : function(el){
			if(!this.current){
				this.setCurrent(el);
			}
			this.current = el;
		}
	}
})();