﻿var DMPF;
document.observe('dom:loaded', function() { window.setTimeout('DMPF = new DigitalMarketingPopoutForms()', 1); });
var DigitalMarketingPopoutForms = Class.create({
    initialize: function() {
        this.emptyClass = 'digihp_right_box';
        this.highlightedClass = 'digihp_right_box_highlight';
        this.PopupElementClass = 'digihp_pop';
        this.EffectQueue = new EffectQueue();
        this.LoadTabs();
        this.LoadPopups();
        this.addEventHandlers();

    },
    PopupElements: [],
    LoadPopups: function() {
        var popups = this.getElementsByClassName('div', this.PopupElementClass);

        for (var i = 0; i < popups.length; i++) {
            var relative = popups[i].getAttribute('rel');

            for (var x = 0; x < this.Tabs.length; x++) {
                if (this.Tabs[x].getAttribute('rel') == relative) {
                    this.PopupElements.push(new DigitalMarketingPopoutTabs(popups[i], this.Tabs[x]));
                }
            }
        }
    },
    getElementsByClassName: function(strElementType, strClassName) {
        var _returner = new Array();
        var domElements = document.getElementsByTagName(strElementType);
        for (var i = 0; i < domElements.length; i++) {
            if (domElements[i].className == strClassName) {
                _returner.push(domElements[i]);
            }
        }

        return _returner;
    },
    LoadTabs: function() {
        this.Tabs = this.getElementsByClassName('div', this.emptyClass);
        this.SelectedTabs = this.getElementsByClassName('div', this.highlightedClass);
    },
    addEventHandlers: function() {
        //document.observe('mouseover', (function(event) { this.highlightTab(event) }).bind(this));
        document.observe('click', (function(event) { this.showPopup(event) }).bind(this));
    },
    getElement: function(event) {
        var clicker;
        if (Prototype.Browser.IsIE4up()) {
            clicker = $(Event.findElement(event));
        } else {
            clicker = $(Event.findElement(event)).next();
        }
        for (var i = 0; i < this.Tabs.length; i++) {
            if (clicker == this.Tabs[i]) {
                return (clicker);
            }
        }
        return null;
    },
    highlightTab: function(event) {
        var oElement = this.getElement(event);
        if (oElement != null) {
            oElement.className = this.highlightedClass;
            var SelectedArray;
            if (Prototype.Browser.IsIE4up()) {
                SelectedArray = this.Tabs;
            } else {
                SelectedArray = this.SelectedTabs;
            }
            for (var i = 0; i < SelectedArray.length; i++) {
                if (oElement != SelectedArray[i]) {
                    this.unselectTab(SelectedArray[i]);
                }
            }
        }
    },
    unselectTab: function(oElement) {
        if (oElement != null) {
            oElement.className = this.emptyClass;
        }
    },
    showPopup: function(event) {
        var oElement;
        var oElement2;
        var shower = null;
        var hider = null;
        if (Prototype.Browser.IsIE4up()) {
            oElement = $(Event.findElement(event));
        } else {
            oElement = $(Event.findElement(event));
        }
        try{
            oElement2 = oElement.up(1) 
        }catch(err){}

        for (var i = 0; i < this.PopupElements.length; i++) {
            if (this.PopupElements[i].TabElement == oElement
                || this.PopupElements[i].TabElement == oElement.parentNode
                || this.PopupElements[i].TabbyArrow == oElement2) {
                shower = this.PopupElements[i];
            } else if (this.PopupElements[i].Visible || this.PopupElements[i].transitioning) {
                hider = this.PopupElements[i];
            }
        }
        if (shower != null) {
            if (hider != null && shower != null) {
                this.EffectQueue.AddEffect(new EffectQueueItem(hider, false));
            }
            if (shower.Visible && !shower.transitioning) {
                this.EffectQueue.AddEffect(new EffectQueueItem(shower, false));
            } else {
                this.EffectQueue.AddEffect(new EffectQueueItem(shower, true));
            }

            if (!this.EffectQueue.Running)
                this.NextInQueue();
        }


    },
    NextInQueue: function() {
        this.EffectQueue.Next();
    }
});

var DigitalMarketingPopoutTabs = Class.create({
    initialize: function(PopupElement, tabElement) {
        this.TabElement = tabElement;
        this.TabbyArrow = $(this.TabElement).previous();
        this.PopupElement = $(PopupElement).firstDescendant().next();
        this.PopupPlaceHolder = PopupElement;
        this.PopupElement.style.display = 'block';
        this.PopupPlaceHolder.style.display = 'block';
        this.Height = this.PopupElement.getHeight();
        this.PlaceHolderHeight = this.PopupPlaceHolder.getHeight();
        this.PopupElement.style.display = 'none';
        this.PopupPlaceHolder.style.display = 'none';
        this.Visible = false;
        this.transitioning = false;
        this.Duration = 0.5;

        this.TabbyArrow.observe('mouseover', (function() { this.HighLightTab(true); }).bind(this));
        this.TabbyArrow.observe('mouseout', (function() { if (!this.Visible && !this.transitioning) { this.HighLightTab(false); } }).bind(this));

    },
    Show: function() {
        if (this.PopupElement.style.display == 'none') {
            this.HighLightTab(true);
            this.transitioning = true;
            Effect.Appear(this.PopupPlaceHolder, { duration: this.Duration });
            var heightCurrent = this.PopupPlaceHolder.getHeight();
            this.yScale = (this.Height / heightCurrent) * 100;
            new Effect.SlideDown(this.PopupPlaceHolder, { scaleTo: this.yScale, duration: this.Duration });

            new Effect.Appear(this.PopupElement, { duration: this.Duration, delay: this.Duration - 0.1, afterFinish: (function() { this.Visible = true; this.transitioning = false; DMPF.NextInQueue(); }).bind(this) });
        } else {
            DMPF.NextInQueue();
        }

    },
    Hide: function() {
        if (this.PopupElement.style.display != 'none') {

            this.transitioning = true;
            new Effect.Fade(this.PopupElement, { duration: this.Duration });
            new Effect.Fade(this.PopupPlaceHolder, { duration: this.Duration, delay: this.Duration - 0.1 });
            new Effect.SlideUp(this.PopupPlaceHolder, { duration: this.Duration, scaleTo: 0, delay: this.Duration - 0.1, afterFinish: (function() { this.Visible = false; this.transitioning = false; this.HighLightTab(false); DMPF.NextInQueue(); }).bind(this) });

        } else {
            DMPF.NextInQueue();
        }
    },
    HighLightTab: function(blnHighlight) {
        if (blnHighlight)
            this.TabElement.className = 'digihp_right_box_on';
        else
            this.TabElement.className = 'digihp_right_box';
    }
});

var EffectQueue = Class.create({
    initialize: function() {
        this.addEventHandlers();
    },
    addEventHandlers: function() {
        document.observe('click', (function(event) {
            var target = event.findElement('a[rel^=closePopup]');
            if (target) {
                event.stop();
                this.CloseAll();
            }
        }).bind(this));
    },
    AddEffect: function(QueueItem) {
        try {
            if (QueueItem.Element == this.CurrentItem.Element && QueueItem.Show == true && this.CurrentItem.Show == false)
                return;
        } catch (err) { }
        for (var i = 0; i < this.Queue.length; i++) {
            if (this.Queue == QueueItem) {
                return;
            }
        }
        this.Queue.push(QueueItem);
    },
    Next: function() {
        if (this.Queue.length > 0) {
            this.Running = true;
            if (this.Queue[0].Show == true && this.CurrentlyOpen.length > 0) {
                this.CurrentItem = this.CurrentlyOpen.shift();
                this.CurrentItem.Element.Hide();
            } else {
                this.CurrentItem = this.Queue.shift();
                if (this.CurrentItem.Show) {
                    this.CurrentItem.Element.Show();
                    this.CurrentlyOpen.push(this.CurrentItem);
                } else {
                    this.CurrentItem.Element.Hide();
                }
            }

        } else {
            this.CurrentItem = null;
            this.Running = false;
        }
    },
    CloseAll: function() {
        while (this.Queue.length > 0) {
            this.Queue.shift();
        }
        for (var i = 0; i < this.CurrentlyOpen.length; i++) {
            var x = this.CurrentlyOpen.shift();
            this.AddEffect(new EffectQueueItem(x.Element, false));
        }

        this.Next();
    },
    CurrentlyOpen: [],
    Queue: []
});

var EffectQueueItem = Class.create({
    initialize: function(oElement, blnShow) {
        this.Element = oElement;
        this.Show = blnShow;
    }
})

Object.extend(Prototype.Browser, {
    Version: navigator.appVersion.toLowerCase(),
    GetMinor: function() {
        iePos = this.Version.indexOf('msie');
        return (iePos != -1) ? parseFloat(this.Version.substring(iePos + 5, this.Version.indexOf(';', iePos))) : parseFloat(this.Version);
    },
    GetMajor: function() { return parseInt(this.GetMinor()); },
    IsIE3: function() { return (this.IE && this.GetMajor() < 4); },
    IsIE4: function() { return (this.IE && this.GetMajor() == 4); },
    IsIE4up: function() { return (this.IE && this.GetMinor() >= 4); },
    IsIE5: function() { return (this.IE && this.GetMajor() == 5); },
    IsIE5up: function() { return (this.IE && this.GetMajor() >= 5); },
    IsIE5_5: function() { return (this.IE && (navigator.userAgent.indexOf("msie 5.5") != -1)); },
    IsIE5_5up: function() { return (this.IE && this.GetMinor() >= 5.5); },
    IsIE6: function() { return (this.IE && this.GetMajor() == 6); },
    IsIE6up: function() { return (this.IE && this.GetMinor() >= 6); }
});