
/*
Script: objects.js
javascript options for Pri-med.com
Depends on the mootools library (mootools.v1.11.js)
*/


// hashtable to populate banner URLs
var bannerUrls = {};
bannerUrls["default"] = "javascript:false;";
// implementing pages should follow these naming conventions
var bannerDivId = 'Banner';
var bannerFrameId = 'BannerFrame';

var bannerUrlsTop = {};
bannerUrlsTop["default"] = "javascript:false;";
var bannerDivIdTop = 'BannerTop';
var bannerFrameIdTop = 'BannerFrameTop';

/* 

Class: tabSet
tabSet requires collection of tab objects as well as optional declaration or selected tab.  Default is 0.




*/

var tabSet = Fx.Base.extend({
    getExtended: function() {
        return {
            selected: null,
            type: 'tab',
            url: document.location,
            useHistory: true
        };
    },

    /*
		
		Constructor: initialize
    Sets up tabSet for navigation
		
		Arguments: 
    tab - The collection of DOM elements that will serve as tab objects
    content - collection of DOM elements (usually DIVs) that will serve as switchable 
    options:type - 'date' or 'tab' are the only two types so far..
    options:useHistory - Boolean.  Defines whether or not the tabSet will use <HistoryManager>  
		 
    Examples: 
    >var homeTabs = new tabSet($$('.tab'), $$('.content .frmContent'), {url:document.location});		
		    
    See Also:  
    <HistoryManager>
		   
    */

    initialize: function(tab, content, options) {

        this.setOptions(this.getExtended(), options);
        this.u = this.options.url;
        this.current = this.setInitial();
        this.tabs = $$(tab);
        this.content = $$(content);

        if (this.tabs.length > 1 && this.content.length > 1) {
            this.links = $$('.' + scrub_class(this.tabs[0], 'clearfix ') + ' a');
            this.links.each(function(el, i) {
                if (el.firstChild.length > 12 && el.innerHTML.indexOf('<br') == -1 && el.innerHTML.indexOf('day') == -1) {
                    el.innerHTML = truncSplit(el.innerHTML, 12);
                    //need to figure out how to add <br/> tag here				   
                }
                var tempEl = new String;
                tempEl = killSpace(tempEl, el);

                el.addEvent('click', this.moveTabs.bind(this, i));
                el.addEvent('click', this.track.bind(this, this.options.type + '=' + tempEl));
                el.addEvent('click', this.trackDCS.bind(this, i));
            }, this);
        }
        else if (this.tabs.length == 1 || this.content.length == 1) {
            this.tabs[0].addClass('selected');
            this.content[0].setStyle('display', 'block');
        }

        var pv = getQueryString('printview');
        if (pv == 'true' || window.webkit == true) {
            this.options.useHistory = false;
            this.moveTabs(this.current);
        }

        if (this.options.useHistory) {
            this.history = HistoryManager.register(
			        this.options.type,
			        [0],
            //[this.current],
			        function(args) { this.moveTabs(args[0]); } .bind(this),
			        false,
			        false);
        }

        if (this.options.type == 'tab' && pv != 'true') {
            //alert('starting');
            HistoryManager.start();


            /* commented out because it makes the page load count as a double hit for WT */
            // hack to tag entry into tabbed content set, no matter which tab
            //var index = HistoryManager.getState().substr(4);
            //if (index == ''){
            //    index = 0;
            //}
            //this.trackDCS(index);
        }
    },

    /* 
		
		    Function: alignTabs
    Sets tabs for navigation based on this.current
		
		*/

    alignTabs: function() {
        if (this.tabs.length > 0 && this.content.length > 0 && this.tabs.length >= this.current && this.content.length >= this.current) {
            var index = this.tabs.length * 10;
            this.content.each(function(el) {
                el.setStyle('display', 'none');
            });


            this.tabs.each(function(el) {
                el.setStyle('zIndex', index);
                index--;
            });

            this.tabs[this.current].setStyle('zIndex', 1000);
            this.content[this.current].setStyle('display', 'block');
        }

    },

    /* 
    Function: moveTabs(dest)
    Moves the tabSet to the desired tab
		    
    Arguments: 
    dest - Integer representing which zero-based tab to navigate to.  Nullable.
		    
    */

    moveTabs: function(dest) {
        dest = (dest == undefined) ? 0 : dest;
        if (this.history) this.history.setValue(0, dest);
        try {
            var cur = this.tabs[this.current];
            cur.removeClass('selected');
            this.tabs[dest].className += ' selected';
            this.current = dest;
        }
        catch (err) { }
        this.alignTabs();

        this.setBanner(dest);
        this.setBannerTop(dest);

        /*** Case only for Live Events ***/
        if ($$('.infoBox', '.confBox').length > 0 && this.options.type == 'tab') {

            switch (parseInt(dest)) {
                case 1:
                    $$('.confBox').each(function(el) { el.style.display = "block" });
                    $$('.infoBox').each(function(el) { el.style.display = "none" });
                    break;
                case 4:
                    $$('.confBox').each(function(el) { el.style.display = "none" });
                    $$('.infoBox').each(function(el) { el.style.display = "block" });
                    break;


                default:
                    $$('.confBox').each(function(el) { el.style.display = "none" });
                    $$('.infoBox').each(function(el) { el.style.display = "none" });
                    break;
            }
        }
        if (this.history) this.history.setValue(0, dest);

    },

    /*
    Function: setBanner(dest)
    Set the banner iframe based on the destination tab name

		    Arguments: 
    dest - Integer representing which zero-based tab to navigate to.  Nullable.
    */
    setBanner: function(dest) {
        if (!$defined(this.tabs[dest]) || !$defined(this.tabs[dest].getAttribute('name'))) {
            // do nothing
        }
        else {
            var tabName = this.tabs[dest].getAttribute('name');

            var invokeUrl = bannerUrls[tabName];

            if ($defined($(bannerDivId))) {
                if (invokeUrl != null) {
                    $(bannerDivId).style.display = "block";
                    $(bannerDivId).style.visibility = "visible";

                    $(bannerFrameId).src = invokeUrl;
                }
                else {
                    $(bannerDivId).style.display = "none";
                    $(bannerDivId).style.visibility = "hidden";
                }
            }
        }
    },

    setBannerTop: function(dest) {
        if (!$defined(this.tabs[dest]) || !$defined(this.tabs[dest].getAttribute('name'))) {
            // do nothing
        }
        else {
            var tabName = this.tabs[dest].getAttribute('name');

            var invokeUrl = bannerUrlsTop[tabName];

            if ($defined($(bannerDivIdTop))) {
                if (invokeUrl != null) {
                    $(bannerDivIdTop).style.display = "block";
                    $(bannerDivIdTop).style.visibility = "visible";

                    $(bannerFrameIdTop).src = invokeUrl;
                }
                else {
                    $(bannerDivIdTop).style.display = "none";
                    $(bannerDivIdTop).style.visibility = "hidden";
                }
            }
        }
    },

    /* 
		
		    Function: track(type)
    Sends tab information to Google Analytics.
		    
    Arguments: 
    type - String. Represents tab destination and type.
    */

    track: function(type) {
        /*  Analytic tracking code below*/
        if (self['urchinTracker']) {
            urchinTracker(type);
        }
    },


    /* 
		
		    Function: trackDCS(type)
    Sends tab information to Web Trends.
		    
    Arguments: 
    type - String. Represents tab destination and type.
    */

    trackDCS: function(dest) {
        if (TagClientSideTabSwitch) {
            TagClientSideTabSwitch(dest, this.tabs[dest].getAttribute('name'), document.location);
        }

    },


    /* 
		
		    Function: setInitial
    Sets the first tab based on first this.current
		    
		    
    */

    setInitial: function() {
        var tabIndex = (this.options.type == 'tab') ? getQueryString("tabIndex") : false;
        var val = '';
        var dl = document.location.toString();

        var hashIndex = (dl.indexOf('#') != -1) ? getHashVal(this.options.type, dl.substring(dl.indexOf("#"), dl.length)) : false;
        if (tabIndex != false && hashIndex == false) {

            val = (parseInt(tabIndex));
            //alert('tabIndex=' + tabIndex);
        }
        else if (this.options.selected != 0 && this.options.selected != null && hashIndex == false) {
            val = this.options.selected;
        }
        else if (hashIndex != false) {
            val = hashIndex;
        }
        else { val = 0; }
        //alert(val);
        return val;


    }
});


/* 

Function: killSpace
eliminates space around text element

Arguments: 
tempEl - element
el - element to strip space from

Returns: 
tempEl - stripped text element.


*/


function killSpace(tempEl, el) {

    tempEl = el.innerHTML;
    tempEl.trim();
    tempEl.normalize_space();
    tempEl = tempEl.split(" ");
    tempEl = tempEl.join('')
    tempEl = tempEl.split("<br>");
    tempEl = tempEl.join('');
    tempEl = tempEl.split("\n");
    tempEl = tempEl.join('');

    return tempEl;
}


/*
 

 
 
Class: featureBox
Feature Box sits on home page.  Declaring content blocks, controls, counters and optional variable of 
selected will instantiate this object.
 
 
*/

var featureBox = Fx.Base.extend({

    getExtended: function() {
        return { show: 0
        };
    },


    /* 
		
		  Function: initialize
		  
    Arguments: 
    blocks - Collection of DOM Elements which serve as switchable content
    controls - Collection of DOM Elements which serve as clickable controls to navigate through the blocks
    counters - Collection of DOM Elements (Should be 2 elements) which serve to indicate the progress through total activities
    options:show - integer.  Defines initial zero-based feature block to initialize on load. 
		
		  Examples: 
    > var homeFeature = new featureBox($$('#feature .container'), $$('.nav .control'), $$('.counter'));
		
		*/

    initialize: function(blocks, controls, counters, options) {
        this.setOptions(this.getExtended(), options);
        this.current = this.options.show;
        this.blocks = $$(blocks);
        this.controls = $$(controls);
        this.counters = $$(counters);
        try {
            this.blocks.each(function(el) { el.setStyle('display', 'none') });
            this.blocks[this.options.show].setStyle('display', 'block');
            this.blocks[this.options.show].setStyle('visibility', 'visible');
            this.controls[0].addEvent('click', this.move.bind(this, 'previous'));
            this.controls[1].addEvent('click', this.move.bind(this, 'next'));
            this.checkControls();
        }
        catch (err) { }
    },

    /* 
		
		    Function: move
    Arguments: 
    method - String. Either 'next' or 'previous' telling FeatureBox which direction to navigate.
		    
		
		*/

    move: function(method) {
        this.moveOpacity(this.blocks[this.current], 1, 0);
        this.checkBox(this.blocks[this.current], 'none');
        if (this.current == 0 && method == 'previous') { this.current = this.blocks.length - 1; }
        else if (this.current == (this.blocks.length - 1) && method == 'next') { this.current = 0; }
        else { this.current = (method == 'next') ? ++this.current : --this.current; }


        this.checkBox(this.blocks[this.current], 'block');
        this.moveOpacity(this.blocks[this.current], 0, 1);
        this.checkControls();

    },

    /* 
		
		    Function: moveOpacity
    Method for transitioning from one block to another.  Does not transition for IE6.
		    
    Arguments: 
    obj - DOM object for which to change opacity
    start - Double. 0 - 1 based decmimal for which to begin transition
    finish - Double. 0 - 1 based decmimal for which to end transition
		    
    */

    moveOpacity: function(obj, start, finish) {
        if (navigator.userAgent.indexOf("MSIE 6.0") != -1) {
            var featTrans = new Fx.Style(obj, 'opacity', { duration: 0, transition: Fx.Transitions.linear });
            featTrans.start(start, finish);
        }
        else {
            var featTrans = new Fx.Style(obj, 'opacity', { duration: 600, transition: Fx.Transitions.linear });
            featTrans.start(start, finish);
        }


    },

    /* 
		
		Function: checkBox
		
		Arguments: 
    obj - DOM Object
    display - 'block' or 'none'
		
		*/

    checkBox: function(obj, display) {
        obj.setStyle('display', display);
    },

    /* 
		
		Function: checkControls
    Displays the current index of the featureBox as well as the total number of blocks.
				
		
		*/
    checkControls: function() {
        this.counters[0].innerHTML = this.current + 1;
        this.counters[1].innerHTML = this.blocks.length;
    }
});

/*
		
Class: navSet
Object controlling main navigation of site
	
*/

var navSet = Fx.Base.extend({

    getExtended: function() {
        return {
            url: ''
        };
    },

    /* 
		
		Function: initialize
		
		Arguments: 
    elems - images that will be switched between
    links - the links that contain location of 
    options:url -  will contain the url of the page the navSet is on.  This is in order to most easily match the highlighted image.

        Examples: 
    >var globalNav = new navSet($$('.siloNav img'), $$('.siloNav a'),{url:document.location})
		
		*/


    initialize: function(elems, links, options) {
        this.setOptions(this.getExtended(), options);
        this.elems = $$(elems);
        this.links = $$(links);
        this.u = this.options.url.toString();
        this.u = this.u.substring(this.u.lastIndexOf('//') + 2);

        this.assign();
    },

    /* 
		
		Function: goRoll
    The method bound to navigation images to properly change images onMouseover 
		
		Parameters: 
    i - Integer.  Designates destination in this.elems collection
		
		*/
    goRoll: function(i) {
        //alert('I made it go ' + this.elems[i].className);
        this.elems[i].src = this.elemsRoll[i].src;
        return false;
    },

    /* 
    Function: rollOff
    The method bound to navigation images to properly change images onMouseout 
		
		*/
    rollOff: function(i) {
        //alert('I made it go ' + this.elems[i].className );
        this.elems[i].src = this.elemsOff[i].src;
        return false;
    },

    /* 
		
		Function: assign
    The method to assign events to DOM objects on page  
		
		*/
    assign: function() {
        //alert(this.elems[0].src.toString());
        this.elemsOff = new Array;
        this.elemsRoll = new Array;
        this.elemsOn = new Array;
        var matchUrl = this.u.substring(this.u.lastIndexOf('/'), this.u.lastIndexOf('.aspx') + 5);
        //alert('match url is: ' + matchUrl);
        this.elems.each(function(el, i) {
            var imgOff = new Image();
            imgOff.src = el.src.toString();
            this.elemsOff[i] = imgOff;
            
            var imgRoll = new Image();
            imgRoll.src = el.src.substr(0, el.src.lastIndexOf('_')) + '_roll.gif';
            this.elemsRoll[i] = imgRoll;

            var imgOn = new Image();
            imgOn.src = el.src.substr(0, el.src.lastIndexOf('_')) + '_on.gif';
            this.elemsOn[i] = imgOn;
        }, this);

        if ($('navFlag')) {
            var navFlag = $('navFlag').value;
        }
        else {
            var navFlag = '';
        }
        this.elems.each(function(el, i) {

            var v = el.parentNode.pathname.substring(el.parentNode.pathname.lastIndexOf('/'), el.parentNode.pathname.length);
            if (v == matchUrl || el.parentNode.id == navFlag) {   //el.parentNode.replaceWith(el);
                el.src = this.elemsOn[i].src;
            }
            else {
                el.addEvent('mouseover', this.goRoll.bind(this, i));
                el.addEvent('mouseout', this.rollOff.bind(this, i));
            }

        }, this);
    }

});


/* 

Class: openDivSet
Based on the moo class Fx.Elements, this object displays individual divs with transition effects

*/

var openDivSet = Fx.Elements.extend({
    getExtended: function() {
        return {
            onActive: Class.empty,
            onBackground: Class.empty,
            display: 0,
            show: false,
            height: true,
            width: false,
            opacity: true,
            fixedHeight: false,
            fixedWidth: false,
            alwaysHide: false,
            duration: 500


        };
    },

    /* 
		
		Function: initialize
		
		Arguments: 
    heads - Collection of DOM elements which house the controls to open/close elements
    body - Collection of DOM elements which serve as 
    controls - Collection of DOM elements (usually links) which serve as clickable controls to open/close body content
    options -
		
		*/

    initialize: function(heads, body, controls, options) {
        this.setOptions(this.getExtended(), options);
        this.heads = $$(heads);
        this.body = $$(body);
        this.controls = $$(controls);
        this.heights = {};

        this.body.each(function(el, i) {
            this.heights[i] = el.getSize().scrollSize.y;
        }, this);

        if (this.options.display != -1) {
            this.display(this.options.display);
        }
        //alert(this.body[0].getSize().scrollSize.y + 'px');

        this.heads.each(function(tog, i) {
            tog.addEvent('click', this.display.bind(this, i));
        }, this);

    },

    /* 
		
		Function: display
    Sets the vertical transition to open div
	    	
    Arguments: 
    index - Integer.  zero-based number which sets div to open. 	
	
		
    */

    display: function(index) {

        var objToggle = new Fx.Style(this.body[index], 'height', { duration: this.options.duration });

        if (this.controls[index].className.indexOf('open') != -1) {
            objToggle.start(0);
            this.toggle('closed', index);
        }
        else {
            objToggle.start(this.heights[index]);
            this.toggle('open', index);
        }
    },

    /* 
	    
    Function: toggle
	    
    Parameters: 
    dest - Integer.
    index - Integer. 
	    
    */

    toggle: function(dest, index) {

        switch (dest) {
            case 'open':
                this.controls[index].className = scrub_class(this.controls[index], 'closed');
                this.controls[index].addClass('open');
                break;

            case 'closed':
                this.controls[index].className = scrub_class(this.controls[index], ' open');
                break;
        }
    }


});


/*

Class: divFBoxSet
An object which controls a collection of dhtml pop-ups and their links.

*/


var divFBoxSet = Fx.Base.extend({

    getExtended: function() {
        return {
            width: 279
        };
    },

    /* 
		
		Function: initialize
		
		Arguments: 
		
		*/

    initialize: function(links, inputs, floatbox, options) {
        this.optionsPassed = false;

        this.setOptions(this.getExtended(), options);
        this.links = $$(links);
        this.inputs = $$(inputs);
        this.fBox = $(floatbox);

        /* optionsPassed is a fix for pages using the same class for each
        floating box - we should be using a different class or identifier
        for each */
        if (this.options.width != undefined && this.options.width != '') {
            this.fBox.style.width = this.options.width + 'px';
        }
        if (this.options.left != undefined && this.options.left != '') {
            this.fBox.style.left = this.options.left + 'px';
            this.optionsPassed = true;
        }
        if (this.options.top != undefined && this.options.top != '') {
            this.fBox.style.top = this.options.top + 'px';
            this.optionsPassed = true;
        }
        this.links.each(function(el, i) {

            el.addEvent('click', this.openB.bind(this, i));
        }, this);

    },

    openB: function(el) {
        var name = this.inputs[el].getAttribute('value').split('::');

        $E('#' + this.fBox.id + ' h1').innerHTML = name[0];
        $E('#' + this.fBox.id + ' .descriptionContent').innerHTML = name[1];
        open_helper(this.fBox, this.links[el], this.optionsPassed);
    }
});
