/* (C) Copyright IBM Corp. 2007, 2008 All Rights Reserved. */
Tabs = {
	findSelected: function(el) {
		var tabs = document.getElementsByClassName('lotusSelected', el);
		if(tabs.length != 0)
			return tabs[0];
	},
	
	switchTo: function(newtabId, containerId, func ) {		
		var seltab = Tabs.findSelected($(containerId));
		//if we just want to switch to nothing, just unselect the old tab
		if(newtabId == null) {
			Element.removeClassName(seltab, 'lotusSelected');
		} else { //else switch to the newer tab
			var newtab = $(newtabId);
			//if the clicked tab is the one already selected, do nothing
			if(newtab == seltab)
				return;
		
			Element.addClassName(newtab, 'lotusSelected');
			Element.removeClassName(seltab, 'lotusSelected');
			
			Element.hide(seltab.id + 'Pane');
			Element.show(newtabId + 'Pane');
			
			func = func  || Prototype.emptyFunction;
			func.call();
		}
		
	}
};

MemberAddForm = {
	ID: 'memberAddDialog',
	DEFAULT_FOCUS_FIELD: 'memberAddAuthors',

    toggle: function(showOrHide) {
    	if (showOrHide == "show") {
           Element.show(this.ID);                        
        } else if (showOrHide == "hide") {
           Element.hide(this.ID);                   
        }
    },        
    
    setTabAndFocus: function( role, focusElement ) {
        $('roleToAddTo').value = role;
        DEFAULT_FOCUS_FIELD = focusElement;
        $(focusElement).focus();
	}

};

NotificationForm = {
	ID: 'NotificationFormDiv',
	
	init: false,

    isOpened: function() {
	    return (document.getElementById(this.ID).style.display != "none");
	},
	
    toggle: function(showOrHide, clearData) {
    	if (showOrHide == "show") {
    	   if(document.getElementById(this.ID).style.display == "none") {
           	Element.show(this.ID);   
           	//document.forms["NotificationForm"].receivers.value = "";
           	document.getElementById("notificationForm_message").innerHTML = ""; 
           	document.getElementById("notificationForm_messages").style.display = "none";     
           	showNoEmailService(); 
           	
           	if(!this.init) {
           	    _memberList.init(dijit.byId("receivers_typeahead"), $("widget_receivers_typeahead"));
           	    this.init = true;
           	}
           }                       
        } else if (showOrHide == "hide") {
           if(document.getElementById(this.ID).style.display != "none") {
            Element.hide(this.ID); 
            document.getElementById("notificationForm_message").innerHTML = ""; 
            document.getElementById("notificationForm_messages").style.display = "none";     
            
            if(clearData) {
           	    document.forms["NotificationForm"].description.value = "";  
           	    _memberList.init(dijit.byId("receivers_typeahead"), $("widget_receivers_typeahead"));
           	}       
           }            
        }
    }
}

FlagAsInappropriateForm = {
	ID: 'FlagAsInappropriateFormDiv',
	
	isOpened: function() {
	    return (document.getElementById(this.ID).style.display != "none");
	},

    toggle: function(showOrHide, clearData) {
    	if (showOrHide == "show") {
    	   if(document.getElementById(this.ID).style.display == "none") {
           	Element.show(this.ID);     
           	document.getElementById("flagAsInappropriateForm_message").innerHTML = ""; 
           	document.getElementById("flagAsInappropriateForm_messages").style.display = "none";        
           }                       
        } else if (showOrHide == "hide") {
           if(document.getElementById(this.ID).style.display != "none") {
            Element.hide(this.ID);       
            document.getElementById("flagAsInappropriateForm_message").innerHTML = ""; 
           	document.getElementById("flagAsInappropriateForm_messages").style.display = "none";   
           	
           	if(clearData) {
           	    document.forms["FlagAsInappropriateForm"].comment.value = ""; 
           	}
           }            
        }
    }
}

QuarantineThisEntryForm = {
	ID: 'QuarantineThisEntryFormDiv',
	
	isOpened: function() {
	    return (document.getElementById(this.ID).style.display != "none");
	},

    toggle: function(showOrHide, clearData) {
    	if (showOrHide == "show") {
    	   if(document.getElementById(this.ID).style.display == "none") {
           	Element.show(this.ID);     
           	document.getElementById("quarantineThisEntryForm_message").innerHTML = ""; 
           	document.getElementById("quarantineThisEntryForm_messages").style.display = "none";        
           }                       
        } else if (showOrHide == "hide") {
           if(document.getElementById(this.ID).style.display != "none") {
            Element.hide(this.ID);       
            document.getElementById("quarantineThisEntryForm_message").innerHTML = ""; 
           	document.getElementById("quarantineThisEntryForm_messages").style.display = "none";   
           	if(clearData) {
           	    document.forms["QuarantineThisEntryForm"].comment.value = ""; 
           	}
           }            
        }
    }
}

AddWeblogEntryCommentForm = {
	ID: 'AddWeblogEntryCommentFormDiv',
	
	isOpened: function() {
	    return (document.getElementById(this.ID).style.display != "none");
	},

    toggle: function(showOrHide, clearData) {
    	if (showOrHide == "show") {
    	   if(document.getElementById(this.ID).style.display == "none") {
           	Element.show(this.ID);     
           }                       
        } else if (showOrHide == "hide") {
           if(document.getElementById(this.ID).style.display != "none") {
            Element.hide(this.ID); 
            
            if(clearData) {
                document.forms["AddWeblogEntryCommentForm"].content.value = ""; 
            } 
            
            if(document.getElementById("commentspanel_preview")) {
                document.getElementById("commentspanel_preview").style.display = "none";
            }
            if(document.getElementById("commentspanel_list")) {
	            document.getElementById("commentspanel_list").style.display = "";  
	        }
           }            
        }
    }
}

DirectoryAutoCompleter = Class.create();
Object.extend(Object.extend(DirectoryAutoCompleter.prototype, Ajax.Autocompleter.prototype), {

	initialize: function(element, update, url, options, theindicator) {
		var options = {};

    	options.paramName = "member";
    	options.tokens = [','];
    	options.updateElement= this.myUpdateElement.bind(this);
        options.frequency= 0.01;
        options.minChars= 2;
		
		this.baseInitialize(element, update, options);
	    this.options.asynchronous = true;
	    this.options.onComplete = this.onComplete.bind(this)
	    this.options.method = 'get';
	    this.options.defaultParams = this.options.parameters || null;
	    this.options.indicator = theindicator;
	    this.url = url;
	    this.cache = {};
	    this.options.mine = options.mine || [];
	    this.useDirectory = false;
	    
	    this.options.afterUpdateElement = function(element, selectedElement) {
	        element.value = element.value + ",";
	    };
	    
        this.options.effectExecutors = new Object();
        this.options.effectExecutors.appearExecutors = new Object();
        this.options.effectExecutors.fadeExecutors = new Object();
        
        this.options.onShow = function(element, update){ 
      		if(!update.style.position || update.style.position=='absolute') {
        		update.style.position = 'absolute';
        		Position.clone(element, update, {setHeight: false, offsetTop: element.offsetHeight});
      		}
      		if(this.effectExecutors.appearExecutors[update]) {
      			return;
        	}
        	if(this.effectExecutors.fadeExecutors[update]) {
        		this.effectExecutors.fadeExecutors[update].cancel();
        		this.effectExecutors.fadeExecutors[update] = null;
        	}
      		this.effectExecutors.appearExecutors[update] = Effect.Appear(update,{duration:0.3});
   	 	};
        
        this.options.onHide = function(element, update){ 
        	if(this.effectExecutors.fadeExecutors[update]) {
      			return;
        	}
        	if(this.effectExecutors.appearExecutors[update]) {
        		this.effectExecutors.appearExecutors[update].cancel();
        		this.effectExecutors.appearExecutors[update] = null;
        	}
         	this.effectExecutors.fadeExecutors[update] = Effect.Fade(update,{duration:0.15}); 
        };
	  },
	  
   myUpdateElement: function(li) {
  	this.options.updateElement = null;
	if (li.id == "directory") {
    	this.useDirectory = true;
    	this.startIndicator();
    	this.activate ();    	
	    // always turn the directory off
	    // after this ajax call.. we will otherwise bring down the directory
	    this.useDirectory = false;

	} else {
 		this.updateElement(li);
	}
    this.options.updateElement= this.myUpdateElement.bind(this);
  },
  
  startIndicator: function() {
    if(this.options.indicator && this.useDirectory) Element.show(this.options.indicator);
  },
  
  show: function() {
    this.options.onShow(this.element, this.update);
    if(!this.iefix && 
      (navigator.appVersion.indexOf('MSIE')>0) &&
      (navigator.userAgent.indexOf('Opera')<0) &&
      (Element.getStyle(this.update, 'position')=='absolute')) {
      new Insertion.After(this.update, 
       '<iframe id="' + this.update.id + '_iefix" '+
       'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
       'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
      this.iefix = $(this.update.id+'_iefix');
    }
    if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
    
    this.element.focus();
  },
  
  hide: function() {
    this.options.onHide(this.element, this.update);
    if(this.iefix) Element.hide(this.iefix);
  },  
  
  onBlur: function(event) {
    // needed to make click events working
    setTimeout(this.hide.bind(this), 1);
    this.hasFocus = false;
    this.active = false;     
  }, 
  	
  getUpdatedChoices: function() {
  	var t = this.getToken();
  	if (this.cache[t])
  	{
  		this.updateChoices(this.cache[t]);
  		return;
  	}
  	
	this.options.parameters = this.options.callback ?
	this.options.callback(this.element, entry) : "";

	if (this.useDirectory) {
	  	this.options.parameters += '&member=' + encodeURIComponent(t) + '&directory=true&' + this.options.defaultParams;
	} else {
	    this.options.parameters += '&member=' + encodeURIComponent(t) + '&' + this.options.defaultParams;
	}
			
	    
	new Ajax.Request(this.url, this.options);
  },

	onComplete: function(request) {
		data = request.responseText; 
		this.updateChoices(data);
	}

});
