var DestinationListControl=Class.create();
DestinationListControl.prototype = {
	initialize: function(callback, frequency) {
		this.callback = callback;
		this.frequency = frequency;
		this.currentlyExecuting = false;
		this.selecteditems = Array();
		this.tbody = null;
		this.target = null;
		this.headers = Array();
		this.aptlist = Array();
		this.maxr = 0;
		this.aptcodes = new Object();
		this.input = null;
		this.regids = Array();
		this.lastselection_h = null
		this.lastselection_d = null
		this.selection = false;
		this.renderfilter = null;
		this.selecteddst = null;
		//this.styleType='profi';
		this.aptbycode = Array();
		this.regionsid = Array();
	},

	render: function() {
		this.noselection = true;
		this.tbody = $('#dstaptlist');
		var l = document.desregions.length;
		for (var i=1;i<l;i++) {
			$('<div id="destgroup'+i+'"></div>').appendTo("#dstaptlist");
			//$("#dstaptlist").append('<div id="destgroup'+i+'"></div>');
			this.regrender("#destgroup"+i,i);
		}
		//document.dstlistpopup.fixframe();
		//if(!$.browser.msie) document.dstlistpopup.calculateColor($("#destheader0"));
		$("#destheader0").mouseover( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			//$(this).css("background-color","rgb("+$(this).attr("colmover")+")");
			ep_setBgColor ($(this), "over", "#store_popup2_color");
		});
		$("#destheader0").mouseout( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			//$(this).css("background-color","rgb("+$(this).attr("colmout")+")");
			ep_setBgColor ($(this), "out", "#store_popup2_color");
		});
		$("#p2popuptable .popup_loader").css("display","none");
		$("#p2popuptable #dstaptlist").css("display","block");
		document.dstlistpopup.fixframe();
	},
  
	regrender: function(target_id,id) {
		if(imagespath)
			this.imgBase = imagespath;
		else
			this.imgBase='';
		$('<div id="destheader'+id+'" class="color9bg dest_header"><img class="imgarrow" border="0" alt="-->" '+imgsrc('listarrowr')+' />'+document.desregions[id][1]+'</div>').appendTo(target_id);
		
		target = $("#destheader"+id);
		target.attr("isenabled","true");
		target.attr("selected","false");
		target.attr("type","h");
		target.attr("cnum",id);
		target.attr("regid",id);
		this.regionsid[id] = target;
		if (this.renderfilter) return ;
		target.click( function(){
			document.dstlist.renderselected($(this).attr("regid"));
		});
		target.mouseover( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			//$(this).css("background-color","rgb("+$(this).attr("colmover")+")");
			ep_setBgColor ($(this), "over", "#store_popup2_color");
		});
		target.mouseout( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			//$(this).css("background-color","rgb("+$(this).attr("colmout")+")");
			ep_setBgColor ($(this), "out", "#store_popup2_color");
		});
		//if(!$.browser.msie) document.dstlistpopup.calculateColor(target);
	},
  
	renderselected: function(id) {
		if(this.lastselection_h == id) return;
		this.regexpand(id);
		sender = $("#destheader"+id);
		this.setstate(sender, 1);
		this.lastselection_h = id;
		document.dstlistpopup.fixframe();
	},
  
	regexpand: function(id) {
		var l = document.desregions[id][2].length;
		this.subsellen = l-1;
		var i2 = 0;
		$('<div id="destliste'+id+'" class="destliste"></div>').appendTo("#destgroup"+id);
		$("#destliste"+id).css("display","none");
		for (var i=1;i<l;i++) {
			if ((!this.renderfilter) || (document.descodes[document.desregions[id][2][i]][0].substring(0,this.renderflen)==this.renderfilter)) {
				i2++;
				this.dstrender(id,document.desregions[id][2][i]);
			}
		}
		$("#destliste"+id).css("display","block");
		return i2;
	},

	dstrender: function(reg,id) {
		var dst = document.descodes[id];
		target = $('<div id="destination'+id+'" class="color9bg li"></div>');
		//target = $("#destliste"+reg);
		target.attr("cnum",id);
		target.attr("code",dst[0]);
		target.attr("type","d");
		target.attr("isenabled","true");
		target.attr("selected","false");
		target.attr("dstname",dst[1]);
		this.selecteddst = id;
		target.append('<img alt="-->" '+imgsrc('listarrow')+' /> '+dst[1]);

		//if(!$.browser.msie) document.dstlistpopup.calculateColor(target);
		target.click( function(){
			document.dstlist.selectapt($(this));
		});
		target.mouseover( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			//$(this).css("background-color","rgb("+$(this).attr("colmover")+")");
			ep_setBgColor ($(this), "over", "#store_popup2_color");
		});
		target.mouseout( function() {
			if ($(this).attr("isenabled") == "false" || $(this).attr("selected") == "true")  return false;
			//$(this).css("background-color","rgb("+$(this).attr("colmout")+")");
			ep_setBgColor ($(this), "out", "#store_popup2_color");
		});
		target.appendTo("#destliste"+reg);
	},
	
	selectapt: function(apt) {
		$('#dstbox').html(apt ? apt.attr("dstname") : AnyText);
		if (!apt) return false;
		var input = $('#desCode');
		input.val(apt.attr("code"));
		sender = $("#destination"+apt.attr("cnum"));
		this.setstate(apt, 1);
		this.lastselection_d = apt.attr("cnum");
		document.dstlistpopup.toggle('dstWrapper');
	},
  
	deleteselection: function(id, type) {
		if(type == "h") {
			$("#destliste"+id).remove();
			this.setstate($("#destheader"+id), 0);
			this.lastselection_h = null;
			this.lastselection_d = null;
		}
		if(type == "d") {
			this.setstate($("#destination"+id), 0);
			this.lastselection_d = null;
		}
	},

	picttoggle: function(target_id,open) {
		if(open == true) {
			setimgonobj(target_id,'listarrowd');
		}
		if(open == false)
			setimgonobj(target_id,'listarrowr');
	},

	setstate: function(sender,state) {
		if (state==0){
			sender.attr("selected", "false");
			//sender.css({ backgroundColor: "rgb("+sender.attr("colmout")+")", cursor: "pointer" });
			ep_setBgColor (sender, "out", "#store_popup2_color");
			sender.removeClass("cdefault");
			sender.addClass("cpointer");
			if (sender.attr("type") == "h") {
				this.picttoggle(sender.attr("cnum"),false);
			}
			else { }
		}
		else if (state==1) {
			sender.attr("selected", "true");
			//sender.css({ backgroundColor: "rgb("+$(this).attr("colmover")+")", cursor: "default" });
			ep_setBgColor (sender, "over", "#store_popup2_color");
			sender.removeClass("cpointer");
			sender.addClass("cdefault");
			if (sender.attr("type") == "h") {
				if (this.lastselection_h)
					this.deleteselection(this.lastselection_h, "h");
				this.picttoggle(sender.attr("cnum"),true);
			}
			else {
				if (this.lastselection_d)
					this.deleteselection(this.lastselection_d, "d");
			}
		}
		else {}
	},
  
	checkinput: function() {
		this.input=document.getElementById('desCode');
		this.renderflen=0;
		this.renderfilter=this.input.value.toUpperCase();
		
		if (this.renderflen==3)
		document.deplist.setaptlist(this.selecteddst);
		if (!this.popupactive) document.dstlistpopup.hide();
		this.renderfilter=null;
		this.render();
	}
}
