// window.js custom object for div-based window manipiulation
// requires: scriptaculous with dragdrop 1.6.1 +

var Win = Class.create();

Win.prototype = {

	initialize:function(name,title,content,focusElement) {
		this.name = name;
		this.title = title;
		this.content = $(content);
		this.isOpen=false;
		if (focusElement.length)  {this.focusElement = $(focusElement);}
		
		this.objRef = this.render();
		this.register();
		new Draggable(this.objRef,{handle:this.label});
	},
	
	register:function() {
		global.windows[this.name]=this;
	},

	render:function() {
		// remove content from document
		document.body.removeChild(this.content);
			
		//create outer container
		var container = document.createElement("div");
		container.setAttribute("id","win" + this.name);
		container.unselectable="on";
		
		//create label
		var label = document.createElement("div");
		label.setAttribute("className","windowLabel");
		label.setAttribute("class","windowLabel");
		label.setAttribute("id","win" + this.name + "Label");
		label.unselectable="on";
		this.label = label;
		
		//create label text
		var labelText = document.createElement("div");
		labelText.setAttribute("className","windowLabelText");
		labelText.setAttribute("class","windowLabelText");
		labelText.setAttribute("id","win" + this.name + "LabelText");
		labelText.innerHTML = this.title;
		labelText.unselectable="on";
		
		//create status box	
		var statusBox = document.createElement("div");
		statusBox.setAttribute("className","windowStatus");
		statusBox.setAttribute("class","windowStatus");
		statusBox.setAttribute("id","win" + this.name + "Status");
		statusBox.unselectable="on";
		this.statusBox = statusBox;
		
		//create error box
		var errorBox = document.createElement("div");
		errorBox.setAttribute("className","error");
		errorBox.setAttribute("class","error");
		errorBox.setAttribute("id","win" + this.name + "LabelText");
		this.errorBox = errorBox;
		
		//assemble pieces
		label.appendChild(labelText);
		container.appendChild(label);
		container.appendChild(errorBox);
		container.appendChild(statusBox); 
		container.appendChild(this.content);	
		document.body.appendChild(container);
		
		//close button hack
		label.innerHTML += '<span class="windowCloseButton" onClick="win' + this.name + '.close();">close</span>';
		
		this.unsetStatus();
		this.unsetError();
		Element.hide(container);
		
		return container;
	},
	
	open:function(evt,position) {
		evt = (evt) ? evt : ((window.event) ? event : null);
		
		if(position == 'pointer') {
			shiftTo(this.objRef,Event.pointerX(evt) - Element.getDimensions(this.objRef).width,Event.pointerY(evt));
		} else  {
			shiftTo(this.objRef,300,200);
		}
		this.objRef.style.display = 'block';
		this.objRef.style.zIndex=10000+i;		
		if(this.focusElement) {this.focusElement.focus();}
		
		//bring it to the front
		var i=0;
		for(var w in global.windows) {
			if(global.windows[w].isOpen) {
				i++;
			}
		}
		this.isOpen=true;
	},
	
	close:function() {
		this.objRef.style.display = 'none';
		this.isOpen=false;
	},
	
	setError:function(message) {
		this.error = 1;
		Element.update(this.errorBox,message);
		Element.show(this.errorBox);
	},
	
	unsetError:function() {
		delete this.error;
		Element.hide(this.errorBox);
	},
	
	setStatus:function(message) {
		Element.update(this.statusBox,message);
		Element.show(this.statusBox);
	},
	
	unsetStatus:function(message) {
		Element.hide(this.statusBox);
	}
}



function extendLinkWindow(o)  {

	var xo={

		textElement:$('linkText'),
		URLElement:$('linkHREF'),
		targetElement:$('linkTarget'),
		
		textControl:$('ctrlLinkText'),
		typeControl:$('ctrlLinkType'),
		URLControl:$('ctrlLinkURL'),
		targetControl:$('ctrlLinkTarget'),
		isForChatControl:$('ctrlLinkIsForChat'),
		
		linkTypes:{
			spacetribe_user:'/user_' + global.user.username,
			spacetribe_toons:'/toons_' + global.user.username
		},
		
		setTypeOptions:function() {		
			var o=this.typeControl;
			o.options[0]=new Option("My specified URL","user_specified",false,false);
			o.options[1]=new Option("My spaceTribe user profile page","spacetribe_user",false,false);
			o.options[2]=new Option("My spaceTribe toons page","spacetribe_toons",false,false);
			if(global.deepLink !== undefined) { o.options[3]=new Option("The current spaceTribe page","spacetribe_this",false,false);  }		
		},
		
		getURL:function() {
			var t = $F(this.typeControl);
			if(t=='spacetribe_this')  {
				return global.deepLink;

			} else {
				return this.linkTypes[t];
			}
		},
		
		reset:function() {
			this.setTypeOptions();
			this.text='';
			this.URL='';
			this.userSpecified=false;
			this.stLink=false;
			this.isForChat=false;
			this.openInNewWindow=false;
			this.textControl.value='';
			this.URLControl.value='';
			this.typeControl.value='user_specified';
			this.isForChatControl.checked=false;
			this.targetControl.checked=false;	
			Element.hide(this.textElement);
			Element.hide(this.targetElement);		
			this.setLink();
		},
		
		setLink:function() {
			this.text=$F(this.textControl);
			this.isForChat=this.isForChatControl.checked;	
			this.userSpecified=($F(this.typeControl)=='user_specified'?true:false);		
			this.openInNewWindow=this.targetControl.checked;
			
			if(this.userSpecified)  {
				this.stLink=false;
				this.URL=$F(this.URLControl);
			} else {
				this.stLink=true;
				this.URL=this.getURL();
			}						
			
			if(!this.text.length) {
				Element.hide(this.textElement);
			} else {
				Element.update(this.textElement,this.text);
				Element.show(this.textElement);
			}
			
			
			if(this.stLink && this.isForChat) {
				this.URL=this.getURL();
				this.URLControl.value=this.URL;
				this.URLControl.disabled=true;
				this.openInNewWindow=false;
				this.targetElement.checked=false;
				this.targetControl.disabled=true;
				Element.hide(this.targetElement);
				
			} else if (this.stLink && !this.isForChat) {
				this.URL=this.getURL();			
				this.URL='http://www.spaceloot.org/?do=' + this.URL;
				this.URLControl.value = this.URL;
				this.URLControl.disabled=true;
				this.targetControl.disabled=false;
				Element[(this.openInNewWindow?'show':'hide')](this.targetElement);
				
			} else if (!(this.stLink) && this.isForChat) {
				this.URLControl.disabled=false;
				this.openInNewWindow=true;
				this.targetControl.checked=true;
				this.targetControl.disabled=true;
				Element.show(this.targetElement);
				
			} else if (!this.stLink && !this.isForChat) {
				this.URLControl.disabled=false;
				this.targetControl.disabled=false;
				Element[(this.openInNewWindow?'show':'hide')](this.targetElement);
			}				
			
			Element.update(this.URLElement,this.URL);
		},
		
		open:function(evt,position) {
			evt = (evt) ? evt : ((window.event) ? event : null);
			
			if(position == 'pointer') {
				shiftTo(this.objRef,Event.pointerX(evt) - Element.getDimensions(this.objRef).width,Event.pointerY(evt));
			} else  {
				shiftTo(this.objRef,300,200);
			}
			this.objRef.style.display = 'block';
			this.objRef.style.zIndex=10000+i;		
			if(this.focusElement) {this.focusElement.focus();}
			
			//bring it to the front
			var i=0;
			for(var w in global.windows) {
				if(global.windows[w].isOpen) {
					i++;
				}
			}
			this.reset();
			this.isOpen=true;
		}
	}
	
	Object.extend(winLink,xo);	
	
	winLink.reset();

	winLink.formStatusObserver=winLink.setLink.bindAsEventListener(winLink);

	Event.observe(winLink.textControl,'keyup',winLink.formStatusObserver,true);	
	Event.observe(winLink.URLControl,'keyup',winLink.formStatusObserver,true);
	Event.observe(winLink.typeControl,'change',winLink.formStatusObserver,false);
	Event.observe(winLink.isForChatControl,'click',winLink.formStatusObserver,false);
	Event.observe(winLink.targetControl,'click',winLink.formStatusObserver,false);
	
	Event.observe(winLink.textControl,'focus',function(){global.fieldFocus=true},false);
	Event.observe(winLink.textControl,'blur',function(){global.fieldFocus=false},false);
}

function extendAlertsWindow(){
	var xo={
		recordset: new Array(),
		execute:function(){
		
			execute_Response=function(obj){
				var s=function(a,b){
					var timeA=new Date(a.timestamp).getTime();
					var timeB=new Date(b.timestamp).getTime();
					return timeA-timeB;
				}					
				this.recordset=obj.data;
				this.recordset.sort(s);
				this.populate();
			}
			var args=new Object;
			args.object="alerts";
			args.method="list";
			Element.update('dspAlerts',"Loading...");
			http('POST','index.cfm',execute_Response.bind(this),args);
		},
		
		populate:function(){
			if(this.recordset.length==0){
				Element.update('dspAlerts','No administrative tasks currently require your attention');
				return;
			}
			
			var s='';
			s+='<table border="0" cellpadding="2" cellspacing="4" width="100%">';
			s+='<thead><tr>';
			s+='<th class="alertsHeaderCell" style="width:180px;">Date</th>';
			s+='<th class="alertsHeaderCell" style="width:450px;">Link</th>';
			s+='<th class="alertsHeaderCell" style="width:50px;">Type</th>';
			s+='<th class="alertsHeaderCell" style="width:300px;">Status</th>';
			s+='</tr></thead>';
			s+='<tbody>';
			for(var i=0;i<this.recordset.length;i++){
				var o=this.recordset[i];
				s+='<tr>';
				s+='<td class="alertsDataCell" style="width:180px;">' + formatArchiveMessageDate(o.timestamp); + '</td>';
				s+='<td class="alertsDataCell" style="width:450px;">' + o.link + '</td>';
				s+='<td class="alertsDataCell" style="width:50px;">' + o.type + '</td>';
				s+='<td class="alertsDataCell" style="width:300px;">' + o.status + '</td>';
				s+='</tr>';
			}
			s+='</tbody></table>';
			Element.update('dspAlerts',s);
		}
	}
	
	Object.extend(winAlerts,xo);
	// observers
}