/** 
 * Easing Equations for Script.aculo.us
 * @author Brian Crescimanno <brian.crescimanno@gmail.com>
 * @version 0.8.1
 * @revised November 20, 2008
 * @copyright 2008 Brian Crescimanno, all rights reserved
 *
 * Released under terms of the BSD License
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * The math for these equations was created by Robert Penner
 * http://www.robertpenner.com/profmx
 * 
 * -----------------------------------------------------------------------------
*/

Effect.Transitions.customExponentialEaseOut = function(pos){
    if (pos==0) return 0;
    if (pos==1) return 1;

    pos+=pos*3;
    return -Math.pow(2, -10 * pos) + 1
}


AC.AnchorPageScroller = Class.create();
Object.extend(AC.AnchorPageScroller.prototype, AC.ViewMaster.Viewer.prototype);
Object.extend(AC.AnchorPageScroller.prototype, {
    triggerClassName: 'scrollToAnchor',

    _triggerClicked: function(evt) {
        var trigger = evt.element();
        if (AC.Detector.isIEStrict() && evt.type === 'mouseup') {
            if (trigger && trigger.nodeName.toLowerCase() === 'a') {
                trigger = trigger.down('.'+this.triggerClassName);
            }
        } else {
            while (trigger && trigger.nodeName.toLowerCase() != 'a' && trigger.nodeName.toLowerCase() != 'body') {
                trigger = trigger.parentNode;
            }
        }

        // ignore if the element is not a trigger
        if (trigger && trigger.href && Element.Methods.hasClassName(trigger, this.triggerClassName)) {
            var parts = trigger.href.split('#');

            if (parts.length === 2) {
                Event.stop(evt);

                this._currentDestination = parts[1];
                var options = {
                    duration: 1.5,
                    transition: Effect.Transitions.customExponentialEaseOut,
                    afterFinish: this.afterScroll.bind(this)
                }
                new Effect.ScrollTo(this._currentDestination, options);

                this.trackClick();
            }
        }
    },

    afterScroll: function() {
        var windowLocationParts = window.location.href.split('#');
        windowLocationParts[1] = this._currentDestination;
        window.location.href = (windowLocationParts[0]+'#'+windowLocationParts[1]);
        delete this._currentDestination;
    },

    sectionWithId: function(initialId) {
        return null;
    },

    trackClick: function() {
        var prop = ' - '+this._currentDestination;
		//var prop = AC.Tracking.pageName()+' - '+this._currentDestination;
        var properties = {
            prop24: prop,
            pageName: ' (US)'
			//pageName: AC.Tracking.pageName()+' (US)'
        }
        //AC.Tracking.trackClick(properties, this, 'o', prop);
    }
});


Event.onDOMReady(function() {
	AC.AnchorPageScroller.defaultAnchorScroller = new AC.AnchorPageScroller();
});
