$(document).ready(function(){
    $('#headersearchterms').example('Search by location');
    $('#searchterms').example('Where are you going?');
    $('#subForm input').example('Enter your email address');
    $('#headersearchterms').keyup(function() {
    	$('.autocomplete').hide();
    	
    	var location_search=$(this).val();
    	if (location_search.length>2){
	    	$.getJSON('ajax.php?action=locationSearch&location_search='+location_search, function(locations) {
	    		if (locations.length>0){
	    			$('.autocomplete li').remove();
		    		$('.autocomplete').show();
		   		 	var ul=$('#header .autocomplete ul');
			   		$.each(locations, function(i,location){
			   			ul.append("<li id='id_region_"+location.id_region+"'><a href='results.php?id_region="+location.id_region+"'>"+location.text+"</a></li>");
			   		});
	    		}
	    	});
    	}
    });
    
	(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));

    
    // $('#headersearchterms').blur(function() {
    //	$('#header .autocomplete').fadeOut();
    // });
 
	if ($('.calendar').length>0){
		$('.calendar').datePicker({clickInput:true});
	}
	
	//Image Thumb Slider
	//start on the first lot of visibile images (image 1-6, screen 1)
	var output = 1;
	//total number of images 
	var totalItems = $('.profilegallery ul li').size();
	//hide arrows for less than 4 photos
	if (totalItems < 4) {
		$('a.next, a.prev').addClass('disabled');
	}
	//upper limit is total number of images Ö 3 (visible) always rounded up 
	var upperLimit = Math.ceil(totalItems/3);
	//when the previous button is clicked
	$('a.prev').click(function() {
		//if currently animating, ignore it
		if ($(':animated').length) {
			return false
		} else {
			//if we're on the first screen
			if (output === 1) {
				return false;
			} else {
				$(this).removeClass('disabled');
				$('.profilegallery ul').animate({
				    marginLeft: '+=262'
				  }, 600, function() {
				    output = output-1;
					return false;
				  });
			}
		return false;
		}
	});
	//when the next button is clicked
	$('a.next').click(function() {
		//if currently animating, ignore it
		if ($(':animated').length) {
			return false
		} else {
			//if we're on the last screen
			if (output === upperLimit) {
				return false;
			} else {
				$('.profilegallery ul').animate({
				    marginLeft: '-=262'
				  }, 600, function() {
				    output = output+1;
					return false;
				  });
			}
		return false;	
		}
	});
	//show bigger image
	$('.profilegallery ul li a').click(function() {
		$('.profilegallery ul li a.current').removeClass('current');
		$('.hero').children('img').attr('src',$(this).children('img').attr('src').replace('tiny','gallery'));
		$(this).addClass('current');
		return false;
	});
});
