
function showUIError(msg, fade, fadeTime, delay){
	if(typeof msg != 'undefined' && msg !== null && msg !== ''){
		if(typeof delay == 'undefined'){
			delay = 8000;
		}
		if(typeof fadeTime == 'undefined'){
			fadeTime = 1000;
		}
		$("#UIErrorMsg").html(msg);
		var elem = $("#UIErrorCont");
		clearTimeout(elem.stop().data('UIErrorTimer'));
		if(fade === false){
			elem.fadeIn(fadeTime);
		}else{
			elem.fadeIn(fadeTime, function() { 
				$.data(this, 'UIErrorTimer', setTimeout(function() { elem.fadeOut(fadeTime) }, delay));
			});
		}
	}
}

function showUIWarning(msg, fade, fadeTime, delay){
	if(typeof msg != 'undefined' && msg !== null && msg !== ''){
		if(typeof delay == 'undefined'){
			delay = 8000;
		}
		if(typeof fadeTime == 'undefined'){
			fadeTime = 1000;
		}
		$("#UIWarningMsg").html(msg);
		var elem = $("#UIWarningCont");
		clearTimeout(elem.stop().data('UIWarningTimer'));
		if(fade === false){
			elem.fadeIn(fadeTime);
		}else{
			elem.fadeIn(fadeTime, function() { 
				$.data(this, 'UIWarningTimer', setTimeout(function() { elem.fadeOut(fadeTime) }, delay));
			});
		}
	}
}

function showUISuccess(msg, fade, fadeTime, delay){
	if(typeof msg != 'undefined' && msg !== null && msg !== ''){
		if(typeof delay == 'undefined'){
			delay = 8000;
		}
		if(typeof fadeTime == 'undefined'){
			fadeTime = 1000;
		}
		$("#UISuccessMsg").html(msg);
		var elem = $("#UISuccessCont");
		clearTimeout(elem.stop().data('UISuccessTimer'));
		if(fade === false){
			$("#UISuccessCont").fadeIn(fadeTime);
		}else{
			elem.fadeIn(fadeTime, function() { 
				$.data(this, 'UISuccessTimer', setTimeout(function() { elem.fadeOut(fadeTime) }, delay));
			});
		}
	}
}

function showUIDialogOK(title, msg, okFunc, okName, options){
	var elem = $("#UIDialogCont");
	//destroy previous dialogs
	elem.dialog('destroy');
	if(typeof okFunc == 'undefined' || okFunc == null){
		okFunc = '$( this ).dialog( "close" );';
	}
	if(typeof okName == 'undefined' || okName == null){
		okName = 'OK';
	}
	elem.attr('title', title);
	elem.html("<p>" + msg + "</p>");
	elem.dialog(eval(options));
	elem.dialog( "option", "title", title );
	elem.dialog( "option", "buttons", [
			{
				text: okName,
				click: function() { eval(okFunc); }
			}
		]
	);

}
function showUIDialogConfirm(title, msg, confirmFunc, confirmName, cancelFunc, cancelName,  options){
	var elem = $("#UIDialogCont");
	//destroy previous dialogs
	elem.dialog('destroy');
	
	if(typeof confirmFunc == 'undefined' || confirmFunc == null){
		confirmFunc = '$( this ).dialog( "close" );';
	}
	if(typeof confirmName == 'undefined' || confirmName == null){
		confirmName = 'Confirm';
	}
	if(typeof cancelFunc == 'undefined' || cancelFunc == null){
		cancelFunc = '$( this ).dialog( "close" );';
	}
	if(typeof cancelName == 'undefined' || cancelName == null){
		cancelName = 'Cancel';
	}
	elem.html("<p>" + msg + "</p>");
	elem.dialog(eval(options));
	elem.dialog( "option", "title", title );
	elem.dialog( "option", "buttons", [
			{
				text: confirmName,
				click: function() { eval(confirmFunc); }
			},
			{
				text: cancelName,
				click: function() { eval(cancelFunc); }
			}
		]
	);
}

//showUIDialog('asd', {'title': 'Tit', 'width': 700, 'buttons': [{text: 'Test' ,click: function() { $( this ).dialog( 'close' ); }}] });
function showUIDialog(msg, options){
	var elem = $("#UIDialogCont");
	//destroy previous dialogs
	elem.dialog('destroy');
	elem.html("<p>" + msg + "</p>");
	elem.dialog(eval(options));

}
function showUIDialogTabbed(msg, options){
	var elem = $("#UIDialogCont");
	//destroy previous dialogs
	elem.dialog('destroy');
	elem.html("<p>" + msg + "</p>");
	elem.dialog(options);
	elem.find('.ui-tab-dialog-close').append($('a.ui-dialog-titlebar-close'));
	elem.find('.ui-tab-dialog-close').css({'position':'absolute','right':'0', 'top':'23px'});
	elem.find('.ui-tab-dialog-close > a').css({'float':'none','padding':'0'});
	var tabul = elem.find('ul:first');
	elem.parent().addClass('ui-tabs').prepend(tabul).draggable('option','handle',tabul); 
	elem.siblings('.ui-dialog-titlebar').remove();
	tabul.addClass('ui-dialog-titlebar');
	elem.find('.ui-widget-content').removeClass('ui-widget-content');
	
	

}
function hideUIDialog(){
	var elem = $("#UIDialogCont");
	elem.dialog( "close" );
}

//Blind Slider!
function blindSlider(self, target){
		self.unbind('click');
		target.show('blind' , {}, 500, null ).unbind('click')
		.click(function(){
			target.unbind('click');
			self.click(function(){
				blindSlider(self, target)
			});
			target.hide('blind' , {}, 500, null );
			
		});
		
}
//Blind Slider!
function blindSlider(self, target, targetBtn){
		self.unbind('click');
		self.hide('blind' , {}, 250, null );
		target.show('blind' , {}, 500, null );
		targetBtn.unbind('click')
		.click(function(){
			targetBtn.unbind('click');
			self.click(function(){
				blindSlider(self, target, targetBtn);
			});
			target.hide('blind' , {}, 500, null );
			self.show('blind' , {}, 500, null );
		});
		
}

//combo box
	(function( $ ) {
		$.widget( 'ui.combobox', {
			_create: function() {
				var self = this,
					select = this.element.hide(),
					selected = select.children( ':selected' ),
					value = selected.val() ? selected.text() : '';
				var input = this.input = $( '<input>' )
					.insertAfter( select )
					.val( value )
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: function( request, response ) {
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), 'i' );
							response( select.children( 'option' ).map(function() {
								var text = $( this ).text();
								if ( this.value && ( !request.term || matcher.test(text) ) )
									return {
										label: text.replace(
											new RegExp(
												'(?![^&;]+;)(?!<[^<>]*)(' +
												$.ui.autocomplete.escapeRegex(request.term) +
												')(?![^<>]*>)(?![^&;]+;)', 'gi'
											), '<strong>$1</strong>' ),
										value: text,
										option: this
									};
							}) );
						},
						select: function( event, ui ) {
							ui.item.option.selected = true;
							self._trigger( 'selected', event, {
								item: ui.item.option
							});
						},
						change: function( event, ui ) {
							if ( !ui.item ) {
								var matcher = new RegExp( '^' + $.ui.autocomplete.escapeRegex( $(this).val() ) + '$', 'i' ),
									valid = false;
								select.children( 'option' ).each(function() {
									if ( $( this ).text().match( matcher ) ) {
										this.selected = valid = true;
										return false;
									}
								});
								if ( !valid ) {
									// remove invalid value, as it didn't match anything
									$( this ).val( '' );
									select.val( '' );
									input.data( 'autocomplete' ).term = '';
									return false;
								}
							}
						}
					})
					.addClass( 'ui-widget ui-widget-content ui-corner-left' );

				input.data( 'autocomplete' )._renderItem = function( ul, item ) {
					return $( '<li></li>' )
						.data( 'item.autocomplete', item )
						.append( '<a>' + item.label + '</a>' )
						.appendTo( ul );
				};

				this.button = $( '<button type=\"button\">&nbsp;</button>' )
					.attr( 'tabIndex', -1 )
					.attr( 'title', 'Show All Items' )
					.insertAfter( input )
					.button({
						icons: {
							primary: 'ui-icon-triangle-1-s'
						},
						text: false
					})
					.removeClass( 'ui-corner-all' )
					.addClass( 'ui-corner-right ui-button-icon' )
					.click(function() {
						// close if already visible
						if ( input.autocomplete( 'widget' ).is( ':visible' ) ) {
							input.autocomplete( 'close' );
							return;
						}

						// pass empty string as value to search for, displaying all results
						input.autocomplete( 'search', '' );
						input.focus();
					});
			},

			destroy: function() {
				this.input.remove();
				this.button.remove();
				this.element.show();
				$.Widget.prototype.destroy.call( this );
			}
		});
	})( jQuery );
