/*
 * jQuery JavaScript Plugin jquery.carousel3D 0.9.0
 * http://bugsoftware.co.uk/jQuery/
 *
 * Copyright (c) 2009 Ritchie Comley
 * Dual licensed under the MIT and GPL licenses.
 *
 * Date: 2009-09-16 (Thu, 16 Sept 2009)
 * Revision: 3
 *
 * Dependencies:
 * jQuery 1.3.2 (jquery.com)
 * jquery.uniqueId.js (http://bugsoftware.co.uk/jQuery/)
 * 
 */
 
jQuery.fn.carousel3D = function(settings) {

	var flashPath = 'http://www.skuare.net/test/flash/';
	
	if(typeof(settings.flashPath) != 'undefined')
	{
		flashPath = settings.flashPath;
	}
	
	if (!swfobject.hasFlashPlayerVersion("10.0.0"))
	{
		// Attach express install if we don't have new enough version of flash
		var id = jQuery("<div></div>").uniqueId().appendTo(jQuery("body")).attr("id"); 
	
		var att = {data:flashPath + "expressInstall.swf", width: "100%", height: "100%"};
		var par = {};
			
		var newFlash = swfobject.createSWF(att, par, id);
		
		return;
	}

	var path = flashPath + "Carousel3D.swf";

	
	return this.each(function()
	{
		if(settings == null)
		{
			settings = {};
		}
		
		var getImagesForCarousel = function (val)
		{	
			return imagePaths;
		}
		
		var imagePaths = [];
	
		var jWrapper = jQuery(this);
		var jImageList = jWrapper.find("img");
		
		jImageList.each(function()
		{
			var jImage = jQuery(this);
			imagePaths.push(jImage.attr("src"));
		});
		
		var id = jQuery("<div></div>").uniqueId().appendTo(jWrapper).attr("id"); 
		
		jQuery('<p><a href="#" class="prev">prev</a> <a href="#" class="next">next</a></p>').appendTo(jWrapper);
		
		jWrapper.find("ul").remove();
		
		var att = {data:path, width: "100%", height: "100%"};
		var par = {
			wmode:"transparent",
			flashvars:"pathsCallback=jQuery.fn.carousel3D.instances." + id + ".getPaths&settingsCallback=jQuery.fn.carousel3D.instances." + id + ".getSettings"};
			
		jQuery.fn.carousel3D.instances[id] = {
			getPaths:function()
				{
					return imagePaths;
				},
			loaded:function()
				{
					console.log(id + "loaded");
				},
			getSettings:function()
				{
					return settings;
				}};
			
		var newFlash = swfobject.createSWF(att, par, id);
		
		jWrapper.find("a.prev").click(function(evt)
			{
				evt.preventDefault();
				newFlash.sendToActionscript("left");
			});
			
		jWrapper.find("a.next").click(function(evt)
			{
				evt.preventDefault();
				newFlash.sendToActionscript("right");
			});
			
		if(settings.settingExperimenter)
		{
			
			var jExpArea = jQuery('<div class="experimenter"><p><strong>Setting experimenter:</strong></p><div class="settingFields"></div><p><a href="#" class="redraw">redraw</a></p></div>').appendTo(jWrapper);
		
			for(val in settings)
			{
				jQuery('<p><label>' + val + ': <input type="text" class="' + val + '" value="' + settings[val] + '" /></label></p><div class="clear"></div>').appendTo(jExpArea.find('div.settingFields'));
			}
		
			jExpArea.find("a.redraw").click(function(evt)
				{
					evt.preventDefault();
					
					var val;
					var jInput;
					
					for(val in settings)
					{
						jInput = jExpArea.find("input." + val);
						
						if(jInput.size())
						{
							try
							{
								settings[val] = eval(jInput.val());
							} catch(e) {};
						}
					}
					
					newFlash.redraw(settings);
				});
		}
	});
};

jQuery.fn.carousel3D.instances = {};
