/*
 *
 *	IntuiShop E-Commerce Framework
 *	Copyright (C) 2005-2006 Jeremy Clifton and other contributors (see
 *	CONTRIBUTORS.TXT for details).
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 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 General Public License for
 *  more details.
 *
 *  You should have received a copy of the GNU General Public License along with
 *  this program; if not, write to the Free Software Foundation, Inc., 51
 *  Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
 
Object.extend(Draggables, {

	/**
	 *	Helps us keep up with the z-index order of draggable layers on the page.
	 *	@access private
	 *	@var array
	 */
	layers: [],
	
	/**
	 *	Extends the original activate() function ... now we move the draggable to the
	 *	top of the zindex order when it activates. This ensures that layers are on top,
	 *	and act like windows when we click on them.
	 *	@param object draggable
	 */
	activate: function(draggable) {
    	window.focus();
    	this.moveToTop(draggable);
    	this.activeDraggable = draggable;
  	},
  	
  	/**
  	 *	Moves the specified draggable to the top of the stack.
  	 *	@param draggable
  	 */
  	moveToTop: function(draggable) {
  		
  		// If this element is already on top, just return without doing anything.
  		if (this.layers[(this.layers.length-1)] == draggable) {
  			return;
  		}
  		
  		// First, check to see if it's already in the array.
  		for (var i = 0; i < this.layers.length; i++) {
  			// If we find it, we'll remove it from its current position; then we'll need
  			// to re-number the z-indexes of the items still in the array.
  			if (this.layers[i] == draggable) {
  				this.layers.splice(i, 1);
  				var zIdx	= 1499;
  				for (var j = 0; j < this.layers.length; j++) {
  					Element.setZIndex(this.layers[j].element, zIdx);
  					this.layers[j].options.zindex	= zIdx;
  					zIdx--;
  				}
  			}
  		}
  		
  		// Now set the z-index of the current layer and add it to the top of the stack.
  		Element.setZIndex(draggable.element, 1500);
  		draggable.options.zindex	= 1500;
  		this.layers.push(draggable);
  		
  	}

});
 

Object.extend(Element, {

	/**
	 *	Changes the specified element's display value to hidden.
	 *	@param object element
	 */
	makeHidden: function(element) { Element.setStyle(element, { visibility: 'hidden', display: 'none' }); },
	
	/**
	 *	Makes the specified element's position type absolute.
	 *	@param object element
	 */
	makePositionAbsolute: function(element) { Element.setStyle(element, { position: 'absolute' }); },
	
	/**
	 *	Changes the specified element's display value to hidden.
	 *	@param object element
	 */
	makeVisible: function(element) { Element.setStyle(element, { visibility: 'visible', display: 'block' }); },

	/**
	 *	Moves the specified element to the specified location.
	 *	@param object element
	 *	@param int x
	 *	@param int y
	 */
	moveTo: function(element, x, y) { Element.setStyle(element, { top: y+'px', left: x+'px' }); },
	
	/**
	 *	Sets the image's src url to the specified value.
	 *	@param object element
	 *	@param string url
	 */
	setImageSrc: function(element, url) { element.src = url; },
	
	/**
	 *	Sets the z-index of the specified element.
	 *	@param object element
	 *	@param int newIndex
	 */
	setZIndex: function(element, newIndex) { Element.setStyle(element, { zIndex: newIndex }); }

});


Object.extend(Sortable, {

	/**
	 *	Gets the sort order of the specified Sortable parent element.
	 *	@param string data
	 *	@return array
	 */
	getSortOrder: function(name)
	{
		var arrData		= Sortable.serialize(name).split('&');
		for (var i = 0; i < arrData.length; i++) {
			arrData[i]	= arrData[i].split('=')[1];
		}
		return arrData;
	}
	
});
