(function($){
	var baseURL="http://"+window.location.hostname+"/";
	var pathURL=baseURL+"/wp-content/themes/gardengate";
	var currentURL=window.location;
	
	// Load
	$("input[type=submit]").val("");
	$("#js").val("yes");
	$("#latest_news li:last").css("border","0px");
	$("#likebox").html('<fb:fan profile_id="135030909919313" stream="false" connections="10" width="280" css="'+pathURL+'/css/style.css"></fb:fan>');
	$(".more_info").parent().addClass("has_event");
	function loadMap(address,zoom,div) {
		geocoder = new google.maps.Geocoder();
		geocoder.geocode({'address':address},function(results,status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var center = results[0].geometry.location;
				var myOptions = { zoom:zoom,center:center,mapTypeId:google.maps.MapTypeId.ROADMAP };
				var map = new google.maps.Map(document.getElementById(div),myOptions);
				var marker = new google.maps.Marker({map:map,position:center});
			}
		});
	}
	if ($("#google_map").length>0) { loadMap("Daresbury Point, Greenwood Drive, Daresbury, WA7 1UG, UK",12,"google_map"); }
	
	// Slider
	var getContainers = $("#slider").find(".container");
	var totalSlides = getContainers.length;
	
	if (totalSlides>1) {
	
		$("#slider_controls").find(".control:first").addClass("active");
		
		setInterval(function(){
			var getActive = $("#slider_controls").find(".active");
			var totalNext = getActive.nextAll(".control").length;
			var totalPrev = getActive.prevAll(".control").length;
			
			var activeSlide = getActive.attr("data-slide");
			$("#slider").find("."+activeSlide).fadeOut(500);
			
			if (totalNext>0) {
				getActive.removeClass("active").next(".control").addClass("active");
			} else {
				getActive.removeClass("active");
				$("#slider_controls").find(".control:first").addClass("active");
			}
			
			var thisSlide = $("#slider_controls").find(".active").attr("data-slide");
			$("#slider").find("."+thisSlide).fadeIn(500);
			
		},7000);
		
		$(".control").on("click",function(){
			
			var thisControl = $(this);
			
			var getActive = $("#slider_controls").find(".active");
			var activeSlide = getActive.attr("data-slide");
			
			$("#slider").find("."+activeSlide).fadeOut(500);
			getActive.removeClass("active");
			
			var thisSlide = thisControl.attr("data-slide");
			$("#slider").find("."+thisSlide).fadeIn(500);
			thisControl.addClass("active");
		
		});
		
	}
	
	// Interaction
	$(".has_event").live("mouseover",function(){
		$(".more_info",this).stop(true,true).fadeIn(250);
	}).live("mouseout",function(){
		$(".more_info",this).fadeOut(250);
	}).live("click",function(){
		window.location="https://www.facebook.com/event.php?eid="+$(this).find(".more_info").attr("data-id");
	});
	$(".more_info").live("mouseover",function(){
		$(this).stop(true,true).fadeIn(250);
	}).live("mouseout",function(){
		$(this).fadeOut(250);
	}).live("click",function(){
		window.location="https://www.facebook.com/event.php?eid="+$(this).find(".more_info").attr("data-id");
	});
	$(".change_month").live("click",function(){
		$("#calendar_loading").fadeIn();
		$.post(pathURL+"/php/calendar.php",{month:$(this).find("span").attr("data-month"),year:$(this).find("span").attr("data-year")},function(data){
			$("#calendar_content").hide().html(data).fadeIn();
			$("#calendar_loading").fadeOut();
			$(".more_info").parent().addClass("has_event");
		});
	});
	$("#attending").live("click",function(){
		var thisLink = $(this);
		var thisID = thisLink.attr("data-id");
		var thisAccess = thisLink.attr("data-access");
		$.post("https://graph.facebook.com/"+thisID+"/attending/?access_token="+thisAccess);
		$.post(pathURL+"/php/update_activity.php",{
			user_id:$("#id").text(),
			forename:$("#forename").text(),
			surname:$("#surname").text(),
			activity:"Is attending <a href='https://www.facebook.com/event.php?eid="+thisID+"' target='_blank'>"+$("#latest_event .title:first").text()+"</a>",
			activity_id:thisID
		},function(data){
			setTimeout(function(){ window.location=currentURL; },1000);
		})
		return false;
	});
	$("input[type=text]").each(function(){
		var thisVal=$(this).val();
		$(this).bind("focus",function(){
			if ($(this).val()==thisVal) { $(this).val(""); }
		}).bind("blur",function(){
			if ($(this).val()=="") { $(this).val(thisVal); }
		});
	});
	$("#newsletter_signup form").live("submit",function(){
		$.post(pathURL+"/php/newsletter.php",$(this).serialize(),function(data){
			if (data.status==0) {
				$("#feedback").hide().text(data.error).fadeIn();
			} else {
				$("#newsletter_signup form").hide();
				$("#feedback").hide().html(data.error).fadeIn();
				$("#name").val("Full name");
				$("#email").val("Email address");
				$("input").each(function(){
					var thisVal=$(this).val();
					$(this).bind("focus",function(){
						if ($(this).val()==thisVal) { $(this).val(""); }
					}).bind("blur",function(){
						if ($(this).val()=="") { $(this).val(thisVal); }
					});
				});
			}
		},"json");
		return false;
	});
})(this.jQuery);
