/*
    Disclaimer

    While we make every effort to ensure that this code is fit for its intended
    purpose, we make no guarantees as to its functionality. CoreTrek AS will
    accept no responsibility for the loss of data or any other damage or
    financial loss caused by use of this code.


    Copyright

    This programming code is copyright of CoreTrek AS. Permission to run this
    code is given to approved users of CoreTrek's publishing system CorePublish.

    This source code may not be copied, modified or otherwise repurposed for use
    by a third party without the written permission of CoreTrek AS.

    Contact webmaster@coretrek.com for information.

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype. Scriptaculous is also
    needed for opacity effects to work.
    ============================================================================
    
    SiteComponents Lightbox
  
    Displays a given URL in a modal layer. All anchor tags that is set with
    the css-class "lightbox" will be opened in a lightbox. Just include this
    javascript to enable. The script is dependant on Prototype and
    Scriptaculous.
 
    Example use:
    lightbox.show('http://coretrek.no/cms/corepublish/webpublisering/');

    @arg contentUrl string url to content being displayed
 
*/

function Lightbox() { } 

Lightbox.prototype.initialize = function(contentUrl) {
    objBody = document.getElementsByTagName('body').item(0);
    
    // Add the lightbox html structure to end of page
    var objLightbox = document.createElement('div');
    objLightbox.setAttribute('id', 'lightbox');
    objLightbox.style.display = 'none';
    objBody.appendChild(objLightbox);
    
    var objOverlay = document.createElement('div');
    objOverlay.setAttribute('id', 'lightbox-overlay');
    objOverlay.style.display = 'none';
    objLightbox.appendChild(objOverlay);
    
    var objContainer = document.createElement('div');
    objContainer.setAttribute('id', 'lightbox-container');
    objLightbox.appendChild(objContainer);
    
    var objCloseButtonContainer = document.createElement('div');
    objCloseButtonContainer.setAttribute('id', 'lightbox-close-container');
    objContainer.appendChild(objCloseButtonContainer);
    
    var objCloseButton = document.createElement('a');
    objCloseButton.setAttribute('id', 'lightbox-close');
    objCloseButton.setAttribute('href', '#');
    
    var objCloseButtonSpan = document.createElement('span');
    objCloseButton.appendChild(objCloseButtonSpan);
    
    var objCloseText = document.createTextNode("Close");
    objCloseButtonSpan.appendChild(objCloseText);
    objCloseButtonContainer.appendChild(objCloseButton);
    
    var objStart = document.createElement('div');
    objStart.setAttribute('id', 'lightbox-start');
    objContainer.appendChild(objStart);
    
    var objSpinner = document.createElement('div');
    objSpinner.setAttribute('id', 'lightbox-spinner');
    objContainer.appendChild(objSpinner);
    
    var objContent = document.createElement('div');
    objContent.setAttribute('id', 'lightbox-content');
    objContainer.appendChild(objContent);
    
    var objEnd = document.createElement('div');
    objEnd.setAttribute('id', 'lightbox-end');
    objContainer.appendChild(objEnd);
    
	objLightbox.appendChild(objContainer);
	
	// Set opacity on overlay layer
	try {
		$('lightbox-close').observe('click', function(event) {
	        event.stop();
	        lightbox.hide();
	    });
	    
	    $('lightbox-overlay').observe('click', function(event) {
            event.stop();
	        lightbox.hide();
	    });
	    
	    new Effect.Opacity('lightbox-overlay', {duration:0.0, to:0.8});
    } catch (e) {
        // scriptaculous is not available, cannot initialize lightbox
    }
}

Lightbox.prototype.show = function(contentUrl) {
    
    this.showPersistentObjects(false);
    $('lightbox-spinner').show();
    $('lightbox-overlay').show();
    new Effect.SlideDown('lightbox');
    
    // Disable scrollbars for main window when displaying lightbox in IE6
    if(Prototype.Browser.IE && navigator.appVersion.match(/MSIE 6\.0/)) {
        this.scolloffset = document.viewport.getScrollOffsets();
        document.getElementsByTagName('html').item(0).style.overflow = 'hidden';
        window.scrollTo(0, 0);
    }
                
    new Ajax.Request(contentUrl, {
        method: 'get',
        
        onSuccess: function(transport) {
            $('lightbox-spinner').hide();
            $('lightbox-content').update(transport.responseText);
            $('lightbox-content').show();
        },
        
        onFailure: function(request) {
            lightbox.hide();
            alert("Could not connect to URL");
        }
    });
}

Lightbox.prototype.hide = function() {
    // Re-enable window scrollbars for IE6
    if(Prototype.Browser.IE && navigator.appVersion.match(/MSIE 6\.0/)) {
	    window.scrollTo(this.scolloffset[0], this.scolloffset[1]);
	    document.getElementsByTagName('html').item(0).style.overflow = 'auto';
    }
    
    $('lightbox').hide();
    $('lightbox-content').hide();
    $('lightbox-overlay').hide();
    
    this.showPersistentObjects(true);
    
    $('lightbox-content').update('');
}

// In IE6 some items will always show on top of the lightbox. This is a
// function used to hide these elements.
Lightbox.prototype.showPersistentObjects = function(show) {
    $$('select', 'embed', 'object').each(function(element) {
        if(show) {
            element.style.visibility = 'visible';
        } else {
            element.style.visibility = 'hidden';
        }
    });
}

lightbox = new Lightbox();
document.observe('dom:loaded', function(event) {
    lightbox.initialize();
    $$('a.lightbox').each(function (element) {
        element.observe('click', function (event) {
            element = Event.findElement(event, 'a');
            event.stop();
            lightbox.show(element.getAttribute('href'));
        });
    });
});
