$(document).ready(function(){
   setLink();
   
   var path = location.pathname.substring(1);
   var searchString = location.search;
   newpath = path.slice(path.lastIndexOf('/')+1);
   
   if(searchString){
	 searchString = searchString.replace(/\//gi, "");
	 searchString = searchString.replace(/%20/gi, " ");
	 searchString = searchString.replace(/\\/gi, "\/");
	 
	 newpath = newpath+searchString;
   }
   //alert(newpath + " " + topmenu );
   if (newpath){$('a.navText[href="' + newpath + '"]').addClass('selected');}
   if($("#weather").length > 0){
	 $('#weather').weatherfeed(['LAXX0001'],{unit:'c',image:false,highlow:false,wind:false,link:false,showerror:false,city_title:'Luang Prabang'});
   }
   
   if($("#dhClock").length > 0){
	 updateClock();
	 setInterval('updateClock()', 1000 );
   }
	
	if($("#vatphou_cruise_video").length > 0){
		$("#vatphou_cruise_video").click(function(){
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut': 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'			: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'				: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
			return false;
		});
	}
   
});

function setLink(){
   var url="load_visit.php";
   xmlhttp = getXMLHttpObject();
   xmlhttp.open("GET", url, false);
   xmlhttp.send(null);
   var linkarr=xmlhttp.responseText.split('[#]');
   var table=document.getElementById('tbVisit');
   for(i=0;i<linkarr.length;i++){
	 var row=table.insertRow(table.rows.length);
	 var cell=row.insertCell(0);
	 var tmpArr=linkarr[i].split('[:]');
	 cell.innerHTML='<a href="town_village_names.php?'+tmpArr[0]+'" class="navText">'+tmpArr[1]+'</a>';
   }
}

function updateClock(){
   var currentTime = new Date();
   var currentHours = currentTime.getHours();
   var currentMinutes = currentTime.getMinutes();
   var currentSeconds = currentTime.getSeconds();
   
   // Pad the minutes and seconds with leading zeros, if required
   currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
   currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
   
   // Choose either "AM" or "PM" as appropriate
   var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
   
   // Convert the hours component to 12-hour format if needed
   currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
   
   // Convert an hours component of "0" to "12"
   currentHours = ( currentHours == 0 ) ? 12 : currentHours;
   
   // Compose the string for display
   var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
   
   // Update the time display
   $("#dhClock").html(currentTimeString);
}
