//Generic AJAX function to call a php script and send results to specified DIV tag
function ajaxCall(div,php,ldstr)
{
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  } else {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function() {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
		document.getElementById(div).innerHTML=xmlhttp.responseText;
		} else {
		if (ldstr!=undefined) {
		document.getElementById(div).innerHTML=ldstr;
		}
	 }}
	xmlhttp.open("GET","./"+php,true);
	xmlhttp.send();
}
function getTides(){
	var dt=document.getElementById('nav-date').value;
	var ts=document.getElementById('nav-tidal-station');
	ajaxCall ("nav-data-tides","/wp-content/themes/sanity/get-tides.php?ts="+ts.value+"&date="+dt,"<center><image src='/wp-content/themes/sanity/images/ui-loading_16x16.gif' id='ld' />");
}
function getNavData() {
	var dt=document.getElementById('nav-date').value;
	var ts=document.getElementById('nav-tidal-station');
	ajaxCall ("nav-right-content","/wp-content/themes/sanity/nav-data.php?ts=data/tides/"+ts.value+".dat&date="+dt,"<center><image src='/wp-content/themes/sanity/images/ui-loading_16x16.gif' id='ld' />");
}
function getPosition(){	
	ajaxCall ("live-data-position","/wp-content/themes/sanity/get-position.php","<center><image src='/wp-content/themes/sanity/images/ui-loading_16x16.gif' id='ld' />");
}
function showDisclaimer(){	
	ajaxCall ("live-data","/wp-content/themes/sanity/disclaimer.php","<center><image src='/wp-content/themes/sanity/images/ui-loading_16x16.gif' id='ld' />");
}
function closeDisclaimer(){	
	ajaxCall ("live-data","/wp-content/themes/sanity/livedata.php","<center><image src='/wp-content/themes/sanity/images/ui-loading_16x16.gif' id='ld' />");
}
//popup camera images
$(function() {
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();		
		var id = $(this).attr('href');
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});	
		$('#mask').fadeIn(500);	
		$('#mask').fadeTo("fast",0.8);	
		var winH = $(window).height();
		var winW = $(window).width();
		$(id).css('top',  150);	
		$(id).css('left', winW/2-$(id).width()/2);
		$(id).fadeIn(1000); 
	});
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		changeCamImage(1);
		$('#mask').hide();
		$('.window').hide();
	});		
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
});	
//Show date picker
$(function() {
		$( "#nav-date" )
		.datepicker (
			{beforeShowDay: 
			function(date){
				var cDt = new Date();
				cDt.setDate(cDt.getDate()-1);
				if (date >= cDt){
					return [true,""];
				} else {
					return [false,""];
				}
			}
			,onSelect: function(dateText, inst) {getNavData();} })		
});		
function setNavDate(){
	var dt=document.getElementById("nav-date");
	vdt=newDate();
	dt.value=vdt;
}
function getLongDate() {
	var dayNames=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	var monthNames=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	var ext;
	var dt;
	var now = new Date();
	dt=now.getDate();
	ext="th";
	if (dt==1 || dt==21 || dt==31){ ext="st"; }
	if (dt==2 || dt==22){ ext="nd"; }
	if (dt==3 || dt==23){ ext="rd"; }
	return dayNames[now.getDay()]+" "+now.getDate()+ext+" "+monthNames[now.getMonth()]+" "+now.getFullYear();
}
function formatDate (dtf) {
	var rdt = new Date(dtf);
	var dy=rdt.getDate();
	var mth=rdt.getMonth()+1;
	if (dy < 10) dy="0"+rdy;	
	if (mth < 10) mth="0"+mth;	
	return dy+"/"+mth+"/"+rdt.getFullYear();
}
$(function() {
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-form" ).dialog({
		autoOpen: false,
		height: 350,
		width: 580,
		modal: true,
		buttons: {
			Close: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
		}
	});

	$( "#flights-dialog" )
		.button()
		.click(function() {
			$( "#dialog-form" ).dialog( "open" );
		});
});
function changeCamImage(lnum){
	ajaxCall ("dialog-content","/wp-content/themes/sanity/camwindow.php?q="+lnum,"<image src='/wp-content/themes/sanity/images/cam-loading.png' id='ld' />");
}

(function($){var current=null;$.fn.rssfeed=function(url,option7s){var defaults={limit:3,header:true,titletag:'h4',date:true,content:true,snippet:true,showerror:true,errormsg:'',key:null};var options=$.extend(defaults,options);return this.each(function(i,e){var $e=$(e);if(!$e.hasClass('rss-feed'))$e.addClass('rssFeed');if(url==null)return false;var api="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+url;if(options.limit!=null)api+="&num="+options.limit;if(options.key!=null)api+="&key="+options.key;$.getJSON(api,function(data){if(data.responseStatus==200){_callback(e,data.responseData.feed,options);}else{if(options.showerror)
if(options.errormsg!=''){var msg=options.errormsg;}else{var msg=data.responseDetails;};$(e).html('<div class="rss-error"><p>'+msg+'</p></div>');};});});};var _callback=function(e,feeds,options){if(!feeds){return false;}
var html='';var row='odd';if(options.header)
html+='<div class="rss-header">'+feeds.title+'</div>';html+='<div class="rss-body">'+'<ul>';for(var i=0;i<feeds.entries.length;i++){var entry=feeds.entries[i];var entryDate=new Date(entry.publishedDate);var pubDate=entryDate.toLocaleDateString()+' '+entryDate.toLocaleTimeString();html+='<li class="rss-row '+row+'">'+'<'+options.titletag+'><a href="'+entry.link+'" target="_blank" title="View this feed at '+feeds.title+'">'+entry.title+'</a></'+options.titletag+'>'
if(options.date)html+='<div>'+pubDate+'</div>'
if(options.content){if(options.snippet&&entry.contentSnippet!=''){var content=entry.contentSnippet;}else{var content=entry.content;}
html+='<p>'+content+'</p>'}
html+='</li>';if(row=='odd'){row='even';}else{row='odd';}}
html+='</ul>'+'</div>'
$(e).html(html);};})(jQuery);


