//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//

var windx = {
	buildWindow: function(windxID) {
		windxID = this.makeID(windxID);
		if($('windxoverlay') == null) {
			var overlay = document.createElement('div');
			overlay.id = 'windxoverlay';
			document.body.appendChild(overlay);
			/*Event.observe(overlay, 'click', function(e){
				windx.hideAllWindx(e);
				Event.stop(e);
			});*/
		}
		if($(windxID) == null) {
			/*
			build the window like this:
				<div class="windx">
					<div class="windxbox">
						<a class="windxclose">close</a>
						<h3>title goes here</h3>
						<div class="windxcontent">content goes here</div>
						<div class="windxreturn"><a>return to page</a></div>
					</div>
				</div>
			*/
			var w = document.createElement('div');
			Element.extend(w);
			w.id = windxID;
			w.addClassName('windx').hide();

			var box = document.createElement('div');
			Element.extend(box);
			box.addClassName('windxbox');
			w.appendChild(box);
			
			var a = document.createElement('a');
			Element.extend(a);
			a.addClassName('windxclose');
			a.setAttribute('title', 'close');
			Element.update(a, 'X');
			Event.observe(a, 'click', function(e){
				windx.hideWindow(windxID);
				Event.stop(e);
			});
			box.appendChild(a);

			var t = document.createElement('h3');
			Element.update(t, '&nbsp;');
			box.appendChild(t);

			var c = document.createElement('div');
			Element.update(c, '&nbsp;');
			c.addClassName('windxcontent');
			box.appendChild(c);

			var r = document.createElement('div');
			Element.update(r, ''); //set the innerHTML to blank for IE (ugh)
			r.addClassName('windxreturn');
			box.appendChild(r);
			
			a = document.createElement('a');
			Element.extend(a);
			a.setAttribute('title', 'return to page');
			Element.update(a, '&lt;&lt; return to page');
			Event.observe(a, 'click', function(e){
				windx.hideWindow(windxID);
				Event.stop(e);
			});
			r.appendChild(a);
			
			document.body.appendChild(w);
			new Draggable(windxID, {handle:t});
		}
	},
	showHTML: function(windxID, title, html, windxstyles, windxcontentstyles) {
		//ex: windx.showHTML('popup', 'my title', 'some html', {top:'10px'}, {height:'200px'});
		windxID = this.makeID(windxID);
		this.buildWindow(windxID);
		var w = $(windxID);
		if(windxstyles) {
			w.setStyle(windxstyles);
		} else {
			windxstyles = {}; //create an empty windxstyles so we can try to reference windxstyles.top & windxstyles.left later on
		}
		if(windxcontentstyles) {
			w.down('div.windxcontent').setStyle(windxcontentstyles);
		} else {
			windxcontentstyles = {}; //create an empty windxcontentstyles so we can try to reference it later on
		}
		if(!windxstyles.top) {
			var arrayPageScroll = this.getPageScroll();
			if(!windxstyles.scrolltop) {
				windxstyles.scrolltop = 100;
			}
			var newTop = arrayPageScroll[1] + parseInt(windxstyles.scrolltop);
			newTop += 'px';
			w.setStyle({top:newTop});
		}
		if(!windxstyles.left) {
			var arrayPageSize = this.getPageSize();
			var newLeft = (arrayPageSize[2] / 2) - (w.getWidth() / 2);
			newLeft += 'px';
			w.setStyle({left:newLeft});
		}
		if(title != null) {
			w.down('h3').update(title);
		}
		if(html != null) {
			w.down('div.windxcontent').update(html);
		}
		new Effect.BlindDown(windxID);
		this.showOverlay();
	},
	showElement: function(windxID, title, element) {
		windxID = this.makeID(windxID);
		var html = "";
		if(element != null && $(element) != null) {
			html = $(element).innerHTML;
		} else {
			html = "Windx Error: Element not defined.";
		}
		this.showHTML(windxID, title, html);
	},
	hideWindow: function(windxID) {
		windxID = this.makeID(windxID);
		new Effect.SwitchOff(windxID);
		this.hideOverlay();
	},
	showOverlay: function() {
		$$('select').invoke('setStyle', {visibility:'hidden'}); //hide elements (IE puts these always on top, no matter what ... stupid IE!!)
		var arrayPageSize = this.getPageSize();
		Element.setHeight('windxoverlay', arrayPageSize[1]);
		Element.setWidth('windxoverlay', arrayPageSize[2]);
		new Effect.Appear('windxoverlay', { duration: 0.2, from: 0.0, to: 0.8 });
	},
	hideOverlay: function() {
		new Effect.Fade('windxoverlay', { duration: 0.2, from: 0.8, to: 0.0 });
		$$('select').invoke('setStyle', {visibility:''}); //show the elements we just hid in showOverlay()
	},
	makeID: function(windxID) {
		if(windxID.indexOf('windx_') != 0) {
			windxID = 'windx_' + windxID;
		}
		return windxID;
	},
	getPageScroll: function() {
		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
		
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	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;
	},
	hideAllWindx: function(e) {
		//hide all windx that are currently shown
		$$('div.windx').each(function(div) {
			windx.hideWindow(div.id);
		});
}
};

//when the window unloads (user leaves this page) hide all the windx that are open
//otherwise, if user hit back button, the windx would still show
Event.observe(window, 'unload', windx.hideAllWindx);


// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";

	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});
