
// Add a class name to the body as a global flag that the user has javascript.
document.observe('dom:loaded', function(){
	$(document.body).addClassName('js-enabled');
});


// start SwitchDivClassName
var SwitchDivClassName = Class.create ({
    initialize: function(divId, btnA, linkA, linkB, classA, classB)
    {
		this.divId = divId;
		this.btnA = btnA;
		this.linkA = linkA;
		this.linkB = linkB;
		this.classA = classA;
		this.classB = classB;

        var bindClickA = this.__ClickA.bindAsEventListener(this);
        var bindClickB = this.__ClickB.bindAsEventListener(this);
        this.btnA.observe('click', bindClickA);
        this.linkA.observe('click', bindClickA);
        //this.linkB.observe('click', bindClickB);
    },
    __ClickA: function(e)
    {
        var el = e.element();
        this.divId.className = this.classB;
    },
    __ClickB: function(e)
    {
        var el = e.element();
        this.divId.className = this.classA;
    }
});
// end SwitchDivClassName

// start ShowHideVideo
function playvideo(playerId, vidId)
{
		var ytplayer = $(playerId);
		ytplayer.loadVideoById(vidId);
}
function stopvideo(playerId)
{
        var ytplayer = $(playerId);
        ytplayer.stopVideo();
}
var ShowHideVideo = Class.create ({
    initialize: function(trigger, panel, playerId)
    {
		this.vidTrigger = trigger;
			this.vidID = this.vidTrigger.rel;
		this.vidPanel = panel;
		this.playerId = playerId;
		this.lnkClose = $('btn_closeX');
		this.isVisible = false;

        var bindClickOpen = this.__ClickOpen.bindAsEventListener(this);
        var bindClickClose = this.__ClickClose.bindAsEventListener(this);
        var bindBodyClickClose = this.__BodyClickClose.bindAsEventListener(this);
        this.vidTrigger.observe('click', bindClickOpen);
        this.lnkClose.observe('click', bindClickClose);
		$(document.body).observe('click', bindBodyClickClose);
    },
    __ClickOpen: function(e)
    {
        e.stop();
        var el = e.element();
        this.OpenVideoPanel();
    },
    __ClickClose: function(e)
    {
        //e.stop();
        var el = e.element();
        if (this.isVisible)
        {
			this.CloseVideoPanel();
        }
    },
    __BodyClickClose: function(e)
    {
        //e.stop();
        var el = e.element();
        if (this.isVisible && !el.descendantOf(this.vidPanel))
        {
			this.CloseVideoPanel();
        }
    },
    OpenVideoPanel: function()
    {
        this.isVisible = true;
        this.vidTrigger.addClassName('active');
        this.vidPanel.addClassName('active');
		playvideo(this.playerId, this.vidID);
    },
    CloseVideoPanel: function()
    {
        this.isVisible = false;
        this.vidTrigger.removeClassName('active');
        this.vidPanel.removeClassName('active');
        var ytplayer = $(this.playerId);
        stopvideo(this.playerId);
    }
});
// end ShowHideVideo

// start ShareLinks
var ShareLinks = Class.create ({
    initialize: function(trigger, panel)
    {
		this.slTrigger = trigger;
		this.slPanel = panel;
		this.isVisible = false;

        var bindClickOpen = this.__ClickOpen.bindAsEventListener(this);
        var bindClickClose = this.__ClickClose.bindAsEventListener(this); // not used, use to bind to 'close' button
        var bindBodyClickClose = this.__BodyClickClose.bindAsEventListener(this);
        this.slTrigger.observe('click', bindClickOpen);
        $(document.body).observe('click', bindBodyClickClose);
    },
    __ClickOpen: function(e)
    {
        e.stop();
        var el = e.element();
        this.OpenSharePanel();
    },
    __ClickClose: function(e)
    {
        //e.stop();
        var el = e.element();
        if (this.isVisible)
        {
			this.CloseSharePanel();
        }
    },
    __BodyClickClose: function(e)
    {
        //e.stop();
        var el = e.element();
        if (this.isVisible && !el.descendantOf(this.slPanel))
        {
			this.CloseSharePanel();
        }
    },
    OpenSharePanel: function()
    {
        this.slTrigger.addClassName('active');
        this.slPanel.addClassName('active');
        this.isVisible = true;
    },
    CloseSharePanel: function()
    {
        this.slTrigger.removeClassName('active');
        this.slPanel.removeClassName('active');
        this.isVisible = false;
    }
});
// end ShareLinks

// start VideoSwapper
var VideoSwapper = Class.create ({
    initialize: function(links)
    {
		this.links = links;
			this.length = this.links.size();
		var boundLinkClick = this.__Click.bindAsEventListener(this);
        this.links.invoke('observe', 'click', boundLinkClick);
    },
    __Click: function(e)
    {
        e.stop();
        var element = e.element();
        if (!element.hasClassName('playbtn-watchnow'))
        {
			element = element.up('a');
        }
        for (var i=0; i<this.length; i++)
        {
	        if (this.links[i] == element)
	        {
		        this.SwapVid(i);
		        break;
	        }
        }
    },
    SwapVid: function(i)
    {
        var vidID = this.links[i].rel.toString();
        $('videoPlayer').loadVideoById(vidID);
    }
});
// end VideoSwapper


// start CustomModalPopover
var CustomModalPopover = Class.create ({
    initialize: function(popoverID)
    {
		var body = $(document.body);
		this.lnkClose = $('btn_closeX');
		this.modalOverlay = $('modaloverlay');
			this.modalOverlay.setOpacity(0.0);
		this.modalWindow = popoverID;
			this.modalWindow.remove();
			this.modalOverlay.insert({after: this.modalWindow});
			this.leftPos = (body.getWidth() - this.modalWindow.getWidth()) / 2  + 'px';
			this.topPos = (body.getHeight() - this.modalWindow.getHeight()) / 2  + 'px';
			this.modalWindow.setStyle({left: this.leftPos, top: this.topPos});
        var bindClickClose = this.__ClickClose.bindAsEventListener(this);
        this.modalOverlay.observe('click', bindClickClose);
        this.lnkClose.observe('click', bindClickClose);
        this.OpenModWin();
    },
    __ClickClose: function(e)
    {
        e.stop();
        var element = e.element();
        this.CloseModWin();
    },
    OpenModWin: function()
    {
        this.modalOverlay.addClassName('active').appear({
			duration: 0.2, from: 0.0, to: 0.8,
            afterFinish: function() {
                this.modalWindow.addClassName('active');
            }.bind(this)
        });
    },
    CloseModWin: function()
    {
        this.modalOverlay.removeClassName('active').setOpacity(0.0);
        this.modalWindow.removeClassName('active');
    }
});
// end CustomModalPopover


// start PopupWindow
var PopupWindow = Class.create ({
	initialize: function(link, width, height, extras)
	{
		this.winName = 'popup';
		this.altWidth = 640;
		this.altHeight = 360;
		this.altExtras = 'location=no,menubar=no,statusbar=no,toolbar=no,scrollbars=no,resizable=yes';
		this.link = link;
		this.url = link.getAttribute('href');
		this.w = width || this.altWidth;
		this.h = height || this.altHeight;
		this.x = extras || this.altExtras;
		this.features = 'width=' + this.w + ',height=' + this.h + ',' + this.x;
		this.link.observe('click', this.__Click.bindAsEventListener(this));
	},

	__Click: function(e)
	{
		e.stop();
		var element = e.element();
		var win = window.open(this.url, this.winName, this.features);
		win.focus();
	}

});
// end PopupWindow


// start ClosePopup
var ClosePopup = Class.create ({
	initialize: function(link)
	{
		this.link = link;
		this.link.observe('click', this.__Click.bindAsEventListener(this));
	},

	__Click: function(e)
	{
		e.stop();
		var element = e.element();
		window.close();
	}

});


//$(document).observe(function() {
//    if($('span.pds-answer-group') {
//        $('div.box3').setStyle({
//            backgroundImage: url("/RussellInvestments/_ui/img/weigh-in.jpg"),
//            backgroundRepeat: 'no-repeat',
//            height: '238px',
//            width: '310px'
//        });
//    }
//    if($('span.pds-feedback-group') {
//        $('div.box3').setStyle({
//            background-image:url("/RussellInvestments/_ui/img/weigh-in.jpg"),
//            background-repeat:no-repeat,
//            height:238px,
//            width:310px
//        });
//    } 
//    
//});

// end ClosePopup

