jQuery.fn.extend({
   absolutePosAndSize : function() {
       obj = $(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
       }
       return {left:curleft,top:curtop, width:$(this).width(), height:$(this).height()};
   }
});

var ZeroClipboard = {
	client: null, 
	dispatch: function(id, eventName, args) {
		client.receiveEvent(eventName, args);
	},
	Client: function() { 
		client = this;
	}
};

ZeroClipboard.Client.prototype = {
	ready: false, 
	movie: null, 
	clipText: '', 
	handCursorEnabled: true,
	jqobjUnder: null,
	jqobjNotifier: null,
	
	glue: function(jqobj) {
	
		if ($('#swfCopyToClipboardContainer').length <= 0)
		{
			swfHTML = AC_FL_GetContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
				'width', jqobj.width(),
				'height', jqobj.height(),
				'menu', 'false',
				'src', 'http://' + location.hostname + '/js/jQuery.copy.swf',
				'quality', 'best',
				'allowScriptAccess', 'always',
				'loop', 'false',
				'id', 'swfCopyToClipboard',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'movie', 'http://' + location.hostname + '/js/jQuery.copy.swf',
				'flashvars', 'id=swfCopyToClipboard&width=' + jqobj.width() + '&height=' + jqobj.height(),
				'wmode', 'transparent'
			);

			// Old method else IE bug !!!!
			this.div = document.createElement('div');
			this.div.id = 'swfCopyToClipboardContainer';
			var body = document.getElementsByTagName('body')[0];
			body.appendChild(this.div);
			this.div.innerHTML = swfHTML;
		} else {
			$('#swfCopyToClipboard').attr('width', jqobj.width()).attr('height',jqobj.height());
			this.jqobjUnder.trigger('mouseout');
		}
		this.jqobjUnder = jqobj;
		$('#swfCopyToClipboardContainer').css(jqobj.absolutePosAndSize());

	},

	setText: function(newText) {
		this.clipText = newText;
		if (this.ready) this.movie.setText(newText);
	},
	
	glueAndSetText: function(jqobj, newText, notifier) {
		this.jqobjNotifier = notifier;
		this.setText(newText);
		this.glue(jqobj);
	},
	
	receiveEvent: function(eventName, args) {
		eventName = eventName.toString().toLowerCase().replace(/^on/, '');
		switch (eventName) {
			case 'load':
				this.movie = document.getElementById('swfCopyToClipboard');

				if (!this.movie) {
					var self = this;
					setTimeout( function() { self.receiveEvent('load', null); }, 1 );
					return;
				}
				
				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;
			case 'mouseover':
				this.jqobjUnder.addClass('hover');
				this.jqobjUnder.trigger('mouseover');
				break;
			
			case 'mouseout':
				this.jqobjUnder.removeClass('hover');
				this.jqobjUnder.trigger('mouseout');
				break;
			case 'complete':
				this.jqobjNotifier.text(args + '');
				
				//first slide down and blink the message box
				$("#message-alert").animate({top:"40px"}, 500).animate({top:"20px"}, 250).animate({top:"20px"}, 2000).animate({top:"40px"}, 500).animate({top:"-350px"}, 500)
				
				//close the message box when cross red image is clicked
				//$("#close_message").click(function()
				//{
				//$("#message-alert").fadeOut("slow");
				//});
				
				break;
		}
	}
};

var clipBoardCopy = new ZeroClipboard.Client();
