(function($) {
	$.fn.extend({
		attendeeControl: function(tabs, callback){
			
            $(this)[0]._attendeeTabs = tabs;
            $(this)[0]._attendeeCallback = callback;			
			if((arguments[2]) && (typeof arguments[2] == 'object')){
				$(this)[0]._options = arguments[2];
			}else{
				$(this)[0]._options = {};
			}
            
			if ($(this)[0]._options.keepHistory == undefined){
				$(this)[0]._options.keepHistory = true;
			}            
            if($(this)[0]._options && ! $(this)[0]._options.no_click_event) {
                $(this).click($(this).runAttendeeController);
            } 
			return this;
		},
        runAttendeeController: function(event) {
            if (typeof $(this)[0]._attendeeTabs == 'object'){
                $('#modalWindow').jqm({
                    modal:true,
                    ajax:BASE_HREF+'windows/select/attendees/?tabs[]='+ $(this)[0]._attendeeTabs.join('&tabs[]=')
                    })
                $('#modalWindow').jqmShow();

                $('#modalWindow')[0]._callback = $(this)[0]._attendeeCallback;
                $('#modalWindow')[0]._caller = $(this)[0];

                event.preventDefault();
                return false;
            }
        },
		defaultOption: function(data){
			if(typeof(data) == 'object'){
				$(this)[0]._defaultOption = data;
			}
		},
		setElements: function(elements){
			var qsf = new quickSearchFormat();
			var attendeesArray = new Array();
			
	        for(i = 0; i < elements.length; i++){
	          attendeesArray.push(qsf.objectToArray(elements[i]));
	        }
	        
			$(this)[0]._attendeeElements = attendeesArray.slice();
		},
		removeElement: function(type, id){
			var qsf = new quickSearchFormat();
			
			for(i = 0; i < $(this)[0]._attendeeElements.length; i++){
				var chk = qsf.arrayToObject($(this)[0]._attendeeElements[i]);
				if ((chk.type == type) && (chk.id == id)){
					//Deleting keeps the length attribute in tact... we need to fix this by re-ordering the array
					delete $(this)[0]._attendeeElements[i];
					
					for(ii = (i+1); ii < $(this)[0]._attendeeElements.length; ii++){
						$(this)[0]._attendeeElements[ii-1] = $(this)[0]._attendeeElements[ii];
					}
					
					//The length is still not fixed!! Remove 1 element
					$(this)[0]._attendeeElements.length -= 1;
					return true;
				}
			}
			
			return false;
		}
	}); 
})(jQuery);

function quickSearchFormat(){}

quickSearchFormat.prototype = {
	objectToArray:function(standardJSON){
		var arr = new Array(6);
	
		for(attr in standardJSON){
		  var key = -1;
		  
		  switch(attr){
		    case 'name':
		      key = 0;
		      break;
		    case 'organisation':
		      key = 1;
		      break;
		    case 'id':
		      key = 2;
		      break;
		    case 'type':
		      key = 3;
		      break;
		    case 'phone':
		      key = 4;
		      break;
		    case 'email':
		      key = 5;
		      break;
		  }
		  
		  if (key != -1){
		    arr[key] = standardJSON[attr];
		  }
		}
		
		return arr;
	},
	
	arrayToObject:function(input){
		return eval('('+this.arrayToString(input)+')');
	},
	
	arrayToString:function(input){
		return '{'+this.arrayToElements(input).join(',')+'}'
	},
	
	arrayToElements:function(input){
		info = new Array();
		for (iChk = 0; iChk < input.length; iChk++){
			var key = '';
			switch(iChk){
				case 0:
					key = 'name';
					break;
				case 1:
					key = 'organisation';
					break;
				case 2:
					key = 'id';
					break;
				case 3:
					key = 'type';
					break;
				case 4:
					key = 'phone';
					break;
				case 5:
					key = 'email';
					break;
			}
			
			if ((key != '')){
				var match = new RegExp("'", "ig");
				var str = '';
				
				if (input[iChk]){
					str = input[iChk].replace(match,"\\'")
				}
				
				info.push('\''+key+'\':\''+str+'\'');
			}
		}
		
		return info;
	}
}
