YAHOO.namespace("reunion");

//DynamicSelectPopulator ties two select tags together (e.g., state/city search
YAHOO.reunion.DynamicSelectPopulator = function(sourceSelectTag, destSelectTag, queryUrl, schema){
	this.sourceSelectTag = YAHOO.util.Dom.get(sourceSelectTag);
	this.destSelectTag = YAHOO.util.Dom.get(destSelectTag);
	this.queryUrl = queryUrl;
	this.schema = schema;
	if (YAHOO.lang.isUndefined(this.schema.returnTypeIsLiteral)){
		this.schema.returnTypeIsLiteral = false;
	}
	this.populationEvent = new YAHOO.util.CustomEvent("populationEvent", this);
	
	YAHOO.util.Event.addListener(this.sourceSelectTag, 'change', this.doQuery, this, true);
}

YAHOO.reunion.DynamicSelectPopulator.prototype.doQuery = function(){
	this.destSelectTag.disabled = true;
	var self = this;
	this.destSelectTag.options.add(new Option("Loading...", ""));
	this.destSelectTag.selectedIndex = this.destSelectTag.length-1;
	
	if (YAHOO.lang.isUndefined(this.additionalParameter)){
		this.queryUrl.call(this, this.getSourceValue(), function(resultsList){self.interpretResults.call(self, resultsList)});
	} else {
		this.queryUrl.call(this, this.getSourceValue(), this.additionalParameter, function(resultsList){self.interpretResults.call(self, resultsList)});
	}
}

YAHOO.reunion.DynamicSelectPopulator.prototype.interpretResults = function(resultsList){
	this.clear(this.destSelectTag);
	this.destSelectTag.disabled = false;
	for (result in resultsList) {
		var label = resultsList[result];
		var value = resultsList[result];
		if (!this.schema.returnTypeIsLiteral){
			label = resultsList[result][this.schema.label];
			value = resultsList[result][this.schema.value];
		} 
		this.destSelectTag.options.add(new Option(label, value));			
   }		
   this.destSelectTag.style.visibility = 'visible';
   this.populationEvent.fire();	
}

YAHOO.reunion.DynamicSelectPopulator.prototype.clear = function(selectElement) {
  while(selectElement.length > 1) {
  	selectElement.remove(selectElement.length-1);
  }
}

YAHOO.reunion.DynamicSelectPopulator.prototype.getSourceValue = function(){
	return this.sourceSelectTag.options[this.sourceSelectTag.selectedIndex].value;
}

//LightBox for overlays and such

YAHOO.reunion.LightBox = function(elementToDisplay, elementsToHide, closeButton){
	this.elementToDisplay = elementToDisplay;
	this.elementsToHide = elementsToHide;
	
	if (!YAHOO.lang.isUndefined(closeButton)){
		this.closeButton = closeButton;
		YAHOO.util.Event.addListener(this.closeButton, "click", this.close, this, true);
	}

	this.greyScreen = document.createElement("div");
	this.greyScreen.id = "greyScreen";
	var greyScreen = this.greyScreen;
	YAHOO.util.Event.onDOMReady(function(){
		document.body.appendChild(greyScreen);
	});

	YAHOO.util.Dom.setStyle(this.greyScreen, "display", "none");
	this.openEvent = new YAHOO.util.CustomEvent("openLightBoxEvent");
	this.closeEvent = new YAHOO.util.CustomEvent("closeLightBoxEvent");
	
	YAHOO.util.Event.addListener(window, "resize", this.resizeGreyScreen, this, true);
	YAHOO.util.Event.addListener(window, "scroll", this.resizeGreyScreen, this, true);
	
}


YAHOO.reunion.LightBox.prototype.resizeGreyScreen = function(){
	var arrayPageSize = getPageSize();			
	YAHOO.util.Dom.setStyle(this.greyScreen, "width", "100%");
	YAHOO.util.Dom.setStyle(this.greyScreen, "height", arrayPageSize[1]+"px");
}

YAHOO.reunion.LightBox.prototype.open = function(){
		//hide advertisements or other divs and selects with competing z-indexes
		
		YAHOO.util.Dom.setStyle(this.elementsToHide, "visibility", "hidden");
		
		this.hideSelects('hidden'); 
	
	 	this.resizeGreyScreen();
		
		YAHOO.util.Dom.setStyle(this.greyScreen, "display", "block");
		YAHOO.util.Dom.setStyle(this.elementToDisplay, "display", "block");
		
		this.openEvent.fire();
}

YAHOO.reunion.LightBox.prototype.close = function(){

		YAHOO.util.Dom.setStyle(this.greyScreen, "display", "none");
		YAHOO.util.Dom.setStyle(this.elementToDisplay, "display", "none");
		
		YAHOO.util.Dom.setStyle(this.elementsToHide, "visibility", "visible");
		this.hideSelects('visible');
		
		this.closeEvent.fire();
}

YAHOO.reunion.LightBox.prototype.hideSelects = function(action) {
	var cp_selects = document.getElementsByTagName("select");
	for (var i=0; i< cp_selects.length; i++) {
		if(!YAHOO.util.Dom.isAncestor(this.elementToDisplay, cp_selects[i])){
			YAHOO.util.Dom.setStyle(cp_selects[i], "visibility", action);
		}
	}

    var cp_adFlash = document.getElementById("adFlash");
    YAHOO.util.Dom.setStyle(cp_adFlash, "visibility", action);
}


//====================================================================
//following is for popup floating layer  detect page size  
//====================================================================

function getPageSize(){        	
     	var xScroll, yScroll;			
    	
     	if (window.innerHeight && window.scrollMaxY) {	
     		xScroll = window.innerWidth + window.scrollMaxX;
     		yScroll = window.innerHeight + window.scrollMaxY;
     	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
     		xScroll = document.body.scrollWidth;
     		yScroll = document.body.scrollHeight;
     	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
     		xScroll = document.body.offsetWidth;
     		yScroll = document.body.offsetHeight;
     	}
     	
     	var windowWidth, windowHeight;
			
	        	
     	if (self.innerHeight) {	// all except Explorer
     		if(document.documentElement.clientWidth){
     			windowWidth = document.documentElement.clientWidth; 
     		} else {
     			windowWidth = self.innerWidth;
     		}
     		windowHeight = self.innerHeight;
     	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
     		windowWidth = document.documentElement.clientWidth;
     		windowHeight = document.documentElement.clientHeight;
     	} else if (document.body) { // other Explorers
     		windowWidth = document.body.clientWidth;
     		windowHeight = document.body.clientHeight;
     	}	
     	
     	if(yScroll < windowHeight){
     		pageHeight = windowHeight;
     	} else { 
     		pageHeight = yScroll;
     	}
     
     

     	if(xScroll < windowWidth){	
     		pageWidth = xScroll;		
     	} else {
     		pageWidth = windowWidth;
     	}

     	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 
     	return arrayPageSize;
     }