// JavaScript Document

/* Excute JQ load functions
------------------------------------------------------------------*/
	$(document).ready(function() {
		// Remove borders from images that are links (can't do in pure CSS)
		$('img').parent('a').addClass('bdr0');
	
		// Unhide stuff that's for JS users only
		$('.jsOnly').addClass('unhide');
		
		// Show quick links
		$('#scrollLink').show();
		
		// Attach scroll events to quick links
		$('#scrollLink a').click(
			function() {
				$.scrollTo( '#blog-' + $(this).attr('id'), 2000, {offset: {top: -10, left: 0} } );
				$('#toTop').show(400);
			}
		);
		
		// Attach scroll event to return button
		$('#toTop').click(function() {
			$.scrollTo( '#scrollLink', 2000, {offset: {top: -10, left: 0} } );
			$('#toTop').hide(200);
		});
		
		// Scale blog images
		var blogW = $(".blogWrap").width();
		$(".blogWrap img").each( function() {
			var imgW = $(this).width();
			if ( imgW > blogW ) {
				var imgH = $(this).height();
				$(this).css( { width : blogW + "px", height : blogW * (imgH / imgW) + "px" } );
			};
		});
	});


/* Show loading animation (after clearing any existing animation showing after Back)
------------------------------------------------------------------*/
	function throb(id) {
		$(".throbber").css("visibility", "hidden");
		document.getElementById('throb' + id).style.visibility = 'visible';
	}


/* Manage expand/collapse table rows (e.g. tasting notes)
------------------------------------------------------------------*/
	function expand(id) {
		$('#minus' + id).removeClass('squash');
		$('#plus' + id).removeClass('unhide');
		$('#plus' + id).addClass('squash');
		$('#wine' + id + 'extra').removeClass('squash');
	};
	
	function collapse(id) {
		$('#minus' + id).addClass('squash');
		$('#plus' + id).removeClass('squash');
		$('#plus' + id).addClass('unhide');
		$('#wine' + id + 'extra').addClass('squash');
	};


/* Service "all" checkbox (e.g. wine table Adv. Search/Region)
------------------------------------------------------------------*/
	// by Shawn Olson www.shawnolson.net
	function checkUncheckAll(theElement) {
		var theForm = theElement.form, z = 0;
		for(z=0; z<theForm.length;z++)	{
			if (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
				theForm[z].checked = theElement.checked;
			}
		}
	}


/* Warn if attempting to change redirected URL
------------------------------------------------------------------*/
	function warn() {
		alert("Only change the rewritten URL if you are sure");
	}


/* Present number with thousands separators
------------------------------------------------------------------*/
	// from www.mredkj.com
	function thou(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while ( rgx.test(x1) ) {
			x1 = x1.replace( rgx, '$1' + ',' + '$2' );
		}
		return x1 + x2;
	}	
	

/* Pad number to 2dp
------------------------------------------------------------------*/
	function pad2(x) {
		var y = "" + x;
		while ( y.length < 2 ) { y = "0" + y };
		return y;
	}


/* Current date-time
------------------------------------------------------------------*/
	function now() {
		var dt = new Date();
		var stamp = pad2( dt.getDate() ) + "/" + pad2( dt.getMonth() + 1 ) + "/" + dt.getFullYear() +
			" " + pad2( dt.getHours() ) + ":" + pad2( dt.getMinutes() ) + ":" + pad2( dt.getSeconds() );
		return stamp;	
	}

	

