// COS Utility
// To be included in every *.cos.ucf.edu page head
// By Daniel Walker
// Feb 14, 2007
// Instance of CosUtil must be called 'C'
// This scripts requires:
//	*yahoo-min.js	
//	*dom-min.js
//	*CosUtil.js
// in that order

var $Y = YAHOO.util;
var $D = $Y.Dom;
var $E = $Y.Event;

function CosUtil() {

	this.transBGshowing = 0;
	this.transBG = 'CalendarTransBG';
	
	// _E returns the HTMLElement by id string, o
	this._E = function(eid)
	{
		return $D.get(eid);
	}
	
	// _C clears the text out of an element, o
	this._C = function(eid)
	{
		this._E(eid).innerHTML = '';
	}
	
	this.showElement = function(eid, opac)
	{
		$D.setStyle(eid, 'visibility', 'visible');
		$D.setStyle(eid, 'opacity', opac);
	}
	
	this.hideElement = function(eid, opac)
	{
		$D.setStyle(eid, 'visibility', 'hidden');
		$D.setStyle(eid, 'opacity', opac);
	}
	
	this.setWidth = function(eid, w)
	{
		if(typeof w != 'string') w += 'px';
		$D.setStyle(eid, 'width', w);
	}
	
	this.setHeight = function(eid, h)
	{
		if(typeof h != 'string') h += 'px';
		$D.setStyle(eid, 'height', h);
	}
	
	this.showTransBG = function(opac) {
		this.transBGshowing = 1;
		// set size for the transparentBG to fullscreen
		var arrayPageSize = this.getPageSize();
		this.setWidth(this.transBG, arrayPageSize[0]);
		this.setHeight(this.transBG, arrayPageSize[1]);
		this.showElement(this.transBG, opac);
	}
	
	this.hideTransBG = function() {
		if(this.transBGshowing == 1)
		{
			this.hideElement(this.transBG, 0); // 0.0 opacity
			this.setWidth(this.transBG, 0);
			this.setHeight(this.transBG, 0);
		}
	}
	
	this.overlayElement = function(eT, eP, eH)
	// eT is target element to move and resize
	// eP is element for position
	// eH is optional element for height
	// PreReq's: eP.style.zIndex is lower than eT.style.zIndex
	// eT must be absolutely positioned
	{
		// put top left corner in same place
		$D.setXY(eT, $D.getXY(eP));
		
		// now match width and height
		var w = $D.getStyle(eP,'width');
		var h = (eH == undefined) ? $D.getStyle(eP, 'height') : $D.getStyle(eH, 'height');
		
		if(w == 'auto')
		// only for ie
		{
			w = C._E(eP).offsetWidth;
			pr = $D.getStyle(eP, 'padding-right');
			pl = $D.getStyle(eP, 'padding-left');
			w -= pr.substring(0,(pr.indexOf('p')));
			w -= pl.substring(0,(pl.indexOf('p')));
			h = (eH == undefined) ? C._E(eP).offsetHeight : C._E(eH).offsetHeight;
		}
		
		this.setWidth(eT, w);
		this.setHeight(eT, h);
	}
	
	//thank you quirksmode.org & thank you pHaez for the Firefox Edit ~DW
	this.centerElementInWindow = function(width, height, elementId) {
		var arrayPageSize = this.getPageSize();
		var scrollOffset = this.getScrollXY();
		
		var x	= arrayPageSize[2];
		var y	= arrayPageSize[3];
		
		this._E(elementId).style.right	= ((x - width) / 2 ) + scrollOffset[0]	+'px';
		this._E(elementId).style.top		= ((y - height) / 2 ) + scrollOffset[1]	+'px';
	}
	
	this.getPageSize = function(){
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			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
			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;
		}
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}
		
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}
	
	this.getScrollXY = function() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
	}
	
	this.newImage = function(src) {
		var img = new Image();
		img.src = src;
		return img;
	}
}

C = new CosUtil();

Preloader = {
	
	callbacks: [],
	images: [],
	loadedImages: [],
	imagesLoaded: 0,
	
	add: function(image)
	{
		if(typeof image == 'string') this.images.push(image);
		if(typeof image == 'array' || typeof image == 'object')
		{
			for(var i=0; i< image.length; i++)
			{
				this.images.push(image[i]);
			}
		}
	},
	
	onFinish: function(func)
	{
		if(typeof func == 'function') this.callbacks.push(func);
		if(typeof func == 'array' || typeof func == 'object')
			for(var i=0; i< func.length; i++)
				this.callbacks.push(func[i]);
	},
	
	load: function()
	{
		for(var i=0; i<this.images.length; i++)
		{
			this.loadedImages[i] = new Image();
			this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
			this.loadedImages[i].src = this.images[i];
		}
	},
	
	checkFinished: function()
	{
		this.imagesLoaded++;
		if(this.imagesLoaded == this.images.length)
			this.fireFinish();
	},
	
	fireFinish: function()
	{
		for (var i=0; i<this.callbacks.length; i++)
			this.callbacks[i]();
		
		this.images = [];
		this.loadedImages = [];
		this.imagesLoaded = 0;
		this.callbacks = [];
	}
}
