/*
	ColinxEvents is a repository of events used with Dojo's 
	event system.
*/
var ColinxEvents = {
	grid: {
		onCellEnter: "onCellEnter",/* gridName */
		beforePage:  "beforePage",/* gridName */
		afterPage:	 "afterPage",/* gridName */
		showFormView: "showFormView", /* gridName */
		showCalendarPopup: "showCalView", /* gridName */
		stageNow: "stageNow", /* gridName */
		beforeDelete: "beforeDelete", /* gridName */
		beforeSave: "beforeSave", /* gridName */
		afterSave: "afterSave", /* gridName */
		error: "gridError", /* gridName, error */
		sortColumn: "gridSortColumn" /* gridName, colIndex */
	},
	tree: {		
		nodeClicked: "nodeClicked", /* gridName, nodeId */  
		nodeAdded: "nodeAdded", /* gridName, nodeId, parentId, nodeName */
		nodeDeleted: "nodeDeleted", /* gridName, nodeId */
		loadEnded: "treeLoadEnded", /* treeName, tree_object */
		openNodeEnded: "openNodeEnded" /* treeName, nodeId */
	},
	treetoolbar: {
		addCategoryClicked: "addCategoryClicked", /* treeName */
		deleteCategoryClicked: "deleteCategoryClicked" /* treeName */
	},
	dialog: {
		createCategory: "createCategory", /* treeName */
		deleteCategory: "deleteCategory", /* treeName */
		deleteSelectedRows: "deleteSelectedRows", /* gridName */ 
		removeProducts: "removeProducts", /* gridName, action */
		moveToCategory: "moveToCategory", /* gridName, categoryId */
		dragMoveToCategory: "dragMoveToCategory", /* gridName, categoryId */
		uncategorize: "uncategorize", /* gridName */
		insertRow: "insertRow", /* gridName */
		closeDialog: "closeDialog", /* widgetId */
		openDialog: "openDialog", /* widgetId */
		textAreaPopup: "textAreaPopup", /* textarea, popupId, htmlOnly */
		saveTextAreaPopup: "saveTextAreaPopup",
		cancelTextAreaPopup: "cancelTextAreaPopup",
		contentItemChooserPopup: "contentItemChooserPopup",
		changeContentIdentifier: "changeContentIdentifier",
		contentItemSelected: "contentItemSelected",
		contentCopiedToStores: "contentCopiedToStores" /* storeServiceNames */
	},
	/**
	*	Events for a grid toolbar. All events should include
	*	the name of the grid it is associated with
	*	@param gridName - the name of the grid
	*/
	gridtoolbar: {
		addBtnClick: "addBtnClicked",
		delBtnClick: "delBtnClicked",
		moveBtnClick: "moveBtnClicked",
		dragMoveBtnClick: "dragMoveBtnClicked",
		prevRowBtnClick: "prevRowBtnClicked",
		nextRowBtnClick: "nextRowBtnClicked",
		prevPageBtnClick: "prevPageBtnClicked",
		nextPageBtnClick: "nextPageBtnClicked"
	},
	server: {
		eventStatusUpdate: "eventStatusUpdate" /* statusList, pageObjectName */
	},
	global: {
		serverError: "serverError" /* pageObjectName */
	},
	ruletoolbar: {
		addClicked: "ruleAddClicked",
		renameClicked: "ruleRenameClicked",
		deleteClicked: "ruleDeteleClicked",
		moveUpClicked: "ruleMoveUpClicked",
		moveDownClicked: "ruleMoveDownClicked"
	},
	rules: {
		addRule: "addRule", /* ruleName */
		updateRuleName: "updateRuleName", /* ruleName */
		ruleSelected: "ruleSelected", /* ruleName */
		changeSelectionType: "changeRuleSelectionType",
		changeCategoryClicked: "changeCategoryClicked",
		changeCategory: "changeCategory", 
		changeObsCategoryClicked: "changeObsCategoryClicked",
		changeObsCategory: "changeObsCategory", 		
		changePredicateAndOr: "changeRulePredicatesAndOr",
		changeContentIdentifierClicked: "changeRuleContentIdentifierClicked",
		changeContentIdentifier: "changeRuleContentIdentifier", /* contentId */
		changeSortDirection: "changeRuleSortDirection",
		changeSortColumn: "changeRuleSortColumn",
		addPredicateClicked: "addRulePredicateClicked",
		addPredicate: "addRulePredicate", /* predicateName */
		changeDDLBOption: "changeRuleDDLBOption", /* ddlbChoice, property*/
		changeConditionOperatorClicked: "changeRuleConditionOperatorClicked", /* predicateNum, conditionNum */
		changeConditionValueClicked: "changeRuleConditionValueClicked", /* predicateNum, conditionNum */
		deleteCondition: "deleteRuleCondition", /* predicateNum, conditionNum */
		addConditionClicked: "addRuleConditionClicked", /* predicateNum, conditionNum */
		addCondition: "addRuleCondition", /* predicateNum, conditionNum, conditionCondOp  */
		deleteConditionClicked: "deleteRuleConditionClicked", /* predicateNum, conditionNum */
		saveRuleSet: "saveRuleSet",
		changeTextValue: "changeRuleTextValue", /* predicateNum, conditionNum, textValue  */
		ruleStatusChange: "ruleStatusChange", /* ruleName  */
		rulesetEvaluateAllClick: "rulesetEvaluateAllClick",
		changePredConditionalOpClicked: "changePredConditionalOpClicked", /* predicateNum */
		changePredConditionalOp: "changePredConditionalOp", /* predicateNum, conditionCondOp */
		changeDateValue: "chagePredicateDateValue", /* predicateNum, conditionNum, dateValue */
		contentItemSelected: "contentItemSelected", /* key */
		loadRuleSet: "loadRuleSet", /* ruleSetName */
		changeObsContentIdentifierClicked: "changeObsRuleContentIdentifierClicked",
		changeObsContentIdentifier: "changeObsRuleContentIdentifier" /* contentId */
	},
	advSearch: {
		addCriteria: "addAdvSearchCriteria",
		changeAndOr: "changeAdvSearchAndOr",
		deleteConditionClicked: "deleteAdvSearchConditionClicked", /* conditionNum */
		changeConditionColumnClicked: "changeAdvSearchConditionColumnClicked", /* conditionNum */
		changeConditionOperatorClicked: "changeAdvSearchConditionOperatorClicked", /* conditionNum */
		changeConditionValueClicked: "changeAdvSearchConditionValueClicked", /* conditionNum */
		changeDDLBOption: "changeAdvSearchDDLBOption", /* ddlbChoice, property*/
		changeNumberValue: "changeAdvSearchNumberValue", /* conditionNum, textValue */
		changeTextValue: "changeAdvSearchTextValue", /* conditionNum, numberValue */
		doSearch: "advSearchdoSearch",
		changeCategoryValue: "changeCategoryValue" /*conditionNum,catId,catName*/
	}
};
/**
 * 
 */
function ColinxTopic() {
}

ColinxTopic.filters = [];

ColinxTopic.subscribeWithFilter = function(topic,filter,obj,func) {
	/* alert(topic + ' subscribed, filter: ' + filter["gridName"]); */
	var filterObj = new EventFilter(filter,obj,func);
	filterObj.connect(obj,func);
	dojo.event.topic.subscribe(topic,filterObj,"doFilter");
}
ColinxTopic.subscribeNoFilter = function(topic,obj,func) {
	dojo.event.topic.subscribe(topic,obj,func);
}

function EventFilter(filter) {
	this.filter = filter;	
	
	this.doFilter = function(msg) {
		var success = true;
		for (prop in this.filter) {
			if (!dojo.lang.isFunction(this.filter[prop])) {
				if ( dojo.lang.isUndefined(msg[prop]) ) {
					success = false;
					break;
				} 				
				var prevSuccess = success;
				success = ( prevSuccess && (msg[prop].match(new RegExp(this.filter[prop])) != null) );
			}
		}
		if (success) {
			this.execMethod(msg);
		}		
	}		
	this.connect = function(obj,func) {	
		var tf = func || obj;
		var to = (!func) ? dj_global : obj;		
		dojo.event.kwConnect({
			srcObj:		this, 
			srcFunc:	"execMethod", 
			adviceObj:	to,
			adviceFunc: tf
		});		
	}	
	this.execMethod = function(msg) {
		/* msg passed on */
	}
}



