var IE7 = false;
IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;



//TODO add this to a framework-level base class like "Eel.Base"

function getPageSize() {

	//page size function taken from Lightbox.js
	//thanks, Lokesh
	/*
	//	Lightbox v2.04
	//	by Lokesh Dhakar - http://www.lokeshdhakar.com http://lokeshdhakar.com/projects/lightbox2/
	//
	//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	//  	- Free for use in both personal and commercial projects
	//		- Attribution requires leaving author name, author link, and the license info intact.
	//
	*/

        
     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;
	}	
	
	// 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 = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function docHeight(){
	return getPageSize()[1];
}

function docWidth(){
	return getPageSize()[0];
}

/*
Enumerable.prototype.sum = function(a){
	var s = 0;
	this.each(function(e){
		if (a==null){
			s += e;
		} else {
			s += e.a;
		}
	})
	return s;
}
*/
//Enumerable prototype product
//Enumerable prototype atDepth
//Enumerable prototype

/*
 * Orginal: http://adomas.org/javascript-mouse-wheel/
 * prototype extension by "Frank Monnerjahn" themonnie @gmail.com
 */

Object.extend(Event, {
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
			} else if (event.detail) { delta = -event.detail/3;     }
			return Math.round(delta); //Safari Round
		}
});



/*
 * end of extension 
 */ 



 



//TODO
//track element stacking
//register "need to be hidden on overlay"
//register "need to be revealed"
//register singletons (dropdowns, overlays)


//element utilities
var MyUtils = {
	followOnResize: function (element, listener, position){
		element = $(element);
		element.absolutize();		
		element.clonePosition(position);
		Event.observe(listener,"resize",function(){
			element.clonePosition(position);
		});
	},
	alwaysCentered: function(element){
		element = $(element);
		element.absolutize();
		element.setStyle({left:(docWidth() *0.5)- (element.getWidth()*0.5) + "px"});
		element.setStyle({top:(document.viewport.getScrollOffsets()["top"]) + "px"});
		Event.observe(window,"resize",function(){
			element.setStyle({left:(docWidth() *0.5)- (element.getWidth()*0.5) + "px"})
			element.setStyle({top:(document.viewport.getScrollOffsets()["top"]) + "px"})
		})
	},
	alwaysFullWindow:function(element){
		element = $(element);
		element.absolutize();
		element.setStyle({top:"0px",left:"0px",height:docHeight()+"px",width:docWidth()+"px"});
		Event.observe(window,"resize",function(){
			element.setStyle({top:"0px",left:"0px",height:docHeight()+"px",width:docWidth()+"px"});
		});
	},
	fullWindow: function(element){
		var kludge = 100;
		element = $(element);
		element.absolutize();
		if (IE7) kludge = 400;
		element.setStyle({top:"0px",left:"0px",height:docHeight()+kludge+"px",width:docWidth()+"px"});
	},
	center: function(element) {
		element = $(element);
		element.absolutize();
		element.setStyle({left:(docWidth() *0.5)- (element.getWidth()*0.5) + "px"});
		element.setStyle({top:(document.viewport.getScrollOffsets()["top"]) + "px"});
	},
	//add anchor-like rollover state to any element.
	pseudoLink: function(element){
		element = $(element);
		Event.observe(element,"mouseover",function(){
			element.addClassName("rollover");
		});
		Event.observe(element,"mouseout",function(){
			element.removeClassName("rollover");
			element.removeClassName("active");
		});
		Event.observe(element,"click",function(){
			element.addClassName("active");
		});
	}
}

Element.addMethods(MyUtils);
