var DepartureListControl = Class.create();
DepartureListControl.prototype = {
	initialize: function(callback, frequency) {
		this.callback=callback;
		this.frequency=frequency;
		this.currentlyExecuting=false;
		this.listcontrols=Array();
		this.selecteditems=Array();
		this.tbody=null;
		this.target=null;
		this.headers=Array();
		this.maxitems = 0;
		this.aptlist=Array();
		this.defaultvalues=Array();
		this.aptcodes=new Object();
		this.aptids=Array();
		this.lastaptlist=-1;
	},
	
	setmaxitems: function(num) { this.maxitems = num; },
	write: function() {
		//this.target.innerHTML='<table id="depaptlist" cellspacing="0" cellpadding="0" border="0"><tbody><tr></tr></tbody></table>';
	},
	
	addheader: function(aheader) { this.headers.push(aheader); },
	
	addapt: function(r,code,apt,rid) {
		if(!this.aptlist[r])
			this.aptlist[r] = new Array();
		this.aptlist[r].push(Array(code.toUpperCase(),apt,rid));
	},
	
	setstate: function(sender,state) {
		sender.attr("isenabled", (sender.attr("valid") == "true" ? true : false));
		if (state==0) {
			$("#depcheck"+sender.attr("aptid")).attr("checked",false);
		}
		else if (state==1) {}
		else if (state==2) {
			$("#depcheck"+sender.attr("aptid")).attr("checked",true);
		}
		else if (state==3) {}
	},
	
	render: function(c) {
		for(var gr=0; gr<this.aptlist.length; gr++) {
			// set group div and header
			$("#deptheader"+gr).append(this.headers[gr]);
			$("#deptheader"+gr).append('<a href="javascript:void(0)" class="selectAll color28bg color29">+ / -</a>');
			var aps = this.aptlist[gr].length;
			for(var a=0; a<aps; a++){
				this.renderapt(this.aptlist[gr][a], gr);
			}
		}  
        for(var gr=this.aptlist.length; gr<8; gr++) {
            $("#deptgroup"+gr).css("display","none");       
        }
        
		$("#p1popuptable .popup_loader").css("display","none");
		$("#p1popuptable #depaptlist_1").css("display","block");
		$("#p1popuptable #depaptlist_2").css("display","block");
	},
	
	renderapt: function(apt, targetid) {
		var aptid = apt[2];
		var str = '<li id="deplistapt'+aptid+'" class="color12bg color9bob">';
		str += '&nbsp;<input type="checkbox" id="depcheck'+aptid+'" />&nbsp;'+apt[1]+'</li>';
		$(str).appendTo("#deplist"+targetid);
		var target = $("#deplistapt"+aptid);
		this.aptcodes[apt[0]] = target;
		this.aptids[apt[2]] = target;
		var aptlist = this;
		target.attr("valid", "true");
		target.attr("aptid", apt[2]);
		target.attr("aptname", apt[1]);
		target.attr("code", apt[0]);
		target.attr("isenabled", "true");
		target.attr("selected", "false");
		target.click( function() {
			document.deplist.selectitem($(this));
		});
		target.mouseover( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			ep_setBgColor ($(this), "over", "#store_popup_color");
		});
		target.mouseout( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			ep_setBgColor ($(this), "out", "#store_popup_color");
		});
		//if(!$.browser.msie) document.deplistpopup.calculateColor(target);
		if (this.defaultvalues.toString().indexOf(target.attr("code"))>-1)
			this.addselected(target);
	},
	
	selectitem: function(sender) {
		if (!sender.attr("isenabled")) return false;
		if ( sender.attr("selected") == 'true')
			this.removeselected(sender);
		else {
			if ((this.maxitems!=0) && (this.selecteditems.length<this.maxitems))
				this.addselected(sender);
			else
				$("#depcheck"+sender.attr("aptid")).attr("checked",false);
		}
		this.rebuildvals();
	},
	
	addselected: function(item) {
		if (item.attr("selected") == "true") return;
		this.selecteditems.push(item.attr("aptid"));
		item.attr("selected","true");
		item.removeClass("cpointer");
		item.addClass("cdefault");
		ep_setBgColor (item, "over", "#store_popup_color");
		/*item.css({
			backgroundColor: "rgb("+item.attr("colmover")+")",
			cursor: "default"
		});*/
		this.setstate(item,2);
	},
	
	removeselected: function(item) {
		if (item.attr("selected") == "false") return false;
		var i=this.selecteditems.length;
		while ((i>=0) && (this.selecteditems[i]!=item.attr("aptid")))i--;
		if (i<0) return false;
		this.selecteditems.splice(i,1);
		item.attr("selected","false");
		item.removeClass("cdefault");
		item.addClass("cpointer");
		ep_setBgColor (item, "out", "#store_popup_color");
		/*item.css({
			backgroundColor: "rgb("+item.attr("colmout")+")",
			cursor: "pointer"
		});*/
		this.setstate(item,0);
	},
	
	rebuildvals: function() {
		inp = $("#depCode");
		var values = inp.val().toUpperCase().split(',');
		for (var i=values.length-1;i>=0;i--) {
			if  ((values[i]=='') || ((found=this.aptcodes[values[i]]) && ((!found) || (found.attr("selected") == "false")))) {
				values.splice(i,1);
				i--;
			}
		}
			
		for (var s=this.selecteditems.length-1;s>=0;s--) {
			var found=false;
			for (var i=values.length-1;i>=0;i--)
			if (values[i] == $("#deplistapt"+this.selecteditems[s]).attr("code")) {
				found = true;
				break;
			}
			if (!found)
				values.push($("#deplistapt"+this.selecteditems[s]).attr("code"));
		}
		
		var dptName=Array();
		for (var i=0;i<=values.length-1;i++) {
			if (this.aptcodes[values[i]] != undefined)
			{
				dptName.push(this.aptcodes[values[i]].attr("aptname"));
			}
		}
		$("#depCode").val(values.join(','));
		$('#flightpanel_hin_text').html(dptName.join(', '));
	},
	
	setdefaultvalues: function(text) { this.defaultvalues = text.split(','); }  
}

