var currentdaydetail = "";
$(document).ready(function(){	
	$(".getfc,#open1,#open2,#open3,#key,#centfare,#milekilo,#gmap,#gmap_change").button();
	$("#LocAjax").autocomplete("inc/searchDBdup.php",
		{
			delay:0,
			minChars:1,
			matchSubset:0,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true,
			highlight:0
		}
	);
	$("#LocAjax").result(function(event, data, formatted) {
		$("#loc_id").val(data[1]);
	});

	var $dialog1 = $('#dialog1').dialog({autoOpen:false, position:[400,50],width:400,buttons: { "Close": function() { $(this).dialog("close");}}});
	var $dialog2 = $('#dialog2').dialog({autoOpen:false, position:[550,50],width:400,buttons: { "Close": function() { $(this).dialog("close"); }}});
	var $dialog3 = $('#dialog3').dialog({autoOpen:false, position:[700,50],width:400,buttons: { "Close": function() { $(this).dialog("close"); }}});
	var $dialog4 = $('#dialog4').dialog({autoOpen:false, position:[750,50],width:300,buttons: { "Close": function() { $(this).dialog("close"); }}});
	$('#open1').click(function() {
		$dialog1.dialog('open');
		return false;
	});
	$('#open2').click(function() {
		$dialog2.dialog('open');
		return false;
	});
	$('#open3').click(function() {
		$dialog3.dialog('open');
		return false;
	});
	$('#key').click(function() {
		$dialog4.dialog('open');
		return false;
	});
	$('#centfare').click(function() {
		var curbutcont = $('#centfare').html();
		if (curbutcont.indexOf('F') != -1) {
			$('#centfare').html(curbutcont.replace("F","C"));
			$('.centigrade').hide();
			$('.farenheit').show();
		} else {
			$('#centfare').html(curbutcont.replace("C","F"));
			$('.centigrade').show();
			$('.farenheit').hide();
		}
		return false;
	});
	$('#milekilo').click(function() {
		var curbutcont = $('#milekilo').html();
		if (curbutcont.indexOf('K') != -1) {
			$('#milekilo').html(curbutcont.replace("K","M"));
			$('.mileperhr').hide();
			$('.kiloperhr').show();
		} else {
			$('#milekilo').html(curbutcont.replace("M","K"));
			$('.mileperhr').show();
			$('.kiloperhr').hide();
		}
		return false;
	});
});
$(function(){
	$("#LocAjax").watermark("Enter a location, UK postcode or lat/long here...");
});

$(function(){
    $('#slide-out-twit').tabSlideOut({
        tabHandle: '#handle-twit',pathToTabImage: 'images/vtab_twit.png',          
        imageHeight: '160px',imageWidth: '27px',tabLocation: 'left',                               
        speed: 300,action: 'hover',topPos: '95px',fixedPosition: false                               
    });
    $('#slide-out-FB').tabSlideOut({
        tabHandle: '#handle-FB',pathToTabImage: 'images/vtab_FB.png',          
        imageHeight: '160px',imageWidth: '27px',tabLocation: 'left',                               
        speed: 300,action: 'hover',topPos: '256px',fixedPosition: false                               
    });
});


function changeurlvars(varin, oldval, newval) {
	var currentUrl = window.location.href;
	if (currentUrl.indexOf(varin) != -1) currentUrl = currentUrl.replace("&"+varin+"="+oldval,"");
	window.location = currentUrl+"&"+varin+"="+newval;
	return false;
}
function findValue(li) {
	if( li == null ) return alert("No match!");
	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];
}
function selectItem(li) {
	findValue(li);
}
function formatItem(row) {
	return row[0];
}
function lookupAjax(){
	var oSuggest = $("#LocAjax")[0].autocomplete;
	oSuggest.findValue();
	return false;
}
function openlocation() {
	$('#search-one').hide();
	$('#search-two').hide();
//	$('#fadeaway').delay(2000).fadeOut("slow");
//	$('#fadeaway_ie').delay(2000).fadeOut("slow");
//	animate_bg($('#hpmap'), 10);
}
function closelocation() {
	$('#resultsdiv').hide();
	$('#search-one').fadeIn("slow");
	$('#search-two').fadeIn("slow");
}

function ddshow(id) {
	$("#ddheader-"+id).addClass('highlighted');
	$("#ddlink-"+id).show();
	$("#ddcontent-"+id).show();
}
function ddhide(id) {
	$(".dropdownhd").removeClass('highlighted');
	$(".dropdownln").hide();
	$(".dropdowndd").hide();
}

/********************/
/* viewer functions */
/********************/

var rootChartUrl = "/Charts"; // change for live

var i=2,wxcht="transp_accumulatedprecip",area="nweurope",imagepreload=[];
var chartImageExt = ".gif";

function ddhide(){
	$(".dropdownhd").removeClass("highlighted");
	$(".dropdownln").hide();
	$(".dropdowndd").hide();
}
function toggleLocs(){
	$("#locationsImg").attr("src",rootChartUrl+"/topo/"+area+"_locations.gif");
	$("#locationsImg").toggle();
	return false;
}
function toggleLats(){
	$("#latlongsImg").attr("src",rootChartUrl+"/topo/"+area+"_grid.gif");
	$("#latlongsImg").toggle();
	return false;
}
function getarea(a,c){
	area=a;
	if(c!="")wxcht=c;
	if (wxcht == "transp_accumulatedprecip") chartImageExt = ".png"; // switch for 'weather' charts

	/* IE6 PNG fix...
	var shim = 'x.gif'; // Path to a transparent GIF image
	function(img) {
		var src = img.src;
		img.style.width = img.width + "px";
		img.style.height = img.height + "px";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		img.src = shim;
	}
	*/

	$("#backgimage").attr("src",rootChartUrl+"/topo/"+area+"_topo.gif");
	$("#locationsImg").attr("src",rootChartUrl+"/topo/"+area+"_locations.gif");
	$("#latlongsImg").attr("src",rootChartUrl+"/topo/"+area+"_grid.gif");
	$("#foregimage").attr("src",rootChartUrl+"/"+area+"/"+wxcht+i+chartImageExt);
	preload();
}
function preload(){
	imagepreload[0]=new Image;
	imagepreload[0].src="images/icons/controls/Stop.gif";
	for(j=1;j<=61;j++){imagepreload[j]=new Image;imagepreload[j].src=rootChartUrl+"/"+area+"/"+wxcht+j+chartImageExt};
}
function nextimage(){
	unhighlight(i);
	if(i<=60)i+=1;
	else i=2;
	highlight(i);
}
function plus24(){
	unhighlight(i);
	if(i<=53)i+=8;
	else i=i-60+8;
	highlight(i);
}
function minus24(){
	unhighlight(i);
	if(i>9&&i<62)i-=8;
	else i=i+61-8;
	highlight(i);
}
function previmage(){
	unhighlight(i);
	if(i>2)i-=1;
	else i=61;highlight(i);
}
function highlight(a){
	$("#foregimage").attr("src",rootChartUrl+"/"+area+"/"+wxcht+a+chartImageExt);
	$("#t_"+(a-1)).attr("class","timeimage_on1");
	$("#t_"+a).attr("class","timeimage_on");
	$("#t_"+(a+1)).attr("class","timeimage_on1");
}
function unhighlight(a){
	$("#t_"+(a-1)).attr("class","timeimage_off");
	$("#t_"+a).attr("class","timeimage_off");
	$("#t_"+(a+1)).attr("class","timeimage_off");
}
function changetime(a){
	unhighlight(i);
	i=a;highlight(i);
}
function playstop(){
	if($("#playbutton").attr("title")=="play"){
		$("#playbutton").attr({src:"images/icons/controls/Stop.gif",alt:"stop",title:"stop","class":"stopgerp"});
		interval=setInterval("nextimage()",300);
	} else {
		$("#playbutton").attr({src:"images/icons/controls/Play.gif",alt:"play",title:"play","class":"playgerp"});
		clearInterval(interval);
	}
}
function keycontr(a){
	var c;
	if(!a)a=window.event;
	if(a.keyCode)c=a.keyCode;
	c==39&&nextimage();
	c==37&&previmage();
	c==38&&changetime(61);
	c==40&&changetime(2);
}
document.onkeydown=keycontr;
function replaceViewerTitle(a,c){
	if(c>0){
		$("#timeinfo").html(a);
		clearInterval(interval_title);
	} else {
		interval_title=setInterval("replaceViewerTitle('"+a+"',1)",1E4);
	}
}
function animate_bg(ele, from) {
	from--;
	ele.css("border-width", from); 
	if(from > 1) setTimeout(function() { animate_bg(ele, from) }, 70);
}

