﻿//
//  Configuration
//
BellBoxOptions = Object.extend({
    overlayOpacity: 0.8,   // controls transparency of shadow overlay
    animate: true,         // toggles resizing animations
    resizeSpeed: 7,        // controls the speed of the image resizing animations (1=slowest and 10=fastest)
    borderSize: 10,         //if you adjust the padding in the CSS, you will need to update this variable
    width: 800,
    height: 600
}, window.BellBoxOptions || {});

// -----------------------------------------------------------------------------------

var BellBox = Class.create();

BellBox.prototype = {
    initialize: function() 
    {    
	    this.overlayDuration = BellBoxOptions.animate ? 0.2 : 0;  // shadow fade in/out duration

        // When BellBox starts it will resize itself from 250 by 250 to the current image dimension.
        // If animations are turned off, it will be hidden as to prevent a flicker of a
        // white 250 by 250 box.
        var size = (BellBoxOptions.animate ? 250 : 1) + 'px';


        var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div',{id:'overlay'}));
	
        objBody.appendChild(Builder.node('div',{id:'BellBox'}, [
            Builder.node('div',{id:'outerContainer'},[
                Builder.node('iframe',{id:'bbIFrame', border:0,width:BellBoxOptions.width,height:BellBoxOptions.height})
            ])
        ]));
        
        this.BellBox = $('BellBox');
        this.overlay = $('overlay');
        this.outerContainer = $('outerContainer');
        this.iframe = $('bbIFrame');

		$('overlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('BellBox').hide().observe('click', (function(event) { if (event.element().id == 'BellBox') this.end(); }).bind(this));
		//$('outerContainer').setStyle({ width: size, height: size });
    },
    
    frameLoaded: function(e)
    {
        Event.observe(window.frames["bbIFrame"].document, 'click', function(event)
        {
            var target = event.findElement('a[rel^=closebellbox]');
            if (target) 
            {
                event.stop();
            }
        });  
    },

    start: function(url, width, height) 
    {   
//        this.iframe.observe("load", (this.frameLoaded).bind(this));
        this.iframe.width = width;
        this.iframe.height = height; 
        this.iframe.src = url;
        
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

        // stretch overlay to fill page and fade in
        var pageSize = this.getPageSize();
        this.overlay.setStyle({ width: pageSize.width + 'px', height: pageSize.height + 'px' });
        this.overlay.appear({ duration: this.overlayDuration, from: 0.0, to: BellBoxOptions.overlayOpacity });       

        // calculate top and left offset for the BellBox 
        //alert(document.viewport.getWidth() + "x" + document.viewport.getHeight());
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var BellBoxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var BellBoxLeft = arrayPageScroll[0];
        
        this.BellBox.setStyle({ top: BellBoxTop + 'px', left: BellBoxLeft + 'px' }).show();
    },    

    //
    //  end()
    //
    end: function() 
    {
        this.BellBox.hide();
        this.overlay.fade({ duration: this.overlayDuration });
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
    },

    //
    //  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;
		}
		
		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 { width:pageWidth, height: pageHeight };
	}
}

var bellbox
Event.observe(window, "load", function()
{
    bellbox = new BellBox();
});

document.observe('click', function(event)
{
    var target = event.findElement('a[rel^=bellbox]');
    if (target) 
    {
        event.stop();
        
        var rel = target.rel;
        var start = rel.indexOf("[", 0) + 1;
        var sub = rel.substr(start, rel.length - start - 1);
        
        var size = sub.split(",");
        if(size.length > 1)
            bellbox.start(target.href, size[0], size[1]);
        else
            bellbox.start(target.href, 640, 480);
    }
});
