/*
 * jQuery JavaScript Plugin jquery.carousel3D 0.9.6
 * http://bugsoftware.co.uk/jQuery/
 *
 * Copyright (c) 2009 - 2010 Ritchie Comley
 * Dual licensed under the MIT and GPL licenses.
 *
 * Date: 2010-02-03 (Wed, 03 February 2010)
 * Revision: 33
 *
 * Dependencies:
 * jQuery 1.4.1 (jquery.com)
 * jquery.uniqueId.js (http://bugsoftware.co.uk/jQuery/)
 * jQuery plugin: Tooltip 1.3 (http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/)
 * 
 */
 
jQuery.fn.carousel3D = function(settings) {

	var flashPath = 'flash/';
	
	if(typeof(settings) == 'undefined')
	{
		settings = {};
	}	

	if(typeof(settings.flashPath) != 'undefined')
	{
		flashPath = settings.flashPath;
	}

	if (!swfobject.hasFlashPlayerVersion("10.0.0"))
	{
		return this.each(function()
		{
			var jWrapper = jQuery(this);
		
			jWrapper.find("ul").remove();
		
			// Attach express install if we don't have new enough version of flash
			var id = jQuery("<div></div>").uniqueId().appendTo(jWrapper).attr("id"); 
		
			var att = {data:flashPath + "expressInstall.swf", width: "100%", height: "100%"};
			var par = {};
				
			var newFlash = swfobject.createSWF(att, par, id);
		});
	}

	var dateObj = new Date; 
	
	var path = flashPath + "Carousel3D.swf?" + dateObj.getTime();

	return this.each(function()
	{
		if(settings == null)
		{
			settings = {};
		}
		
		var images = [];
	
		var jWrapper = jQuery(this);
		var jImageList = jWrapper.find(">ul>li");
		
		jImageList.each(function()
		{
			var jImageListItem = jQuery(this);
			var jImageLink = jImageListItem.find("a");
			var description = jImageLink.text();

			var imageData = {src:jImageLink.attr("href")};
			
			if(description)
			{
				imageData.description = description;
			}
			
			images.push(imageData);
		});

		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><div class="helper"></div>').appendTo(jWrapper);
		
		jWrapper.find("ul").remove();
		
		var flashVars = "pathsCallback=jQuery.fn.carousel3D.instances." + id + ".getImageData" + 
			"&settingsCallback=jQuery.fn.carousel3D.instances." + id + ".getSettings" +
			"&showDescriptionCallback=jQuery.fn.carousel3D.instances." + id + ".showDescription" +
			"&hideDescriptionCallback=jQuery.fn.carousel3D.instances." + id + ".hideDescription";
		
		var att = {data:path, width: "100%", height: "100%"};
		var par = {
			wmode:"transparent",
			flashvars:flashVars};
			
		var newFlash = swfobject.createSWF(att, par, id);
		
		var jHelper = jWrapper.find(".helper");
		
		jHelper.tooltip({
			track: true,
			bodyHandler: function() { 
				return newFlash.getDescription(); 
			}});
			
		jQuery.fn.carousel3D.instances[id] = {
			getImageData:function()
				{
					return images;
				},
			loaded:function()
				{

				},
			getSettings:function()
				{
					return settings;
				},
			showDescription:function()
				{
					jHelper.trigger("mouseover");
				},
			hideDescription:function()
				{
					jHelper.trigger("mouseout");
				}};
			
		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)
			{
				var html = '<p><label>' + val + ': '
				
				if(typeof(settings[val]) == 'boolean')
				{
					html += '<input type="checkbox" class="' + val + '" value="1"'
					
					if(settings[val])
					{
						html += ' checked="checked"';
					}
						
					html += '/>';
				}
				else
				{
					html += '<input type="text" class="' + val + '" value="' + settings[val] + '"/>';
				}
				
				html += '</label></p><div class="clear"></div>';
				
				jQuery(html).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())
						{
							if(jInput.attr('type') == 'checkbox')
							{
								settings[val] = jInput.is(':checked');
							}
							else
							{
								try
								{
									settings[val] = eval(jInput.val());
								} catch(e) {};
							}
						}
					}
					
					newFlash.redraw(settings);
				});
		}
	});
};

jQuery.fn.carousel3D.instances = {};
