/* Common JS */

/* hover state for left nav action outer list, since  *
 * IE does not support CSS :hover on anything but <a> */
Event.observe(window, 'load', actionLinksInit, false);

function actionLinksInit() {
    actions = $A($$('div.leftNav div.action'));
    actions.each( function(act){
        Event.observe(act, 'mouseover', function(){ this.addClassName('hover'); return false; }, false);
        Event.observe(act, 'mouseout', function(){ this.removeClassName('hover'); return false; }, false);
    });
}


/* hover states for buttons, since IE6 does     *
 * not support CSS :hover an anything but <a>  */
Event.observe(window, 'load', buttonHoverInit, false);

function buttonHoverInit() {
    //check for IE6 using Dean Edward method 
    var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
    if(!IE6) return;

    buttons = $A($$('button.green','button.arrow','form.headerSearch button'));
    buttons.each( function(btn){
        Event.observe(btn, 'mouseover', function(){ return function(){ hoverButton(btn); }(btn); }, false);
        Event.observe(btn, 'mouseout', function(){ return function(){ hoverButton(btn); }(btn); }, false);
    });
}

function hoverButton(button) {
    hoverClassSuffix = 'Hover';
    buttonClasses = button.className.split(' ');
    buttonClass = buttonClasses.pop();
    if(buttonClass.match(hoverClassSuffix)) {
        inactiveClass = buttonClass.replace(hoverClassSuffix,'');
        button.addClassName(inactiveClass);
        button.removeClassName(buttonClass); 
    } else {
        button.addClassName(buttonClass+hoverClassSuffix);
        button.removeClassName(buttonClass);
    }
}

/* Custom Lytebox Modification, Use an alternate close button */
Event.observe(window, 'load', lyteboxModInit, false);

function lyteboxModInit() {
    cControls = $A($$('p#controlClose'));
    if (cControls.length > 0) {
        cControls.each( function(controlClose){
            Event.observe(controlClose, 'click', function(){ window.parent.myLytebox.end(); return false; }, false);
            Event.observe(controlClose, 'mouseover', function(){ this.addClassName('hover'); return false; }, false);
            Event.observe(controlClose, 'mouseout', function(){ this.removeClassName('hover'); return false; }, false);
        });
    }
}

/* Tabs Interface */

Event.observe(document, 'dom:loaded', tabsInit, false);

function tabsInit() {
	/* setup tabs object */
	TabsModel = Object;
	TabsModel.tabLabelNameSpace = 'div.main div.pageBody div.leftColumn dl.sectionTabs > dt';
	TabsModel.tabNameSpace = 'div.main div.pageBody div.leftColumn dl.sectionTabs > dd';

	/* assign event handlers to tab controls/labels */
    tabControls = $A($$(TabsModel.tabLabelNameSpace));
	if (tabControls.length) {
		tabControls.each( function(tabC){
			Event.observe(tabC, 'click', function(){ return function(){ showTab(tabC); }(tabC); }, false);
			Event.observe(tabC, 'mouseover', function(){ 
					if (!tabC.className.match(/active/)) { 
					tabC.addClassName('hover'); 
					}
				}, false);
			Event.observe(tabC, 'mouseout', function(){ tabC.removeClassName('hover'); }, false);
		});
		/* show first tab */
		showTab(tabControls[0]);
	}
}

function showTab(tabC) {
	/* hide all tabs */
	if (tabs = $A($$(TabsModel.tabNameSpace))) {
		hideall(tabs);
	}
	/* switch all tab labels to inactive state */
	if (tabControls = $A($$(TabsModel.tabLabelNameSpace))) {
		tabControls.each( function(tabC){
			tabC.removeClassName('active');
			tabC.removeClassName('hover');
		});
	}
	/* show tab for clicked label */
	tabSelector = TabsModel.tabNameSpace + '.' + tabC.className;
	if (tabs = $A($$(tabSelector))) {
		tabs.each( function(tab){ tab.show(); } );
	}
	/* switch clicked label to active state */
	tabC.addClassName('active');
	
}

function hideall(elements) {
	elements.each( function(el){ el.hide(); } );
}


/* activate various boxes */

Event.observe(window, 'load', callToAction, false);

function callToAction() {

	/*
	adds hover state to Call To Action boxes
	and since Download and other boxes do  
	the same, we'll mix them in
	____________________________________________*/
	
	var calls = $A($$('dl.callToAction a','dl.callToActionMed a','dl.callToActionSm a','dl.callToActionTiny a','dl.news a','div.tools a','dl.tools a'));

	calls.each(
		function(call) { 
			var container = call.ancestors()[1];
            var containerClass = container.className.match(/^\w+/);
			Event.observe(container, 'click', function(){ window.location = call.href; }, false);
			Event.observe(container, 'mouseover', function(){ this.addClassName(containerClass+'Hover');this.removeClassName(containerClass);this.style.cursor = 'pointer'; }, false);
			Event.observe(container, 'mouseout', function(){ this.addClassName(containerClass);this.removeClassName(containerClass+'Hover'); }, false);
		}
	);
		
}


/* Print page function */

PrintPage = new Object();

PrintPage.init = function() {
    if (printEls = $A($$('div.pageFunctions ul li.printPage'))) {
        printEls.each( function(pControl){
            Event.observe(pControl, 'click', function(){ window.print(); }, false);
        });
    }
}

//print page init listener
Event.observe(window, 'load', PrintPage.init, false);

/* Text size page function         *
 * Allow user to set font size via 3 controls: *
 * Base: default, normal size                  *
 * Larger: one size larger                     *
 * Largest: largest size                       *
 * The setting is retained across page loads   *
 * via cookie                                  */

TextSize = new Object();

TextSize.init = function() {
    TextSize.baseFontSize = 1.1;
    TextSize.sizeRatioLarger = 1.2;
    TextSize.sizeRatioLargest = 1.3;
    // Capture current font size. priority order: cookie, document, hardcoded
    if (TextSize.getCookie('TextSize')) {
        TextSize.originalFontSize = TextSize.getCookie('TextSize');
    } else if (MainEls = $A($$('div.main'))) {
        if (MainEls.length > 0) {
            MainEls.each( function(main){
                if (cSize = main.style.fontSize) {
                    TextSize.originalFontSize = cSize;
                } else {
                    TextSize.originalFontSize = TextSize.baseFontSize + 'em';
                }
            });
        } else {
            TextSize.originalFontSize = TextSize.baseFontSize + 'em';
        }
    }
    TextSize.currentFontSize = TextSize.originalFontSize;

    // Has sizing been set over base size? (cookie)
    // If so, adjust size to proper ratio
    var currentSize = TextSize.currentFontSize.replace(/[A-Za-z%]+/,'');
    if (currentSize > (TextSize.baseFontSize * TextSize.sizeRatioLarger)) { // size setting is largest, so override doc default
        TextSize.changeSize(TextSize.sizeRatioLargest, $('controlTextLargest'));
    } else if (currentSize > TextSize.baseFontSize) { // size setting is larger than doc default, so override it
        TextSize.changeSize(TextSize.sizeRatioLarger, $('controlTextLarger'));
    } else { // set default
        TextSize.revertSize($('controlTextBase'));
    }

    // attach event handlers
    if (controlBase = $('controlTextBase')) {
        Event.observe(controlBase, 'mouseover', function(){ this.addClassName('hover'); }, false);
        Event.observe(controlBase, 'mouseout', function(){ this.removeClassName('hover'); }, false);
        Event.observe(controlBase, 'click', function(controlBase){ return function(){TextSize.revertSize(controlBase);} }(controlBase), false);
    }
    if (controlLg = $('controlTextLarger')) {
        var sizeRatio = TextSize.sizeRatioLarger;
        Event.observe(controlLg, 'mouseover', function(){ this.addClassName('hover'); }, false);
        Event.observe(controlLg, 'mouseout', function(){ this.removeClassName('hover'); }, false);
        Event.observe(controlLg, 'click', function(sizeRatio,controlLg){ return function(){
            TextSize.changeSize(sizeRatio,this);
         } }(sizeRatio,controlLg), false);
    }
    if (controlLgst = $('controlTextLargest')) {
        var sizeRatio = TextSize.sizeRatioLargest;
        Event.observe(controlLgst, 'mouseover', function(){ this.addClassName('hover'); }, false);
        Event.observe(controlLgst, 'mouseout', function(){ this.removeClassName('hover'); }, false);
        Event.observe(controlLgst, 'click', function(sizeRatio,controlLgst){ return function(){
            TextSize.changeSize(sizeRatio,this);
         } }(sizeRatio,controlLgst), false);
    }
}

TextSize.changeSize = function(sizeRatio,control) {
    // Set controls to inactive state
    TextSize.inactivateControls();
    // Activate current control
    if (!(control === undefined) && !(control === null) && !(control == '')){
        control.addClassName('active');
    }
    // Change font size to new ratio
    if (MainEls = $$('div.main')) {
        MainEls.each( function(main){
            var toSize = parseFloat(TextSize.baseFontSize) * parseFloat(sizeRatio);
            toSize = toSize + 'em';
            main.style.fontSize = toSize;
            TextSize.currentFontSize = toSize;
            TextSize.setCookie('TextSize', toSize);
        });
    }
}

TextSize.revertSize = function(sourceControl) {
    // Set controls to inactive state
    TextSize.inactivateControls();
    // Activate current control
    if (!(sourceControl === undefined) && !(sourceControl === null) && !(sourceControl == '')){
        sourceControl.addClassName('active');
    }
    // Revert font size to base font size
    if (MainEls = $$('div.main')) {
        MainEls.each( function(main){
            var revertTo = TextSize.baseFontSize + 'em';
            main.style.fontSize = revertTo;
            TextSize.currentFontSize = revertTo; 
            TextSize.setCookie('TextSize', revertTo);
        });
    }
}

TextSize.inactivateControls = function() {
    if (controls = $A($$('div.main div.pageBody div.rightColumn div.pageFunctions ul li.textSize span'))) {
        controls.each( function(el){
            el.removeClassName('active');
        });
    }
}


// Heinle's function for retrieving a cookie
TextSize.getCookie = function (name){
    var cname = name + "=";
    var dc = document.cookie;
    if (dc.length > 0) {
        begin = dc.indexOf(cname);
        if (begin != -1) {
            begin += cname.length;
            end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        }
    }
    return null;
}

// Adaptation of Dorcht's function for setting a cookie
TextSize.setCookie = function (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    ((secure == null) ? "" : "; secure");
}

// TextSize init listener
Event.observe(document, 'dom:loaded', TextSize.init, false);


