// JavaScript Document
var search_type = 'group';
var PeopleTagger = Class.create({
	initialize:function(element,options){
		this.setOptions(options);
		if(!element)return;
		this.element=$(element);
		this.selectedArray=[];
		this.element.value=this.options.labelText;
		this.element.setAttribute('autocomplete','off');
		if(document.readyState&&!(/loaded|complete/.test(document.readyState))){
			Event.observe(window,'load',this.prepareTagger.bind(this));
		}else{
			this.prepareTagger();
		}
	this.initSearcher();
},initSearcher:function(){
	this.searcher = new AutoCompleteSearch(path+"ajaxrequest/auto_complete_user_array",
   {
		search_properties:["name","full_name"],
		query_param:'str',
		processData:function(json){ 
		if(json==null){
			if(search_type=='user'){
				var json = search_user_container;
			}else if(search_type=='tags'){
				var json = search_tag_container; 
			}else if(search_type=='group'){
				var json = '';//{"tags": [{"name": "tamilnadu", "messages": "2", "followers": "1", "id": "130027"}, {"name": "test", "messages": "1", "followers": "1", "id": "124757"}]};
			}
		}
		var obj=[];
		//var json = search_user_container;
	if(json.users){
		 obj=obj.concat(json.users.each(function(item){item.type='user'}));
	}
	if(json.tags){
		obj=obj.concat(json.tags.each(function(item){item.type='tag'}));
	}
	if(json.groups){
		obj=obj.concat(json.groups.each(function(item){item.type='group'}));
	}
	return obj
},
onMatch:
	this.buildListItems.bind(this)});
},
prepareTagger:function(){
	this.update=$(document.createElement("div")).addClassName(this.options.className).hide();
	Element.insert(this.options.attachTo,{bottom:this.update});
	Event.observe(this.element,'focus',this.showTagger.bind(this));
	this.prepareIE();
},
showTagger:function(event){
	if(this.active)return;
	this.active=true;
	if(this.options.clearOnFocus){
		this.element.value="";
	}
if(this.iefix)
	setTimeout(this.fixIEOverlapping.bind(this),1);
	this.registerTaggerEvents();
	this.setPosition();
	this.updateChoices();
},
hideTagger:function(){
	this.active=false;
	if(this.iefix)this.iefix.hide();
	this.element.value=($F(this.element)==this.options.labelText||$F(this.element).blank())?this.options.labelText:$F(this.element);
	if(this.options.disableTab){
		this.element.blur();
	}
	this.unregisterTaggerEvents();
	this.update.hide();
},
registerTaggerEvents:function(){
	this.mouseDownEvent=this.onMouseDown.bindAsEventListener(this);
	this.keyDownEvent=this.onKeyDown.bindAsEventListener(this)
	Event.observe(document,"mousedown",this.mouseDownEvent);
	Event.observe(this.element,'keydown',this.keyDownEvent);
},
unregisterTaggerEvents:function(){
	Event.stopObserving(document,"mousedown",this.mouseDownEvent);
	Event.stopObserving(this.element,'keydown',this.keyDownEvent);
},
setPosition:function(){
	Position.clone(
		this.element,this.update,{
			setHeight:false,setWidth:false,offsetTop:this.element.getHeight()});},
prepareIE:function(){
	if(!this.iefix&&(Prototype.Browser.IE)&&(Element.getStyle(this.update,'position')=='absolute')){
		new Insertion.After(
			this.update,'<iframe id="'+this.update.id+'_iefix" '+'style="display:none;position:absolute;width:0;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" '+'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
		this.iefix=$(this.update.id+'_iefix');}},
		fixIEOverlapping:function(){
			Position.clone(this.update,this.iefix,{setTop:(true)});this.iefix.setStyle({'zIndex':'1'});
			this.update.setStyle({'zIndex':'2'});
			this.iefix.show();
		},
		onMouseDown:function(event){
			var x=Event.pointerX(event);
			var y=Event.pointerY(event);
			if(!Position.within(this.update,x,y)&&Event.element(event)!=this.element)this.hideTagger();
		},
		onKeyDown:function(event){
			switch(event.keyCode){
				case Event.KEY_RETURN:
				if(this.options.onNoSelection){
					if(!this.getEntry(this.index))this.options.onNoSelection();
				}
		event.stop();
	case Event.KEY_TAB:this.selectEntry(event);
	if(this.options.disableTab){
		event.stop();
	}else{
		this.hideTagger();
	}
	return;case Event.KEY_ESC:this.hideTagger();
Event.stop(event);return;
	case Event.KEY_LEFT:case Event.KEY_RIGHT:return;
	case Event.KEY_UP:this.markPrevious();return;
	case Event.KEY_DOWN:this.markNext();return;
	default:this.defaultKeyDown();
}},
defaultKeyDown:function(){
	if(this.observer)clearTimeout(this.observer);
		this.observer=setTimeout(
			function(){this.updateChoices()}.bind(this),0*1000);
		},
markPrevious:function(){
	this.index=(this.index>0)?this.index-1:this.entryCount-1;
	var entry=this.getEntry(this.index);
	if(entry){
		if(this.options.updateInputOnMark)
			this.element.value=entry.profile.name;
			this.render();
		this.updateScroll();
		}
	},
markNext:function(){
	this.index=(this.index<this.entryCount-1)?this.index+1:0
var entry=this.getEntry(this.index);
if(entry){
	if(this.options.updateInputOnMark)
		this.element.value=entry.profile.name;
		this.render();
		this.updateScroll();
	}
},
updateScroll:function(){
	if(this.index==-1){
		return;
	}
	var entryHeight=this.getEntry(this.index).getHeight();
	var numShowing=Math.floor(this.options.maximumHeight/entryHeight)-1;
	var topEntry=Math.ceil(this.update.scrollTop/entryHeight)
	var isVisible=((this.index>topEntry)&&(this.index<topEntry+numShowing));
	var offset=(this.index<=topEntry)?entryHeight:this.options.maximumHeight
	if(!isVisible){
		this.update.scrollTop=(this.index+1)*(entryHeight)-offset;
	}
},
getEntry:function(index){
	return $A(this.update.getElementsByTagName('li'))[index];
},
updateChoices:function(){
	this.update.update();
	var input=$F(this.element);
	this.findMatches(input);
},
buildListItems:function(data){
	var input=$F(this.element);
	if(!this.active)return
if(input.blank())return this.getTip();
if(data.length<1){
	if(this.options.showNoMatches){
		this.searchForm(false);
	}else{
		this.update.hide();
	}
}else{
	var ul=document.createElement('ul');
	data.each(function(item){
		var li=$(document.createElement('li'));
		li.profile=item;
		switch(item.type){
			case"user":
				li.update(item.name+" <span>"+item.full_name+" ("+item.messages+")</span>");
				break;
			case"tag":
				li.update("#"+item.name+" <span>("+item.messages+")</span>");
				break;
			case"group":
				li.update(item.full_name+" <span>("+item.messages+")</span>");
				break;
			}
	ul.appendChild(li);
});
		
this.update.update().insert(ul);
var entrys=$A(this.update.getElementsByTagName('li'));
entrys.each(function(el,i){
	el.autocompleteIndex=i;
	Event.observe(el,"mouseover",this.onHover.bindAsEventListener(this));
	Event.observe(el,"click",this.selectEntry.bindAsEventListener(this));}.bind(this));
	this.entryCount=entrys.length;this.index=(input.blank()||!this.entryCount)?-1:(this.options.selectFirstEntry)?0:-1;this.render();
	this.update[(input.blank()&&this.options.tip.blank())?'hide':'show']();}
	this.setDimensions();
	this.setPosition();
},
selectEntry:function(){
	if(this.index<0)return false;
	var entry=this.getEntry(this.index);
	if(!entry)return false;
	this.element.value=(this.options.clearAfterSelection)?this.options.labelText:entry.profile.name;
	this.hideTagger();
	this.addSelected(entry.profile);
	if(this.options.onSelect)
		this.options.onSelect(entry.profile);
	if(this.options.onSelectUrl){
	var params=this.options.onSelectParameters;
	params[this.options.paramName]=entry.profile;
	new Ajax.Request(this.options.onSelectUrl,{parameters:params})}
	return true;
	},
onHover:function(event){
	this.index=Event.findElement(event,'li').autocompleteIndex;
	this.render();
	Event.stop(event);
},
render:function(){
	if(this.entryCount<=0)return;
	this.update.getElementsBySelector('ul li.hover').invoke('removeClassName','hover')
	if(this.index>=0)
		this.getEntry(this.index).addClassName("hover");
	},
searchForm:function(remote){
	var form=$(document.createElement('div')).addClassName('search');
	form.innerHTML="No matches found. "
	this.update.insert(form);
},
getTip:function(){
	if(!this.options.tip){
		this.update.hide();
		return;
	}
var tip=$(document.createElement('div')).addClassName('tip');
tip.innerHTML=this.options.tip
	this.update.insert(tip);
},
findMatches:function(searchString){
	if(!searchString){return this.update.hide();}
	if(this.options.prepend_to_search){
		searchString=this.options.prepend_to_search+searchString;
		this.searcher.discard_prefix=this.options.prepend_to_search;
	}
	this.searcher.searchFor(searchString);
},
addSelected:function(profile){
	this.selectedArray.push(profile);
	if(!this.options.allowDuplicates)this.listArray=this.listArray.without(profile);
	this.updateSelected();
},
removeSelected:function(profile){
	this.selectedArray=this.selectedArray.without(profile);
	if(!this.options.allowDuplicates)this.listArray.push(profile);
	this.updateSelected();if(this.options.onRemove)
	this.options.onRemove(profile);
	if(this.options.onRemoveUrl){
		var params=this.options.onRemoveParameters;params[this.options.paramName]=profile;
		new Ajax.Request(this.options.onRemoveUrl,{parameters:params})
	}
},
updateSelected:function(){
	if(this.options.selectedList)
	$(this.options.selectedList).update(this.selectedArray.collect(function(p){
		var profile=p;
		var template=new Template(this.options.selectedListTemplate);
		return template.evaluate(profile);}.bind(this)).join(', '));
},
setDimensions:function(){
	this.update.setStyle({width:"auto",height:"auto"})
	var ow=this.update.getWidth();
	var oh=this.update.getHeight();
	var sw=0;if(oh>this.options.maximumHeight){var nh=this.options.maximumHeight;sw=18;}else{var nh=oh;}
	var nw=((ow+sw)<this.element.getWidth())?this.element.getWidth():(ow+sw)
	this.update.setStyle({'width':nw+'px','height':nh+'px'})},
createEntryObject:function(entry){
	var obj={};
	this.options.entryItems.each(function(key,index){obj[key]=entry[index];})
	return obj;
},
setOptions:function(options){
	this.options=Object.extend({
	   attachTo:document.body,
	   entryItems:['name','id','username','permalink','message_count'],
	   onNoSelection:null,
	   onSelect:null,
	   onSelectUrl:'',
	   onSelectParameters:{},
	   onRemove:null,
	   onRemoveUrl:'',
	   onRemoveParameters:{},
	   paramName:"id",
	   clearAfterSelection:true,
	   clearOnFocus:true,
	   selectFirstEntry:false,
	   disableTab:true,
	   arrayLimit:50,
	   labelText:"",
	   className:"auto_load",
	   maximumHeight:250,
	   updateInputOnMark:true,
	   tip:null,
	   accept_newlines:false,
	   allowDuplicates:true,
	   selectedList:null,
	   selectedListTemplate:"<a href="+path+"'user/profile/#{id}'>#{name}</a>",
	   showNoMatches:true,
	   allowRemoteSearch:true,
	   remoteSearchUrl:"/autocomplete/people",
	   remoteSearchParamName:'text',
	   searchText:"search entire tree",
	   hideGroupAddressing:false,
	   prepend_to_search:null},(options||{}));}
});

PeopleTagger.MessageInput=Class.create(PeopleTagger,{
	   initialize:function($super,element,options){ $super(element,options); },
		showTagger:function(event){
			if(this.active)return;
			this.active=true;
			if(this.iefix)setTimeout(this.fixIEOverlapping.bind(this),1);
			this.registerTaggerEvents();
			this.setPosition();
		},
registerTaggerEvents:function($super){
	$super();
	this.keyUpEvent=this.onKeyUp.bindAsEventListener(this);
	this.keyPressEvent=this.onKeyPress.bindAsEventListener(this);
	this.element.observe('keyup',this.keyUpEvent);
	this.element.observe('keypress',this.keyPressEvent);
},
unregisterTaggerEvents:function($super){
	$super();
	Event.stopObserving(this.element,'keyup',this.keyUpEvent);
	Event.stopObserving(this.element,'keypress',this.keyPressEvent);
},
markNext:function(){
		if(!this.activated)return;
		this.index=(this.index<this.entryCount-1)?this.index+1:0
	var entry=this.getEntry(this.index);
	if(entry){
		this.element.value=this.updatedEntry(entry.profile.name);
		this.render();
		this.updateScroll();
		setTimeout(function(){
							this.setCaretPosition(this.tagPosition+entry.profile.name.length+1);
						}.bind(this),1
		);
}},
markPrevious:function(){
	if(!this.activated)return;
	this.index=(this.index>0)?this.index-1:this.entryCount-1;
	var entry=this.getEntry(this.index);
	if(entry){
		this.element.value=this.updatedEntry(entry.profile.name);
	this.render();
	this.updateScroll();
	setTimeout(function(){
						this.setCaretPosition(this.tagPosition+entry.profile.name.length+1);
				}.bind(this),1);
	}
},
onKeyPress:function(event){
	if(event.keyCode==13&&!this.options.accept_newlines)event.stop();
},
onKeyUp:function(event){
	switch(event.keyCode){
		case 16:return;
		case 13:if(!this.options.accept_newlines){
					if(!this.getEntry(this.index)){
						if(this.options.onEnter)this.options.onEnter()
						return;
					}
					this.index=-1;event.stop();
				}return;
		case 9:this.index=-1;
			   return;
		case 32:this.activated=false;
				this.active=false;
				this.tagPosition=null;
				this.update.hide();
				this.index=-1;
				return;
}
var input=$F(this.element);
var caretPos=this.getCaretPosition();
if(input.substring(caretPos-2,caretPos).match(/^(\s|[,;:])@$|^@$/)){
	this.tagPosition	=caretPos;
	this.activated		=true;
	this.active			=true;
	this.showInvite		=true;
	this.prefix			='@';
	search_type			='user';
	this.searcher.discard_prefix='@';
	this.updateChoices();
	this.showTip('Start typing a user name to see options');
}else if(input.substring(caretPos-2,caretPos).match(/^(\s|[,;:])#$|^#$/)){
		this.tagPosition=caretPos;
		this.activated=true;
		this.active=true;
		this.showInvite=false;
		this.prefix='#';
		search_type			='tags'; 
		this.searcher.discard_prefix='#';
	this.updateChoices();
	this.showTip('Start typing a tag name to see options');
}else if(!this.options.hideGroupAddressing&&input.substring(0,caretPos).match(/^to:$/)){
	this.tagPosition=caretPos;
	this.activated=true;
	this.active=true;
	this.showInvite=false;
	this.updateChoices();
	this.prefix='to:';
	this.searcher.discard_prefix='to:';
	this.showTip('Start typing a user or group name to see options');
}
},
showTip:function(tip_text){var tip=new Element('div',{'class':'tip'}).update(tip_text);this.update.update().insert(tip);this.setDimensions();this.update.show();},onKeyDown:function($super,event){if(event.keyCode==13){if(this.options.accept_newlines&&!this.activated){return;}else{event.stop();}}

$super(event);if(event.keyCode==Event.KEY_LEFT||event.keyCode==Event.KEY_RIGHT){this.defaultKeyDown();}},defaultKeyDown:function(){if(this.observer)clearTimeout(this.observer);this.observer=setTimeout(function(){this.updateChoices(this.currentArray)}.bind(this),0*1000);},
getCaretPosition:function(){
	if(document.selection){
		var i=this.element.value.length+1;
		if(this.element.createTextRange){
			theCaret=document.selection.createRange().duplicate();
				while(theCaret.parentElement()==this.element&&theCaret.move("character",1)==1)--i;
		}
			return i==this.element.value.length+1?-1:i;
	}else{
			return this.element.selectionStart;
	}
},
setCaretPosition:function(pos){if(this.element.setSelectionRange){this.element.focus();this.element.setSelectionRange(pos,pos);}else if(this.element.createTextRange){var range=this.element.createTextRange();range.collapse(true);range.moveEnd('character',pos);range.moveStart('character',pos);range.select();}},selectEntry:function($super,event){if(this.index<0||Object.isUndefined(this.index))return;var entry=this.getEntry(this.index);if(!entry)return;var entryText=entry.profile.name;this.element.value=this.updatedEntry(entryText);this.hideTagger();this.setCaretPosition(this.tagPosition+entryText.length+1)
	this.activated=false;
	if(this.options.onSelect){
		this.options.onSelect(entry.profile,this.arrayType);
	}
},
onMouseDown:function(event){
	var x=Event.pointerX(event);
	var y=Event.pointerY(event);
	var scrollX=0;var scrollY=0;
	if(!Prototype.Browser.IE){var scrollX=document.viewport.getScrollOffsets().left;
	var scrollY=document.viewport.getScrollOffsets().top;
}
var isWithin=((y>=this.update.cumulativeOffset().top+scrollY)&&(x>=this.update.cumulativeOffset().left+scrollX)&&(y<(this.update.cumulativeOffset().top+scrollY+this.update.getHeight()))&&(x<(this.update.cumulativeOffset().left+scrollX+this.update.getWidth())))
if(!isWithin&&(Event.element(event)!=this.element))this.hideTagger();},
	updateChoices:function(){
		var input=$F(this.element);
		var finished=input.substring(this.tagPosition-1,this.getCaretPosition()).match(/\.| |^$/);
		if(input.blank()||!this.activated||finished){
			this.activated=false;
			this.tagPosition=null;
			this.arrayType=null;
			this.update.hide();
			this.index=-1;
			return null;
		}
	this.findMatches(this.getCurrentSearchTerm());

}

,searchForm:function(){
	if(!this.getCurrentSearchTerm(true))return;
	if(this.showInvite&&this.options.allowInvites){
		var form=this.inviteForm();
	}else{
		var form=$(document.createElement('div')).addClassName('search');
		form.innerHTML="No matches found. ";
	}
this.update.update().show().insert(form);
this.setDimensions();
},
inviteForm:function(){
	this.update.update()
	var invitee=this.getCurrentSearchTerm(true);
	var form=$(document.createElement('div')).addClassName('search');
	form.update("<strong>There is no match for @"+invitee+".</strong><br />");
	var inviteLink=new Element('a',{href:'javascript://'}).update("Click here to invite this person to your network.");
	form.insert(inviteLink);inviteLink.observe('click',function(){Workfeed.Ajax.ajax_request('/invitations/emails',{parameters:{'usernames[]':invitee,authenticity_token:Workfeed.Ajax.encode_authenticity_token()},onSuccess:function(){form.update("<strong>We have sent an invite to "+invitee+".</strong>");},onFailure:function(){form.update("<strong>Uh oh. There was a problem with your request.</strong>");}})});return form;},getCurrentSearchTerm:function(no_prefix){var st=$F(this.element).substring(this.tagPosition).split(" ")[0]||null;if(!st)return null;return(no_prefix)?st:this.prefix+st;},updatedEntry:function(entry){var prefix=$F(this.element).substring(0,this.tagPosition);var suffix=$F(this.element).substring(this.tagPosition).split(" ").slice(1).join(" ");return(prefix+entry+" "+suffix);},setOptions:function($super,options){$super(options);Object.extend(this.options,{selectFirstEntry:true,labelText:"",clearAfterSelection:false,onEnter:null});Object.extend(this.options,options);}});AutoCompleteCache=[];
AutoCompleteSearch=Class.create({
	initialize:function(data,options){
		this.data=data;
		this.setOptions(options);
		this.initData();
	},initData:function(){
		this.data_array=[];
		if(!Object.isString(this.data)){
			this.createOptimizedData();
			this.is_local_data=true;
		}else{
			this.local_data=false;
			this.request_timer=null;
			}
		},createOptimizedData:function(array){
			var array=array||this.data;
			this.data_array=[];
			array.each(function(item){
					if(this.exceptions&&this.exceptions(item))return;
					  var search=(this.search_properties)?this.search_properties.collect(function(p){return item[p]}).join(' '):Object.values(item).join(' ');
					  this.data_array.push([search,item]);
					 }.bind(this));
			return this.data_array;
		},getMatches:function(search_term){
			if(this.discard_prefix){
				search_term=search_term.replace(this.discard_prefix,'');
			}
			return this.data_array.collect(function(item){
													return(item[0].match(new RegExp("(^| )"+search_term,"i")))?item[1]:null;
													}).compact();
			},searchFor:function(search_term){
				search_term=search_term.stripTags();
				if(!search_term||search_term.blank()){
					return this.onMatch([]);
				}
				if(AutoCompleteCache["cache_"+search_term]){
					return this.onMatch(AutoCompleteCache["cache_"+search_term]);
				}
				if(this.local_data||this.data_array.length<this.max_results&&search_term.match(new RegExp("^"+this.previous_search_term,"i"))){
					var matches=this.getMatches(search_term).slice(0,this.choices);
					return this.onMatch(matches);
				}
				if(!this.local_data){
					clearTimeout(this.request_timer);
					this.request_timer=setTimeout(function(){
					   this.previous_search_term=search_term;
					   var params={};
					   params[this.query_param]=search_term;
					   new Ajax.Request(this.data,{
										method:'post',parameters:params,evalJSON:true,onComplete:this.processJSON.bind(this)
						})
						this.request_timer=null;
					}.bind(this),this.delay_request)
				}
			},processJSON:function(request){
				eval(request.responseText);
				var json=(this.processData)?this.processData(search_container):search_container;
				//var json=request.responseText;
				AutoCompleteCache["cache_"+request.request.parameters[this.query_param]]=json;
				this.createOptimizedData(json);
				
				this.onMatch(json)},setOptions:function(options){
					var options=Object.extend({
					  search_properties:null,
					  exceptions:null,
					  processData:null,
					  max_results:50,
					  choices:20,
					  query_param:'query',
					  delay_request:300,
					  discard_prefix:null,
					  allowInvites:false
					},(options||{}));
					Object.extend(this,options);
				}
});
var UIObayoo=UIObayoo||{};
var Effect=Effect||undefined;
UIObayoo.Panel=Class.create({initialize:function(options){this.setOptions(options);
this.selectshidden=false;
this.isIE6=(navigator.userAgent.indexOf('MSIE 6.0')!=-1);
this.buildElements();
},
buildElements:function(){
	this.buildPanel();
	this.buildOverlay();
	if(this.isIE6)this.buildIE6iframe();
	},
buildPanel:function(){
	this.panel=$(this.options.panelId)||new Element('div',{id:this.options.panelId}).hide();
	this.panel.setStyle({position:((this.isIE6)?'absolute':'fixed'),
	zIndex:9001});
	if(this.options.attachTo){
		Element.insert($(this.options.attachTo),this.panel);
	}else{
		Element.insert(document.body,{top:this.panel});
	}
},
buildOverlay:function(){
	if(!this.options.displayOverlay)return;
	this.overlay=$(this.options.overlayId)||new Element('div',{id:this.options.overlayId}).hide();
	this.overlay.setStyle({backgroundColor:this.options.overlayColor,opacity:this.options.overlayOpacity,position:((this.isIE6)?'absolute':'fixed'),width:'100%',height:'100%',zIndex:9000,left:0,top:0})
	$(document.body).insert({top:this.overlay});
},
buildIE6iframe:function(){
	this.iefix=new Element('iframe',{'src':'javascript:false;'}).setStyle({opacity:0,position:'absolute',zIndex:8999}).hide();
	$(document.body).insert(this.iefix);
},
updateContent:function(content){
	if(this.options.url){
		this.loadInfo();
	}else{
		var content=content||this.options.content;
		if(content){
			this.panel.update().insert(content);
		}
	this.panel.className=this.options.panelClass;
	this.showPanel();
	}
},
clickOut:function(event){
	var x=Event.pointerX(event);
	var y=Event.pointerY(event);
	if(!this.options.positionRelativeTo){
		x-=document.viewport.getScrollOffsets()[0];
		y-=document.viewport.getScrollOffsets()[1];
	}
	if(Position.within(this.panel,x,y))return;
	if(!this.options.enableClickOutOnTarget){
		if(this.target&&Position.within(this.target,x,y))return;
	}
	Event.stopObserving(document,'mouseup',this.clickout);
	this.close();
},open:function(){
	this.fixIE6();
	if(this.overlay)this.overlay.show();
	this.updateContent();
	this.toggleSelects();
	if(this.options.enableClickOut){
		this.clickout=this.clickOut.bind(this);
		Event.observe(document,'mouseup',this.clickout);
	}
	if(this.options.onOpen)this.options.onOpen(this)},close:function(){
		this.fixIE6();
		this.toggleSelects();
		if(this.overlay)this.overlay.hide()
	this.hidePanel();
	if(this.options.onClose)this.options.onClose(this)
},
toggleSelects:function(){
	if(!this.isIE6)return;
	if(this.options.displayOverlay){
		$$('select').invoke('toggle');
		this.panel.select('select').invoke('show');
	}else{
		this.iefix.clonePosition(this.panel);
		this.iefix.toggle();
	}
},
fixIE6:function(){
	if(!this.isIE6||!this.options.displayOverlay)return;
	if(!this.overlay.visible()){
		this.scrollPos=document.viewport.getScrollOffsets();
		$$('body')[0].setStyle({height:'100%',overflow:'hidden'})
		$$('html')[0].setStyle({height:'100%',overflow:'hidden'});
	}else{$$('body')[0].setStyle({height:'',overflow:''})
		$$('html')[0].setStyle({height:'',overflow:''});
		window.scrollTo(this.scrollPos[0],this.scrollPos[1]);
	}
},
loadInfo:function(url){
	if(this.options.showLoading){
		this.panel.update(this.options.loadingText)
		this.panel.className=this.options.loadingClass;
	}
	this.showPanel();
	this.options.ajaxOptions=Object.extend({evalScripts:true,onComplete:function(){
		this.panel.className=this.options.panelClass;
		this.showPanel();
		if(this.options.onLoadComplete)this.options.onLoadComplete(this)}.bind(this),onFailure:function(){this.close();
		throw("There was a problem loading the content.");
	}.bind(this)},
	this.options.ajaxOptions)
	setTimeout(function(){
		new Ajax.Updater(this.panel,url||this.options.url,this.options.ajaxOptions);
	}.bind(this),1000)},
setPosition:function(){
	if(this.options.positionRelativeTo){
		this.target=$(this.options.positionRelativeTo);
		var targetCorner=this.getCorner(this.target,this.options.hooks[0]);
		var panelCorner=this.getCorner(this.panel,this.options.hooks[1]);
		var tp=this.target.cumulativeOffset();
		var xPos=tp.left+targetCorner.left-panelCorner.left+this.options.offsetX;
		var yPos=tp.top+targetCorner.top-panelCorner.top+this.options.offsetY;
		this.panel.setStyle({position:'absolute',left:xPos+'px',top:yPos+'px',margin:0});
	}else{
		var panelDimensions=this.panel.getDimensions();
		var xPos=((panelDimensions.width/2)*-1)+this.options.offsetX;
		var yPos=((panelDimensions.height/2)*-1)+this.options.offsetY;
		if(this.isIE6&&!this.options.displayOverlay){
			var scrollOffsets=document.viewport.getScrollOffsets()
			xPos+=scrollOffsets.left;yPos+=scrollOffsets.top;
		}
		this.panel.setStyle({top:'50%',left:'50%',marginLeft:xPos+'px',marginTop:yPos+'px'});
	}
},
showPanel:function(){
	this.setPosition();
	if(!Object.isUndefined(Effect)&&this.options.showEffect){
		Effect[this.options.showEffect](this.panel,this.options.showEffectOptions);}else{this.panel.show();
	}
	this.addActions();},hidePanel:function(){
		if(!Object.isUndefined(Effect)&&this.options.hideEffect){Effect[this.options.hideEffect](this.panel,this.options.hideEffectOptions);
	}else{
		this.panel.hide();
	}
},
addActions:function(){
	this.panel.select("a.panel_action").each(function(el){
		Event.observe(el,'click',function(event){
			this[el.rel](el.href);
			event.stop();
		}.bind(this))}.bind(this));
},
update:function(url){
	this.loadInfo(url);
},
getCorner:function(target,corner){
	if(this.options.eventObject&&corner.match(/MOUSE/i)){
		var position=target.cumulativeOffset();
		return{left:Event.pointerX(this.options.eventObject)-position.left,top:Event.pointerY(this.options.eventObject)-position.top}}
		var dimensions=$(target).getDimensions();	
		var xPos=(corner.match(/R/i))?dimensions.width:(corner.match(/L/i))?0:dimensions.width/2;
		var yPos=(corner.match(/B/i))?dimensions.height:(corner.match(/T/i))?0:dimensions.height/2;
		return{left:xPos,top:yPos}},
setOptions:function(options){
	this.options=Object.extend({panelId:'panel',
	   overlayId:'overlay',
	   overlayOpacity:0.3,
	   overlayColor:'#666666',
	   displayOverlay:true,
	   positionRelativeTo:null,
	   attachTo:null,hooks:['C','C'],
	   offsetX:0,offsetY:0,
	   showEffect:null,
	   showEffectOptions:{},
	   hideEffect:null,
	   hideEffectOptions:{},
	   url:null,
	   ajaxOptions:{},
	   loadingText:'Loading...',
	   content:null,
	   panelClass:'panel_tab',
	   loadingClass:'class_loading',
	   showLoading:true,
	   enableClickOut:false,
	   enableClickOutOnTarget:false,
	   onOpen:function(){},
	   onClose:null,
	   onLoadComplete:null,
	   eventObject:null},(options||{}));
	}});


var $F=Form.Element.Methods.getValue;