Object.extend(Element, {
	show : function() {$A(arguments).each(function(i) {
		$(i).style.display =
			($(i).tagName.toLowerCase() == 'div' ? 'block' : '');
	});}
});

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'}, Ibox = {id: 'tiny-img'};
var Tiny = {
    key_listener: null,
    
	initialize: function() {
		[Over, Load, Tbox, Ibox].each(function(i) {
			new Insertion.Bottom($T('body'), '<div id="' + i.id + '"></div>');
		});
		Event.observe(document, 'click', Tiny.toggle.bindAsEventListener(Tiny));
	},

	toggle    : function(e) {
	    if(Event.element(e).rel != null || Event.element(e).alt != null) {
	        var link = Event.findElement(e, 'a');
	        if(link == null) {
	            link = Event.element(e).up('a');
	            if(link != null){ Element.extend(link); }
	        }
	        if(link != null) {
	            if(link.hasClassName('disabled')){ Event.stop(e); return; }
        		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;
        			case 'tiny-show-img': Tiny.show_img(e); Event.stop(e); break;
        			case 'tiny-show-form': Tiny.show_form(e); Event.stop(e);
        		}
    		}
		}
	},
	
	start_key_listener  : function(){
        Tiny.key_listener = Event.observe(document, 'keypress', Tiny.key.bindAsEventListener(Tiny));
	},
	
	stop_key_listener   : function(){
	    Event.stopObserving(document, 'keypress', Tiny.key_listener);
	},
	
	key       : function(e){
        if(Tiny.is_key(e, 27)){Tiny.hide();}
	}, 

	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');
		});
		@*/
		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);
				Tiny.start_key_listener();
			},
			evalScripts: true,
			method: 'get'
		});
	},
	
	show_img      : 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');
		});
		@*/
		Over.show ? Over.show() : Element.show(Over.id);
		new Ajax.Updater(Ibox.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);
				Ibox.show ? Ibox.show() : Element.show(Ibox.id);
				Tiny.start_key_listener();
			},
			evalScripts: true,
			method: 'get'
		});
	},
	
	show_form      : 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');
		});
		@*/
	    var a = Event.element(e);
        if(a != null){
            Element.extend(a);
            var params = '';
            if(a.hasClassName('disabled')){ return false; }
            var form = a.up('form');
            if(form != null){ 
                Element.extend(form);
                params = Form.serialize(form);
            }
            if(!a.href.include('?')){ a.href += '?'; }
            else if(!a.href.include('&')){ a.href += '&'; }
    		Over.show ? Over.show() : Element.show(Over.id);
    		new Ajax.Updater(Tbox.id, a.href + params, {
    			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);
    	    		Page.refresh_forms();
    	    		Tiny.start_key_listener();
    			},
    			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);
		@*/
		Tiny.stop_key_listener();
		$(Tbox.id).update('');
		$(Ibox.id).update('');
		Ibox.hide ? Ibox.hide() : Element.hide(Ibox.id);
		Tbox.hide ? Tbox.hide() : Element.hide(Tbox.id);
		Over.hide ? Over.hide() : Element.hide(Over.id);
	},
	
	is_key: function(e,key){
        if(!e){ return false; }
        var code;
        if(e.keyCode){ code = e.keyCode; }
        else{ code = e.which; }
        if(code == key){ return true; }
        return false;
    }
};

Event.observe(window, 'load', Tiny.initialize);
