/*
	LIBRARY:		Popups
	CREATED:		2008-04-30
	AUTHOR:			BP + OJ
	PURPOSE:		Attaches ability to open popup windows to objects
	DEPENDENCIES:	mm_main, yahoo-dom-event, yahoo animation
	
	
	SERVED FROM LIVE SITE
*/

/*
NAME:
	Popup

DESCRIPTION:
	Provides functionality to open pop-up windows and set up
	linkages to allow objects to open popup windows when clicked

USAGE:
	Place the XHTML shown in the example on the page and make a call to the object

		YAHOO.Muscle.popupwin.init();

	The XHTML must be marked up with the following class definitions
		Add 'popupOpener' class to an object, along with a known window sizing class (if required)

NOTES:
	Normally associated with a link/anchor, but has "step-up" ability - for example, an image tag within
	an anchor will cause the click-through to return the img tag as the calling object - and we obviously
	need the anchor.

EXAMPLES :

	<a href="/toolsanddownloads/dietplan1500cals.html" class="popupOpener bigWindow">
		Click here to open a big window!
	</a>

	<a href="/toolsanddownloads/dietplan1500cals.html" class="popupOpener">
		This link has no sizing class and will default to medium when clicked
	</a>

********/
YAHOO.Muscle.popupwin = {
    init: function() {
        var popups = Dom.getElementsByClassName("popupOpener");

        if (popups.length > 0) {
            for (var i = 0; i < popups.length; i++) {
                this.attachHandleControl(popups[i]);
            }
        }
    },
    attachHandleControl: function(handle) {
        Evt.addListener(handle, "click", this.clickHandler);
    },
    clickHandler: function(e) {
        var xSize = 800;
        var ySize = 600;
        var winName = 'maxi_PopupMedium';
        var winUrl = '';
        var eventFrom = Evt.getTarget(e);
        var eventTarget;

        if (eventFrom.tagName == 'A') {
            eventTarget = eventFrom;
            winUrl = eventTarget.href;
        } else if (eventFrom.tagName == 'AREA') {
            eventTarget = eventFrom;
            winUrl = eventFrom.pathname;
        } else {
            eventTarget = Dom.getAncestorByClassName(eventFrom, 'popupOpener');
            winUrl = eventTarget.href;
        }

        if (Dom.hasClass(eventTarget, 'smallWindow')) {
            xSize = 640;
            ySize = 480;
            winName = 'maxi_PopupSmall';
        } else if (Dom.hasClass(eventTarget, 'bigWindow')) {
            xSize = 1024;
            ySize = 768;
            winName = 'maxi_PopupBig';
        } else if (Dom.hasClass(eventTarget, 'trainingAnimation')) {
            xSize = 180;
            ySize = 140;
            winName = 'trainingAnimation';
        } else if (Dom.hasClass(eventTarget, 'videoPlayer')) {
            xSize = 380;
            ySize = 340;
            winName = 'maxi_VideoPlayer';
        } else if (Dom.hasClass(eventTarget, 'newWindow')) {
            xSize = 0;
            ySize = 0;
            // winName = window.name + "1";
            eventTarget.target = "_new"
        }

        if (xSize > 0) {
            NewWindow(winUrl, winName, xSize, ySize, 'yes');
            Evt.preventDefault(e);
        }
    }
}

function NewWindow(newAddress,winName,xSize,ySize,scrollFlag) {
	var xPos = (screen.width) ? (screen.width - xSize)/2 : 0;
	var yPos = (screen.height) ? (screen.height - ySize)/2 : 0;
	var winSetts = 'height=' + ySize + ',width=' + xSize + ',top=' + yPos + ',left=' + xPos + ',scrollbars=' + scrollFlag + ',resizable';
	var newWin = window.open(newAddress,winName,winSetts);

	if (!newWin.opener) {
		newWin.opener = self;
	}
	if (window.focus) {
		newWin.focus();
	}
};

