/* --------- BEGIN LICENSE NOTICE ---------
 * Copyright 2011 Extentech Inc. All Rights Reserved.
 *
 * This file is a part of the Sheetster Web Application.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * If you would like to redistribute this software in a closed-source
 * application, dual-licensed commercial versions are available. For a
 * fully supported and redistributable commercial license, please visit
 * <http://www.extentech.com> or contact us at:
 * 
 * sales@extentech.com
 * Extentech Inc.
 * 1032 Irving Street #910
 * San Francisco, CA 94122
 * 415-759-5292
 * ---------- END LICENSE NOTICE ----------
 */

// use the parent definition of clipboard if available
var clipboard;
var useparent = false;
try{
	if (window.parent !== window && window.parent.clipboard) {
		clipboard = window.parent.clipboard;
		useparent = true;
	}
}catch(x){
	; // typically permission denied. suppress
}

if(!useparent) {

/** @namespace Manages the DocsFree clipboard for cut/copy/paste operations.
 */
clipboard = {
	active: null,
	
	/** Determines whether the clipboard has content that may be pasted.
	 */
	isActive: function() {
		return this.active !== null;
	},
	
	/** Sets the active clipboard instance.
	 */
	setActive: function (target) {
		if (this.active && this.active !== target) try {
			this.active.clear();
		} catch (e) {}
		
		this.active = target;
	},
	
	/** Gets the active clipboard instance.
	 */
	getActive: function() {
		return this.active;
	},
	
	/** Gets the clipboard of the current active document.
	 * @private
	 */
	getTarget: function() {
		return uiWindowing.getActiveDocument().getClipboard();
	},
	
	/** Cuts the selection from the active document to the clipboard.
	 */
	cut: function() {
		var target = this.getTarget();
		if (!target.cut()) return null;
		this.setActive( target );
		return target;
	},
	
	/** Copies the selection from the active document to the clipboard.
	 */
	copy: function() {
		var target = this.getTarget();
		if (!target.copy()) return null;
		this.setActive( target );
		return target;
	},
	
	/** Pastes the contents of the clipboard into the active document.
	 */
	paste: function() {
		this.getTarget().paste();
	}
};

/** @namespace Singleton clipboard instance representing the system clipboard.
 * 
 */
clipboard.system = {
	cut: function() {
		var active = clipboard.cut();
		if (!active) return;
		
		var value = active.getContents();
		this.lastActive = active;
		this.lastCopied = value;
		
		return value;
	},
	
	copy: function() {
		var active = clipboard.copy();
		if (!active) return;
		
		var value = active.getContents();
		this.lastActive = active;
		this.lastCopied = value;
		
		return value;
	},
	
	paste: function (value) {
		// If (and only if) the contents of either the system clipboard or
		// the DocsFree clipboard have changed, set the DocsFree clipboard
		// to the system clipboard.
		var active = clipboard.getActive();
		if (active !== this.lastActive
				|| value !== this.lastCopied
				|| active.getContents() !== value) {
			this.value = value;
			clipboard.setActive( this );
		}
		
		clipboard.paste();
	},
	
	isActive: function() {
		return this.value !== null;
	},
	
	clear: function() {
		this.value = null;
	},
	
	getContents: function() {
		return this.value;
	}
};

} // end ifndef clipboard
