// JScript File

/*button mouseovers */

function initButtonMouseOvers(){

    elemButtons = YAHOO.util.Dom.getElementsByClassName('Button', 'input');

    for (i=0;i<elemButtons.length;i++){
        YAHOO.util.Event.addListener(elemButtons[i], "mouseout", setMouseOutButtonStyle);
        YAHOO.util.Event.addListener(elemButtons[i], "mouseover", setMouseOverButtonStyle);
    }
}

function setMouseOverButtonStyle(e) { 
    
    //Modify two css classes on the mouse event
    setMainClass(e, this);
    setBackgroundPosition(e, this);
}

function setMouseOutButtonStyle(e) {
    //Modify two css classes on the mouse event 
    setMainClass(e, this);
    setBackgroundPosition(e, this);
}

function setMainClass(e, elem)
{
    var arrMainClass = new Array(1);
    
    //The classes to look for to be replaced
    //Buttons have two colors blue or brown
    arrMainClass[0] = "Blue";
    arrMainClass[1] = "Brown";

    changeClassOnMouseEvent(e, elem, arrMainClass);
}

function setBackgroundPosition(e, elem)
{
    var arrWidths = new Array(2);
    
    //The classes to look for to be replaced
    //Buttons have three sizes
    //if there is another size added, add it to this array
    arrWidths[0] = "Width60";
    arrWidths[1] = "Width120";
    arrWidths[2] = "Width180";
    
    changeClassOnMouseEvent(e, elem, arrWidths);      
}

function changeClassOnMouseEvent(e, elem, arr)
{
    var classToRemove = "";
    var classToAdd = "";
   
    for (i=0;i<arr.length;i++){ 
        classToRemove = arr[i];
        classToAdd = arr[i] + "Hover";
        
        //depending upon the mouse over change the class to Hover or change it back to normal
        if(e.type=="mouseout"){    
            classToRemove = arr[i] + "Hover";
            classToAdd = arr[i];
        }
        
        //If the element has this class replace it
        if (YAHOO.util.Dom.hasClass(elem, classToRemove)){   
            YAHOO.util.Dom.replaceClass(elem, classToRemove, classToAdd);
            return; //quit if the class is found
        }
    }
    
}

/*end of button mouseovers */

function createMenus(){       
    //create all the menus using the YUI

    //render the GlobalNav    
    YAHOO.util.Event.onContentReady("GlobalNavYUI", function () {

        /*
        Instantiate a MenuBar:  The first argument passed to the 
        constructor is the id of the element in the page 
        representing the MenuBar; the second is an object literal 
        of configuration properties.
        */

        var oMenuBar = new YAHOO.widget.MenuBar("GlobalNavYUI", { 
                                                autosubmenudisplay: true, 
                                                hidedelay: 200, 
                                                lazyload: true });

        /*
        Call the "render" method with no arguments since the 
        markup for this MenuBar instance is already exists in 
        the page.
        */

        var aSubmenuData = [

            {
            id: "Topics",
                itemdata: [
                            { text: "Accounting & Financial Reporting", url: "http://www.afponline.org/pub/res/topics/topics_acct.html" },
                            { text: "Bank Relationship Management", url: "http://www.afponline.org/pub/res/topics/topics_brm.html" },
                            { text: "Cash Flow Forecasting", url: "http://www.afponline.org/pub/res/topics/topics_cff.html" },
                            { text: "Corporate Finance", url: "http://www.afponline.org/pub/res/topics/topics_finance.html" },
                            { text: "Financial Software & Technology", url: "http://www.afponline.org/pub/res/topics/topics_tech.html" },
                            { text: "Global Liquidity Management", url: "http://www.afponline.org/pub/res/topics/topics_glm.html" },
                            { text: "Investment & Policies", url: "http://www.afponline.org/pub/res/topics/topics_invest.html" },
                            { text: "Payments", url: "http://www.afponline.org/pub/res/topics/topics_pay.html" },
                            { text: "Pensions & Benefits", url: "http://www.afponline.org/pub/res/topics/topics_pension.html" },
                            { text: "Regulations & Compliance", url: "http://www.afponline.org/pub/res/topics/topics_regulat.html" },
                            { text: "Risk Management", url: "http://www.afponline.org/pub/res/topics/topics_risk.html" },
                            { text: "Treasury Operations", url: "http://www.afponline.org/pub/res/topics/topics_treasury.html" },
                            { text: "Working Capital Management", url: "http://www.afponline.org/pub/res/topics/topics_wcm.html" },
                        ]
            }
            ,
            {
            id: "Conferences",
                itemdata: [
                            { text: "Annual Conference", url: "http://www.AFPonline.org/pub/conf/annual_conference.html" },
							{ text: "AFP of Canada Treasury Management Forum", url: "http://www.afponline.ca/pub/forums/tmf/tmf.html" },
                         ]
            }
																		,
																		{
            id: "Development",
                itemdata: [
                            { text: "By Topic", url: "http://www.afponline.org/pub/ed/master_cal.html" },
                            { text: "Virtual", url: "http://www.afponline.org/pub/ed/ed_marketplace/virtual.html" },
                            { text: "In-Person", url: "http://www.AFPonline.org/pub/ed/ed_marketplace/in-person.html" },
							{ text: "Self Directed", url: "http://www.afponline.org/pub/ed/ed_marketplace/self_study.html" },
                            { text: "Archives", url: "http://www.afponline.org/pub/store/vesarchive.html" },
                            { text: "Onsite and Custom Training", url: "http://www.afponline.org/pub/ed/seminars.html" },
                        ]
            }
																		,
																		{
            id: "Certification",
                itemdata: [
                            { text: "Become a CTP", url: "http://www.afponline.org/pub/cert/value.html" },
							{ text: "How to Register", url: "http://www.afponline.org/pub/cert/register.html" },
                            { text: "Prepare for the Exam", url: "http://www.afponline.org/pub/cert/examprep.html" },
						    { text: "Taking the Exam", url: "http://www.afponline.org/pub/cert/taking.html" },
                            { text: "Recertification", url: "http://www.afponline.org/pub/cert/recert.html" },
							{ text: "Submit Recert Credits", url: "http://www.AFPonline.org/recert" },
							{ text: "Sponsor Program (pdf)", url: "http://www.afponline.org/pub/pdf/CTP_Program_Sponsor_Reg_form.pdf" },
							{ text: "CTP FAQs", url: "http://www.afponline.org/pub/cert/faq.html" },
                        ]
            }
																		,
																		{
            id: "Career",
                itemdata: [
                            { text: "Job Seekers", url: "http://www.afponline.org/pub/cs/online_job_center.html" },
                            { text: "Employers", url: "http://www.afponline.org/pub/cs/jobemp.html" },
                            { text: "Financial Career Coach", url: "http://www.afponline.org/pub/cs/fincarcoach.html" },
							{ text: "Resume Services", url: "http://www.afponline.org/pub/cs/resume_critique_service.html" },
                            { text: "Career Trust", url: "/pub/cs/career_trust_service.html" },
                        ]
            }
																		,
																		{
            id: "SCT",
                itemdata: [
                            { text: "Join SCT", url: "/pub/sct/sct.html" },
																												{ text: "Executive Roundtables", url: "/pub/sct/luncheons.html" },
																												{ text: "SCT Executive Committee", url: "/pub/a/bod/sct_committee.html" },
                        ]
            }
																		,
																		{
            id: "Membership",
                itemdata: [
                            { text: "Member Benefits", url: "/pub/a/value.html" },
                            { text: "Join AFP of Canada", url: "/pub/a/join_afp.html" },
                            { text: "Renew Your Membership", url: "/mbr/mem/renew.html" },
							{ text: "Member Profile", url: "https://eweb.afponline.org/eweb/dynamicpage.aspx?site=afpC&webcode=MyInformation" },
							{ text: "Refer a Member", url: "/mbr/mem/referral.html" },
                            
                        ]
            }
																		,
																		{
            id: "Community",
                itemdata: [
                            { text: "Discussion Boards", url: "http://www.afponline.ca/pub/a/dl_overview.html" },
                            { text: "Member Directory", url: "http://eweb.afponline.org/eweb/DynamicPage.aspx?Site=afpC&WebKey=0d0517eb-5ef3-489c-8d26-5fbbc6d110e4" },
                            { text: "Ask AFP", url: "http://www.afponline.org/askafp" },
							{ text: "LinkedIn Groups", url: "http://www.afponline.ca/pub/network/AFP_CA_linkedin.html" },
							{ text: "Women in Finance", url: "http://www.afponline.org/pub/network/women_in_finance.html" },
							{ text: "AFP Aware", url: "http://www.afponline.org/pub/network/afp_aware.html" },
                            { text: "Blogs", url: "http://blogs.afponline.org" },
                        ]
            }
        ];//close the array


        oMenuBar.subscribe("beforeRender", function () {

        if (this.getRoot() == this) {

            this.getItem(0).cfg.setProperty("submenu", aSubmenuData[0]);
            this.getItem(1).cfg.setProperty("submenu", aSubmenuData[1]);
			this.getItem(2).cfg.setProperty("submenu", aSubmenuData[2]);
			this.getItem(3).cfg.setProperty("submenu", aSubmenuData[3]);
			this.getItem(4).cfg.setProperty("submenu", aSubmenuData[4]);
			this.getItem(5).cfg.setProperty("submenu", aSubmenuData[5]);
			this.getItem(6).cfg.setProperty("submenu", aSubmenuData[6]);
			this.getItem(7).cfg.setProperty("submenu", aSubmenuData[7]);                        
        }

        });


        oMenuBar.render();

    });
    
    //Create the Button menu for the More links in the bandana
    YAHOO.example.init = function () {

        // "click" event handler for each item in the Button's menu

        function onMenuItemClick(p_sType, p_aArgs, p_oItem) {

            MoreLinks.set("label", p_oItem.cfg.getProperty("text"));
                            
        }

        //  Create an array of YAHOO.widget.MenuItem configuration properties

        var aMoreLinksMenu = [

		   { text: "Corporate Treasurers Council", value: 1, url: "http://www.corporatetreasurers.org", onclick: { fn: onMenuItemClick } },
		   { text: "Asia Pacific Forum", value: 2, url: "http://www.asiapacificforum.com", onclick: { fn: onMenuItemClick } },
  		   { text: "CIEBA", value: 3, url: "http://www.afponline.org/pub/cieba/cieba1.html", onclick: { fn: onMenuItemClick } }       
        ];

        /*
            Instantiate a Menu Button using the array of YAHOO.widget.MenuItem 
            configuration properties as the value for the "menu" configuration 
            attribute.
        */
        
        var MoreLinks = new YAHOO.widget.Button({ type: "menu", label: "More", name: "mymenubutton", menu: aMoreLinksMenu, container: "MoreContainer" });            

    } ();
    

            //Create the Button menu for the More links in the bandana
    YAHOO.example.init = function () {

        // "click" event handler for each item in the Button's menu

        function onMenuItemClick(p_sType, p_aArgs, p_oItem) {

            QuickLinks.set("label", p_oItem.cfg.getProperty("text"));
                            
        }

        //  Create an array of YAHOO.widget.MenuItem configuration properties

        var aQuickLinksMenu = [

            { text: "Join/Renew", value: 1, url: "http://www.afponline.ca/pub/a/join_afp.html", onclick: { fn: onMenuItemClick } },
			{ text: "Country Profiles", value: 2, url: "http://www.afponline.org/pub/country/profiles.html", onclick: { fn: onMenuItemClick } },
			{ text: "Key Rates", value: 3, url: "http://www.afponline.ca/sp/rates/key_rates_ca.html", onclick: { fn: onMenuItemClick } },
			{ text: "Newsletters", value: 4, url: "http://www.afponline.org/pub/store/periodicals.html", onclick: { fn: onMenuItemClick } },
			{ text: "Quizzes", value: 5, url: "http://www.afponline.org/pub/cert/ctp_quiz.html", onclick: { fn: onMenuItemClick } },			
			{ text: "Research", value: 6, url: "http://www.afponline.org/pub/res/research/research_reports.html", onclick: { fn: onMenuItemClick } },
			{ text: "RSS News Feeds", value: 7, url: "http://www.afponline.org/pub/res/rss/afp_newsfeed.html", onclick: { fn: onMenuItemClick } },
			{ text: "Submit Recert Credits", value: 8, url: "http://www.AFPonline.org/recert", onclick: { fn: onMenuItemClick } },
			{ text: "Service Codes", value: 9, url: "http://www.afponline.org/pub/sc/srvc.html", onclick: { fn: onMenuItemClick } },
			{ text: "Bank Pricing Data", value: 10, url: "http://www.afponline.org/pub/res/brm/bpd/bpd.html", onclick: { fn: onMenuItemClick } },
			{ text: "Videos/Podcasts", value: 11, url: "http://www.afponline.org/mbr/multimedia/videos.html", onclick: { fn: onMenuItemClick } },
            { text: "Webinars", value: 12, url: "http://www.AFPonline.org/pub/store/ves.html", onclick: { fn: onMenuItemClick } }
			        ];

        /*
            Instantiate a Menu Button using the array of YAHOO.widget.MenuItem 
            configuration properties as the value for the "menu" configuration 
            attribute.
        */
        
        var QuickLinks = new YAHOO.widget.Button({ type: "menu", label: "Quick Links", name: "QuickLinksmenubutton", menu: aQuickLinksMenu, container: "QuickLinks" });            

    } ();



}

/******* Carousel*******/
/** adapted from http://billwscott.com/carousel/ *****/


/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'mycarousel'.) See the
 * HTML code below.
 **/
var carousel; // carousel component
var autoPlayStatus //Sets if autoplay is on or not
var autoPlayTiming = 15000; //Time for the panel rotation 3000 = 3 seconds

var createCarousel = function() {
    
    //creates and inits the carousel
    carousel = new YAHOO.extension.Carousel("mycarousel", 
        {
            numVisible:        1,
            animationSpeed:    0.5,
            animationMethod:   YAHOO.util.Easing.easeOutStrong, 
            scrollInc:         1,
            navMargin:         0,
            prevElement:     "prev-arrow",
            nextElement:     "next-arrow",
            autoPlay:           autoPlayTiming,
            wrap:               true,
            size:              5,
            loadNextHandler:   loadNextItems,
            loadPrevHandler:   loadNextItems
        }
    );

    YAHOO.util.Event.addListener(this.carouselNext, "click", this._scrollNext, this);

    addEventListenersToTabs();
    addEventListenersToPrevNex();
}

function addEventListenersToTabs(){
    elemTabs = getElemTabs();
    
    //got through all tabs and attache events
    for (i=0;i<elemTabs.length;i++){
        iTabToSet = i + 1 //adjust for a zero based index
        iTabToSetString = "tab" +iTabToSet +''; //sets the id of tab to attach an event to
                  
        YAHOO.util.Event.addListener(iTabToSetString, "click", changePage, [carousel, iTabToSet]);
        YAHOO.util.Event.addListener(iTabToSetString, "mouseover", setMouseOver, [iTabToSet]);
        YAHOO.util.Event.addListener(iTabToSetString, "mouseout", setMouseOver, [iTabToSet]);
    }
}

function addEventListenersToPrevNex(){
   YAHOO.util.Event.addListener("prev-arrow", "mouseover", setMouseOverPrevNext);
   YAHOO.util.Event.addListener("next-arrow", "mouseover", setMouseOverPrevNext);
   YAHOO.util.Event.addListener("prev-arrow", "mouseout", setMouseOverPrevNext);
   YAHOO.util.Event.addListener("next-arrow", "mouseout", setMouseOverPrevNext);
}


var changePage = function(e, args) {
    var carousel = args[0];
    var pageNum = args[1];
    
    carousel.scrollTo(pageNum);
    
    setActiveTab(pageNum);   

};

var loadNextItems = function(e, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	setActiveTab(start);
};

/**
 * start and stop autoplay
 */
var stopStartAutoPlay = function(e) {

    var elemAutoPlay = YAHOO.util.Dom.get("AutoPlay")

    if (carousel.getProperty("autoPlay")>0){
	    elemAutoPlay.innerHTML = "AUTO PLAY<span>OFF</span>";
	    YAHOO.util.Dom.addClass(elemAutoPlay, "AutoPlayOff");
	    carousel.stopAutoPlay();
        carousel.setProperty("autoPlay", 0) //set it to zero so it can be tested 
    }else{
	    elemAutoPlay.innerHTML = "AUTO PLAY<span>ON</span>";
	    carousel.startAutoPlay(autoPlayTiming);
	    YAHOO.util.Dom.removeClass(elemAutoPlay, "AutoPlayOff");
	}

};

function setActiveTab(iActiveTab){
    iActiveTab = iActiveTab -1 //adjust for a zero based index
    
    elemTabs = getElemTabs();
    
    //turn them all off
    for (i=0;i<elemTabs.length;i++){        
        YAHOO.util.Dom.removeClass(elemTabs[i], "Selected");
    }
    
    //remove the mouse over class
    YAHOO.util.Dom.removeClass(elemTabs[iActiveTab], "MouseOver");
    
    //now turn on the selected one
    YAHOO.util.Dom.addClass(elemTabs[iActiveTab], "Selected");
}

function setMouseOver(e, myid){

    elemTabs = getElemTabs();
    
    iActiveTab = myid -1 //adjust for a zero based index

    if(e.type=="mouseover"){    
        //now turn on the selected one
        if (!YAHOO.util.Dom.hasClass(elemTabs[iActiveTab], "Selected")){
            YAHOO.util.Dom.addClass(elemTabs[iActiveTab], "MouseOver");
        }
    }else{
        YAHOO.util.Dom.removeClass(elemTabs[iActiveTab], "MouseOver");
    }
}

function setMouseOverPrevNext(e){

    if(e.type=="mouseover"){    
        //now turn on the selected one        
        YAHOO.util.Dom.addClass(this, "MouseOver" + this.id);       
    }else{
        YAHOO.util.Dom.removeClass(this, "MouseOver" + this.id);
    }
}


function getElemTabs(){
      //get the li tabs
    elemTabs = YAHOO.util.Dom.getElementsByClassName("Tab", "li", YAHOO.util.Dom.get("carousel-component-pager"))    
    return elemTabs;  
}


/******* End of Carousel *******/


/******* Set Article Font-size *******/
function setFontSizers(){
   YAHOO.util.Event.addListener("MakeSmaller", "click", changeFontSize);
   YAHOO.util.Event.addListener("MakeLarger", "click", changeFontSize);
}

function changeFontSize(e){
    var amountToChange = 0;
    var FontStyleEnding = "%";
    
    var elemArticle = YAHOO.util.Dom.get("ArticleContent");
    
    var currentFontSize = YAHOO.util.Dom.getStyle(elemArticle, "font-size");
    
    //IE was a little odd with the percents, so if it is set at 75% which is the default for the site, start this code at 100%
    
    if (currentFontSize == "75%"){
        currentFontSize = "100%";       
    }
    
    //parse out the percent sign and px to get the numeric value
    CurrentFontSizeAsInteger = getCurrentFontSizeAsInteger(currentFontSize);
    
    
    
    //parse out the percent sign and px to get the numeric value
    CurrentFontSizeAsInteger = getCurrentFontSizeAsInteger(currentFontSize);
    
    //check if the browser is setting the size as a percent or an absolute value
    if (currentFontSize.indexOf("%", 0) > -1){
        
        //set the limit as to how big or small the font can be
        if (CurrentFontSizeAsInteger > 130 && this.id == 'MakeLarger'){return}
        if (CurrentFontSizeAsInteger < 80 && this.id == 'MakeSmaller'){return}
    
        amountToChange = "10";        
    }else{
        
        //set the limit as to how big or small the font can be
        if (CurrentFontSizeAsInteger > 18 && this.id == 'MakeLarger'){return}
        if (CurrentFontSizeAsInteger < 10 && this.id == 'MakeSmaller'){return}       
        
        amountToChange = "1";
        FontStyleEnding = "px";
    }
    
    //make bigger or small depending on what was clicked
    if (this.id == 'MakeSmaller'){
        amountToChange = "-" + amountToChange;
    }
    
    var newFontSize = Number(CurrentFontSizeAsInteger) + Number(amountToChange);
    
    var newFontStyle = String(newFontSize) + FontStyleEnding;
    
    YAHOO.util.Dom.setStyle(elemArticle, "font-size", newFontStyle);
         
}

function getCurrentFontSizeAsInteger(currentFontSize){
    var FontSizeAsInteger
    
    FontSizeAsInteger = currentFontSize.replace(/px/, "");
    return FontSizeAsInteger.replace(/%/, "");
}

/******* End of Set Article Font-size *******/

/******* Set Article Print Link to print *******/


function setPrintOnClick(){
//add the print event to each li with a PrintVersion class	
   YAHOO.util.Event.addListener(YAHOO.util.Dom.getElementsByClassName("PrintVersion", "li"), "click", callPrintWindow);
}

function callPrintWindow(){
    window.print();
}

/******* End of Article Print *******/

/******* Multimedia Playlist *******/

//if this came from another page then click the created node

    //this will check if the user has selected a video from another page to watch and load it first
function loadSelectedVideo(){
    if(videoQS!='empty'){
        $(function(){    
            
            //find the matching node for the selected video file                
            var matchSelected = $("a[href$='" + videoQS + "']:first")                
            var matchSelectedClone = matchSelected.clone();
            
            //add it to the playlist, but hid the html from view
            matchSelectedClone.prependTo($("#playlist"));
            matchSelectedClone.css("display", "block");
            
        });
    }        
    
    //creates the playlist for the video files    
    $(function() {
                 
        $("#playlist").playlist( 
         
            // flash configuration (required) 
             
            {src:"../flash/FlowPlayerLight.swf",bgcolor:'#009dff',height:"290",width:"400"},                
            // flowplayer configuration (optional) 
            {initialScale:'scale'},  
             
            // playlist configuration (optional) 
            {player:'#playerContainer', loop:true, autoPlay:true} 
        ); 
    });
}

function startSelectedVideo(){
    if(videoQS!='empty'){
        $(function(){
            //click the hidden node    
            $("#playlist > a:first").click();
            
            //now display the html for the selected video below the player
             var matchSelectedP = $("a[href$='" + videoQS + "']:first p")
             $("#CurrentVideoPlaying").html(matchSelectedP.html())
      
        });       
    }
}

//parse the query string
function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
    
    return "empty";
}



/******* End of Multimedia Playlist *******/
