﻿/**
*  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 = {

    blockId: '',
    blockClass: '',
    headersId: '',
    lmode: null,
    loaderId: '',
    blurbId: '',

    blocks: new Array(),
    headers: new Array(),

    initialize: function(args) {

        //show paging
        $(".paging").show();

        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 = $("#" + this.blockId + " 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 aTags = $("#" + this.headersId + " 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;
            obj = obj.substring(1);
        } else {
            var obj = el;
        }
        for (i = 0; i < l; i++) {
            if (this.blocks[i].id.substring(1) == 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.loaderId).hide(); }

    },

    display: function(el) {
        el.style.display = 'block';
    },

    hide: function(el) {
        if(el.style)
            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.substring(1) == 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 = {

    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 = $("#" + this.blockId + " 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 aTags = $("." + this.headersId + " 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;
        }
        if(obj)
            obj = obj.substring(2);
        for (i = 0; i < l; i++) {
            if (this.blocks[i].id.substring(1) == 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.substring(2) == obj) {
                this.headers[i].className = 'active';
                
                $("#pageof1").text("Page " + obj + " of " + this.headers.length / 2);
                $("#pageof2").text("Page " + obj + " of " + this.headers.length / 2);
                
            } else {
                this.headers[i].className = '';
            }
        }
    },

    setParam: function(e, param, value) {
        e[param] = value;
    }

};