;
/**
 * Author:Simon Au-Yong
 * WebMart UI plugins that will enable rapid Ajax development
 * @module wmui
 * @namespace jQuery/$
 */
(function(){
	/**
	 * Plugin for turning everything into a button
	 * @param buttontext string Content (either HTML or plain text) that will be nested in the button
	 * @example <pre>$('.wui_button').wmui_buttonize();</pre> or <pre>$('#submit_button').wmui_buttonize('<div style="width:300px;"></div>');</pre>
	 */
	jQuery.fn.wmui_buttonize = function(buttontext){
		var btxt;
		jQuery(this).each(function(){
			(typeof buttontext==='undefined') ? btxt = jQuery(this).html() : btxt = buttontext.toString();
			jQuery(this).html('<span class="rbtn_container"><span class="bleft bleft_default rbtn"></span>' +
				'<span class="bmid bmid_default rbtn"><span class="btext btext_default">' +
				btxt +
				'</span></span>' +
				'<span class="bright bright_default rbtn"></span></span>');
			jQuery(this).toggleClass('rbutton', true);
			jQuery(this).hover(function(){
				jQuery(this).find('.btext').toggleClass('btext_default',false);
				jQuery(this).find('.btext').toggleClass('btext_hover',true);
				jQuery(this).find('.bleft').toggleClass('bleft_default',false);
				jQuery(this).find('.bleft').toggleClass('bleft_hover',true);
				jQuery(this).find('.bmid').toggleClass('bmid_default',false);
				jQuery(this).find('.bmid').toggleClass('bmid_hover',true);
				jQuery(this).find('.bright').toggleClass('bright_default',false);
				jQuery(this).find('.bright').toggleClass('bright_hover',true);
			},function(){
				jQuery(this).find('.btext').toggleClass('btext_default',true);
				jQuery(this).find('.btext').toggleClass('btext_hover',false);
				jQuery(this).find('.bleft').toggleClass('bleft_default',true);
				jQuery(this).find('.bleft').toggleClass('bleft_hover',false);
				jQuery(this).find('.bmid').toggleClass('bmid_default',true);
				jQuery(this).find('.bmid').toggleClass('bmid_hover',false);
				jQuery(this).find('.bright').toggleClass('bright_default',true);
				jQuery(this).find('.bright').toggleClass('bright_hover',false);
			});
		});
		return this;
	};
	
	/**
	 * Plugin for default text appearing in text boxes, which will go away
	 *  when the entryevent(s) is/are bound and which will re-appear if the
	 *  text box is empty
	 * @param o_options object This has exactly three members: entryevents, exitevents and default text; all of 'em have to be strings
	 * @example $('.comments').wmui_defaulttextevent({entryevents:'click focus',exitevents:'blur',defaulttext:'Type your comments here'});
	 */
	jQuery.fn.wmui_defaulttextevent = function(o_options){
		//test for object inputs; the paranoid survive :)
		if(typeof o_options==='object' 
			&& typeof o_options.length==='undefined'
			&& typeof o_options.entryevents==='string'
			&& typeof  o_options.exitevents==='string'
			&& typeof  o_options.defaulttext==='string'){
			jQuery(this).each(function(){
				var context;
				context = this;
				$(context).bind(o_options.entryevents,function(){
					if($(context).val()==o_options.defaulttext){
						if(context.nodeName=='TEXTAREA'){
							$(context).html('');
						} else {
							$(context).val('');
						}
					}
				}).bind(o_options.exitevents,function(){
					if($(context).val().length===0){
						if(context.nodeName=='TEXTAREA'){
							$(context).html(o_options.defaulttext)
						} else {
							$(context).val(o_options.defaulttext)
						}
					}
				});
			});			
		} else {
			throw new Error("Invalid parameters for wmui_defaulttextevent plugin.");
		}
	};
	
	/**
	 * notification modal
	 * @param message string The string to appear in the modal
	 * @param callback function The callback function
	 * @param height
	 * @param width
     * @param top
	 */
	jQuery.wmui_notify = function(message,callback,width,height,topy){
		width = typeof(width) != 'undefined' ? width : 400;
		height = typeof(height) != 'undefined' ? height : 200;
        topy = typeof(topy) != 'undefined' ? topy : 'center';
		if($('#wmui_notify').attr("id")=="wmui_notify"){
			// Do nothing
		}else{
			$('body').append('<div id="wmui_notify" class="wui_modal dn_"></div>');
			$('#wmui_notify').dialog({
				bgiframe:true,
				draggable:false,
				closeOnEscape:true,
				resizable:false,
				modal:false,
                position: ['center',topy],
				autoOpen:false,
				height:height,
				width:width
			});
			if(typeof message==='string'){
				$('#wmui_notify').html('<table height="100%" width="100%"><tr><td align="center" valign="middle"><h1>' + message + '</h1></td></tr></table>');
			}
			$('#wmui_notify').dialog('open');
			$('#wmui_notify').animate({marginLeft:0}, 2000).fadeOut('normal',function(){
				$('#wmui_notify').dialog('close').dialog('remove').remove();
				if(typeof callback==='function'){
					callback.call(this);
				}
			});
		}
	}
	/**
	 * @param a_input array
	 * @todo parameter checks
	 */
	jQuery.fn.getvals = function(a_input){
		var i;
		var j=0;
		var ao_return;
		//init the array
		if(typeof a_input!=='undefined' && typeof a_input.length!=='undefined' && jQuery(this)[0].nodeName==='INPUT'){
			ao_return = {};
			for(i=0;i<a_input.length;i++){
				ao_return[a_input[i]] = [];
			}
			jQuery(this).each(function(){
				var i;
				for(i=0;i<a_input.length;i++){
					ao_return[a_input[i]].push(jQuery(this).attr(a_input[i]));
				}
				j++;
			});			
		} else if(jQuery(this)[0].nodeName!=='INPUT'){
			ao_return = [];
			jQuery(this).each(function(){
				ao_return.push(jQuery(this).html());
			});
		}
		return ao_return;
	}
	/**
	 * @todo parameter checking
	 */
	jQuery.fn.replaceattr = function(o_input){
		var counterr = 0;
		jQuery(this).each(function(){
			if(o_input['switch']==="on"){//yes it's a reserved word :)
				jQuery(this).attr(o_input.attr,o_input.replacement);
			} else if(o_input['switch']==="off"){
				jQuery(this).attr(o_input.attr,o_input.replacement[counterr]);
				counterr++;				
			}
		});
		return this;
	}
	/**
	 * {'newkeys':[],'orig_bucket':[...]}
	 */
	jQuery.fn.revert_names = function(o_input){
		var j;
		var r;
		r = [];
		for(j=0;j<o_input['newkeys'].length;j++){
			r[j] = o_input['orig_bucket'][o_input['newkeys'][j]];
		}
		jQuery(this).replaceattr({
				'replacement':r,
				'attr':'name',
				'switch':'off'});
		return this;
	};
})();
