﻿var ExternalLinks = Class.create(
{
    initialize: function()
    {
        if (!document.getElementsByTagName) return;
        var anchors = document.getElementsByTagName("a");
        for (var i=0; i < anchors.length; i++)
        {
            var anchor = anchors[i];
            if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
        }
    }
});

var IE6ImageFlicker = Class.create(
{
    initialize: function()
    {
        try { document.execCommand("BackgroundImageCache", false, true); } 
        catch(err) {}
    }
});

var HomeLinks = Class.create(
{
    initialize: function()
    {
        $$('.homelinkwrapper').each(function(item) {
            if (item.descendants()[2] != null)
                CreateRolloverMenu(item, item.descendants()[2]);
        });
    }
});

var Gallery = Class.create(
{
    initialize: function()
    {
        //Pre Load images to prevent flickering
        if ($$(".gallerymain")[0] != null)
        {
            $$('.galleryhiddenimage').each(function(item)
            {
                var i = new Image();
                i.src = item.value;
            });
        }
    }
});

var ImageRotation = Class.create(
{
    images : null,
    currentImageIndex : -1,
    redirectLocation : null,
    initialize: function()
    {
        if ($('imagerotation'))
        {
            this.images = $$('#imagerotation img');
            var redirectLocationField = $('imagerotationredirect');
            if(redirectLocationField)
                this.redirectLocation = redirectLocationField.value;
            
            //Preload images
            var image = new Image();
		    this.images.each(function(item) 
		    {
			    image.src = item.getAttribute('src');
	        });
	    
            //Start rotation
            this.next();
        }
    },
    next : function()
    {
        this.currentImageIndex++;
        var doRedirect = false;
        if(this.currentImageIndex >= this.images.length)
        {
            if(this.redirectLocation == null)
                this.currentImageIndex = 0;
            else
                doRedirect = true;
        }
        
        if(doRedirect)
            window.location = this.redirectLocation;
        else
            this.showImage();
        
        this.next.bindAsEventListener(this).delay(5);
    },
    showImage : function()
    {
        //If we already have an image then set as the background image of the div and hide the image
        if ($('newImage'))
        {
            $('banner').setStyle({ backgroundImage: 'url(' + $('newImage').src + ')' });
            $('newImage').setStyle({ opacity: '0' });
        }
        
        var newImage;
        var banner = $('banner');
        if (banner.childNodes.length == 0)
        {
            //Create a new image element
            newImage = new Element('img');
            newImage.setStyle(
            {
                opacity: '0'
            });
            newImage.setAttribute('id', 'newImage');
            
            //Append image to the DOM
            banner.appendChild(newImage);
        }
        else
        {
            //Use the existing image
            newImage = banner.childNodes[0];
        }
 
        //Set the src of the image
        newImage.src = this.images[this.currentImageIndex].src;
        
        //Fade in the image
        new Effect.Morph(newImage, {
            style: 'opacity:1;',
            duration: 3
        });
    }
});

var MainNavMenus = Class.create(
{
    initialize: function() {
        $$('#mainnavigation li').each(function(item) {
            var subnavs = item.select('.subnav');
            if (subnavs.length == 1) {
                CreateRolloverMenu(item, subnavs[0]);
            }
        });
    }
});

var YesNoTextFieldRevealer = Class.create(
    {
        initialize: function(yesRadioID, noRadioID, textFieldWrapperID)
        {
            this.yesRadio = $(yesRadioID);
            this.noRadio = $(noRadioID);
            this.textFieldWrapper = $(textFieldWrapperID);
            
            Event.observe(this.yesRadio, 'change', this.setTextFieldVisibility.bindAsEventListener(this));
            Event.observe(this.noRadio, 'change', this.setTextFieldVisibility.bindAsEventListener(this));
            Event.observe(this.yesRadio, 'click', this.setTextFieldVisibility.bindAsEventListener(this));
            Event.observe(this.noRadio, 'click', this.setTextFieldVisibility.bindAsEventListener(this));
            
            this.setTextFieldVisibility();
        },
        setTextFieldVisibility: function()
        {
            if(this.yesRadio.checked)
                this.textFieldWrapper.removeClassName('displaynone');
            else
                this.textFieldWrapper.addClassName('displaynone');
        }
    }
);

var SelectBoxOtherTextRevealer = Class.create(
    {
        initialize: function(selectBoxID, textFieldWrapperID, otherValue)
        {
            this.selectBox = $(selectBoxID);
            this.textFieldWrapper = $(textFieldWrapperID);
            this.otherValue = otherValue;
            
            Event.observe(this.selectBox, 'change', this.setTextFieldVisibility.bindAsEventListener(this));
            
            this.setTextFieldVisibility();
        },
        setTextFieldVisibility: function()
        {
            if(this.selectBox.options[this.selectBox.selectedIndex].value == this.otherValue)
                this.textFieldWrapper.removeClassName('displaynone');
            else
                this.textFieldWrapper.addClassName('displaynone');
        }
    }
);

Event.observe(window, 'load', function() {
    new ExternalLinks();
    new IE6ImageFlicker();
    new ImageRotation();
    new HomeLinks();
    new Gallery();
    new MainNavMenus();
});

function CreateRolloverMenu(parent, child)
{
    //Create drop down child element within parent, then use events to show/hide it
    parent.dropDownElement = child; 
    Event.observe(parent, 'mouseover', function(e) { this.dropDownElement.removeClassName('displaynone'); });
    Event.observe(parent, 'mouseout', function(e) { this.dropDownElement.addClassName('displaynone'); Event.stop(e); });
}

var CSSColumns = {
    maxHeight: 0,
    els: new Array(),
    equalise: function() {
        for (var i = 0; i < arguments.length; i++) if (!$(arguments[i])) return;
        for (var i = 0; i < arguments.length; i++) {
            this.els.push($(arguments[i]));
        }
        this.maxHeight = this.calcMaxHeight();
        for (var i = 0; i < this.els.length; i++) {
            this.els[i].style.height = this.maxHeight + "px";
        }
    },
    calcMaxHeight: function() {
        var h = 0;
        for (var i = 0; i < this.els.length; i++) {
            if (this.els[i].getHeight() > h) {
                h = this.els[i].getHeight();
            }
        }
        return h;
    }
}

function setupSelectOnFocusTextBox(textboxId, defaultText)
{
    var textbox = $(textboxId);
    Event.observe(textbox, 'focus', function(){
        if(this.value == defaultText)
          this.value = "";
        else
          this.select();
    });
    Event.observe(textbox, 'blur', function(){
        if(this.value == "")
	        this.value = defaultText;
    });
}

function GallerySwap(element)
{
    if ($$(".gallerymain")[0] != null)
    {
        $$(".gallerymain")[0].src = $(element).select('.galleryhiddenimage')[0].value;
        $$(".gallerybutton").each(function(item){
            item.removeClassName("selected");
        });
        element.addClassName("selected");
    }
    return false;
}