var updateLocationHelp = function () {
	var locationHelp = "Town, city, or postcode";

	//      var focus = $(this).is(":focus");

	if ($(this).attr("value") == locationHelp) {
	    $(this).attr("value", "");
		$(this).css("color", "black");
	} else if ($(this).attr("value").length == 0) {
	    $(this).attr("value", locationHelp);	
		$(this).css("color", "#777");
	}
}

$(document).ready(function() {
	$(".locationsearch").each(updateLocationHelp);
	$(".locationsearch").focus(updateLocationHelp);
	$(".locationsearch").blur(updateLocationHelp);

	$(".flink").hide();
	$(".fc").mouseover(function() {
		$(".flink", this).show();
	});
	$(".fc").mouseout(function() {
		$(".flink", this).hide();
	});
	$("#show_links").change(function () {
		var checked = $(this).is(":checked");
		if (checked) {
			$(".flink").show();
		} else {
			$(".flink").hide();
		}
	});

	$(".wind").hide();
	$("#show_wind").change(function () {
		var checked = $(this).is(":checked");
		if (checked) {
			$(".wind").show();
		} else {
			$(".wind").hide();
		}
	});

	$(".adv").hide();
	$(".advchk").click(function () {
		var panel = $(this).closest("tr").find(".adv");
		panel.toggle();
		$(this).hide();
	});

	$(".advbtn").click(function() {
		var locateStr = $(this).closest("tr").find(".advstr").attr("value");
		var selector = $(this).closest("tr").find(".advsel");
		var editbutton = $(this).closest("tr").find(".advchk");
		var wp_id = $(this).attr("id").split("_")[1];
		var panel = $(this).closest("tr").find(".adv");
		panel.hide();
		editbutton.show();
		selector.empty();
		selector.append('<option>Searching, please wait...</option>');
		$.ajax({
			url: "/api/locate.php?location", 
			type : "POST",
			data: {location: locateStr, wp : wp_id},
			dataType:'json',
			success: function(data, status, xhr) {
				selector.empty();
				for (var i in data) {
					var selected = '';
					if (data[i].region == "UK") {
					  selected = " selected='true'";
					}
 				  selector.append('<option' + selected + ' value=' + data[i].forecast_id + '>' + data[i].text + '</option>');					
				}
			}
			
		});
	});


	// external social media stuff
	// unfortunately scripts will halt page load so after main load
	var script = "script";
	$("#twitter_follow").append('<a href="http://twitter.com/WeatherSuperM" class="twitter-follow-button" data-show-count="false">Follow WeatherSuperM</a><' + script + ' src="http://platform.twitter.com/widgets.js" type="text/javascript"></' + script + '>');
	$("#facebook_like").append('<iframe src="http://www.facebook.com/plugins/like.php?app_id=172448579487702&amp;href=https%3A%2F%2Fwww.facebook.com%2Fpages%2FWeather-Supermarket%2F144574078954083&amp;send=false&amp;layout=button_count&amp;width=120&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:120px; height:21px;" allowTransparency="true"></iframe>');


//	rounded borders for browsers without CSS3 support
	/*   
var e = document.createElement("div");
var css3border = "border-radius" in e.style;
   if (! css3border) {
	   $("#header input").corner();
	   $("#header select").corner();
	   $(".tagcloud").corner();	   
   }
	 */   

});

