<?xml version="1.0" encoding="utf-8" ?>
<html-fragment>
	<div class = "buttons_bar" rid = "buttons-bar">
		<input type="button" rid = "button_refresh" class = "designer_button refresh_button" button-action = "Refresh" />
		<input type="button" rid = "button_new" class = "designer_button new_button" button-action = "New" />
		<input type="button" rid = "button_open" class = "designer_button open_button" button-action = "Open"  />
		<input type="button" style ="display:none;" rid = "button_save" class = "designer_button save_button" button-action = "Save" />
		<input type="button" rid = "button_delete" class = "designer_button delete_button" button-action = "Delete"  />
		<div class = "designer_button_spacer"></div>
		<input type="button" rid = "button_move_first" disabled = "true" class = "designer_button" button-action = "MoveFirst" value = "&lt;&lt;" />
		<input type="button" rid = "button_move_prev" disabled = "true" class = "designer_button" button-action = "MovePrev" value = "&lt;" />
		<input type="button" rid = "button_move_next" disabled = "true" class = "designer_button" button-action = "MoveNext" value = "&gt;" />
		<input type="button" rid = "button_move_last" disabled = "true" class = "designer_button" button-action = "MoveLast" value = "&gt;&gt;" />
		<div class = "designer_button_label" rid = "record_label"></div>
	</div>
	<div rid = "richselect-header">
	</div>
	<div rid = "richselect-items">
	</div>
	<div rid = "richselect-footer">
	</div>
	<!--
	Members:
		InitializeRichSelect
		LoadRichSelect
		SetTotalItemCount
		ToggleRichSelectItem
		
	Virtuals:
		CreateNewItem
		SaveSelectedItem
		OpenListItem
		DeleteSelectedItem
		RefreshList
		DrawRichSelectItem
		DrawRichSelectHeader
		DrawRichSelectFooter
	-->
	<embedded-script>
		<![CDATA[
		InitializeRichSelect : function(){

			this.getProperties().start_record = 0;
			this.getProperties().record_count = 10;
			
			this.scopeHandler("richselect_load",0,0,1);
			this.scopeHandler("richselect_highlight",0,0,1);
			this.scopeHandler("richselect_open",0,0,1);
			this.scopeHandler("richselect_toggle",0,0,1);
			this.scopeHandler("richselect_show_profile",0,0,1);
		},
		embedded_init : function(){
		
		},
		embedded_destroy : function(){
			this._prehandle_richselect_load = 0;
			this._prehandle_richselect_highlight = 0;
			this._prehandle_richselect_open = 0;
			this._prehandle_richselect_toggle = 0;
			this._prehandle_richselect_showprofile = 0;
		},
		ShowButton : function(n){
				var o = this.GetElementByRID("button_" + n);
				if(o) o.style.display = "";
		},
		HideButton : function(n){
				var o = this.GetElementByRID("button_" + n);
				if(o) o.style.display = "none";
		},
		_handle_exec_button : function(e){
			var o = Hemi.event.getEventSource(e);
			var d = this.get_frame("designer");
			var s = o.getAttribute("button-action");
			var bRM = this.getProperties().response_mode;
			
			switch(s){
				case "New":
					if(typeof this.CreateNewItem == "function") this.CreateNewItem();
					break;
				case "Save":
					if(typeof this.SaveSelectedItem == "function") this.SaveSelectedItem();
					break;
				case "MoveLast":
					this.getProperties().start_record = this.getProperties().total_count - this.getProperties().record_count;
					if(typeof this.RefreshList == "function") this.RefreshList();
					break;
				case "MoveFirst":
					this.getProperties().start_record = 0;
					if(typeof this.RefreshList == "function") this.RefreshList();
					break;
				case "MovePrev":
					this.getProperties().start_record -= this.getProperties().record_count;
					if(this.getProperties().start_record < 0) this.getProperties().start_record = 0;
					if(typeof this.RefreshList == "function") this.RefreshList();
					break;
				case "MoveNext":
					this.getProperties().start_record += this.getProperties().record_count;
					if(this.getProperties().total_count > 0 && this.getProperties().start_record >= this.getProperties().total_count){
						this.getProperties().start_record = this.getProperties().total_count - this.getProperties().record_count;
					}
					if(typeof this.RefreshList == "function") this.RefreshList();
					break;
				case "Refresh":
					if(typeof this.RefreshList == "function") this.RefreshList();
					break;
				case "Open":
					if(!this.getProperties().item_toggled) return;
					if(typeof this.OpenListItem == "function") this.OpenListItem(this.getProperties().item_toggled);
					break;
				case "Delete":
					if(!this.getProperties().item_toggled) return;
						if(confirm("Delete this item?") && typeof this.DeleteSelectedItem == "function" && this.DeleteSelectedItem(this.getProperties().item_toggled)){
							this.DisableButton("button_delete");
							if(typeof this.RefreshList == "function") this.RefreshList();
							return;
						}
						else{
							if(typeof this.setStatus == "function") this.setStatus("Did not delete article");
						}
					break;
			}
		},
		DisableButtons : function(){
			this.DisableButton("button_delete");
			this.DisableButton("button_open");
			this.DisableButton("button_move_first");
			this.DisableButton("button_move_last");
			this.DisableButton("button_move_next");
			this.DisableButton("button_move_prev");		
		},
		LoadRichSelect : function(sUrl, sListElementName, sListItemElementName){
			this.DisableButtons();
			this.getProperties().item_toggled =0;
			
			if(!sListElementName) sListItemElementName = "List";
			this.getProperties().list_element_name = sListElementName;
			if(!sListItemElementName) sListItemElementName = "Item";
			this.getProperties().list_item_element_name = sListItemElementName;
			
			//Hemi.xml.removeChildren(this.GetElementByRID("record_label"));
			Hemi.xml.setInnerXHTML(this.GetElementByRID("record_label"), "Loading ...");
			
			Hemi.xml.removeChildren(this.GetElementByRID("richselect-header"));
			Hemi.xml.removeChildren(this.GetElementByRID("richselect-footer"));
			Hemi.xml.removeChildren(this.GetElementByRID("richselect-items"));
			if(typeof sUrl == "object"){
				this._handle_richselect_load("direct",sUrl);
			}
			else{
				if(sUrl.indexOf("?") == -1) sUrl += "?";
				else sUrl += "&";
				sUrl += "is-xml=1" + "&start-record=" + this.getProperties().start_record
					+ "&record-count=" + this.getProperties().record_count
					+ "&ts=" + (new Date().getTime())
				;

				Hemi.xml.getXml(sUrl,this._prehandle_richselect_load,1);
			}
		},
		SetTotalItemCount : function(i){
			this.getProperties().total_count = i;
			if(this.getProperties().start_record > 0){
				this.GetElementByRID("button_move_prev").disabled = false;
				this.GetElementByRID("button_move_first").disabled = false;
			}
			
			if((this.getProperties().start_record + this.getProperties().record_count) < this.getProperties().total_count){
				this.GetElementByRID("button_move_next").disabled = false;
				this.GetElementByRID("button_move_last").disabled = false;
			}

			if(this.getProperties().total_count > 0) Hemi.xml.setInnerXHTML(this.GetElementByRID("record_label"),(this.getProperties().start_record + 1) + "-" + (this.getProperties().start_record + this.getProperties().record_count) + " of " + this.getProperties().total_count);
			else Hemi.xml.setInnerXHTML(this.GetElementByRID("record_label"),"0 of 0");

		},
		_handle_richselect_open : function(e){
			var o = this.GetRichSelectItemFromEvent(e);
			if(typeof this.OpenListItem == "function") this.OpenListItem(o);
		},
		_handle_richselect_toggle : function(e){
			var o = this.GetRichSelectItemFromEvent(e);
			this.ToggleRichSelectItem(o);
		},
		GetRichSelectItemFromEvent : function(e){
			var o = Hemi.event.getEventSource(e);
			e = Hemi.event.getEvent(e);
			while(o && !o.item_id && !o.avoid) o = o.parentNode;
			if(o.avoid) return 0;
			return o;
		},
		ToggleRichSelectItem : function(o){
			if(this.getProperties().item_toggled && this.getProperties().item_toggled != o){
				this.ToggleRichSelectItem(this.getProperties().item_toggled);
			}
			if(o.toggled){
				o.className = "richselect_item";
				o.toggled = 0;
				this.getProperties().item_toggled = 0;
				this.DisableButton("button_delete");
				this.DisableButton("button_open");
			}
			else{
				o.className = "richselect_item richselect_item_toggle";
				o.toggled = 1;
				this.getProperties().item_toggled = o;
				this.EnableButton("button_delete");
				this.EnableButton("button_open");
			}
			
		},


		_handle_richselect_highlight : function(e){
			var o = Hemi.event.getEventSource(e);
			e = Hemi.event.getEvent(e);
			while(o && !o.item_id && !o.avoid) o = o.parentNode;
			if(!o || !o.item_id || o.toggled || o.avoid) return;
			if(e.type == "mouseover")
				o.className = "richselect_item richselect_item_highlight";
			else
				o.className = "richselect_item";
		},
		_handle_richselect_load : function(s, v){
			var oX = (s == "direct" ? v : v.xdom);

			if(this.IsLoginRequired(oX)){
				this.ShowLogin();
				// if(typeof this.Close == "function") this.Close();
				return;
			}
			if(!oX || !oX.documentElement){
				alert("Null XML document");
			}
			
			var oDataList = oX.getElementsByTagName(this.getProperties().list_element_name);

			if(oDataList.length <= 0){
				this.setStatus("Invalid data list for '" + this.getProperties().list_element_name + "'");
				this.setTitle("Invalid data list '" + this.getProperties().list_element_name + "'");
				return;
			}
			
			oDataList = oDataList[0];
			var iCount = parseInt(this.GetElementText(oX.documentElement,"TotalCount"));

			if(!isNaN(iCount)) this.SetTotalItemCount(iCount);

			var aItems = oDataList.getElementsByTagName(this.getProperties().list_item_element_name);

			var oList = this.GetElementByRID("richselect-items");
			
			if(typeof this.DrawRichSelectHeader == "function") this.DrawRichSelectHeader(oX, oDataList, aItems, this.GetElementByRID("richselect-header"));
			
			for(var i = 0; i < aItems.length; i++){
				var oItem = document.createElement("div");
				oItem.item_id = aItems[i].getAttribute("Id");
				oItem.group_id = aItems[i].getAttribute("GroupId");
				oItem.item_name = this.GetElementText(aItems[i],"Name");
				oItem.item_date = this.GetElementText(aItems[i],"Date");
				oItem.item_description = this.GetElementText(aItems[i],"Description");
				oItem.className = "richselect_item";
				
				if(typeof this.DrawRichSelectItem == "function") this.DrawRichSelectItem(oX, oDataList,aItems[i], oItem);
				
				oList.appendChild(oItem);
				oItem.onmouseover = this._prehandle_richselect_highlight;
				oItem.onmouseout = this._prehandle_richselect_highlight;
				oItem.onclick = this._prehandle_richselect_toggle;
				oItem.ondblclick = this._prehandle_richselect_open;
			}
			
			if(typeof this.DrawRichSelectFooter == "function") this.DrawRichSelectFooter(oX, oDataList, aItems, this.GetElementByRID("richselect-footer"));

		},
		_handle_richselect_show_profile : function(e){
			var o = Hemi.event.getEventSource(e);
			if(!o.user_id) return;
			Hemi.app.createWindow(0,g_application_path + 'ActionForms/Profile.xml','Profile-' + o.user_id,'oWindowManager',0,{user_id:o.user_id});
		}

	]]></embedded-script>
</html-fragment>

