/** vim:se ai lbr sts=4 sw=4 ts=4: **/
/**
 * TinyBox v.0.2.2 - a tiny library for overlaying content over a page
 *
 * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */


Object.extend(Element, {
	show : function() {$A(arguments).each(function(i) {
		$(i).style.display =
			($(i).tagName.toLowerCase() == 'div' ? 'block' : '');
	});}
});
/*
document.onclick = function(objEvt){
										var objEvt = (window.event)? window.event: objEvt;
										var objSrc = (objEvt.target)? objEvt.target : objEvt.srcElement;

										if(objSrc.id == 'tiny-over')
										{
											if(confirm('Are you sure you want to close this box? Information will be lost.'))
											{
												Tiny.hide();
											}
										};
									};
*/
function $T() {
	var a = [];
	$A(arguments).each(function(i) {
		a.push($A(document.getElementsByTagName(i)));
	});
	return((a = a.flatten()).length > 1 ? a : a[0]);
};

var Over = {id: 'tiny-over'}, Load = {id: 'tiny-load'}, Tbox = {id: 'tiny-tbox'};
var Tiny = {
	initialize: function() {
		Tiny.boolCC = false;
		/*@cc_on
		Tiny.boolCC = true;
		@*/
		[Over, Load, Tbox].each(function(i) {
			new Insertion.Bottom($T('body'), '<div id="' + i.id + '"></div>');
		});
		Event.observe(document, 'click', Tiny.toggle.bindAsEventListener(Tiny));
	},
	toggle : function(e) {
		var element = Event.element(e);
		if ('A' == element.tagName){
			switch(Event.findElement(e, 'a').rel) {
			case 'tiny-hide': Tiny.hide(e); Event.stop(e); break;
			case 'tiny-show': Tiny.show(e); Event.stop(e); break;
		}
	}
	return(false);
	},
	show      : function(e) {
		/*@cc_on
		Tiny.scroll = {x: $T('html').scrollLeft, y: $T('html').scrollTop};
		window.scrollTo(0, 0);
		$T('html', 'body').each(function(i) {
			Element.addClassName(i, 'tiny-ie-hack');
		});
		@*/
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

		if(!Tiny.boolCC)
		{ //if the browser is not IE5+
			Tiny.scroll = {x: $T('html').scrollLeft, y: $T('html').scrollTop};
			window.scrollTo(0, 0);
		}

		var arrayPageSize = Tiny.getPageSize();
		var cursor = Tiny.getCursorPosition(e);

        $('tiny-over').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

		Over.show ? Over.show() : Element.show(Over.id);
		new Ajax.Updater(Tbox.id, Event.findElement(e, 'a').href, {
			onCreate  : function() {
				Load.show ? Load.show() : Element.show(Load.id);
			},
			onComplete: function() {
				Load.hide ? Load.hide() : Element.hide(Load.id);
				Tbox.show ? Tbox.show() : Element.show(Tbox.id);
			},
			evalScripts: true, method: 'get'
		});


	},

	hide      : function(e) {
		/*@
		$T('html', 'body').each(function(i) {
			Element.removeClassName(i, 'tiny-ie-hack');
		});
		window.scrollTo(Tiny.scroll.x, Tiny.scroll.y);
		@*/
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });

		if(!Tiny.boolCC)
		{ //if the browser is not IE5+
			window.scrollTo(Tiny.scroll.x, Tiny.scroll.y);
		}

		$(Tbox.id).update('');
		Tbox.hide ? Tbox.hide() : Element.hide(Tbox.id);
		Over.hide ? Over.hide() : Element.hide(Over.id);
	},

	//
    //  getPageSize()
    //
    getPageSize : function() {
		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;
		}
		//alert( 'x:' + xScroll + ' y: ' + yScroll );
		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,windowHeight,yScroll];
	},
	getCursorPosition : function(e) {
		e = e || window.event;
		var cursor = {x:0, y:0};
		if (e.pageX || e.pageY)
		{
			cursor.x = e.pageX;
			cursor.y = e.pageY;
		}
		else
		{
			var de = document.documentElement;
			var b = document.body;
			cursor.x = e.clientX +
				(de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
			cursor.y = e.clientY +
				(de.scrollTop || b.scrollTop) - (de.clientTop || 0);
		}
		return cursor;
	}

};

Event.observe(window, 'load', Tiny.initialize);