/**
 * Marketplace Namespace. All Marketplace specific classes will extend
 * this Namspace in order to avoid namspace conflicts with other libraries
 * on Adobe.com 
 */
adobe.marketplace = function(){};

//CONSTANTS
/**
 * Address for AJAX requests
 */
adobe.marketplace.AJAXURL = "/cfusion/marketplace/remote/uiWidgetsService.cfc";


//OBJECT CLASSES
/**
 * Class for creating Generic Data Objects for storing data
 * @constructor
 */
adobe.marketplace.DataObject = Class.create({
	/**
	 * Initializes Data Object
	 */
	initialize: function(){
		
	},
	/**
	 * Prepares and provides data formatted for submssion via POST or GET
	 * @return String of formatted data.  
	 * @type String
	 */
	getQueryString: function(){
		//TODO: determine data prep plan.
		// Perhaps use DO id as identifier in each field?
		// example: "IDsomefield=somefieldValue&IDsomeotherfield=someotherfieldValue&..."
		// or use prototype's toJSON() method
//		var returnStr = "";
//		for(prop in this){
//			if (this.hasOwnProperty(prop)) {
//		  	returnStr += (returnStr != "") ? "," : "";
//		  	returnStr += '"' + prop + '":"' + this[prop] + '"';
//		  }
//		}
//		returnStr = "{"+returnStr+"}";
//		return returnStr;
	}
});

/**
 * Mixin for adding form text field default text clearing/replacement on focus/blur.
 * 
 * @param {Object} inputField Text input html element.
 */
adobe.marketplace.InputClearMixin = {
	setupInputClear: function(inputField){
		
		this.replaceText = inputField.value;
		Event.observe(inputField,'focus',function(){
			inputField.value = (inputField.value == this.replaceText) ? "" : inputField.value;
		}.bind(this,inputField));
		Event.observe(inputField,'blur',function(){
			inputField.value = (inputField.value == "") ? this.replaceText : inputField.value;
		}.bind(this,inputField));
	}
}
/**
 * Generic input field object with InputClearMixin mixed in.
 */
adobe.marketplace.InputClear = Class.create(adobe.marketplace.InputClearMixin,{
	initialize: function(){
		
	}
});

//UTILITY FUNCTIONS
/**
 * Generic style class swap.  Toggles between "-over" and "" suffix.
 * @param {Object} item2swap Object reference to toggle class on
 * @param {String} class2swap Class to swap (passed in to handle class overload situations)
 */
adobe.marketplace.swapOver = function(item2swap,class2swap){
	if(item2swap.hasClassName(class2swap)){
		item2swap.removeClassName(class2swap)
		item2swap.addClassName(class2swap + "-over");
		item2swap.style.cursor = "pointer";
	} else {
		item2swap.removeClassName(class2swap + "-over")
		item2swap.addClassName(class2swap);
	}
}
/**
 * Generic form submit call for use in standard sidebar component or other generic form controls.
 * Should be overwritten or onsubmit even captured, in the local scope if pre-submit logic necessary
 */
adobe.marketplace.formSubmit = function(){
	$('marketplaceForm').submit();
}
/**
 * Generic form reset call for use in standard sidebar component or other generic form controls.
 * Should be overwritten or onreset even captured, in the local scope if pre-reset logic necessary or
 * alternate confirm message desired
 */
adobe.marketplace.formReset = function(){
	if (confirm("Are you sure you want to discard all form edits?")) {
  	$('marketplaceForm').reset();
  }
}

adobe.marketplace.toggleDisplay = function(item2toggle){
	item2toggle.style.display=(item2toggle.style.display == "none")? "" : "none";
}
adobe.marketplace.toggleLink = function(link2toggle){
	if(Element.hasClassName(link2toggle,"disable")) {
  	link2toggle.onclick = link2toggle.oldClick;
		link2toggle.removeClassName("disable");
  } else {
  	var oldClick = document.createAttribute("oldClick");
  	if(!link2toggle.oldClick) link2toggle.setAttributeNode(oldClick);
  	link2toggle.oldClick = link2toggle.onclick;
  	link2toggle.onclick = function(){
  		return false;
  	};
  	link2toggle.addClassName("disable");
  }
}

adobe.marketplace.getUrlParam = function( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

Event.observe(window,"load",function(){
	$$('.initialHide').each(function(item){
		item.style.display="none";
		item.removeClassName('initialHide');
	})
	
});

adobe.marketplace.addLoadEvent = function (func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}