/* Custom codes to fix 508 issue to override DK CallToAction() function in Common.js */
function CallToAction(ArgumentsObj) {
    /* Arg defaults */
    var NewArguments = { DOMReference : false };
    /* Replace default args */
    for (var argName in ArgumentsObj) {
       NewArguments[argName] = ArgumentsObj[argName];
    }
    /* Set Obj properties from args */
    this.DOMObject = NewArguments.DOMReference;

    /* More Obj Properties */
    this.containerObject = this.DOMObject.ancestors()[1];
    this.containerClass = this.containerObject.className.match(/^\w+/);

    this.doHover = function() {
        this.containerObject.addClassName(this.containerClass+'Hover');
        this.containerObject.removeClassName(this.containerClass);
        this.containerObject.style.cursor = 'pointer'; 
    }

    this.undoHover= function() {
        this.containerObject.addClassName(this.containerClass);
        this.containerObject.removeClassName(this.containerClass+'Hover'); 
    }

    this.handleClick = function() {
        /* Inherit HTML target attribute behaviors */
        if (this.target && (this.target === "_new" || this.target === "_blank")) {
            /* Create a new window for the link */
            var newWindowName = "callToActionDestination"+this.DOMObject.id;
            var newWindow=window.open(this.href,newWindowName);
        } else {
            /* Link to same window */
            window.location = this.href; 
       }
    }

    this.disableNativeAnchor = function() {
        /* Copy DOM Obj properties into JS Obj and  
           then clear them so only JS will handle them */
        this.href = this.DOMObject.href;
        //this.DOMObject.removeAttribute("href");
        this.target = this.DOMObject.target;
        //this.DOMObject.removeAttribute("target");
    }

    this.setupEvents = function() {
        var CTAObj = this;
        Event.observe(this.containerObject, 'click', function(){ CTAObj.handleClick();return false; }, false);
        Event.observe(this.containerObject, 'mouseover', function(){ CTAObj.doHover(); }, false);
        Event.observe(this.containerObject, 'mouseout', function(){ CTAObj.undoHover(); }, false);
    }

    this.disableNativeAnchor();
    this.setupEvents();
    return true;
}


/* Custom codes to override DK  callToAction_init() function in Common.js */

function callToAction_init() {
	/*
	Add hover and click state to containers with
    anchors inside them, 2nd level down, aka
    dl > dt > a OR div > h6 > a
	____________________________________________*/
	
    /* Setup and Config */
    var CallToActionBoxes = new Array();
    var DOMIDUtil = new DOMObjectsIDCollector();

    /* Input as CSS selectors for use with prototype.js */
    var cssStyleSelectors = new Array('dl.callToActionWide a','dl.callToActionMed a','dl.callToAction a','dl.callToActionTiny a','dl.retireeSolutions a','dl.healthRecords a','dl.behavioralBenefits a','dl.providerPair a','dl.readingBanner a','dl.laptopSideBanner a','dl.medChartBanner a','dl.ourOfferings a','dl.ourTeam a','dl.emailBanner a','dl.news a','dl.docList a','dl.miniBanner a','dl.doctorsBanner a','dl.newPlans a','dl.msaPlans a','dl.healthTerms a','dl.careGivers a','dl.mailRequest a','dl.navBox a','dl.planningCallout a','dl.nationalExercise a','dl.portalDemo a','dl.enrollment a','dl.serviceArea a','dl.requestMore a');

    /* Loop over the CSS selector strings */
    for(var i=0, l = cssStyleSelectors.length; i < l; i++){

        /* Create an array of prototype.js DOM objects */
        var DOMObjects = $A($$(cssStyleSelectors[i]));

        /* reset a uniqueness util used in following loop */
        DOMIDUtil.reset();

        /* Iterate over each Obj / layout element */
        DOMObjects.each( function(obj,index){

            /* We only need the first anchor in each Call to Action box 
               Determine this carefully since collecting prototype DOM Objs 
               by CSS selector can group them from separate layout boxes into
               this loop. Instead of iteration order, use parent object's id */
            var parentObj = obj.ancestors()[1];
            var parentID = parentObj.identify();
            
            /* Add parentID to collection */
            DOMIDUtil.push(parentID);

            /* Is parentID for this layout obj unique? */
            if (DOMIDUtil.isIDUnique(obj.id)) {
                /* Enhance it, create a new Call To Action JS Obj */
                var newCall = new CallToAction({ DOMReference:obj });
                CallToActionBoxes.push(newCall);
            } else {
                /* It's in a box that's already activated, so don't create a duplicate 
                   Call To Action JS Obj but still disable the native HTML anchor behavior */
               // obj.removeAttribute("href");
               // obj.removeAttribute("target");
            }
        });
    }

    /* unset DOMIDUtil */
    DOMIDUtil.destroy();
}
Event.observe(window, 'load', callToAction_init, false);