﻿
function addLoadEvent(func, args) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = function() {
            func(args);
        }
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func(args);
        }
    }
}

/**
*  Accordion Effects
*  Initialises accordion, and desactivate links
*
*  param   divId               [string]    Accodion Block ID
*  param   displayClass        [string]    Trigger Class Name
*  param   stretcherClass      [string]    Stretcher Class Name
*  param   onActive            [function]  Function called after the Accordion effect
*  param   onActiveArgs        [array]     onActive Function Parameters
*  param   disableIE6          [boolean]   enable/disable accordion effect for IE<=6 browser
*  param   openFirstElement    [boolean]   open first accordion stretcher
*/
var StartEffects = {

    divId: null,
    displayClass: null,
    stretcherClass: null,
    onActive: null,
    onActiveArgs: new Array(),
    disableIE6: false,
    openFirstElement: false,

    init: function(args) {

        this.disableIE6 = args[5];
        if (this.disableIE6) {
            var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
            var isIE6 = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7);
        } else {
            var isIE6 = false;
        }
        if (!isIE6) {

            this.divId = args[0];
            this.displayClass = args[1];
            this.stretcherClass = args[2];
            this.onActive = args[3];
            this.onActiveArgs = args[4];
            var stretchers = document.getElementsByClassName(this.stretcherClass);
            var toggles = document.getElementsByClassName(this.displayClass);

            // disable link
            var tl = toggles.length;
            for (i = 0; i < tl; i++) {
                var aTags = toggles[i].getElementsByTagName("a");
                Content.setParam(aTags[0], 'originalClassName', aTags[0].className);
                aTags[0].onclick = function() {
                    return false;
                };
            }

            // accordion effect
            var myAccordion = new fx.Accordion(
		        toggles,
		        stretchers,
		        { opacity: true, duration: 300 },
		        this.onActive,
		        this.onActiveArgs
	        );

            // open first div
            var qs = location.href.split('?')[1];
            if (qs != 'undefined' && qs != null) {
                var acc_id;
                if (qs.indexOf('&') > 0) { // Categories present
                    acc_id = qs.split('&')[0].split('=')[1];
                } else {
                    acc_id = qs.split('=')[1];
                }
                myAccordion.showThisHideOpen(stretchers[acc_id - 1]);
            } else {
                // Default - open first div
                this.openFirstElement = args[6];
                if (this.openFirstElement) {
                    myAccordion.showThisHideOpen(stretchers[0]);
                } else {
                    // Hide loader
                    if ($('loader') != null) {
                        Content.hide($('loader'));
                    }
                }
            }

        }
    }

};

/**
*  Activates the link with the "active" class
*  And initialises the Content functions 0689295381
*/
var ActiveEffects = {

    mode: '',
    obj: null,
    params: new Array(),
    tracker: 0,

    init: function(m, o, a) {
        this.mode = m;
        this.obj = o;
        this.params = a;
        var aTag = this.obj.getElementsByTagName('a')[0];
        switch (this.mode) {
            case 'open':
                aTag.className = 'active';
                break;
            case 'close':
                aTag.className = aTag.originalClassName;
                break;
            case 'closeAndOpen':
                aTag.className = aTag.originalClassName;
                break;
        }

        // Call the Content init function - only once
        if (this.params != null) {
            if (this.tracker <= 0) {
                this.tracker++;
                var args = new Array();
                args = this.params[1];
                var func = this.params[0];
                func(args);

                // Hide loader
                if ($('loader') != null) {
                    Content.hide($('loader'));
                }
            }
        }


    }

};

/**
*  Content page Functions
*  Reveals content when header is clicked
*
*  param   blockId         [string]    ID containing all the contents blocks
*  param   blockClass      [string]    Block Class Name
*  param   headersId       [string]    ID containing all the headers
*  param   headersClass    [string]    Header Class Name (optional)
*
*  Headers and Contents format:
*      - name_id
*      - name_cid::id
*/

/* Event Ino Content */

var Content = {

    blockId: null,
    blockClass: null,
    headersId: null,
    headersClass: null,

    blocks: new Array(),
    headers: new Array(),
    hasSubCat: false,
    loaderEl: null,
    blurbEl: null,

    init: function(args) {
        Content.blockId = args[0];
        Content.blockClass = args[1];
        Content.headersId = args[2];
        Content.headersClass = args[3];

        // defines whether a header is within a category (ie: in an accordion)
        if (Content.headersClass != null) Content.hasSubCat = true;

        Content.loaderEl = $('loader');
        Content.blurbEl = $('blurb');

        // Build info blocks objects
        var block = $(Content.blockId);
        var el = block.getElementsByTagName('div');
        var el_length = el.length;
        for (i = 0; i < el_length; i++) {
            if (el[i].className == Content.blockClass) {
                Content.setParam(el[i], 'id', el[i].id.split('_')[1]);
                Content.blocks.push(el[i]);
            }
        }

        // Disable headers links & add click events
        var block = $(Content.headersId);
        var aTags = block.getElementsByTagName('a');
        var aL = aTags.length;
        for (i = 0; i < aL; i++) {
            if (Content.hasSubCat) {
                if (aTags[i].className == Content.headersClass) {
                    Content.setParam(aTags[i], 'id', aTags[i].id.split('_')[1]);
                    Content.setParam(aTags[i], 'originalClassName', aTags[i].className);
                    aTags[i].onclick = function() {
                        Content.showBlock(this);
                        return false;
                    }
                    Content.headers.push(aTags[i]);
                }
            } else {
                Content.setParam(aTags[i], 'id', aTags[i].id.split('_')[1]);
                Content.setParam(aTags[i], 'originalClassName', aTags[i].className);
                aTags[i].onclick = function() {
                    Content.showBlock(this);
                    return false;
                }
                Content.headers.push(aTags[i]);
            }
        }

        // Show correct block function of query string
        var offset = 0;
        var qs = location.href.split('?')[1];
        if (qs != 'undefined' && qs != null) {
            // The ID is present in the QueryString - show the corresponding block
            var contentId;
            if (Content.hasSubCat) {
                var priId = qs.split('&')[0].split('=')[1];
                var secId = qs.split('&')[1].split('=')[1];
                contentId = priId + "::" + secId;
                Content.showBlock(contentId);
            } else {
                contentId = qs.split('=')[1];
                Content.showBlock(contentId);
            }
        } else if (Content.blurbEl == null) {
            // The ID is not present and there is no blurb - show the 1st block
            if (Content.hasSubCat) {
                Content.showBlock(Content.headers[0]);
            } else {
                Content.showDefault();
            }
        } else {
            // The ID is not present and there is a blurb
            //  - show the blurb (already showing => nothing to do)
            //  - hide the loader
            if (Content.loaderEl != null) Content.hide(Content.loaderEl);
        }

    },

    showDefault: function() {
        var d = new Date();
        var day = d.getDay();
        var date = d.getDate();

        /** To remove post festival **/
        if (day == 4 && date == 3) {
            Content.showBlock(Content.headers[0]);
        } else if (day == 5 && date == 4) {
            Content.showBlock(Content.headers[1]);
        } else if (day == 6 && date == 5) {
            Content.showBlock(Content.headers[2]);
        } else if (day == 0 && date == 6) {
            Content.showBlock(Content.headers[3]);
        } else {
            Content.showBlock(Content.headers[0]);
        }
    },

    showBlock: function(el) {
        var l = Content.blocks.length;
        if (Content.hasSubCat) {
            // Headers are inside an accordion: ie: subcategory of a main category
            var activeEl = new Array(null);
            if (typeof (el) == 'object') { // Action triggered by a click event
                var cat_obj = el.id.split('::')[0];
                var obj = el.id.split('::')[1];
            } else {
                var cat_obj = el.split('::')[0];
                var obj = el.split('::')[1];
            }
            for (i = 0; i < l; i++) {
                if (Content.blocks[i].id.split('::')[0] == cat_obj) {
                    if (Content.blocks[i].id.split('::')[1] == obj) {
                        Content.display(Content.blocks[i]);
                        activeEl = cat_obj + "::" + obj;
                    } else {
                        Content.hide(Content.blocks[i]);
                    }
                } else {
                    Content.hide(Content.blocks[i]);
                }
            }
        } else {
            // Headers are singletons
            var activeEl = null;
            if (typeof (el) == 'object') { // Action triggered by a click event
                var obj = el.id;
            } else {
                var obj = el;
            }
            for (i = 0; i < l; i++) {
                if (Content.blocks[i].id == obj) {
                    Content.display(Content.blocks[i]);
                    activeEl = obj;
                } else {
                    Content.hide(Content.blocks[i]);
                }
            }
        }

        // Activate active header
        if (activeEl != null) Content.activate(activeEl);

        // Hide blurb
        if (Content.blurbEl != null) Content.hide(Content.blurbEl);

        // Hide loader
        if (Content.loaderEl != null) Content.hide(Content.loaderEl);

    },

    display: function(el) {
        el.style.display = 'block';
    },

    hide: function(el) {
        el.style.display = 'none';
    },

    activate: function(el) {
        var l = Content.headers.length;
        if (Content.hasSubCat) {
            if (typeof (el) == 'object') { // Action triggered by a click event
                var cat_obj = el.id.split('::')[0];
                var obj = el.id.split('::')[1];
            } else {
                var cat_obj = el.split('::')[0];
                var obj = el.split('::')[1];
            }
            for (i = 0; i < l; i++) {
                if (Content.headers[i].id.split('::')[0] == cat_obj) {
                    if (Content.headers[i].id.split('::')[1] == obj) {
                        Content.headers[i].className = Content.headers[i].originalClassName + 'IsActive';
                    } else {
                        Content.headers[i].className = Content.headers[i].originalClassName;
                    }
                } else {
                    Content.headers[i].className = Content.headers[i].originalClassName;
                }
            }
        } else {
            if (typeof (el) == 'object') { // Action triggered by a click event
                var obj = el.id;
            } else {
                var obj = el;
            }
            for (i = 0; i < l; i++) {
                if (Content.headers[i].id == obj) {
                    Content.headers[i].className = Content.headers[i].originalClassName + 'IsActive';
                } else {
                    Content.headers[i].className = Content.headers[i].originalClassName;
                }
            }
        }
    },

    setParam: function(e, param, value) {
        e[param] = value;
    }

};


/**
*  Content Hide/Show Class
*  Reveals content when header is clicked
*
*  @param  blockId         content blocks container ID
*  @param  blockClass      content block Classname
*  @param  heardersId      headers container ID
*  @param  l_mode          loading mode: null, 'loader' or 'blurb'
*  @param  loaderId        loader ID
*  @param  blurbId         blurb ID
*/

/* News Content */

var newsContent = Class.create();
newsContent.prototype = {

    blockId: '',
    blockClass: '',
    headersId: '',
    lmode: null,
    loaderId: '',
    blurbId: '',

    blocks: new Array(),
    headers: new Array(),

    initialize: function(args) {

        var caller = this;

        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = args[2];
        this.lmode = args[3];
        this.loaderId = args[4];
        this.blurbId = args[5];

        /** Build info blocks objects **/
        var block = $(this.blockId);
        var el = block.getElementsByTagName('div');
        var el_length = el.length;
        for (i = 0; i < el_length; i++) {
            if (el[i].className == this.blockClass) {
                this.blocks.push(el[i]);
            }
        }

        /** Disable headers links & add click events **/
        var block = $(this.headersId);
        var aTags = block.getElementsByTagName('a');
        var aL = aTags.length;
        for (i = 0; i < aL; i++) {
            aTags[i].onclick = function() {
                caller.showBlock(this);
                return false;
            }
            this.headers.push(aTags[i]);
        }

        /** Show correct block **/
        var offset = 0;
        var qs = location.href.split('?')[1];
        if (qs != 'undefined' && qs != null) { // The info ID is present in the QueryString
            var contentId = qs.split('=')[1];
            this.showBlock(contentId);
        } else {
            if (this.lmode != null) { // Default - Show first block            
                switch (this.lmode) { // Loader or blurb present                
                    // If loader - display first block   
                    case 'loader':
                        this.showBlock(this.headers[0]);
                        break;
                    // If blurb - Insert content of blurb in loader   
                    case 'blurb':
                        var txt = $(this.blurbId).innerHTML;
                        $(this.loaderId).className = '';
                        $(this.loaderId).innerHTML = txt;
                        break;
                }
            } else {
                // No loader or blurb - only display block
                this.showBlock(this.headers[0]);
            }
        }

    },

    showBlock: function(el) {
        var l = this.blocks.length;
        var activeEl = null;
        if (typeof (el) == 'object') { // Action triggered by a click event
            var obj = el.id;
        } else {
            var obj = el;
        }
        for (i = 0; i < l; i++) {
            if (this.blocks[i].id == obj) {
                this.display(this.blocks[i]);
                activeEl = obj;
            } else {
                this.hide(this.blocks[i]);
            }
        }

        // Activate active header
        if (activeEl != null) this.activate(activeEl);

        // Hide loader
        if (this.lmode != null) this.hide($(this.loaderId));

    },

    display: function(el) {
        el.style.display = 'block';
    },

    hide: function(el) {
        el.style.display = 'none';
    },

    activate: function(el) {
        var l = this.headers.length;
        if (typeof (el) == 'object') { // Action triggered by a click event
            var obj = el.id;
        } else {
            var obj = el;
        }
        for (i = 0; i < l; i++) {
            if (this.headers[i].id == obj) {
                this.headers[i].className = 'active';
            } else {
                this.headers[i].className = '';
            }
        }
    },

    setParam: function(e, param, value) {
        e[param] = value;
    }

};


/**
*  SimpleContent Hide/Show Class
*  Reveals content when header is clicked
*  Simplified version of Content class => no deep linking
*  Usually used as a secondary content area 
*
*  @param  blockId         content blocks container ID
*  @param  blockClass      content block Classname
*  @param  heardersId      headers container ID
*  @param  l_mode          loading mode: null, 'loader' or 'blurb'
*  @param  loaderId        loader ID
*  @param  blurbId         blurb ID
*/
var SimpleContent = Class.create();
SimpleContent.prototype = {

    blockId: '',
    blockClass: '',
    headersId: '',
    lmode: null,
    loaderId: '',
    blurbId: '',

    blocks: new Array(),
    headers: new Array(),

    initialize: function(args) {

        var caller = this;

        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = args[2];
        this.lmode = args[3];
        this.loaderId = args[4];
        this.blurbId = args[5];

        /** Build info blocks objects **/
        var block = $(this.blockId);
        var el = block.getElementsByTagName('div');
        var el_length = el.length;
        for (i = 0; i < el_length; i++) {
            if (el[i].className == this.blockClass) {
                this.blocks.push(el[i]);
            }
        }

        /** Disable headers links & add click events **/
        var block = $(this.headersId);
        var aTags = block.getElementsByTagName('a');
        var aL = aTags.length;
        for (i = 0; i < aL; i++) {
            aTags[i].onclick = function() {
                caller.showBlock(this);
                return false;
            }
            this.headers.push(aTags[i]);
        }

        /** Show correct block **/
        if (this.lmode != null) { // Default - Show first block            
            switch (this.lmode) { // Loader or blurb present                
                // If loader - display first block   
                case 'loader':
                    this.showBlock(this.headers[0]);
                    break;
                // If blurb - Insert content of blurb in loader   
                case 'blurb':
                    var txt = $(this.blurbId).innerHTML;
                    $(this.loaderId).className = '';
                    $(this.loaderId).innerHTML = txt;
                    break;
            }
        } else {
            // No loader or blurb - only display block
            this.showBlock(this.headers[0]);
        }

    },

    showBlock: function(el) {
        var l = this.blocks.length;
        var activeEl = null;
        if (typeof (el) == 'object') { // Action triggered by a click event
            var obj = el.id;
        } else {
            var obj = el;
        }
        for (i = 0; i < l; i++) {
            if (this.blocks[i].id == obj) {
                this.display(this.blocks[i]);
                activeEl = obj;
            } else {
                this.hide(this.blocks[i]);
            }
        }

        // Activate active header
        if (activeEl != null) this.activate(activeEl);

        // Hide loader
        if (this.lmode != null) this.hide($(this.loaderId));

    },

    display: function(el) {
        el.style.display = 'block';
    },

    hide: function(el) {
        el.style.display = 'none';
    },

    activate: function(el) {
        var l = this.headers.length;
        if (typeof (el) == 'object') { // Action triggered by a click event
            var obj = el.id;
        } else {
            var obj = el;
        }
        for (i = 0; i < l; i++) {
            if (this.headers[i].id == obj) {
                this.headers[i].className = 'active';
            } else {
                this.headers[i].className = '';
            }
        }
    },

    setParam: function(e, param, value) {
        e[param] = value;
    }

};