var timer;

function jgetcoordinates(txtpostcode) {

	var localSearch = new GlocalSearch();

	if (txtpostcode != "")
	{
		localSearch.setSearchCompleteCallback(null,
			function() {
				if (localSearch.results[0])
				{
					//console.log(localSearch.results);
						
					var resultLat = localSearch.results[0].lat;
					var resultLng = localSearch.results[0].lng;
					var point = new GLatLng(resultLat,resultLng);
			
					alert (txtlatitude+"/"+txtlongitude);

					// geocode distance calc from simply dutch!
					
					
				} else {	
					alert("WARNING : Postcode not found!");
				}
			});
		
		localSearch.execute(txtpostcode + ", UK");
		return false;
	} 
}

$(document).ready(function(){

	// ================================================================================================
	// Activate menu
	$("ul.topnav li span").hover(function() { //When trigger is clicked...
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown(300).show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp(300); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

	// ================================================================================================
	// Activate thumbnail viewer if there is one
	jQuery.noConflict();
	$(".shots").yoxview({
			backgroundColor: 'Grey',
			playDelay: 5000
	});

	// ================================================================================================
	// Activate TABS if there are any
	// Simple Tab code from : http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});

	// ================================================================================================
	// Detect shipping calculation click.
//	$("#calculate").click(function() {
//		var postcode = $("#postcode").val();
//		jgetcoordinates (postcode, lat, lng, dsc);
//		return false;
//	});

	$('#calculate').click(function() {	
		$('#shippingresult').html("...calculating...").fadeIn(100);																 
		$.ajax({
			type : 'POST',
			url : 'shipping.php',
			dataType : 'json',
			data: $('#postcode').serialize(),
			success : function(data){
				$('#shippingresult').html(data.msg).fadeIn(900);
//				if (data.msg.search("Invalid") == -1) {
//					$('#login-box-form').fadeOut(0);
//				}

			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
//				alert("Shipping: "+errorThrown);
			}
		});

		return false;
	});
});

$(window).load(function() {

	// ================================================================================================
	// Activate fader if there is one
	$('#slider').nivoSlider({
			effect:'sliceDownLeft', //Specify sets like: 'fold,fade,sliceDown'
			slices:10,
			animSpeed:1000, //Slide transition speed
			pauseTime:5000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:false, //Next & Prev
			directionNavHide:true, //Only show on hover
			controlNav:false, //1,2,3...
			controlNavThumbs:false, //Use thumbnails for Control Nav
			controlNavThumbsFromRel:false, //Use image rel for thumbs
			controlNavThumbsSearch: '.jpg', //Replace this with...
			controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
			keyboardNav:false, //Use left & right arrows
			pauseOnHover:false, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:0.8, //Universal caption opacity
			beforeChange: function(){},
			afterChange: function(){},
			slideshowEnd: function(){}, //Triggers after all slides have been shown
			lastSlide: function(){}, //Triggers when last slide is shown
			afterLoad: function(){} //Triggers when slider has loaded
	});

});
