
// start: Add a class name to the body as a global flag that the user has javascript.
// also used to block non-js users
// this functionality has been moved to the age gate script
/*
document.observe('dom:loaded', function(){
	var body = $(document.body);
	body.addClassName('js-enabled');
});
*/
// end: Add a class name to the body


// start ImageSwapper
var ImageSwapper = Class.create ({
    initialize: function(imgpath,imgs,target)
    {
		this.imgpath = imgpath;
		this.imgs = imgs;
			this.length = this.imgs.size();
		this.target = target;
		this.Swap();
    },
    Swap: function()
    {
		var rn = Math.ceil(Math.random() * this.length);
		this.target.src = this.imgpath + this.imgs[rn -1];
    }
});
// end ImageSwapper


// start PrintPage
var PrintPage = Class.create ({
    initialize: function(link)
    {
        this.link = link;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        this.Print();
    },
    Print: function()
    {
        window.print();
    }
});
// end PrintPage


// start PopupWindow
var PopupWindow = Class.create ({
    initialize: function(link, width, height, xtras)
    {
        this.popName = 'popup';
        this.link = link;
        this.url = this.link.href;
        this.altW = 640;
        this.altH = 480;
        this.altX = 'location=no,menubar=no,statusbar=no,toolbar=no,scrollbars=no,resizable=yes';
        this.popupW = width || this.altW;
        this.popupH = height || this.altH;
        this.popupX = xtras || this.altX;
        this.availH = screen.availHeight;
        this.availW = screen.availWidth;
        this.topPos = (this.availH - this.popupH)/2;
        this.leftPos = (this.availW - this.popupW)/2;
        this.features = 'width=' + this.popupW + ',height=' + this.popupH + ',' + this.popupX + ',top=' + this.topPos + ',left=' + this.leftPos;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        var win = window.open(this.url, this.popName, this.features);
        win.focus();
    }
});
// end PopupWindow


// start MultiPopupWindows
var MultiPopupWindows = Class.create ({
    initialize: function(links, width, height, xtras)
    {
        this.popName = 'popup';
        this.links = links;
        this.length = this.links.size();
        this.altW = 640;
        this.altH = 480;
        this.altX = 'location=yes,menubar=yes,statusbar=yes,toolbar=yes,scrollbars=yes,resizable=yes';
        this.popupW = width || this.altW;
        this.popupH = height || this.altH;
        this.popupX = xtras || this.altX;
        this.availH = screen.availHeight;
        this.availW = screen.availWidth;
        this.topPos = (this.availH - this.popupH)/2;
        this.leftPos = (this.availW - this.popupW)/2;
        this.features = 'width=' + this.popupW + ',height=' + this.popupH + ',' + this.popupX + ',top=' + this.topPos + ',left=' + this.leftPos;
		var boundLinkClick = this.__Click.bindAsEventListener(this);
        this.links.invoke('observe', 'click', boundLinkClick);
    },
    __Click: function(e)
    {
        e.stop();
        var element = e.element();
        for (var i=0; i<this.length; i++)
        {
	        if (this.links[i] == element)
	        {
                var url = this.links[i].href;
                var win = window.open(url, this.popName, this.features);
                win.focus();
                break;
	        }
        }
    }
});
// end MultiPopupWindows


// 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();
        window.close();
    }

});
// end ClosePopup


// start BasicPopover
var BasicPopover = Class.create ({
    initialize: function(link, item, xlink)
    {
        this.link = link;
        this.item = item;
        this.xlink = xlink;
        this.link.observe('click', this.__ClickOpen.bindAsEventListener(this));
        this.xlink.observe('click', this.__ClickClose.bindAsEventListener(this));
    },

    __ClickOpen: function(e)
    {
        e.stop();
        this.item.style.display = 'block';
    },

    __ClickClose: function(e)
    {
        e.stop();
        this.item.style.display = 'none';
    }
});
// end BasicPopover


// start MultiPopovers
var MultiPopovers = Class.create ({
    initialize: function(links, item, xlinks)
    {
        this.links = links;
        this.items = items;
        this.xlinks = xlinks;

        for (var i=0; i<this.links.length; i++)
        {
	        Event.observe(this.links[i], 'click', this.__ClickOpen.bindAsEventListener(this));
        }

        for (var i=0; i<this.xlinks.length; i++)
        {
	        Event.observe(this.xlinks[i], 'click', this.__ClickClose.bindAsEventListener(this));
        }

    },

    __ClickOpen: function(e)
    {
        e.stop();
        var el = e.element();
        for (var i=0; i<this.links.length; i++)
        {
	        if (this.links[i] == el)
	        {
		        this.doShowHide(i);
		        break;
	        }
        }
    },

    __ClickClose: function(e)
    {
        e.stop();
        this.doShowHide(-1);
    },

    doShowHide: function(iWhich)
    {
        for (var i=0; i<this.items.length; i++)
        {
	        if (i == iWhich)
	        {
		        this.items[i].style.display = 'block';
	        } else {
	            this.items[i].style.display = 'none';
	        }
        }
    }
});
// end MultiPopovers


// start ContentSwitcher
var ContentSwitcher = Class.create ({
    initialize: function(links, items, options)
    {
        this.links = links;
        this.items = items;
			this.length = this.items.size();
		// set options
		this.options = Object.extend(
		{
			rotate: false // true || false
		}, options || {});
        this.hash = window.location.hash.replace('#','') || false;
        this.num = 0;

        // show the first, check url hash to override
        if (this.hash)
        {
            for (var i=0; i<this.items.length; i++)
            {
	            if (this.items[i].id == this.hash)
	            {
	                this.num = i;
	            }
            }
            this.doShowHide(this.num);
        }
        else
        {
            this.doShowHide(0); // show the first, hide the rest
        }

        // Loop through each and attach a click handler
        this.links.invoke('observe', 'click', this.__Click.bindAsEventListener(this));

        // show different item every 3 seconds
        if (this.options.rotate)
        {
            if (this.hash){return;} // don't rotate if url hash
            this.interval = setInterval(function()
            {
                this.Rotate();
            }.bind(this), 3000);
        }
    },

    __Click: function(e)
    {
        e.stop();
        var el = e.element();
        clearInterval(this.interval); // clear timer once link is clicked
        for (var i=0; i<this.length; i++)
        {
	        if (this.links[i] == el)
	        {
		        this.doShowHide(i);
		        break;
	        }
        }
    },

    // Rotate thru items
    Rotate: function()
    {
        this.num++;
        if (this.num == this.length)
        {
            this.num = 0;
        }
        this.doShowHide(this.num);
    },

    doShowHide: function(index)
    {
        for (var i=0; i<this.length; i++)
        {
	        if (i == index)
	        {
		        this.links[i].up().addClassName('in');
		        //this.items[i].show();
		        this.items[i].appear({ duration: 0.3 });
	        } else {
	            this.links[i].up().removeClassName('in');
	            this.items[i].hide();
	        }
        }
    }
});
// end ContentSwitcher
