/* 
	in-page popup audio player
	
	REQUIRES:
		- /GS/extras.js

	USAGE:
		- put a class on your anchor tag:

				audioPop-CUSTOMAUDIOID

			customAudioID must begin with a letter. see media/audio-mapping.asp.
			
			for normal customs:	audioPop-whateveryouwant	(then add this ID to audio-mapping)
			for store items:	audioPop-storeC2161			(no changes needed to audio-mapping)
			for unbound:		audioPop-unb-CLIPID-MAINID	(no changes needed to audio-mapping)

		- that's it.
*/

var audioPopupInfo = {
	currentPopBox:null,
	popLocX:0,
	popLocY:0,
	popOffsetX:-70,		// offset from click point
	popOffsetY:20,
	trackFunction:0,	// func(clipID)
	
	// optional -- to initialize custom callbacks, etc.
	init:function( trackFunc) {
		this.trackFunction = trackFunc;
	}
};

		// retrieve the mouse location
		function mouseX(evt) {
			if (evt.pageX) return evt.pageX;
			else if (evt.clientX)
				return evt.clientX + (document.documentElement.scrollLeft ?
					document.documentElement.scrollLeft :
					document.body.scrollLeft);
			else return null;
		}
		function mouseY(evt) {
			if (evt.pageY) return evt.pageY;
			else if (evt.clientY)
				return evt.clientY + (document.documentElement.scrollTop ?
					document.documentElement.scrollTop :
					document.body.scrollTop);
			else return null;
		}

		function playPopupAudio() {
			var url = $('audioFileLink').href.replace(/easylink/,'progressive'),	// "easylink" isn't working !?
				root = rootDir(),
				config = root+"media/audio-config.xml",
				movieFile = root+"media/mp3player.swf?file="+url,
				FO = { movie:movieFile+"&config="+config, id:"flashMP3", width:"180", height:"18",
					majorversion:"6", build:"40"  };
			UFO.create(FO, "popFlashPlayer");
			if ( audioPopupInfo.trackFunction)
				audioPopupInfo.trackFunction( $('audioPersonNameClean').innerHTML);
		}

		function audioPlayerPopupCallback(responseText) {
			var elt = document.createElement('div'), elt2;
			
			setCls(elt,'playerPop');
			// position relative to the page (body)
			elt.style.position = 'absolute';
			elt.style.zindex = '300';
			elt.style.left = (audioPopupInfo.popLocX + audioPopupInfo.popOffsetX) + 'px';
			elt.style.top = (audioPopupInfo.popLocY + audioPopupInfo.popOffsetY) + 'px';
			elt.innerHTML = responseText;
			
			// could use append child, but opera works better like this...
			document.body.insertBefore( (audioPopupInfo.currentPopBox = elt), document.body.firstChild);
			wrapOneDropshadow( $('unbound-popup-audio'));
			
			// offsite the unbound link, if there is one
			elt2 = $('fullLengthLink');
			if(elt2) setupOneOffsiteLink(elt2);
			playPopupAudio();
		}

		function closeAudioPlayerPop() {
			if ( audioPopupInfo.currentPopBox) {
				// first delete the flash player itself (or it continues playing...)
				var fp = $('flashMP3'),
					contain = $('popFlashPlayer');
				if ( fp && contain) contain.removeChild( fp);

				// now delete the popup box itself
				document.body.removeChild( audioPopupInfo.currentPopBox);
			}
			audioPopupInfo.currentPopBox = null;
		}

		// see audio-popup-server.asp for docs. can omit last 3 params
		function openAudioPlayerPopBox(evt, id) {
			var params;
			closeAudioPlayerPop();
			audioPopupInfo.popLocX = mouseX(evt);
			audioPopupInfo.popLocY = mouseY(evt);

			params = "customID="+id;
			new SimpleAjax( rootDir()+"media/audio-popup-server.asp", params, audioPlayerPopupCallback);
		}


function openPopBox(e) {
	var cls = getCls(this),		// "this" is the anchor
		sty = stripMultiCls( cls.substring(9 + cls.indexOf('audioPop-')));

	openAudioPlayerPopBox( e, sty);
	return false;
}

	function setupAudioAnchor(a) { addEvent( a, 'click', openPopBox, true); }

// will set up all audio anchors when the page has loaded. use class:
//		audioPop-CLIPPARTID-MAINID-PERSONID-SHOWPHOTO
//
registerAnchorType( { clsPartial:"audioPop-", apply:setupAudioAnchor} );

function initPopupPage() {
	loadCSS('../_styles/misc/audio-popup.css');
	ensureScript('/GS/ajax.js');
	ensureScript('/GS/ufo.js');
	ensureScript('/GM/dropshadow/dropshadow.js');
}
addLoadEvent( initPopupPage );
