var UI = {
	showHideContainer: function(objectId, containerId, markerNode, imSrcArr) {
		//alert(objectId + ', ' + containerId + ', ' + markerNode);
		jQuery(function($) {
			if(markerNode != null)
				$(markerNode).slideToggle('medium');
			$('#' + containerId).slideToggle('medium');
			if($(objectId).attr('src').indexOf('collapse') != -1)
				$(objectId).attr('src', imSrcArr[0]);
			else
				$(objectId).attr('src', imSrcArr[1]);
		})
	}
	, toggleObjectCollection: function (arr, ctrl, classObj) {
		if(ctrl.checked) {
			var isFound = false;
			var isEqualsImplemented = (typeof(classObj.equals) == 'function') ? true : false;
			
			if(!isEqualsImplemented && typeof(classObj.id) == 'undefined') {
				alert('Implement equals method or atleast it should have id property'); return [];
			}
			
			if(arr.length > 0) {
				$.each(arr, function(i, obj) {
					if(!isFound)
						isFound = (!isEqualsImplemented) ? (obj.id == classObj.id) : (classObj.equals(obj));
				})
				if(!isFound)
					arr.push(classObj);
			} else {
				arr.push(classObj);
			}
		}
		else {
			arr = $.grep(arr, function(obj, i) {
				return (arr[i].id != classObj.id)
				//return (!classObj.equals(arr[i]))
			});
		}
		return arr;
	}
	, toggleCollection: function (arr, ctrl) {
		jQuery(function($) {
			if(ctrl.checked) {
				if($.inArray(ctrl.value, arr) == -1)
					arr.push(ctrl.value);
			}
			else {
				arr = $.grep(arr, function(n, i) {
					return (arr[i] != ctrl.value)
				});
			}
		})
		return arr;
	}
	, toggleAllInCollection: function(arr, objExpr, ctrl) {
		$.each($(objExpr), function(i, obj) {
			(ctrl.checked) ? $(this).attr('checked', 'checked') : $(this).removeAttr('checked');
			arr = UI.toggleCollection(arr, obj);
		})
		return arr;
	}
	, deselectCollection: function(arr, objExpr, ctrl) {
		ctrl.removeAttr('checked');
		return UI.toggleAllInCollection(arr, objExpr, ctrl);
	}
	, toggleGroupOption: function(objExpr, ctrl){
		jQuery(function($) {
			$.each($(objExpr), function(i, obj) {
				(ctrl.checked) ? $(this).attr('checked', 'checked') : $(this).removeAttr('checked');
			})
		})
	}
	, loading: function(objExpr, topAdjust, leftAdjust) {
		jQuery(function($) {
			$(objExpr).append("<div class='busy' style='z-index: 1000000;'><img src='../images/working.gif' border='0'></div>");
			$(".busy").css({top: $(objExpr).position().top + topAdjust, left: $(objExpr).position().left + leftAdjust})
			.ajaxStart(function() { $(this).show(); })
			.ajaxStop(function() { $(this).hide(); })
			.appendTo('body');
		})
	}
	, toggleCollectionByAttribute: function (arr, ctrl, attName, allowDuplicate) {
		if(((ctrl.type == 'select' || ctrl.type == 'select-multiple') && ctrl.selectedIndex != -1) || (ctrl.type == 'checkbox' && ctrl.checked)) {
			if(ctrl.getAttribute(attName) != null) {
				if(allowDuplicate) {
					arr.push(ctrl.getAttribute(attName));
				} else {
					if($.inArray(ctrl.getAttribute(attName), arr) == -1)
						arr.push(ctrl.getAttribute(attName));
				}
			}
		}
		else {
			var found = false;
			arr = $.grep(arr, function(n, i) {
				if(!found && arr[i] != ctrl.value) {
					found = true;
					return true;
				}
			}, true);
		}
		return arr;
	}
	, toggleAllInCollectionByAttribute: function(arr, objExpr, ctrl, attName, allowDuplicate) {
		$.each($(objExpr), function(i, obj) {
			(ctrl.checked) ? $(this).attr('checked', 'checked') : $(this).removeAttr('checked');
			arr = UI.toggleCollectionByAttribute(arr, obj, attName, allowDuplicate);
		})
		return arr;
	}
	, blockUI: function(msg, height, width) {
		jQuery.blockUI({  
			message: msg
			, css: {
				top:  (jQuery(window).height() - height) /2 + 'px' 
        		, left: (jQuery(window).width() - width) /2 + 'px' 
        		, width: width + 'px'
        		//, border: '1px solid #fff'
			}
			, centerX: true
			, centerY: true
	    }); 
	}
	, busy: function() {
		var msg = '<img id="displayLoading" src="/resources/images/layouts/loading.gif" width="75" height="75" />'
		UI.blockUI(msg, 75, 75);
	}
	, selectAll: function(arr, objExpr) {
		$.each($(objExpr), function(i, obj) {
			$(this).attr('checked', 'checked');
			arr = UI.toggleCollection(arr, obj);
		})
		return arr;
	}
}

