﻿/*
    Communities Take Root site JavaScript class
    Resource Interactive 2010
*/
CommunitiesTakeRoot = new Class({
    Implements: [Options],
    options: {
},

initialize: function(options) {
    this.setOptions(options);

    // take all external links and attach target and title
    $$('a[rel="external"]').each(function(el) {
        el.setProperty('title', 'link opens in new window');
        el.setProperty('target', '_blank');
    });

    // plantPage check to init map functions
    if ($('plantContainer')) {
        this.PlantPage();
    }

    if ($('learnContainer')) {
        this.LearnPage();
    }

    // tastPage check to init bubble overlays
    if ($('popsicleContainer')) {
        this.TastePage();
    }
    this.googleEventTracking();
},
PlantPage: function() {
    //placeholder
},
LearnPage: function() {
    pageHash = window.location.hash;

    this.activePanel = 0;
    if (pageHash.length > 0) {
        pageHash = pageHash.substr(1, pageHash.length);

        if ($(pageHash)) {
            if (pageHash == 'learn-video') { this.activePanel = 0; }
            if (pageHash == 'fruitful') { this.activePanel = 1; }
            if (pageHash == 'provide') { this.activePanel = 2; }
            if (pageHash == 'partner') { this.activePanel = 3; }
        }
    }

    panels = $('learnContainer').getElements('.panel');
    panels.each(function(pan, index) {
        if (index != this.activePanel) {
            pan.setStyles({
                'opacity': 0,
                'display': 'none'
            });
            panels[index].getElement('.controls').destroy();
        } else {
            pan.setStyles({
                'opacity': 1,
                'display': 'block'
            });
            controls = panels[index].getElement('.controls');
            controls.set({
                'id': 'controls',
                'class': ''
            }).inject($('learnContainer'), 'bottom');

            leftClick = controls.getElement('.leftControl  a');
            rightClick = controls.getElement('.rightControl  a');

            leftClick.addEvent('click', function(evt) {
                evt.stop();
                if (this.activePanel > 0) {
                    this.FadeOutPanel(this.activePanel);
                    this.FadeInPanel(this.activePanel - 1, 'left');
                    this.FireClickTracking(this.activePanel, 'left');
                }
            } .bind(this));

            rightClick.addEvent('click', function(evt) {
                evt.stop();
                if (this.activePanel < (panels.length - 1)) {
                    this.FadeOutPanel(this.activePanel);
                    this.FadeInPanel(this.activePanel + 1, 'right');
                    this.FireClickTracking(this.activePanel, 'right');
                }
            } .bind(this));

        }
    } .bind(this));


},
FireClickTracking: function(index, direction) {
    var eventTrackingCode = '';
    if (direction == 'right') {
        if (index == 1) {
            eventTrackingCode = '/event/learn/fruitful_endeavor/next_item_arrow';
        }
        if (index == 2) {
            eventTrackingCode = '/event/learn/plant_an_orchard/next_item_arrow';
        }
    }
    if (direction == 'left') {
        if (index == 0) {
            eventTrackingCode = '/event/learn/plant_an_orchard/previous_item_arrow';
        }
        if (index == 1) {
            eventTrackingCode = '/event/learn/our_partners/previous_item_arrow';
        }
    }
    if (eventTrackingCode.length > 0) {
        pageTracker._trackEvent(eventTrackingCode, 'click');
    }
},
FadeOutPanel: function(panel) {
    if (Browser.Engine.trident) {
        panels[panel].setStyle('display', 'none');
    } else {
        panels[panel].fade('out').setStyle('display', 'none');
    }
},
FadeInPanel: function(panel, direction) {
    if (Browser.Engine.trident) {
        panels[panel].setStyles({
            'opacity': '1',
            'display': 'block'
        });
    } else {
        panels[panel].fade('in').setStyle('display', 'block');
    }
    this.UpdateActivePanel(panel, direction);
},
UpdateActivePanel: function(index, direction) {

    if (direction == 'right') {
        if (index == 2) {
            this.activePanel = 2;
            $('controls').getElement('.rightControl').addClass('off');
        } else {
            this.activePanel++;
            $('controls').getElement('.rightControl').removeClass('off');
            $('controls').getElement('.leftControl').removeClass('off');
        }
    }
    if (direction == 'left') {
        if (index == 0) {
            this.activePanel = 0;
            $('controls').getElement('.leftControl').addClass('off');
        } else {
            this.activePanel--;
            $('controls').getElement('.leftControl').removeClass('off');
            $('controls').getElement('.rightControl').removeClass('off');
        }
    }

    $('controls').getElement('.pagination img').set({
        'src': '/Images/txt_pag_' + (index + 1) + '.gif',
        'alt': (index + 1) + ' of ' + $('learnContainer').getElements('.panel').length
    });

},
TastePage: function() {
    var products = $(document.body).getElements('.product');
    var bubbles = $(document.body).getElements('.infoBubble');
    bubbles.each(function(bubble) {
        bubble.setStyles({
            'opacity': '0',
            'display': 'block'
        });
    });

    $('popsicleContainer').getElements('.popsicleRow').each(function(pRow, index) {
        pRow.set('id', 'pRow_' + index)
    });
    products.each(function(el, index) {
        bubbles[index].setStyle('top', '-' + (bubbles[index].offsetHeight - 40) + 'px');
        el.addEvents({
            'mouseenter': function() {
                var scroll = $(document.body).getScroll();
                var scrollFx = new Fx.Scroll(window, { wait: false, duration: 500, transition: Fx.Transitions.Quad.easeInOut });
                if (Browser.Engine.trident) {
                    bubbles[index].setStyles({
                        'opacity': '1',
                        'visibility': 'visible'
                    });
                } else {
                    bubbles[index].fade('in');
                }
                if (scroll.y > 399 && (index <= 2)) {
                    //$(document.body).scrollTo(0, 385);
                    scrollFx.toElement('scrollToID');
                }
                if (scroll.y > 540 && (el.getParent().get('id') == 'pRow_1')) {
                    //$(document.body).scrollTo(0, 385);
                    scrollFx.toElement('storeLocatorButton');
                }
            },
            'mouseleave': function() {
                if (Browser.Engine.trident) {
                    bubbles[index].setStyles({
                        'opacity': '0',
                        'visibility': 'hidden'
                    });
                } else {
                    bubbles[index].fade('out');
                }
            }
        });
    });
},
googleEventTracking: function() {
    // crawl the page for any link with tracking attribute of ga-evt-tracking
    // if attribute has value in it, then call ga event tracking
    $$('a:ga-evt-tracking').each(function(el) {
        trackingCode = el.get('ga-evt-tracking');
        if (trackingCode != null && trackingCode != '') {
            el.addEvent('click', function() {
                pageTracker._trackEvent(this.get('ga-evt-tracking'), 'click');
            });
        }
    });
}
});

window.addEvents({
    'domready': function() {
        new CommunitiesTakeRoot();

        var bigNum = '';
        if (Browser.Engine.trident4) {
            bigNum = 'x=' + new Date().getTime();
        }
        var addThisJS = new Element('script', {
            type: 'text/javascript',
            src: 'http://s7.addthis.com/js/250/addthis_widget.js?' + bigNum + '#username=communitiestakeroot'
        });
        $(document.body).adopt(addThisJS);
    }
});
