/*
 * jQuery JavaScript Plugin jquery.carousel3D 0.9.1
 * http://bugsoftware.co.uk/jQuery/
 *
 * Copyright (c) 2009 Ritchie Comley
 * Dual licensed under the MIT and GPL licenses.
 *
 * Date: 2009-12-06 (Thu, 6 December 2009)
 * Revision: 9
 *
 * Dependencies:
 * jQuery 1.3.2 (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.flashPath) != 'undefined')
	{
		flashPath = settings.flashPath;
	}
	
	if (!swfobject.hasFlashPlayerVersion("10.0.0"))
	{
		return this.each(function()
		{
			var jWrapper = jQuery(this);
		
			// 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 path = flashPath + "Carousel3D.swf";

	
	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 jImage = jImageListItem.find("img");
			var description = jImage.attr("alt");
			var jLink = jImageListItem.find("a");
			
			var imageData = {src:jImage.attr("src")};
			
			if(description)
			{
				imageData.description = description;
			}
			
			var link = "";
			
			if(jLink.size() && (link = jLink.attr("href")))
			{
				imageData.link = link;
			}
			
			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()
				{
					//console.log(id + "loaded");
				},
			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)
			{
				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 = {};