/*
 * -----------------------------------------------------------------
 * Copyright (c) Fluid, Inc. All Right Reserved.
 * This software is the proprietary information of Fluid, Inc.
 * Use is subject to strict licensing terms.
 * -----------------------------------------------------------------
 *
 * CVS Information:
 * $Author: fluiddev $
 * $Date: 2007/08/15 22:20:52 $
 * $Revision: 1.1 $
 *
 */

var fluid = (fluid == null) ? new Object() : fluid;

/**
 * Manages a collection of recently viewed item (products) for use with the Scroller
 * NOTE: This class require swfobject.js
 * @args - An object containing any one of the scroller properties
 *  swfUrl: String - The URL to the Scroller SWF file
 *  swfId: String - A unique per page ID that identifies the Scroller
 *  width: Number - The width of the Scroller
 *  height: Nunmber - The height of the Scroller
 *  userInterfaceXmlUrl: String - The URL to the user interface XML file which describes the Scroller
 *  userInterfaceXml: String - A string of XML that represents the user interface
 *  scrollerId: String - The id of the Scroller as specified in the user interface XML file
 *  itemsXmlUrl: String - URL to an XML file describing the item data the scroller will consume
 *  itemsXml: String - XML string describing the item data the scroller will consume
 *  eventHandler: Strings - An optional JavaScript method name that will execute when the item in the Scroller changes
 *  baseUrl: String - URL that represents the base path in which the Scroller / SWF will load assets from
 */
fluid.Scroller = function(args)
{
	for(var p in args)
	{
		this[p] = args[p]
	}
}

	// Static SWF property IDs
	fluid.Scroller.SWF_ID_BASE = "fluid_Scroller_";
	fluid.Scroller.TEMPLATE_XML = "templateXml";
	fluid.Scroller.ITEMS_XML = "itemsXml";
	fluid.Scroller.ADD_ITEM = "addItem";
	fluid.Scroller.ADD_ITEM_AT = "addItemAt";
	fluid.Scroller.REMOVE_ITEM = "removeItem";
	fluid.Scroller.REMOVE_ITEM_AT = "removeItemAt";
	fluid.Scroller.SHOW_ITEM = "showItem";
	fluid.Scroller.SHOW_ITEM_AT = "showItemAt";
	
	// SWF Properties
	fluid.Scroller.prototype.swfUrl = "standard/v2/swf/Scroller.swf";
	fluid.Scroller.prototype.swfId = null; // default == scrollerId
	fluid.Scroller.prototype.width = null;
	fluid.Scroller.prototype.height = null;
	fluid.Scroller.prototype.backgroundColor = "#ffffff";
	fluid.Scroller.prototype.base = ".";
	fluid.Scroller.prototype.baseUrl = "";
	
	// Scroller Properties
	fluid.Scroller.prototype.userInterfaceXml = null;
	fluid.Scroller.prototype.userInterfaceXmlUrl = null;
	fluid.Scroller.prototype.scrollerId = null;
	fluid.Scroller.prototype.itemsXml = null;
	fluid.Scroller.prototype.itemsXmlUrl = null;
	fluid.Scroller.prototype.eventHandler = null;

	/**
	 * Writes the Scroller to the div specified
	 * @param container: Div
	 */
	fluid.Scroller.prototype.write = function(container)
	{
		if(this.swfId == null) this.swfId = this.scrollerId;
		
		var swf = new SWFObject(this.swfUrl, this.swfId, this.width, this.height, "7", this.backgroundColor);
		
		swf.addParam("wmode", "opaque");
		swf.addParam("swLiveConnect", "true");
		swf.addParam("base", this.base);
		
		swf.addVariable("baseUrl", this.baseUrl);
		swf.addVariable("scrollerId", this.scrollerId);
		
		if(this.userInterfaceXml != null) swf.addVariable("userInterfaceXml", this.userInterfaceXml);
		if(this.userInterfaceXmlUrl != null) swf.addVariable("userInterfaceXmlUrl", this.userInterfaceXmlUrl);		
		if(this.itemsXml != null) swf.addVariable("itemsXml", this.itemsXml);
		if(this.itemsXmlUrl != null) swf.addVariable("itemsXmlUrl", this.itemsXmlUrl);
		if(this.eventHandler != null) swf.addVariable("eventHandler", this.eventHandler);
		
		var ieFSCommandProxy = '<scr' + 'ipt event="FSCommand(command, args)" for="' + this.swfId + '">\n ' + this.swfId + '_DoFSCommand(command, args)</scr' + 'ipt>';
		swf.write(container, ieFSCommandProxy);
		this.createFSCommandProxy(container);
	}
	
	// API
	
	/**
	 * Assigns the Scroller a new User Interface Template
	 * @param xml - A string of XML describing the template
	 */
	fluid.Scroller.prototype.setTemplateXml = function(xml)
	{
		this.templateXml = xml;
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.TEMPLATE_XML;
		window.document[this.swfId].SetVariable(variableId, xml);
	}
	
	/**
	 * Assigns the Scroller a new Set of items
	 * @param xml - A string of XML describing the items
	 */
	fluid.Scroller.prototype.setItemsXml = function(xml)
	{
		this.itemsXml = xml;
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.ITEMS_XML;
		window.document[this.swfId].SetVariable(variableId, xml);
	}
	
	/**
	 * Adds an item to the Scroller
	 * @param id - A unique identifier for the item
	 * @param url - The url to the item
	 */
	fluid.Scroller.prototype.addItem = function(id, url)
	{
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.ADD_ITEM;
		window.document[this.swfId].SetVariable(variableId, "<Item id='" + id + "' url='" + url + "'/>");
	}
	
	/**
	 * Adds an item to the Scroller a the index specified
	 * @param id - A unique identifier for the item
	 * @param url - The url to the item
	 * @param index - The zero based index where the item will be added.
	 */
	fluid.Scroller.prototype.addItemAt = function(id, url, index)
	{
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.ADD_ITEM_AT;
		window.document[this.swfId].SetVariable(variableId, "<Item id='" + id + "' url='" + url + "'/>," + index);
	}
	
	/**
	 * Removes all of the items from the Scroller
	 */
	fluid.Scroller.prototype.removeAllItems = function()
	{
		this.setItemsXml("<items></items>");
	}

	/**
	 * Removes an item from the Scroller by its ID
	 * @param index - The index of the item
	 */
	fluid.Scroller.prototype.removeItem = function(id)
	{
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.REMOVE_ITEM;
		window.document[this.swfId].SetVariable(variableId, id);
	}

	/**
	 * Removes an item from the Scroller at a specified index
	 * @param id - A unique identifier for the item
	 */
	fluid.Scroller.prototype.removeItemAt = function(index)
	{
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.REMOVE_ITEM_AT;
		window.document[this.swfId].SetVariable(variableId, index);
	}
	
	/**
	 * Scrolls to the item specified by its ID
	 * @param id - The ID of the item
	 */
	fluid.Scroller.prototype.showItem = function(id)
	{
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.SHOW_ITEM;
		window.document[this.swfId].SetVariable(variableId, id);
	}
	
	/**
	 * Scrolls to the item specified by its index
	 * @param index - The index of the item
	 */
	fluid.Scroller.prototype.showItemAt = function(index)
	{
		var variableId = fluid.Scroller.SWF_ID_BASE + fluid.Scroller.SHOW_ITEM_AT;
		window.document[this.swfId].SetVariable(variableId, index);
	}
	
	//
	
	/**
	 * Creates the functions that will handle fscommand calls from the Scroller
	 * @private
	 */
	fluid.Scroller.prototype.createFSCommandProxy = function()
	{
		window[this.swfId + "_DoFSCommand"] = function(command, args)
		{
			// Check for FSCommand: in the command/function name as is the case on the Mac
			if(command.indexOf("FSCommand:") == 0) command = command.split("FSCommand:")[1];
			var func = window[command];
			if(func != null) func.apply(window, args.split(","));
		}
	}
	
//

	
