/*===========================================================================
	Actions (Global)
	
	This JavaScript executes dynamic behaviors such as:
	* Collapsing/expanding sections
	* Form validation
	* Popup windows
===========================================================================*/

/* Enable FireBug/etc. logging if logging widget is loaded
===========================================================================*/

if (YAHOO && YAHOO.widget.Logger) {
	YAHOO.widget.Logger.enableBrowserConsole();
	
	// override to support console.info/warn/error and fallback to console.log
	YAHOO.widget.Logger._printToBrowserConsole = function(oEntry) {
		if (window.console && console.log) {
			var category = oEntry.category;
			var label = oEntry.category.substring(0,4).toUpperCase();
			var time = oEntry.time;
			
			if (time.toLocaleTimeString) {
				var localTime  = time.toLocaleTimeString();
			} else {
				localTime = time.toString();
			}
			
			var msecs = time.getTime();
			var elapsedTime = (YAHOO.widget.Logger._lastTime)
				? (msecs - YAHOO.widget.Logger._lastTime)
				: 0;
			
			YAHOO.widget.Logger._lastTime = msecs;
			
			if (console[category]) {
				console[category](localTime + " (" + elapsedTime + "ms): " + oEntry.source + ": " + oEntry.msg);
			} else {
				console.log(localTime + " (" + elapsedTime + "ms): " + "[" + category + "] " + oEntry.source + ": " + oEntry.msg);
			}
		}
	};
}

/* Artwork widget
===========================================================================*/

Event.onContentReady("artwork-widget", function() {
	var contentField  = Dom.get("content-textarea") || Dom.get("page_body");
	var artworkSelect = Dom.get("artwork-select");
	var artworkButton = Dom.get("insert-artwork-button");
	
	Event.addListener(artworkButton, "click", function(e) {
		if (artworkSelect.selectedIndex == 0) { return; }
		
		var wide = (Dom.hasClass(artworkSelect.options[artworkSelect.selectedIndex], "wide"))
			? '(wide)'
			: "";
		
		var filename = artworkSelect.options[artworkSelect.selectedIndex].value;
		
		edInsertContent(contentField, "\n!" + wide + filename + "!\n");
	}, artworkSelect, true);
}, this, true);

/* Attachments widget
===========================================================================*/

Event.onContentReady("attachment-widget", function() {
	var contentField     = Dom.get("content-textarea") || Dom.get("page_body");
	var attachmentSelect = Dom.get("attachment-select");
	var attachmentButton = Dom.get("insert-attachment-button");
	
	Event.addListener(attachmentButton, "click", function(e) {
		if (attachmentSelect.selectedIndex == 0) { return; }
		
		var attachmentClass = attachmentSelect.options[attachmentSelect.selectedIndex].className;
		var filename        = attachmentSelect.options[attachmentSelect.selectedIndex].value;
		var attachmentTitle = attachmentSelect.options[attachmentSelect.selectedIndex].innerHTML;
		
		edInsertContent(contentField, "\n* " + '"(' + attachmentClass + ') ' + attachmentTitle + '":' + filename);
	}, attachmentSelect, true);
}, this, true);
