
var domain = location.href.split("/")[2];

window.addEvent('domready', function() {
        
    initNavigationMenus();
    initExpMod();
    initInfoPanel();

    initInvestmentFirms();

    initHomepageInfo();

    initDisclaimerLinks();
         
    cleanSecondaryTabs();

    setAutoClear();
    collapseElements();

    initPopUpLinks();
});

var initExpMod = function() {
    if ($('expmod')) {
        var skipIntroAnim = false;
        if (Cookie.read('skipIntro')) {
            skipIntroAnim = true;
        } else {
            Cookie.write('skipIntro', 'true', false);
        }
        if (Browser.Plugins.Flash.version >= 9) {   
            var expmod = new Swiff('/swf/preloader.swf',
                {
                    id: 'experientialmodule',
                    width: 942,
                    height: 270,
                    params: {
                        wmode: 'transparent'
                    },
                    vars: {
                        skipIntro: skipIntroAnim.toString()
                    },
                    container: $('expmod')
                }
            );
        } else {
            var expA = new Element('a', {'href': 'http://get.adobe.com/flashplayer/?promoid=DXLUJ', 'target':'_blank'});
            var expImg = new Element('img', {'src': '/img/expmod-noflash.jpg'});
            expImg.inject(expA);
           expA.inject( $('expmod'));
        }
    }
    
    if ($('globpresencemod')) {
        if (Browser.Plugins.Flash.version >= 9) { 
            var globalmod = new Swiff('/swf/global-module.swf',
                {
                    id: 'globalpres',
                    width: 450,
                    height: 278,
                    params: {
                        wmode: 'transparent',
						allowScriptAccess: 'always',
						swLiveConnect: true
                    },
                    vars: {
                        'xml': 'http://' + domain + '/xml/globalpresence.xml'
                    },
                    container: $('globpresencemod')
                }
            );
        } else {
            var presA = new Element('a', {'href': 'http://get.adobe.com/flashplayer/?promoid=DXLUJ', 'target':'_blank'});
            var presImg = new Element('img', {'src': '/img/globalpresence-noflash.jpg'});
            presImg.inject(presA);
            presA.inject( $('globpresencemod'));
        }
    }
}

var initNavigationMenus = function() {

    $$('#main-nav li').each(function(item) {
        item.addEvent('mouseenter', function() {
            this.addClass('hover');         // apply opened submenu style
            // begin to fade all submenus into view
            this.getElements('ul').each(function(ul) {
                if (!ul.fader)
                    ul.fader = new Fx.Tween(ul, {duration:300, transition: Fx.Transitions.Quad.easeOut, link: 'cancel'}).set('opacity',0);
                
                if (ul.removeHover)
                    ul.fader.removeEvent('complete', ul.removeHover);
                    
                ul.fader.start('opacity', 1);
            });
        });
        item.addEvent('mouseleave', function() {
            var _this = this;   // maintain scope
            var count = 0;      // used to track amount of submenus
            // get all submenus...should only be one
            this.getElements('ul').each(function(ul) {
                // only close after the menu has faded out
                
                _this.addClass('animateout');    
                ul.removeHover = function() { _this.removeClass('animateout'); _this.removeClass('hover'); ul.fader.removeEvent('complete', ul.removeHover); };
                if (ul.fader) {
                ul.fader.addEvent('complete', ul.removeHover);
                ul.fader.start('opacity', 0);
                }
                count++;
            });
            
            // if there are no submenus then close automatically
            if (count == 0) {
                _this.removeClass('hover');
            }
        });
    });
};

var initInfoPanel = function() {
    
    $$('#secondarynav li').each(function(item) {
        item.addEvent('mouseenter', function() {
            this.addClass('hover');
        });
        item.addEvent('mouseleave', function() {
            this.removeClass('hover');
        });
    });
};

var initInvestmentFirms = function() {
    
    $$('#investment_firms li span').each(function(item) {
        item.addEvent('mouseenter', function() {
            this.getParent().addClass('hover');
        });
        item.addEvent('mouseleave', function() {
            this.getParent().removeClass('hover');
        });
    });
    
    
    $$('.manager h3').each(function(item) {
        item.addEvent('mouseenter', function() {
            this.addClass('hover');
        });
        item.addEvent('mouseleave', function() {
            this.removeClass('hover');
        });
    });
};

var initHomepageInfo = function() {
    $$('.infopanel .animator').each(function(anim){ anim.setStyle('opacity', 0); });
    var header = $('globalpresence');
    if (header) {
        header.wipe = new Fx.Morph(header, {duration:500, transition: Fx.Transitions.Quad.easeOut, link: 'cancel'});
        header.defaultHeight = 23;
        header.setStyles({'height': header.defaultHeight, 'width': 'auto', 'overflow': 'hidden', 'display':'block', 'margin-top': 10});
    }
    $$('.infodiv').each(function(info) {
        
        info.addEvent('mouseenter', function() {
            info.addClass('hover');
            this.getElements('.infopanel .animator').each(function(panel) {
                if (!panel.fader)
                    panel.fader = new Fx.Morph(panel, {duration:500, transition: Fx.Transitions.Quad.easeOut, link: 'cancel'});
                
                panel.fader.start({
                    'opacity': 1
                });
                header.wipe.start({'height': 0, 'margin-top': header.defaultHeight + 10});
            });
        });
        info.addEvent('mouseleave', function() {
            //close any open dropdowns since they'll remain open when reopening the panel
            closeDHTMLDrops(true);
            info.addClass('animateout');
            this.getElements('.infopanel .animator').each(function(panel) {
                if (panel.fader) {
                    panel.removeHover = function() { if (panel.getStyle('opacity') == 0) { info.removeClass('hover'); info.removeClass('animateout'); }};
                    panel.fader.addEvent('complete', panel.removeHover);
                    panel.fader.start({
                        'opacity': 0
                    });
                    
                    header.wipe.start({'height': header.defaultHeight, 'margin-top': 10});
                }
            });
        });
    });
    
};

var cleanSecondaryTabs = function() {
    $$('#secondarynav li.selected').each(function(tab) {
        var previous = tab.getPrevious('li');
        if (previous) {
            previous.addClass('beforeSelected');
        }
    });
}

var setAutoClear = function() {
    $$('input.autoclear').each(function(input) {
        input.setProperty('default', input.value);
        
        input.addEvent('focus', function() {
            if (this.value == this.getProperty('default'))
                this.value = '';
        });
        input.addEvent('blur', function() {
            if (this.value.trim() == '')
                this.value = this.getProperty('default');
        });
    });
}

var setAutoFocus = function() {
    $$('.autofocus').each(function(item) {
        item.setFocus();
    });
}


var openfirm = null;
var collapseElements = function() {
    var collapse = $$('a.expand').each(function(lnk) {
        var container = lnk.getParent().getParent();
        var ele = container.getElements('ul')[0];
        lnk.addEvent('click', function() {
            ele.slider.toggle();
            if (this.hasClass('expanded')) {
                this.removeClass('expanded');
                openfirm = null;
            }
            else {
                this.addClass('expanded');
                if (openfirm) { openfirm.fireEvent('click'); }
                openfirm = lnk;
            }
                
            return false;
        });
        
        ele.slider = new Fx.Slide(ele, {
            mode: 'vertical',
            duration: 500,
            transition:Fx.Transitions.Quad.easeOut,
            link: 'cancel',
            onComplete: function() {
                if(this.open)
                    ele.getParent().setStyle('overflow','visible');
            },
            onStart: function() {
                if (this.open) {
                    ele.getParent().setStyle('overflow','hidden');
                }
            }            
        });
        ele.slider.hide();       
    });
}

var initDisclaimerLinks = function() {
    $$('a.remote').each(function(a) {
        a.addEvent('click', function() {
            openDisclaimerPopup(this);
            return false;
        });
    });
}

var onDisclaimerPopup = null;
var openDisclaimerPopup = function(link) {
    closeDHTMLDrops();
    var _url =  link.href;
    var _target = link.target;
    var rel = parseLinkRel(link.rel);
    var request = new Request({
        url:"/disclaimer/" + rel.disclaimer + ".aspx", 
        method:'GET', 
        async:true, 
        onSuccess:function(responseText, responseXML) {
            $('disclaimercontainer').setProperty('html', responseText);
            
            repositionDisclaimerPopup();
            animateDisclaimerPopup();
            
            $$('.disclaimer .accept').each(function(btn) {
                btn.href = _url;
                btn.target = _target;
                btn.addEvent('click', function() { destroyDisclaimerPopup(); return true; });
            });
            
            $$('.disclaimer .decline').each(function(btn) {
                btn.addEvent('click', function() { destroyDisclaimerPopup(); return false; });
            });
            
			if (onDisclaimerPopup) {
				onDisclaimerPopup();
			}
        }
    }).send();
}

var disclaimerScreenTween;
var disclaimerModalTween;
var animateDisclaimerPopup = function() {
    $$('.disclaimer .screen').each(function(popup) {
        disclaimerScreenTween = new Fx.Tween(popup, {duration: 'short', transition: Fx.Transitions.Quad.easeOut, link: 'wait'}).set('opacity', 0);
        disclaimerScreenTween.start('opacity', .75);
    });  
    $$('.disclaimer .modal').each(function(mdl) {
        disclaimerModalTween = new Fx.Tween(mdl, {duration: 'short', transition: Fx.Transitions.Quad.easeOut, link: 'wait'}).set('opacity', 0);
        disclaimerModalTween.start('opacity', 1);
    }); 
}


var destroyDisclaimerPopup = function() {
    $$('.disclaimer').each(function(popup) {
        
        disclaimerScreenTween = null;
        disclaimerModalTween = null;
        popup.destroy();
        //initDisclaimerLinks();
    });  
}

var repositionDisclaimerPopup = function() {
    var windowXY = window.getScrollSize();
    var screenXY = window.getSize();
    
    $$('.disclaimer .screen').each(function(scr) {
        scr.setStyles({'width': windowXY.x, 'height': windowXY.y});
    });
    
    
    $$('.disclaimer .modal').each(function(mdl) {
        var modalSize = mdl.getSize();
        mdl.setStyles({'left': (windowXY.x - modalSize.x) / 2, 'top': window.getScroll().y + (screenXY.y / 2) - (modalSize.y / 2)});
    });
}

var parseLinkRel = function(rel) {
    if (rel != "") {
        var namevaluepairs = rel.split('/');
        var relObj = "{";
        for (var i = 0; i < namevaluepairs.length; i++) {
            var namevalue = namevaluepairs[i].split(':');
            var name = namevalue[0];
            var value = namevalue[1];
            if (i > 0) { relObj += ", "; }
            relObj += name + " :" + " '" + value + "'";
        }
        relObj += "}";
    }
    return eval("(" + relObj + ")");
}

var openTimeline = function() {
    var timeline = new Swiff('/swf/timeline_full.swf',
        {
            id: 'timeline_swf',
            width: 633,
            height: 243,
            params: {
                wmode: 'transparent'
            },
            container: $('timeline'),
            vars: {
                xmlFile: '/xml/timeline.xml'
            }
        }
    );
}

var closeTimeline = function() {
    $('timeline').setProperty('html','');
}


var initPopUpLinks = function() {
    $$('a.pop-up').each(function(a) {
        var rel = parseLinkRel(a.rel);
        if(rel){ 
            var width = rel.width;
            var height = rel.height; 
        }
        else { 
            var width = 640;
            var height = 600; 
        }
        var url = a.get('href');
        var left = (screen.width - width) / 2;  
        var top = (screen.height - height) / 2;

        a.addEvent('click', function(e) {
            e.stop();
            var popup = window.open(url, 'popup', 'width='+width+', height='+height+', left='+left+', top='+top+', toolbars=0, scrollbars=1, location=0, statusbars=1, menubars=0, resizable=0');
			if (window.focus) { popup.focus(); }
        });
    });
}

