window.addEvent('domready', function () {
    if(!$$('.content_link')) return;
    else load_content_links($$('.content_link'));
});

function load_content_links(links){
    var options = {
        
    }
    links.each(function(el){
        if(el.tagName.toString().toLowerCase() == "a") new ContentLink(el,options);
        el.getElements("a").each(function(link){
            new ContentLink(link,options);
        });
    });
}

ContentLink = new Class({
	Implements: [Chain, Events, Options],
	options: {
		isLink: false
	},
	
        initialize: function(el, options){
            this.handle = $(el);

	this.setOptions(options);

	if(this.options.isLink) {
		this.link = el + "&ajax=true";		
	}else{
            if(this.checkLink()) return(false);
            this.alterLink();
	}
        },
        checkLink: function(){
            if(this.handle.tagName.toString().toLowerCase() != "a") return(false);
            if(this.handle.onClick) return(false);
            if(!this.handle.href) return(false);
            this.href = this.handle.href;
        },
        alterLink: function(){
            this.link = this.href;
            this.handle.removeProperty("href");
            this.link = this.link + "&ajax=true";
            //this.handle.onClick = function(){this.getContent();}.bind(this);
            this.handle.setStyle("cursor",'pointer');
            
            this.handle.addEvent('click', function(){
                this.getContent();
            }.bind(this));
            
        },
        getContent: function(){
            this.createTree();
            this.overlay.show().position({
	relFixedPosition: true,
	ignoreMargins: false,
	top: '0px'
            });
	var scroll = $$('body')[0].getScroll();
	this.overlay.setPosition({x:0,y:scroll.y});
            new Fx.Morph(this.overlay).start({
                'opacity':[0,0.8]
            });
            this.loadBox.show().position({
	position: 'centerTop',
	relFixedPosition: false
            });
	    /*
	     *'relFixedPosition':true
                position: 'centerTop',
	relFixedPosition: false,
	relativeTo: this.overlay
	*/
	    var url = new Hash(parseUri(this.link));
            new Request.HTML({
		//url: this.link,
		url: url.get('path'),
                onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			//if(this.debug) alert(responseHTML);
                    this.openContent(responseHTML);
                }.bind(this)
            }).get(url.get('query').parseQueryString());
        },
        openContent: function(response){
            this.loadBox.hide();
            this.contentBox.set('html',response);
            if(this.contentBox.getElements('.image_gallery').length) loadGalleries(this.contentBox.getElements('.image_gallery'));
            this.contentBox.show().position({
	position: 'centerTop',
	relFixedPosition: false,
	relativeTo: this.overlay
            });
	    /*
	relFixedPosition: true,
	relativeTo: $$('body')[0],
	ignoreMargins: false
            });

            /*
            var pdfButton = new Element('div',{'class':'button pdf'});
            pdfButton.appendChild(new Element('a',{
                'href':this.createLink('pdf'),
                'target':'_blank'
            }));
            this.contentBox.appendChild(pdfButton);
            */

            var printButton = new Element('div',{'class':'button print'});
            printButton.appendChild(new Element('a',{
                'href':this.createLink('print'),
                'target':'_blank'
            }));
            this.contentBox.appendChild(printButton);


            var closeButton = new Element('div',{'class':'button close'});
            closeButton.addEvent('click',function(){
                this.endContent();
            }.bind(this));
            this.contentBox.appendChild(closeButton);

            /*
             * TODO:
             * Print-Button
             */
        },
        createLink: function(type){
            if(!this.href) return;
            var link = this.href;
            switch(type){
                case 'pdf':
                    link = link + "&print=true&type=123";
                break;
                case 'print':
                    link = link + "&print=true";
                break;
            }
            return(link);
        },
        createTree: function(){
            
            var objBody = $$('body')[0];
            if($($$('.loadOverlay')[0])){
                this.overlay = $($$('.loadOverlay')[0]);
                this.overlay.empty();
            }else{
                this.overlay = new Element('div',{'class':'loadOverlay'});
                objBody.appendChild(this.overlay);
            }
            this.overlay.addEvent('click',function(){
                this.endContent();
            }.bind(this));
            this.overlay.hide();
            if($($$('.loadBox')[0])){
                this.loadBox = $($$('.loadBox')[0]);
                this.loadBox.empty();
            }else{
                this.loadBox = new Element('div',{'class':'loadBox'});
                objBody.appendChild(this.loadBox);
            }
            this.loadBox.hide();
            if($($$('.contentBox')[0])){
                this.contentBox = $($$('.contentBox')[0]);
                this.contentBox.empty();
            }else{
                this.contentBox = new Element('div',{'class':'contentBox'});
                objBody.appendChild(this.contentBox);
            }
            this.contentBox.hide();

        },
        endContent: function(){
            this.contentBox.hide();
            this.overlay.hide();
        }
});
