// Simple Set Clipboard System
// Author: Joseph Huckaby

var ZeroClipboard = {
	
	version: "1.0.7",
	clients: {}, // registered upload clients on page, indexed by id
	moviePath: 'ZeroClipboard.swf', // URL to movie
	nextId: 1, // ID of next movie
	
	setMoviePath: function(path) {
		// set path to ZeroClipboard.swf
		this.moviePath = path;
	},
	
	dispatch: function(id, eventName, args) {
		// receive event from flash movie, send to client		
		var client = this.clients[id];
		if (client) {
			client.receiveEvent(eventName, args);
		}
	},
	
	register: function(id, client) {
		// register new client to receive events
		this.clients[id] = client;
	},
	
	Client: function(elem) {
		// constructor for new simple upload client
		this.handlers = {};
		
		// unique ID
		this.id = ZeroClipboard.nextId++;
		this.movieId = 'ZeroClipboardMovie_' + this.id;
		
		// register client with singleton to receive flash events
		ZeroClipboard.register(this.id, this);
		
		// create movie
		if (elem) this.glue(elem);
	}
};

ZeroClipboard.Client.prototype = {
	
	id: 0, // unique ID for us
	ready: false, // whether movie is ready to receive events or not
	movie: null, // reference to movie object
	clipText: '', // text to copy to clipboard
	handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
	cssEffects: false, // enable CSS mouse effects on dom container
	handlers: null, // user event handlers
	appendElem: null,
    isThumb: false,
	div: null,
	glue: function(elem, appendElem, value) {

        if (this.div) {
            this.div.innerHTML = '';
        }
        
        // glue to DOM element
        // elem can be ID or actual DOM element object
        if (typeof(elem) == 'string') {
            this.domElement = document.getElementById(elem);
            if (!this.domElement) {
                return;
            }
        } else {
            this.domElement = elem;
        }

		// float just above object, or zIndex 99 if dom element isn't set
		var zIndex = 99;
		if (this.domElement.style.zIndex) {
			zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
		}
		
        if (typeof(appendElem) == 'string') {
            this.appendElem = document.getElementById(appendElem);
        } else {
            this.appendElem = appendElem;
        }

        this.clipText = value;

        // create floating DIV above element
        var style = ''
        if (this.isThumb) {
            var width = 160;
            var height = 23;
        } else {
            var width = "100%";
            var height = "100%";
        }

        // get the new div
        var nDiv = jq(this.appendElem).find('.ccplaceholder');
        this.div = nDiv[0];
        
        this.div.innerHTML = this.getHTML(width, height, value);
	},

	getHTML: function(width, height, value) {
		// return HTML for movie
		var html = '';
        
		var flashvars = 'id=' + this.id +
			'&text=' + escape(escape(value));
			
		if (photobucket.browser.isIE) {
			// IE gets an OBJECT tag
			var protocol = 'http://';
			html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+protocol+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" id="'+this.movieId+'" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+ZeroClipboard.moviePath+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/><param name="wmode" value="transparent"/></object>';
		}
		else {
			// all other browsers get an EMBED tag
			html += '<embed id="'+this.movieId+'" src="'+ZeroClipboard.moviePath+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+this.movieId+'" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" wmode="transparent" />';
		}
		return html;
	},
	
	setText: function(newText) {
		// set text to be copied to clipboard
		//this.clipText = newText;
		//if (this.ready) this.movie.setText(newText);
	},
	
	setHandCursor: function(enabled) {
		// enable hand cursor (true), or default arrow cursor (false)
		this.handCursorEnabled = enabled;
		if (this.ready) this.movie.setHandCursor(enabled);
	},

  addEventListener: function(eventName, func) {
    // add user event listener for event
    // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
    eventName = eventName.toString().toLowerCase().replace(/^on/, '');
    if (!this.handlers[eventName]) this.handlers[eventName] = [];
    this.handlers[eventName].push(func);
  },
	
	receiveEvent: function(eventName, args) {
		// receive event from flash
		eventName = eventName.toString().toLowerCase().replace(/^on/, '');
				
		// special behavior for certain events
		switch (eventName) {
			case 'load':
				// movie claims it is ready, but in IE this isn't always the case...
				// bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
				this.movie = document.getElementById(this.movieId);
				if (!this.movie) {
					var self = this;
					setTimeout( function() { self.receiveEvent('load', null); }, 1 );
					return;
				}
				
				// firefox on pc needs a "kick" in order to set these in certain cases
				if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
					var self = this;
					setTimeout( function() { self.receiveEvent('load', null); }, 100 );
					this.ready = true;
					return;
				}
				
				this.ready = true;
				//this.movie.setText( this.clipText );
				//this.movie.setHandCursor( this.handCursorEnabled );
				break;
		} // switch eventName
		
		if (this.handlers[eventName]) {
      for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
        var func = this.handlers[eventName][idx];
      
        if (typeof(func) == 'function') {
          // actual function reference
          func(this, args);
        }
        else if ((typeof(func) == 'object') && (func.length == 2)) {
          // PHP style object + method, i.e. [myObject, 'myMethod']
          func[0][ func[1] ](this, args);
        }
        else if (typeof(func) == 'string') {
          // name of function
          window[func](this, args);
        }
      } // foreach event handler defined
    } // user defined handler for event
		
	},
    
    setIsThumb : function() {
        this.isThumb = true;
    }
	
};

ZeroClipboard.setMoviePath('http://pic.pbsrc.com/flash/ZeroClipboardFV2.swf');

