$(document).ready(function(){
	jQuery('.ambitios_slider').fadeIn(300);
	
	if ($("#s4").length) {
		$.fn.cycle.defaults.timeout = 6000;
		$(function() {
			// run the code in the markup!
			$('#s4').before('<div id="nav" class="nav">').cycle({
				fx:     'fade',
				speed:  'slow',
				autostopCount:   4,
				autostop:   4,
				timeout: 6000,
				pager:  '#nav'
			});
		});
	};
		
	if ($("#featureCarousel").length) {
		$(document).ready(function() {
			$("#featureCarousel").featureCarousel({
			});
		});
	};
		
	$.fn.equalHeight = function() {
		var group = this;
		$(window).bind('resize', function(){
			var tallest = 0;
			$(group).height('auto').each(function() {
				tallest = Math.max(tallest, $(this).height());
			}).height(tallest);
		}).trigger('resize');
	}

	// prettyPhoto
	if ($(".ambitios_lightbox_image").length) {
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	};		
	// lightbox image
	$(".ambitios_lightbox_image").append("<span></span>");
			
	$(".ambitios_lightbox_image").hover(
		function(){
			$(this).find("img").stop().animate({opacity:0.5}, "normal");
		}, 
		function(){
			$(this).find("img").stop().animate({opacity:1}, "normal");
		}
	);
	
	$(".ambitios_lightbox_video").append("<span></span>");
			
	$(".ambitios_lightbox_video").hover(
		function(){
			$(this).find("img").stop().animate({opacity:0.5}, "normal");
		}, 
		function(){
			$(this).find("img").stop().animate({opacity:1}, "normal");
		}
	);

	$('#toc a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 1500);
		$(this.hash).find('#options-examples').text( this.title );
		return false;
	});

	$(".ambitios_height").equalHeight();
	
	jQuery('ul.ambitios_menu').superfish();
	
});


$(document).ready(function(){
	
	// login form
	function ajaxLogin(loginForm) {
		var $ = jQuery;
		var timing = 500;
		$('#loaderLogin').fadeIn(timing);

		var formData = $(loginForm).serialize(),
		note = $('#LoginNotes');
		
		$.ajax({
	        type: "POST",
	        url: "/user/Index/Login",
	        dataType: 'json',
	        data: formData,
	        success: function(response) {
	        	note.removeClass('success').removeClass('error').text('');
				note.fadeIn('slow', function() { $(this).hide(); });
				var c = response.status;
				if (c === 'success') { 
					$('#login_form .field2').animate({opacity: 0},timing, function(){
						$('#login_form .ambitios_input').animate({opacity: 0},timing, function(){
							$('#login_form').html(response.view);
						});
					});
					
					
				}else{
					var r = response.messages;
					for(var i = 0; i < r.length; i++){
						resultHTML = r[i] + '<br />';
					}
					resultHTML += '<br />';
					var i = setInterval(function() {
						if ( !note.is(':visible') ) {
							note.html(resultHTML).addClass(c).slideDown(timing);
							clearInterval(i);
						}
					}, 40);
				}
				
					
				$('#LoadingGraphicLogin').fadeOut(timing, function() {
					//$(this).remove();
				});
	        }
		});

		return false;
	}
	
	if ($("#loginForm").length) {
		
		jQuery("#loginForm").validate({
			submitHandler: function(form) {				
				ajaxLogin(form);
				return false;
			},
			messages: {
				username: "Please specify your username.",
				password: {
					required: "You need a password bigger then 6 characters.",
					min: 6
				}
			}
		});
	}
	
	
	// main contactform
	function ajaxMainContactos(theForm) {
		var $ = jQuery;
		var timing = 500;
		$('#loader').fadeIn();

		var formData = $(theForm).serialize(),
		note = $('#Note');

		$.ajax({
			type: "POST",
			url: "/core/Common/Ajax-Main-Form",
	        dataType: 'json',
			data: formData,
			success: function(response) {
				note.removeClass('success').removeClass('error').text('');
				note.fadeIn('slow', function() { $(this).hide(); });
				
				var c = response.status;
				note.addClass(c);
				
				var r = response.messages;

				var resultHTML = '';
				for(var i = 0; i < r.length; i++){
					resultHTML += r[i] + '<br />';
				}
				resultHTML += '<br />';
				var i = setInterval(function() {
					if ( !note.is(':visible') ) {
						note.html(resultHTML).addClass(c).slideDown(timing);
						clearInterval(i);
					}
				}, 40);
				if(c == 'success')
				{
					$(':input').clearForm();
				}
				$('#LoadingGraphicLogin').fadeOut(timing, function() {
					//$(this).remove();
				});
			}
		});

		return false;
	}
	
	if ($("#contactosForm").length) {
		jQuery("#contactosForm").validate({
			submitHandler: function(contactosForm) {				
				ajaxMainContactos(contactosForm);
				return false;
			},
			messages: {
				formname: "Please specify your name.",
				formcomments: "Please enter your message.",
				formemail: {
					required: "We need your email address to contact you.",
					email: "Your email address must be in the format of name@domain.com"
				}
			}
		});
	}
	
	// contactos ajac
	function ajaxContactos(theForm) {
		var $ = jQuery;
		var timing = 500;
		$('#loader-ajax-contactos').fadeIn();

		var formData = $(theForm).serialize(),
		note = $('#NoteAjaxContactos');

		$.ajax({
			type: "POST",
			url: "/core/Common/Ajax",
	        dataType: 'json',
			data: formData,
			success: function(response) {
				note.removeClass('success').removeClass('error').text('');
				note.fadeIn('slow', function() { $(this).hide(); });
				
				var c = response.status;
				var resultHTML = ''; 
				var r = response.messages;
				for(var m = 0; m < r.length; m++){
					resultHTML += r[m] + '<br />';
				}
				resultHTML += '<br />';
				var i = setInterval(function() {
					if ( !note.is(':visible') ) {
						note.html(resultHTML).addClass(c).slideDown(timing);
						clearInterval(i);
					}
				}, 40);
				if(c == 'success')
				{
					$(':input').clearForm();
				}
				$('#LoadingGraphicAjaxContactos').fadeOut(timing, function() {
					//$(this).remove();
				});
			}
		});

		return false;
	}
	
	if ($("#ajaxContactosForm").length) {
		jQuery("#ajaxContactosForm").validate({
			submitHandler: function(ajaxContactosForm) {				
				ajaxContactos(ajaxContactosForm);
				return false;
			},
			messages: {
				formname: "Please specify your name.",
				formcomments: "Please enter your message.",
				formemail: {
					required: "We need your email address to contact you.",
					email: "Your email address must be in the format of name@domain.com"
				}
			}	
		});
	}
	
	function deleteConfirm(){
		var text = "Ao eliminar este ficheiro, eliminará todas as partilhas deste ficheiro. Tem a certeza que quer apagar o ficheiro?";
		var answer = confirm(text);
        if (answer){
            return true;
        }else{
            return false;
        }
	}
    
    $('a.deleteFile').click(deleteConfirm);
});

$(document).ready(function(){
	
	if ($('#municipioUrl').length) {
		$('#feedLoader').show();
		$('.municipiosFeed').hide();
		$.ajax({
		    type: "POST",
		    url: "/municipios/Index/Get-Feed",
		    dataType: "json",
		    data: {'url': $('#municipioUrl').attr('href')},
		    success: function(data, textStatus) {
		    	if(data.status == 'ok'){
		    		var r = data.entries;
		    		var htmlout = '';
		    		for ( var j = 0; j < r.length; j++) {
						var feedElement = r[j];
						htmlout += '<p>'+'<strong>'+ feedElement['title'] +'</strong>'+'<br /> '+ feedElement['description'] +'<br /><br /></p>';
					}
		    		
		    		//console.log(data);
		      	}else{
		      		var r = data.messages;
		    		var htmlout = '';
		    		for ( var j = 0; j < r.length; j++) {
		    			htmlout +=  r[j] + '<br>';
					}
		      	}
		    	$('.municipiosFeed').html(htmlout);
		    	$('#feedLoader').fadeOut('slow', function () {
		    		$('.municipiosFeed').fadeIn('slow');
		    	});
		    	
		    }
		});
	}
	
});

$(document).ready(function(){
	
	if ($("#Guardar").length) {
		$('#Guardar').val('OK');
	}
	
	
	effectsDemo2 = 'rain,stairs,fade';
	var demoSlider_ = Sliderman.slider({container: 'SliderName_2', width: 623, height: 278, effects: effectsDemo2,
		display: {
			autoplay: 3000,
			loading: {background: '#000000', opacity: 0.5, image: '/assets/img/loading.gif'},
			buttons: {hide: true, opacity: 1, prev: {className: 'SliderNamePrev_2', label: ''}, next: {className: 'SliderNameNext_2', label: ''}}
		}
	});
});

$.fn.clearForm = function() {
	  return this.each(function() {
	    var type = this.type, tag = this.tagName.toLowerCase();
	    if (tag == 'form')
	      return $(':input',this).clearForm();
	    if (type == 'text' || type == 'password' || tag == 'textarea')
	      this.value = '';
	    else if (type == 'checkbox' || type == 'radio')
	      this.checked = false;
	    else if (tag == 'select')
	      this.selectedIndex = -1;
	  });
	};
