/**
 * WLM JavaScript library: DOM related fuctions / helpers
 * 
 * @projectDescription	DOM related utilities and helper classes 
 * @namespace			DOM
 *
 * @author 				$Author: mkrause $
 * @version				$Revision: 0 $
 * @copyright			NEUE DIGITALE / RAZORFISH GmbH
 *
 * @jslint			    2009-01-28
 *
 */

/* create namespace */
KIM.DOM = {};

KIM.DOM._oRegisteredUniqueElements = {};

/* CREATE JQUERY PLUGINS */
/**
 * @see KIM.DOM.identify 
 */
$.fn.WLMidentify = function() {
	KIM.DOM.identify(this);
	return this;
};

/**
 * @see KIM.DOM.unsetUnique
 */
$.fn.WLMunsetUnique = function() {
	this.each(
		function() {
			KIM.DOM.unsetUnique(jQuery(this).WLMidentify().attr('id'));
	});
	return this;
};
/**
 * @see KIM.DOM.setUnique
 */
$.fn.WLMsetUnique = function() {
	this.each(
		function() {
			KIM.DOM.setUnique(jQuery(this).WLMidentify().attr('id'));
	});
	return this;
};


/**
 * Adds an unique ID to the supplied DOM element if necessary. Preserves existing IDs. Returns element.id
 * @method identify
 * @param {HTMLElement} Element
 * @return {String} ID 
 */

// set base values onload, increase counter every time identify() is called.
// since the base values are resetted on every page-load, we don't need additional random numbers
// create timestamp as unique prefix
KIM.DOM._identifyBase = ['WLM_genericId_',new Date().getTime()].join('');
// generate base number to start counting
KIM.DOM._identifyCounter = 0;

KIM.DOM.identify = function (element) {
	 // op & msie gets id as string "null"
	var _sId = jQuery(element).attr('id');
	if (!_sId  || _sId  === null || _sId  == 'null') {
		jQuery(element).attr('id', ((KIM.DOM._identifyBase+'_'+KIM.DOM._identifyCounter++).toString()));
	}
	return jQuery(element).attr('id');
};



/**
 * Removes unique flag on element
 * @method unsetUnique
 * @param {String} Identifier
 * @return {Void}
 */
KIM.DOM.unsetUnique = function(sId){
	sId = (sId.indexOf('#') === -1) ? "#"+sId : sId ;
	KIM.DOM._oRegisteredUniqueElements[sId] = false;
	delete KIM.DOM._oRegisteredUniqueElements[sId];
};

/**
 * Sets element as uique, prevent multiple operations
 * @method setUnique
 * @param {String} Identifier
 * @return {Void}
 */
KIM.DOM.setUnique = function(sId){
	sId = (sId.indexOf('#') === -1) ? "#"+sId : sId ;
	KIM.DOM._oRegisteredUniqueElements[sId] = true;
};

/**
 * Gets element status by id 
 * @method getUnique
 * @param {String} Identifier
 * @return {Void}
 */
KIM.DOM.getUnique = function(sId){
	sId = (sId.indexOf('#') === -1) ? "#"+sId : sId ;
	return !!KIM.DOM._oRegisteredUniqueElements[sId] || false;
};



