function fireExecInlineScritps() {
	$jtm(document).trigger("wls:execInlineScripts");
}

function fireAttachHandlers() {
	$jtm(document).trigger("wls:attachHandlers");
}

function observeEvent(elementId, eventName, handler) {
    $jtm(document).bind("wls:attachHandlers", function() {
        var element = $jtm('#' + elementId);
        if ( element == null ) return;
		element.bind(eventName, handler);
    })
}

function observeChange(elementId, handler) {
    observeEvent(elementId, "change", handler);
}

function observeClick(elementId, handler) {
    observeEvent(elementId, "click", handler);
}

function reloadParentWindow() {
	if (window.opener == null) {
		return;
	}
	
	window.opener.location = window.opener.location;
}

function openWindowCentered(url, title, width, height, addFeatures) {
	var x=0, y=0, ret;
	var features = addFeatures;
	
		// fetch height and width from features string
	ret = features.match(/(\s*width\s*=\s*(\d+),?)/i);
	if( ret ) {
		if( width < 0 )
			width = ret[2];
		features = features.substring(0,ret.index) + features.substring(ret.index+ret[0].length);
	}
	if( width < 0 ) 
		width = -width;
	
	ret = features.match(/(\s*height\s*=\s*(\d+),?)/i);
	if( ret ) {
		if( height < 0 )
			height = ret[2];
		features = features.substring(0,ret.index) + features.substring(ret.index+ret[0].length);
	}
	if( height < 0 ) 
		height = -height;
	features = features.replace(/\s*,\s*$/g,"");
	features += (features!='' ? ',':'') + 'width=' + width + ',height=' + height;

//	alert(features + "\n" + addFeatures);
	if( screen && screen.availWidth) {
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
	}
	if( x && y) {
		features += ',left=' + x + ',top=' + y + ',screenX=' + x + ',screenY=' + y;
	}
	//return window.open(url, title, features);
	window.open(url, title, features);
}

function createProcessingDialog(content, autoOpen) {
	return $jtm('<div id="processing-dialog"><p class="processing">' + content + '</p></div>')
				.dialog({ 
					autoOpen: autoOpen,
					modal: true,
					closeOnEscape: false,
					draggable: false,
					resizable: false,
					open: function(event, ui) { 
						//hide close button.
						$jtm(this).parent().children().children('.ui-dialog-titlebar-close').hide();
					}
				});
}

// string extensions start

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

//string extensions end

// ie hack
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
	        if(this[i]==obj){
	            return i;
	        }
	    }
	    return -1;
    }
}

(function($) {
	$.extend({
		confirm: function(params) {
			var closedWithOkButton = false;
			var textContainer = $jtm('<div id="wls-confirm-dialog"></div>');
			var confirmDialog = textContainer
				.html('<p style="margin-bottom: 0px;">' + params.text.replace('\n', '<br>') + '</p>')
				.dialog({
					modal: true,
					autoOpen: false,										
					resizable: false,
					title: 'Confirmation',
					close: function(event, ui) {
						if(!this.closedWithOkButton) {				
							if(params.onCancel != null) {
								params.onCancel.apply(params.callbacksFor != null ? params.callbacksFor : this);
							}
						}	
						$jtm(this).dialog("destroy");
						textContainer.remove();
					},			
					buttons: {
						"Yes": function() { 
							this.closedWithOkButton = true;
							$jtm(this).dialog("close"); 
							if(params.onOk != null) {
								params.onOk.apply(params.callbacksFor != null ? params.callbacksFor : this);
							}							
						},
						"No": function() {							
							$jtm(this).dialog("close");												
						}						 					
					}
				});		
			confirmDialog.dialog('open');	
		}
	});
})($jtm)
