
// Add a class name to the body as a global flag that the user has javascript.
document.observe('dom:loaded', function()
{
	var body = $(document.body);
	body.addClassName('js-enabled');
});


// start ModalGallery
var ModalGallery = Class.create (
{
    initialize: function(links)
    {
		var body = $(document.body);
		this.modalOverlay = $('modaloverlay');
			this.modalOverlay.setOpacity(0.0);
		this.lnkCloseX = new Element('a', {id: 'lnk_closeX', 'href': '#close', 'title': 'close'}).update('<span class="offscreen">close X</span>');
		this.lnkPrev = new Element('a', {id: 'lnk_prev', 'class': 'lnk-prev', 'href': '#prev', 'title': 'Previous'}).update('<span class="offscreen">Previous</span>');
		this.lnkNext = new Element('a', {id: 'lnk_next', 'class': 'lnk-next', 'href': '#next', 'title': 'Next'}).update('<span class="offscreen">Next</span>');
		this.galleryImg = new Element('img', {id: 'galleryimg', 'src': '', 'alt': ''});
		this.galleryHdr = new Element('h4', {id: 'galleryhdr'});
		this.modalWindow = new Element('div', {id: 'galleryviewer'});
			this.modalWindow.insert({top: this.lnkCloseX}).insert(this.lnkPrev).insert(this.lnkNext).insert(this.galleryImg).insert(this.galleryHdr);
			this.modalOverlay.insert({after: this.modalWindow});
			this.leftPos = (body.getWidth() - this.modalWindow.getWidth()) / 2  + 'px';
			this.topPos = (body.getHeight() - this.modalWindow.getHeight()) / 2  + 'px';
			this.modalWindow.setStyle({left: this.leftPos, top: this.topPos});
        this.links = links;
			this.length = this.links.size();
		this.num = 0;
        var bindClickOpen = this.__ClickOpen.bindAsEventListener(this);
        var bindClickClose = this.__ClickClose.bindAsEventListener(this);
        var bindClickPrev = this.__ClickPrev.bindAsEventListener(this);
        var bindClickNext = this.__ClickNext.bindAsEventListener(this);
        this.links.invoke('observe', 'click', bindClickOpen);
        this.modalOverlay.observe('click', bindClickClose);
        this.lnkCloseX.observe('click', bindClickClose);
        this.lnkPrev.observe('click', bindClickPrev);
        this.lnkNext.observe('click', bindClickNext);
    },
    __ClickOpen: function(e)
    {
        e.stop();
        var element = e.element();
        element = element.up('a');
		//hacky IE6 vertical positioning
		if (ie6) {
			var arrPageScrollOffsets = document.viewport.getScrollOffsets(); //returns array [horiz,vert]
			this.topPos = arrPageScrollOffsets[1] + (document.viewport.getHeight() /2) - (this.modalWindow.getHeight() / 2)  + 'px'; //vert offset + (1/2 viewport height) - (1/2 window height)
			this.modalWindow.setStyle({top: this.topPos});
		}
		this.links.each(function(el, i)
		{
			if (el == element)
			{
				this.OpenGallery(i);
				throw $break;
			}
		}, this);
    },
    __ClickPrev: function(e)
    {
        e.stop();
        if (this.num == 0)
        {
            this.num = this.length;
        }
        this.num--;
        this.GotoPrev(this.num);
    },
    __ClickNext: function(e)
    {
        e.stop();
        this.num++;
        if (this.num == this.length)
        {
            this.num = 0;
        }
        this.GotoNext(this.num);
    },
    __ClickClose: function(e)
    {
        e.stop();
        this.CloseGallery();
    },
    OpenGallery: function(i)
    {
        this.num = i;
        this.modalOverlay.addClassName('active').appear({
			duration: 0.2, from: 0.0, to: 0.5,
            afterFinish: function() {
                this.modalWindow.addClassName('active');
                this.DisplayImage(this.num);
            }.bind(this)
        });
    },
    GotoPrev: function(i)
    {
        this.DisplayImage(i);
    },
    GotoNext: function(i)
    {
        this.DisplayImage(i);
    },
    DisplayImage: function(i)
    {
        this.galleryImg.setAttribute('src', '');
        this.galleryImg.setOpacity(0.0);
        this.galleryImg.setAttribute('src', this.links[i].href);
        this.galleryImg.setAttribute('alt', this.links[i].title);
        this.galleryImg.appear({duration: 0.4});
        this.galleryHdr.update(this.links[i].title);
    },
    CloseGallery: function()
    {
        this.modalOverlay.removeClassName('active').setOpacity(0.0);
        this.modalWindow.removeClassName('active');
    }
});
// end ModalGallery


// start ModalInterstitial
var ModalInterstitial = Class.create (
{
    initialize: function(links)
    {
		var body = $(document.body);
		this.lnkCancel = $('btn_cancel');
		this.lnkOK = $('btn_ok');
		this.modalOverlay = $('modaloverlay');
			this.modalOverlay.setOpacity(0.0);
		this.modalWindow = $('interstitial');
			this.modalWindow.remove();
			this.modalOverlay.insert({after: this.modalWindow});
			this.leftPos = (body.getWidth() - this.modalWindow.getWidth()) / 2  + 'px';
			this.topPos = (body.getHeight() - this.modalWindow.getHeight()) / 2  + 'px';
			this.modalWindow.setStyle({left: this.leftPos, top: this.topPos});
        this.links = links;
			this.length = this.links.size();
        var bindClickOpen = this.__ClickOpen.bindAsEventListener(this);
        var bindClickClose = this.__ClickClose.bindAsEventListener(this);
        var bindClickContinue = this.__ClickContinue.bindAsEventListener(this);
        this.links.invoke('observe', 'click', bindClickOpen);
        this.modalOverlay.observe('click', bindClickClose);
        this.lnkCancel.observe('click', bindClickClose);
        this.lnkOK.observe('click', bindClickContinue);
    },
    __ClickOpen: function(e)
    {
        e.stop();
        var element = e.element();
		//hacky IE6 vertical positioning
		if (ie6) {
			var arrPageScrollOffsets = document.viewport.getScrollOffsets(); //returns array [horiz,vert]
			this.topPos = arrPageScrollOffsets[1] + (document.viewport.getHeight() /2) - (this.modalWindow.getHeight() / 2)  + 'px'; //vert offset + (1/2 viewport height) - (1/2 window height)
			this.modalWindow.setStyle({top: this.topPos});
		}
		this.links.each(function(el, i)
		{
			if (el == element)
			{
				this.OpenInterstitial(i);
				throw $break;
			}
		}, this);
    },
    __ClickClose: function(e)
    {
        e.stop();
        this.CloseInterstitial();
    },
    __ClickContinue: function(e)
    {
        this.CloseInterstitial();
    },
    OpenInterstitial: function(i)
    {
        this.lnkOK.setAttribute('href', this.links[i].href);
        this.modalOverlay.addClassName('active').appear({
			duration: 0.2, from: 0.0, to: 0.5,
            afterFinish: function() {
                this.modalWindow.addClassName('active');
            }.bind(this)
        });
    },
    CloseInterstitial: function()
    {
        this.modalOverlay.removeClassName('active').setOpacity(0.0);
        this.modalWindow.removeClassName('active');
    }
});
// end ModalInterstitial


// start PopupWindow
var PopupWindow = Class.create (
{
    initialize: function(link, width, height, extras)
    {
        this.winName = 'popup';
        this.altWidth = 640;
        this.altHeight = 360;
        this.altExtras = 'location=no,menubar=no,statusbar=no,toolbar=no,scrollbars=no,resizable=yes';
        this.link = link;
        this.url = link.getAttribute('href');
        this.w = width || this.altWidth;
        this.h = height || this.altHeight;
        this.x = extras || this.altExtras;
        this.features = 'width=' + this.w + ',height=' + this.h + ',' + this.x;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        var win = window.open(this.url, this.winName, this.features);
        win.focus();
    }
});
// end PopupWindow


// start ClosePopup
var ClosePopup = Class.create (
{
    initialize: function(link)
    {
        this.link = link;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        //var element = e.element();
        window.close();
    }

});
// end ClosePopup
