$(document).ready(function(){
	
	// Color changer
	var options = { path: '/' };
	
	if($.cookie("cmbatalha-CSSColor")){
		$("#css_color").attr("href",$.cookie("cmbatalha-CSSColor"));
	}
	
	$(".color").bind("click", function(e){
		$("#css_color").attr("href", "/css/color.css");
		$.cookie("cmbatalha-CSSColor", "/css/color.css", options);
		return false;
	});
	
	$(".contrast").bind("click", function(e){
		$("#css_color").attr("href", "/css/contrast.css");
		$.cookie("cmbatalha-CSSColor", "/css/contrast.css", options);
		return false;
	});
	
	// Lightbox
	$('.lightbox').lightbox({
		fitToScreen: true,
		imageClickClose: false,
		disableNavbarLinks: true
	});
	
	$('img.hover').hover(function() {
		$(this).attr('src', $(this).attr('src').replace( /_off/i, '_on' ));
	}, function() {
		$(this).attr('src', $(this).attr('src').replace( /_on/i, '_off' ));
	});
	
	//Close button:
	
	$(".close").click(function () {
		$(this).parent().fadeTo(400, 0, function () { // Links with the class "close" will close parent
			$(this).slideUp(400);
		});
		return false;
	});
	
	
	//Form validation
	var container = $('div.errornotification');

	var validator = $("#newsletter").validate({
		errorContainer: container,
		errorLabelContainer: $("ul", container),
		wrapper: 'li',
		meta: "validate",
		rules: {
			name: {
				required: true,
				minlength: 3
			},
			email: {
				required: true,
				email: true
			},
			"newsletterlist[]": {
				required: true,
				minlength: 1
			}
		}
	});
	
	var validator = $("#sugestoes").validate({
		errorContainer: container,
		errorLabelContainer: $("ul", container),
		wrapper: 'li',
		rules: {
			name: {
				required: true,
				minlength: 3
			},
			email: {
				required: true,
				email: true
			},
			message: {
				required: true
			}
		}
	});
	
	var validator = $("#publicacoes").validate({
		errorContainer: container,
		errorLabelContainer: $("ul", container),
		wrapper: 'li',
		meta: "validate",
		rules: {
			nome: {
				required: true,
				minlength: 3
			},
			email: {
				required: true,
				email: true
			},
			morada: {
				required: true
			}
		}
	});
	
	var validator = $("#orcamento-participativo").validate({
		errorContainer: container,
		errorLabelContainer: $("ul", container),
		wrapper: 'li',
		meta: "validate",
		rules: {
			end_mail: {
				email: true
			},
			"quest1[]": {
				required: true,
				minlength: 5,
				maxlength: 5
			}

		}
	});
	
	if ($(".weather").length) {
			$.simpleWeather({
				zipcode: 'POXX0058',
				unit: 'c',
				success: function(weather) {
				
					var rain = ["1","2","11","12","40","45"];
					var snow = ["5","6","7","8","9","10","13","14","15","16","18","41","42","43","46"];
					var lightning = ["3","4","17","35","37","38","39","47"];
					var clouds = ["19","20","21","22","23","24","25","26"];
					var cloudy = ["27","28","29","30","44"];
					var sun = ["31","32","33","34","36"];
					
					if($.inArray(weather.code, rain) >= 0){
						var todayw = "rain";
					}else if($.inArray(weather.code, snow) >= 0){
						var todayw = "snow";
					}else if($.inArray(weather.code, lightning) >= 0){
						var todayw = "lightning";
					}else if($.inArray(weather.code, clouds) >= 0){
						var todayw = "clouds";
					}else if($.inArray(weather.code, cloudy) >= 0){
						var todayw = "cloudy";
					}else if($.inArray(weather.code, sun) >= 0){
						var todayw = "sun";
					}else{
						var todayw = "none";
					}
					
					if($.inArray(weather.tomorrow.code, rain) >= 0){
						var tomorroww = "rain";
					}else if($.inArray(weather.tomorrow.code, snow) >= 0){
						var tomorroww = "snow";
					}else if($.inArray(weather.tomorrow.code, lightning) >= 0){
						var tomorroww = "lightning";
					}else if($.inArray(weather.tomorrow.code, clouds) >= 0){
						var tomorroww = "clouds";
					}else if($.inArray(weather.tomorrow.code, cloudy) >= 0){
						var tomorroww = "cloudy";
					}else if($.inArray(weather.tomorrow.code, sun) >= 0){
						var tomorroww = "sun";
					}else{
						var tomorroww = "none";
					}
				
					$(".TodayHighTemp").html(weather.high+' &ordm;C');
					$(".TodayLowTemp").html(weather.low+' &ordm;C');
					$(".TomorrowHighTemp").html(weather.tomorrow.high+' &ordm;C');
					$(".TomorrowLowTemp").html(weather.tomorrow.low+' &ordm;C');
					$(".TodayImageTemp").attr("src", '/images/weather/'+todayw+'.png');
					$(".TomorrowImageTemp").attr("src", '/images/weather/'+tomorroww+'.png');
				},
				error: function(error) {}
			});
	};

	if ($('#headerslider').length) {
		$('#headerslider').innerfade({
			speed: 'slow',
			timeout: 5000,
			type: 'sequence',
			containerheight: '240px'
		});
	};
	if ($('#headerslider0').length) {
		$('#headerslider0').innerfade({
			speed: 'slow',
			timeout: 5000,
			type: 'sequence',
			containerheight: '390px'
		});
	};
	if ($('#homeslider').length) {
		$('#homeslider').innerfade({
			speed: 3000,
			timeout: 5000,
			type: 'sequence',
			containerheight: '455px'
		});
	};
	
	if ($('.faqs h3').length) {
		$('.faqs h3').each(function() {
			var tis = $(this), state = false, answer = tis.next('div').hide().css('height','auto').slideUp();
			tis.click(function() {
				state = !state;
				answer.slideToggle(state);
				tis.toggleClass('active',state);
			});
		});
	};
	
	if ($('#pollcontainer').length) {
		$(function(){
			var loader=$('#loader');
			var pollcontainer=$('#pollcontainer');
			loader.fadeIn();
			//Load the poll form
			$.get('/poll.php', '', function(data, status){
				pollcontainer.html(data);
				animateResults(pollcontainer);
				pollcontainer.find('#viewresult').click(function(){
					//if user wants to see result
					loader.fadeIn();
					$.get('/poll.php', 'result=1', function(data,status){
						pollcontainer.fadeOut(1000, function(){
							$(this).html(data);
							animateResults(this);
						});
						loader.fadeOut();
					});
					//prevent default behavior
					return false;
				}).end()
				.find('#pollform').submit(function(){
					var selected_val=$(this).find('input[name=poll]:checked').val();
					if(selected_val!=''){
						//post data only if a value is selected
						loader.fadeIn();
						$.post('/poll.php', $(this).serialize(), function(data, status){
							$('#formcontainer').fadeOut(100, function(){
								$(this).html(data);
								animateResults(this);
								loader.fadeOut();
							});
						});
					}
					//prevent form default behavior
					return false;
				});
				loader.fadeOut();
			});
			
			function animateResults(data){
				$(data).find('.bar').hide().end().fadeIn('slow', function(){
									$(this).find('.bar').each(function(){
										var bar_width=$(this).css('width');
										$(this).css('width', '0').animate({ width: bar_width }, 1000);
									});
								});
			}
			
		});
	};
	
	if ($("ul#ticker01").length) {
		$("ul#ticker01").liScroll();
	};
	
});
