// JavaScript Document

/*
	LIBRARY:		Popups
	CREATED:		2008-04-30
	AUTHOR:			BP
	PURPOSE:		Attaches ability to open popup windows to objects
	DEPENDENCIES:	mm_main, yahoo-dom-event, yahoo animation
*/

/*
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;			
		} else if (eventFrom.tagName == 'AREA') {
			eventTarget = eventFrom;		
		} else {
			eventTarget = Dom.getAncestorByClassName(eventFrom,'popupOpener');		
		}
		winUrl = eventTarget.href;

		if (Dom.hasClass(eventTarget, '320x240Window')) { 
			xSize = 320;
			ySize = 240;
			winName = 'maxi_320x240Window';		
		} else 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 = 395;
            ySize = 325;
            winName = 'maxi_VideoPlayer';
            winUrl = '/general/popupplayer.html?video=' + winUrl.substring(winUrl.lastIndexOf('/') + 1);
		} else if (Dom.hasClass(eventTarget, 'newWindow')) {
			xSize = 0;
			ySize = 0;
			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();
	}
};


/*
	LIBRARY:		Portal
	CREATED:		2008-05-16
	AUTHOR:			OJ
	PURPOSE:		Creates a tab area to show different content
	DEPENDENCIES:	yahoo-dom-event
*/

/*
NAME:
	Portal

DESCRIPTION:
	Creates an area of content, where the content displayed can be changed either by roll-over or by clicking
	on handles.

USAGE:
	Place the XHTML shown in the exaple on the page and make a call to the object

		YAHOO.Muscle.portal.init(areaSize, changeAction[click|hover] );

	The XHTML must be marked up with the following class definitions
		Add 'portalNav' class to the list of handles


NOTES:


EXAMPLE :

********/
YAHOO.Muscle.portal = {
	properties : {
		areaHeight : 145,
		changeAction : 'click',
		attachMethod : 'delegation',
		currentActiveHandle : null
	},
	init : function(changeAction, areaHeight,root) {
		if(areaHeight) {
			this.areaHeight = areaHeight;
		}
		if(changeAction == "mouseover") {
			this.changeAction = changeAction;
		}

		var portals ;
		if (root) {
			portals = Dom.getElementsByClassName("pagePortal","div",document.body);
		} else {
			portals = Dom.getElementsByClassName("pagePortal","div",root);
		}

		if(portals.length > 0) {
			for(var i=0; i<portals.length; i++) {
				var portalViewArea = Dom.getElementsByClassName("viewPortal","div", portals[i]);

				var handles = Dom.getElementsByClassName("portalNav","ul",portals[i]);
				(this.attachMethod == "delegation")?this.attachPortalControlsDelegation(handles):this.attachPortalControls(handles);			}
		}
	},
	attachPortalControls : function(handles) {
		// Attach an event handles to the lowest (anchor) nodes within each handle list
		var handleProperties = { };
		var handelNodes = {}
		var viewPortalObj;

		for(var i=0; i<handles.length; i++) {
			handleNodes = handles[i].getElementsByTagName("a");
			viewPortalObj = Dom.getFirstChild(Dom.getElementsByClassName("viewPortal", "div", handles[i].parentNode)[0]);

			for (var j = 0; j < handleNodes.length; j++) {
				if (Dom.hasClass(handleNodes[j], "active")) {
					// This item has an active class, so store it as the active node
					this.currentActiveHandle = handleNodes[j];
				}
				handleProperties = {
					option : j,
					portalContentHeight : Dom.getElementsByClassName('portalContent','div',viewPortalObj)[0].offsetHeight,
					portalObj : viewPortalObj,
					jsObj : this
				}
				Evt.addListener(handleNodes[j], this.changeAction,this.handler, handleProperties);
			}
		}
	},
	handler : function(e, handleProperties) {

		if (handleProperties.jsObj.currentActiveHandle != null) {
			Dom.removeClass(handleProperties.jsObj.currentActiveHandle, "active");
			Dom.addClass(Evt.getTarget(e), "active");
			handleProperties.jsObj.currentActiveHandle = Evt.getTarget(e);
		}
		Dom.setStyle(handleProperties.portalObj, "top", (-1 * handleProperties.option * handleProperties.portalContentHeight) + "px");
	},
	attachPortalControlsDelegation : function(handles) {
		// Uses Event delegation to only attach event one event handler per handle collection
		// This is usually applied to the list node.  In Firefox it seems to prevent the default action
		// attach the control event to each of the list (note, not each of the list items)

		for(var i=0; i<handles.length; i++) {
			var handleProperties = {
				mainObj : handles[i],
				portalObj : Dom.getFirstChild(Dom.getElementsByClassName("viewPortal", "div", handles[i].parentNode)[0]),
				jsObj : this
			}

			Evt.addListener(handles[i],this.changeAction,this.delegationHandler,handleProperties);
		}
	},
	delegationHandler : function(e,handleProperties) {
		var selectedNodeText = Evt.getTarget(e).innerHTML;
		// Sets the top property of the portal within its view port
		var handleOptions = handleProperties.mainObj.getElementsByTagName("a");
		for (var i = 0; i < handleOptions.length; i++ )  {
			if (handleOptions[i].innerHTML == selectedNodeText) {
				Dom.setStyle(handleProperties.portalObj, "top", (-1 * i * handleProperties.jsObj.areaHeight) + "px");
			}
		}
	}
};

// legacy functions
function NewWindow(mypage,myname,w,h,scroll) {
  var newwindow
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
  newwindow = window.open(mypage,'name',settings);
  if (!newwindow.opener) newwindow.opener = self;
  if (window.focus) { newwindow.focus() }
}

function mmWin(url,wind,w,h,scroll,rand) {
  if (h == 150) { h = 300; }

  NewWindow(url,wind,w,h,"yes")
}