jQuery.fn.buildSlideshow = function() {
	// Custom built jQuery plugin to add basic image viewer
	var loadingimg = '../images/loading.gif';

	this.each(function() {
		var context = this; // to allow multiples of the same code on a page

		jQuery('ul a', context).each(function() {
			jQuery(this).click(function() {
				jQuery('.slideshowimage img', context).attr('src', jQuery(this).attr('href') ).css('background', 'url(' + loadingimg + ') 50% no-repeat'); // show loading image in background;
				// Add visual indicator that image is selected
				jQuery('.selected', context).removeClass('selected');
				jQuery(this).addClass('selected');
				return false;
			});

			// setup to show inital thumbnail as selected
			if ( jQuery('.slideshowimage img', context).attr('src') == jQuery(this).attr('href') ) {
				jQuery(this).addClass('selected');
			}
		});
	});
};

jQuery.fn.centerPolaroids = function() {
	// Custom built jQuery plugin just for this site
	// CSS dependent

	this.each(function() {
		var ss = jQuery(this);

		// get slideshow width
		ss.x = ss.width();

		// get number of Polaroids
		ss.no = ss.find('li a').length;

		// get first Polaroid
		ss.first = ss.find('li a:first'); // assumption: they are all the same style

		// get width of Polaroids
		ss.px = ss.first.width();
		ss.px += parseInt( ss.first.css('padding-left') );
		ss.px += parseInt( ss.first.css('padding-right') );
		ss.px += parseInt( ss.first.css('border-left-width') );
		ss.px += parseInt( ss.first.css('border-right-width') );
		ss.px += parseInt( ss.first.css('margin-left') );
		ss.px += parseInt( ss.first.css('margin-right') );

		// calc margin to add to indent right to make look centered
		ss.indent = (ss.x - (ss.no * ss.px)) / 2;
		ss.first.css('margin-left', ss.indent);
	});
};

$(function() { // like window.onload
	$('.email').defuscate({link: false});
	$('.slideshow').buildSlideshow();
	$('.slideshow').centerPolaroids();
})

var domain = 'angasplainswines.com.au';
function email(val) {
	document.write('<a href="mailto:' + val + '@' + domain + '">');
	document.write(val + '@' + domain + '</a>');
}