/* 
	in-page popup audio player
	
	REQUIRES:
		- /GS/extras.js

	USAGE:
		- put a class on your anchor tag:

				audioPop-CLIPPARTID-MAINID-PERSONID-SHOWPHOTO-MOREOPTIONS-TRKCODE
					or
				audioPop-CUSTOMAUDIOID

			optional params:
			personID: forces only this person's name to show
					(e.g., if you have mult people on an audio, but only want one name)
			showPhoto: 1 forces the photo(s) to show; also subject to personID above
			moreOptions: 1 suppresses the "more about this audio" link
						2 suppresses the name & title
						3 suppresses both
			trkCode: adds "audioTrk="+trkCode to the "more info" link (if shown)
					also adds TWO tracking pixels when the audio popup is played in the
					first place:
						/track/audio-popup-id.asp?clipPartID=...&mainID=..."
						/track/audio-popup-trk.asp?audioTrk=..."
					(without "trkCode", no tracking pixel is loaded at all)

			customAudioID must begin with a letter. see _media/audio-mapping.asp

		- 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/,'pd.global'), //.replace(/easylink/,'pd.global'),	// "easylink" isn't working !?
				root = rootDir(),
				config = root+"unbound/config.xml",
				movieFile = root+"_flash/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'));
				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, mainID, personID, showPhoto, moreOptions, trkCode) {
			var params, googleTracker = '';
			closeAudioPlayerPop();
			audioPopupInfo.popLocX = mouseX(evt);
			audioPopupInfo.popLocY = mouseY(evt);

			if ( /^[a-zA-Z]/.test( id) ) {
				params = "customID="+id;
				googleTracker = id;
			}
			else {
				params = "id="+id+"&mainID="+mainID;
				if( personID) params += "&personID="+personID;
				if( showPhoto) params += "&showPhoto="+showPhoto;
				if( moreOptions) params += "&moreOptions="+moreOptions;
				if( trkCode) {
					params += "&trkCode="+trkCode;
					var i1=new Image(), i2=new Image();		// ?clipPartID=...&mainID=...&audioTrk=..."
					i1.src = rootDir()+'_track/audio-popup-id.asp?clipPartID='+id+'&mainID='+mainID;
					i2.src = rootDir()+'_track/audio-popup-trk.asp?audioTrk='+trkCode;
				}
				googleTracker = "unbound-" + mainID;
			}
			new SimpleAjax( rootDir()+"unbound/audio-popup-server.asp", params, audioPlayerPopupCallback);
			
			if ( typeof(extraGoogleTrack) != 'undefined')
				extraGoogleTrack( '/_media/audioTrack-' + googleTracker);
		}


function openPopBox(e) {
	var cls = getCls(this),		// "this" is the anchor
		sty = stripMultiCls( cls.substring(9 + cls.indexOf('audioPop-'))),
		info = sty.split('-'),
		ct = info.length,
		mainID = (ct >= 2 ? info[1] : ""),
		personID = (ct >= 3 ? info[2] : ""),
		showPhoto = (ct >= 4 ? info[3] : ""),
		moreOptions = (ct >= 5 ? info[4] : ""),
		trkCode = (ct >= 6 ? info[5] : "");

	openAudioPlayerPopBox( e, info[0], mainID, personID, showPhoto, moreOptions, trkCode);
	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/unbound/audio-popup.css');
	ensureScript('/GS/ajax.js');
	ensureScript('/GS/ufo.js');
	ensureScript('/GM/dropshadow/dropshadow.js');
}
addLoadEvent( initPopupPage );
