/* 
 * IMPORTANT
 *
 * This file has been minified using yuicompressor v.2.3.6, and put into the template JS-MIN in the CMS.
 * Changes should be made to this file, then minified and then both versions should be copied into
 * the CMS. The CMS should use the minified version.
 */
function initContent() {
    initCorners();
    initTables();
    initExpandableLists();
    initStepByStepLists();
    new PopupLinkMgr();
}
// execute initContent on DOM ready
YAHOO.util.Event.onDOMReady(initContent);
function initCorners() {
    var featureBoxes = YAHOO.util.Dom.getElementsByClassName("feature_box_1", "div", "content_container");
    var cornerClasses = ["top_left_corner", "top_right_corner", "bottom_left_corner", "bottom_right_corner"];
    
    var tempDiv;
    var borderTopWidth, borderRightWidth, borderBottomWidth, borderLeftWidth;
    var offsetTop, offsetRight, offsetBottom, offsetLeft;
        
    for(var i = 0; i < featureBoxes.length; i++) {
        for(var j = 0; j < cornerClasses.length; j++) {
            tempDiv = document.createElement("div");
            tempDiv.className = cornerClasses[j];                                
            tempDiv.innerHTML = "&nbsp;";
            featureBoxes[i].appendChild(tempDiv);
            borderTopWidth = parseInt(YAHOO.util.Dom.getStyle(featureBoxes[i], "border-top-width"));
            borderRightWidth = parseInt(YAHOO.util.Dom.getStyle(featureBoxes[i], "border-right-width"));
            borderBottomWidth = parseInt(YAHOO.util.Dom.getStyle(featureBoxes[i], "border-bottom-width"));
            borderLeftWidth = parseInt(YAHOO.util.Dom.getStyle(featureBoxes[i], "border-left-width"));
            if( tempDiv.className.indexOf("right") != -1 ) {                
                offsetLeft = featureBoxes[i].offsetWidth - tempDiv.offsetWidth - borderLeftWidth;
                
                if( !(tempDiv.offsetLeft == offsetLeft || tempDiv.offsetLeft == offsetLeft + borderLeftWidth) ) {
                    offsetRight = 0 - (offsetLeft - tempDiv.offsetLeft + borderRightWidth);
                    tempDiv.style.right = offsetRight + "px";                    
                } 
            }
            
            if( tempDiv.className.indexOf("bottom") != -1 ) {    
                offsetTop = featureBoxes[i].offsetHeight - tempDiv.offsetHeight - borderTopWidth;
                if( !(tempDiv.offsetTop == offsetTop || tempDiv.offsetTop == offsetTop + borderTopWidth) ) {
                    offsetBottom = 0 - (offsetTop - tempDiv.offsetTop + borderBottomWidth);
                    tempDiv.style.bottom = offsetBottom + "px";                    
                }                 
                                
            }
                                
        }
    }
    
    var featureBoxes = YAHOO.util.Dom.getElementsByClassName("feature_box_2", "div", "content_container");
    var cornerClasses = ["top_left_corner", "top_right_corner"];
    
    var tempDiv;
        
    for(var i = 0; i < featureBoxes.length; i++) {
        YAHOO.util.Dom.setStyle(featureBoxes[i], "position", "relative");
        for(var j = 0; j < cornerClasses.length; j++) {
            tempDiv = document.createElement("div");
            tempDiv.className = cornerClasses[j];                                
            tempDiv.innerHTML = "&nbsp;";
            featureBoxes[i].appendChild(tempDiv);
        
            if( tempDiv.className.indexOf("right") != -1 ) {                
                offsetLeft = featureBoxes[i].offsetWidth - tempDiv.offsetWidth;
                
                if( tempDiv.offsetLeft != offsetLeft ) {
                    offsetRight = 0 - (offsetLeft - tempDiv.offsetLeft);
                    tempDiv.style.right = offsetRight + "px";                    
                } 
            }
            
        }
    }    
}
/*
 * add the class alternate to odd rows in the tbodies of tables
 * with specified classes if browser does not support nth-child
 * pseudo class
 */
function initTables() {
    // opera 9.5, safari 3.1, and konquerer support the nth-child pseudo-class,
    // so no need to add styles        
    if( YAHOO.env.ua.opera >= 9.5 || YAHOO.env.ua.webkit >= 525.13 || YAHOO.env.ua.webkit == 1 ) {
        return;
    }
    
    // run init on tables with these classes    
    var tableClasses = ["simple", "contacts", "bulletin", "form"];
    
    var tables = [];
    for(var i = 0; i < tableClasses.length; i++) {
        tables = tables.concat(YAHOO.util.Dom.getElementsByClassName(tableClasses[i], "table"));
    }
            
    var i, firstRow = 0;            
    for(var i = 0; i < tables.length; i++) {
        if (YAHOO.util.Dom.hasClass(tables[i], "stock_price_history")
                || YAHOO.util.Dom.hasClass(tables[i], "investment_calculator")
                || YAHOO.util.Dom.hasClass(tables[i], "stock_quote")) {
            firstRow = 1;
        }
        else {
            firstRow = 0;
        }
        
        for(var j = 0; j < tables[i].tBodies.length; j++) {
            for(var k = firstRow; k < tables[i].tBodies[j].rows.length; k += 2) {
                YAHOO.util.Dom.addClass(tables[i].tBodies[j].rows[k], "alternate");
            }
        }
    }    
}
function initExpandableLists() {
    var lists = YAHOO.util.Dom.getElementsByClassName("expandable", "dl");
    for(var i = 0; i < lists.length; i++) {
        new ExpandableList(lists[i]);
    }
}
function initStepByStepLists() {
    var lists = YAHOO.util.Dom.getElementsByClassName("step_by_step", "ol");
    for(var i = 0; i < lists.length; i++) {
        new StepByStepList(lists[i]);
    }
}
/**
 * initialize javascript popup links
 */
var PopupLinkMgr = function() {
    this.init();
}
PopupLinkMgr.prototype = {
    init: function() {    
        this.initLinks();
    },
        
    initLinks: function() {
        var sFunction = function(e) {
            return e.nodeName == "A" || e.nodeName == "AREA";
        };
    
        var bFunction = function(link) {
            LinkFactory().getLink(link);
        };
    
        YAHOO.util.Dom.getElementsBy(sFunction, null, null, bFunction);        
    }        
    
}
/* end javascript popup links */
var ExpandableList = function(el) {
    this.init(el);
}
ExpandableList.prototype = {
    
    el: null,
    sections: null,
    
    init: function(el) {
        this.el = YAHOO.util.Dom.get(el);
        if (!this.el) {
            return;
        }
      
        this.initSections();        
    },
    
    initSections: function() {
        this.sections = [];
        
        var i, sections = this.el.getElementsByTagName("DT");
        for(i = 0; i < sections.length; i++) {
            this.sections[i] = new ExpandableContentSection(sections[i], (i % 2 != 0));                
            if (self.location.hash == "#all" || self.location.hash.substr(1) == sections[i].id.substr(sections[i].id.lastIndexOf("_") + 1)) {
                this.sections[i].open();
            }
        }    
    }
    
}
var ExpandableContentSection = function(el, isAlternate) {
    this.init(el, isAlternate);
}
ExpandableContentSection.prototype  = {
    
    titleEl: null,
    bodyEl: null,
    isOpen: true,
    
    init: function(el, isAlternate) {
        this.titleEl = YAHOO.util.Dom.get(el);
        this.bodyEl = YAHOO.util.Dom.getNextSiblingBy(this.titleEl, function(node) {
            return node.nodeName == "DD";
        });
        
        if (!(this.titleEl && this.bodyEl)) {
            return;
        }
                
        this.initTitleEl(isAlternate);
        this.initBodyEl();
        this.close();
        
        var hash = self.location.hash.substr(1);
        var id = this.titleEl.id.substr(this.titleEl.id.lastIndexOf("_") + 1);
        if (hash == "all" || (hash != "" && hash == id)) {
            this.open();
        }            
    },
    
    initTitleEl: function(isAlternate) {
        if (isAlternate) {
            YAHOO.util.Dom.addClass(this.titleEl, "alternate");
        }                
        
        var linkEl = document.createElement("a");
        YAHOO.util.Dom.addClass(linkEl, "hide");
        linkEl.href = "javascript:void(0);";
        linkEl.innerHTML = lang.hideTxt;    
        
        YAHOO.util.Event.on(this.titleEl, "click", function() {
            this.toggle();
        }, this, true);
        this.titleEl.insertBefore(linkEl, this.titleEl.childNodes[0]);
    },
    
    initBodyEl: function() {
        var divEl = document.createElement("div");
        YAHOO.util.Dom.addClass(divEl, "hide");
        this.bodyEl.appendChild(divEl);
        
        var linkEl = document.createElement("a");
        YAHOO.util.Dom.addClass(linkEl, "hide");
        linkEl.href = "javascript:void(0);";
        linkEl.innerHTML = lang.hideTxt;
        YAHOO.util.Event.on(linkEl, "click", function() {
            this.toggle();
        }, this, true);
        divEl.appendChild(linkEl);       
    },
    
    toggle: function() {
        if (this.isOpen) {
            this.close();
        }
        else {
            this.open();
        }
    },
    
    open: function() {
        var a = this.titleEl.getElementsByTagName("a")[0];
        a.className = "hide";
        a.innerHTML = lang.hideTxt;        
        if (YAHOO.util.Dom.hasClass(this.titleEl, "alternate")) {
            YAHOO.util.Dom.removeClass(this.titleEl, "alternate_closed");
        }
        else {
            YAHOO.util.Dom.removeClass(this.titleEl, "closed");    
        }
        YAHOO.util.Dom.removeClass(this.bodyEl, "closed");    
        this.isOpen = true;
    },
    
    close: function() {
        var a = this.titleEl.getElementsByTagName("a")[0];
        a.className = "show";
        a.innerHTML = lang.showTxt;        
        if (YAHOO.util.Dom.hasClass(this.titleEl, "alternate")) {
            YAHOO.util.Dom.addClass(this.titleEl, "alternate_closed");
        }
        else {
            YAHOO.util.Dom.addClass(this.titleEl, "closed");    
        }
        YAHOO.util.Dom.addClass(this.bodyEl, "closed");    
        this.isOpen = false;        
    }
    
}
var StepByStepList = function(el) {
    this.init(el);
}
StepByStepList.prototype = {
        
    el: null,
    sections: null,
        
    init: function(el) {
        this.el = YAHOO.util.Dom.get(el);
        if( YAHOO.util.Dom.hasClass(this.el, "js") ) {
            return;
        }
        
        YAHOO.util.Dom.addClass(this.el, "js");
                        
        var i, sections = YAHOO.util.Dom.getChildrenBy(this.el, function(node) {
            return node.nodeName == "LI";
        });
        
        this.sections = [];
        for(var i = 0; i < sections.length; i++) {
            this.sections[i] = new StepByStepListSection(sections[i], i + 1);
        }
    }
    
}
var StepByStepListSection = function(el, ordinal) {
    this.init(el, ordinal);
}
StepByStepListSection.prototype = {
    isOpen: false,
    init: function(el, ordinal) {
        this.el = YAHOO.util.Dom.get(el);
        if (!this.el) {
            return;
        }
        
        YAHOO.util.Dom.addClass(this.el, "closed");
        if( ordinal % 2 == 0 ) {
            YAHOO.util.Dom.addClass(this.el, "alternate");
        }
        
        this.bodyEl = YAHOO.util.Dom.getElementsByClassName("bd", "div", this.el)[0];
                    
        this.headEl = document.createElement("div");
        YAHOO.util.Dom.addClass(this.headEl, "hd");
        YAHOO.util.Event.on(this.headEl, "click", function() {
            this.toggle();
        }, this, true);
        
        var ordinalEl = document.createElement("span");
        YAHOO.util.Dom.addClass(ordinalEl, "ordinal");
        ordinalEl.innerHTML = ordinal;
        this.headEl.appendChild(ordinalEl);
        
        var linkEl = document.createElement("a");
        YAHOO.util.Dom.addClass(linkEl, "show");
        linkEl.href = "javascript:void(0);";
        linkEl.innerHTML = lang.showTxt;
        this.headEl.appendChild(linkEl);
        
        for(var j = 0; j < this.el.childNodes.length; j++) {
            if( this.el.childNodes[j].nodeName == "H3" ) {
                this.headEl.appendChild(this.el.childNodes[j]);
            }
        }
        
        this.el.insertBefore(this.headEl, this.bodyEl);
        
        var hash = self.location.hash.substr(1);
        var id = this.el.id.substr(this.el.id.lastIndexOf("_") + 1);
        if (hash == "all" || (hash != "" && hash == id)) {
            this.open();
        }        
    },
    
    toggle: function() {
        if (this.isOpen) {
            this.close();
        }
        else {
            this.open();
        }
    },
    
    open: function() {    
        YAHOO.util.Dom.removeClass(this.el, "closed");
        
        var linkEl = this.headEl.getElementsByTagName("a")[0];        
        linkEl.className = "hide";
        linkEl.innerHTML = lang.hideTxt;    
        
        this.isOpen = true;
    },
    
    close: function() {
        YAHOO.util.Dom.addClass(this.el, "closed");
        var linkEl = this.headEl.getElementsByTagName("a")[0];        
        linkEl.className = "show";
        linkEl.innerHTML = lang.showTxt;    
        
        this.isOpen = false;
    }
}
/* function for history section - DO NOT USE */
function windowOpen(mypage, myname, w, h) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+'resizable=yes,scrollbars=yes';
    win = window.open(mypage, myname, winprops)
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
