/* 
 * PSCHEESECAKE.COM - WEDDING CAKE GALLERY
 *
 * IMPORTANT: Enclose every link in .col2 within <noscript>...</noscript>
 * tags to hide the links from the browser. This will prevent the gallery
 * images from downloading unless Javascript is not enabled, in which case
 * all bets are off.
 *
 */ 

/* 
 * Custom plugin to load images dynamically
 *
 * Based on information in the following blog post:
 * http://letmehaveblog.blogspot.com/2006/08/simple-jquery-plugin-to-load-images.html 
 *
 * src = Image URL or filename
 * f = onload function to execute
 *
 */
(function($) {

	$.fn.image = function(src, f){

   	 	 return this.each(function() { 

   	     var i = new Image();
   	     i.onLoad = f;
   	     i.src = src;
   	 	 this.appendChild(i);

   	 });

	}

})(jQuery)


/* 
 * Set up a click event on each of the cake thumbnails in 
 * the wedding cake gallery.
 *
 * This script assumes all thumbnails are "...-sm.jpg" and their 
 * full-size gallery equivalents are named identically but w/o "-sm".
 *
 */
function getImg () {

	// Set up the initial image. Yeah, it's repetitive.
	// Whatever's in #default-cake in the thumbnail section 
	// will be used as the first image.
	imgIdStr = $('#default-cake').children('img').attr('src');
	lgImgUrl = imgIdStr.replace('-sm', '');
	//$('.col2').append('<img id="progress" alt="Progess indicator animation" src=" />');
	$('.col2').append('<div id="cake"></div>');
	$('#cake').image(lgImgUrl);

	$('.col1 li a').click(function() {

		// Get the name of the image to load
		imgIdStr = $(this).children('img').attr('src');
		lgImgUrl = imgIdStr.replace('-sm', '');

		// Change the ID of #cake so we can cleanly delete it later
		// (If you delete #cake, even the new image will be deleted)
		$('#cake').attr('id', 'cake-delete');

		// Add a container to hold the NEW image
		$('.col2').append('<div id="cake-vis"></div>');
		
		// Add the new image
		$('#cake-vis').image(lgImgUrl);

		// Fade in the new image div (it's 'display: none' by default)
		// Delete the #cake-delete div when the new image is fully visible
		$('#cake-vis').fadeIn('slow', function() {
			$('#cake-delete').remove();
		});

		// Make the new image into #cake so it can be replaced with a 
		// new image 
		$('#cake-vis').attr('id', 'cake');

	});

}


/*
 * See which browser we're dealing with. Added mostly to detect Firefox
 * because of strange Firefox-specific CSS issues.
 *
 */

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


