/* ----------------------------------------------------------------------
 * js/ca/ca.relationbundle.js
 * ----------------------------------------------------------------------
 * CollectiveAccess
 * Open-source collections management software
 * ----------------------------------------------------------------------
 *
 * Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
 * Copyright 2009 Whirl-i-Gig
 *
 * For more information visit http://www.CollectiveAccess.org
 *
 * This program is free software; you may redistribute it and/or modify it under
 * the terms of the provided license as published by Whirl-i-Gig
 *
 * CollectiveAccess is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTIES whatsoever, including any implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *
 * This source code is free and modifiable under the terms of 
 * GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See
 * the "license.txt" file for details, or visit the CollectiveAccess web site at
 * http://www.CollectiveAccess.org
 *
 * ----------------------------------------------------------------------
 */
 
var caUI = caUI || {};

(function ($) {
	caUI.initRelationBundle = function(container, options) {
		options.onInitializeItem = function(id, values, options) { 
			jQuery("#" + options.itemID + id + " select").css('display', 'inline');
			var i, typeList, types = [];
			if (typeList = options.relationshipTypes[values['item_type_id']]) {
				for(i=0; i < typeList.length; i++) {
					types.push({type_id: typeList[i].type_id, typename: typeList[i].typename});
				}
			} 
			
			// look for null
			if (typeList = options.relationshipTypes['NULL']) {
				for(i=0; i < typeList.length; i++) {
					types.push({type_id: typeList[i].type_id, typename: typeList[i].typename});
				}
			}
			jQuery.each(types, function (i, t) {
				jQuery('#' + options.itemID + id + ' select[name=' + options.fieldNamePrefix + 'type_id' + id + ']').append("<option value='" + t.type_id + "'>" + t.typename + "</option>");
			});
			
			jQuery('#' + options.itemID + id + ' select option[value=' + values['relationship_type_id'] + ']').attr('selected', '1');
		}
		
		options.onAddItem = function(id, options) {
			jQuery('#' + options.itemID + id + ' #' + options.fieldNamePrefix + 'autocomplete' + id).autocomplete(options.autocompleteUrl, {onItemSelect: function(li) { 
				jQuery('#' + options.itemID + id + ' #' + options.fieldNamePrefix + 'id' + id).val(li.extra[0]);
				jQuery('#' + options.itemID + id + ' #' + options.fieldNamePrefix + 'type_id' + id).css('display', 'inline');
				var i, typeList, types = [];
				if (typeList = options.relationshipTypes[li.extra[1]]) {
					for(i=0; i < typeList.length; i++) {
						types.push({type_id: typeList[i].type_id, typename: typeList[i].typename});
					}
				} 
				// look for null
				if (typeList = options.relationshipTypes['NULL']) {
					for(i=0; i < typeList.length; i++) {
						types.push({type_id: typeList[i].type_id, typename: typeList[i].typename});
					}
				}
				
				jQuery('#' + options.itemID + id + ' select[name=' + options.fieldNamePrefix + 'type_id' + id + '] option').remove();	// clear existing options
				jQuery.each(types, function (i, t) {
					jQuery('#' + options.itemID + id + ' select[name=' + options.fieldNamePrefix + 'type_id' + id + ']').append("<option value='" + t.type_id + "'>" + t.typename + "</option>");
				});
			}, minChars: 3, matchSubset: 1, matchContains: 1});
		}
		
		var that = caUI.initBundle(container, options);
		
		return that;
	};	
})(jQuery);
