var audio_player_api; 
var ad_player_api;
var audio_ad_params;
/**
 * Sets up the audio player after the ad and calls the final add notification
**/
function onClipDone(clip){
    var ap_popup = $(document.body).getElementById('ap_popup');
    
    if(!audio_player_api){
	   var flashplayer = ap_popup.getElementById('flashplayer');
       flashplayer.empty();
       
       //Ad Notify
       audioNotifyProgress(audio_ad_params['endURL']);	
       
       //Build Audio
       var playList = [{url: ap_popup.getElementById('main_audio_link').get('href'), type: 'mp3' }]; //main audio
       ap_popup.getElementById('main_audio_link').dispose();
       
       ap_popup.getElements('div.asset').each(function(el,index){
       		var audioLink = el.getElement('a.direct_audio_link');
			playList.push({ url: audioLink.get('href'), type: 'mp3' });
			audioLink.dispose();
       })//each related audio
       
       ap_popup.getElements('div.asset')
       
       //New Audio Player
	   audio_player_api = flashembed("flashplayer", {src: '/presentation/v1/flash/flowplayer/FlowPlayerLight.swf', wmode: 'transparent', bgcolor: '#ffffff', height:'26' } , {config: { 
		    baseURL: audio_ad_params['baseURL'],
		    playList: playList,
		    initialScale: 'fit', 
		    showVolumeSlider: true,
		    initialVolumePercentage: 35,
		    showFullScreenButton: false,
		    showMenu: false,
			controlsOverVideo: 'no',
			controlBarBackgroundColor: -1,
			controlBarGloss: 'low',
			videoHeight: 0,
			loop: false
		}});
		
		if(flashplayer.getElement('embed')){
			flashplayer.getElement('embed').set('height','26');
		}//For stupid IE7
		ap_popup.getElementById('audiopanel').setStyle('display','block');
		
		ap_popup.getElements('div.asset').each(function(el,index){
		    el.getElements('a').addEvent('click', function(){
		    	audio_player_api.ToClip(index+2);
		    	return false;
		    });
		});
		
		ap_popup.getElementById('status').set('html','Now Playing:');
	}
}

/**
 * Calls the first ad notification
**/
function onLoadBegin(clip){
	if(!audio_player_api && audio_ad_params['beginURL']){
		audioNotifyProgress(audio_ad_params['beginURL']);
		audio_ad_params['beginURL'] = false;	
	}
}

/**
 * Changes styles when switching between audio clips
**/
function onClipChanged(clip){
    var ap_popup = $(document.body).getElementById('ap_popup');
	var n_offset = audio_player_api.getCurrentClip() - 2;
	var a_assets = ap_popup.getElements('div.asset');
	
	ap_popup.getElement('h1').set('html',a_assets[n_offset].getElement('.caption a').get('html'));
	
	a_assets.each(function(el, index){
		if(index == n_offset){
			el.setStyle('background-image','url(/presentation/v1/images/audioplayer/np.png)');
			el.setStyle('background-repeat','no-repeat');
			el.setStyle('background-position','0px 0px');
		}else{
	    	el.setStyle('background','none');
	    }
	});

}

/**
 * Proccesses Cues
**/
function onCuePoint(cue){
	switch(cue.name){
		case 'middleURL':
	  		audioNotifyProgress(audio_ad_params['middleURL']);
	  	break;    
	}
}

/**
 * Sets up midway cue for ad url
**/
function onMetaData() {
	if(!audio_player_api){
		var duration = ad_player_api.getDuration();
		ad_player_api.addCuePoint(parseInt(duration*0.5), 'middleURL');
	}
}

/**
 * Actually makes the ad notify calls by adding an img tag
**/
function audioNotifyProgress(s_url){
    var notify = new Element('img', { 'src': s_url, id: 'audioAdImage', width: '1', height: '1' });
    notify.setStyle('display','none');
	notify.inject($(document.body),'bottom');
}


/**
 * Sets up initial ad info
**/
window.addEvent('domready', function() {
    var flashplayer = $(document.body).getElementById('ap_popup').getElementById('flashplayer');
    audio_ad_params = new Array()
    flashplayer.getElements('span').each(function(el){  audio_ad_params[el.get('title')] = el.get('html');  });	
	flashplayer.empty();
	
	if(!audio_ad_params['videoFile']) return onClipDone('');

	var clickthrough = new Element('a', {
	    'href': audio_ad_params['clickthruURL'],
	    'class': 'clickthrough',
	    'html': 'Advertisement',
	    'target': '_blank'
	});
	    	
	ad_player_api = flashembed("flashplayer", {src: '/presentation/v1/flash/flowplayer/FlowPlayerLight.swf', wmode: 'transparent' }, {config: { 
	    videoFile: audio_ad_params['videoFile'],
	    initialScale: 'scale', 
	    showVolumeSlider: true,
	    showFullScreenButton: false,
	    showMenu: false,
		controlsOverVideo: 'ease',
		controlBarBackgroundColor: -1,
		controlBarGloss: 'low',
		videoHeight: 320,
		loop: false,
		hideControls: true
	}});
	
	clickthrough.inject(flashplayer,'bottom');
}); 
