var players = {};

function audioPlayer(playerDivId) 
{
	this.config = {
		"playerId" : "",
		"playerDivId" : ""
	};

	this.config.playerDivId = playerDivId;
	
	this.playFile = function(mp3){
		if (this.config.playerId != "") {
			var obj = swfobject.getObjectById(this.config.playerId)
			obj.loadXML(this.getPlayList(mp3));
		}
		else {
			this.renderPlayer(mp3);
		}
	};
	
	this.getPlayList = function(mp3) {
		var xml = '<playlist><clip url="'+mp3+'" autoplay="true"></clip><clip url="//www.daveramsey.com/media/image/general/1x1_invisible.gif" autoplay="true"></clip></playlist>';
		return xml;
	};
	
	this.renderPlayer = function(mp3) {
		
		var xml = this.getPlayList(mp3);
		xml = xml.replace(/"/g, "\%22");		
		var flashvars = {
			XMLData: xml,
			skin: 'http://www.daveramsey.com/media/flash/player_skins/andres_player/skin.css'
		};
		var params = {
			bgcolor: '#888888',
			wmode: 'transparent',
			quality: 'best',
			allowScriptAccess: 'always'
		};
		this.config.playerId = 'audioPlayer' + Math.floor(Math.random() * 1000000);
		var attributes = {
			id: this.config.playerId,
			name: this.config.playerId
		};
		swfobject.embedSWF(
			'//www.daveramsey.com/media/flash/andres_audio_player.swf',
			this.config.playerDivId + 'Player',
			'250',
			'20',
			'9.0.28',
			'//www.daveramsey.com/media/flash/expressinstall.swf',
			flashvars,
			params,
			attributes
		);
		$j("#"+this.config.playerDivId+"StartImg").hide();
		$j("#"+this.config.playerDivId+"Wrapper").show();
		
	};
	
	return true;
};


function toggleArticle(content_item_id, state) {
	if (state == 'on') {
		document.getElementById("body-"+content_item_id).style.display = "block";
		document.getElementById("teaser-"+content_item_id).style.display = "none";
	}
	else {
		document.getElementById("body-"+content_item_id).style.display = "none";
		document.getElementById("teaser-"+content_item_id).style.display = "block";
	}
}

function play(playerDivId, file)
{
	if (players[playerDivId] == null)
	{
		players[playerDivId] = new audioPlayer(playerDivId);
	}	
	players[playerDivId].playFile(file);

}

$j(document).ready(function() {

	$j('a.show-more').each(function() {
		this.onclick = function(){ var content_item_id = this.id.replace('show-more-',''); toggleArticle(content_item_id, 'on'); return false; };
	});
	$j('a.hide-more').each(function() {
		this.onclick = function() { var content_item_id = this.id.replace('hide-more-',''); toggleArticle(content_item_id, 'off'); return false; };
	});

	$j('a.audioLink').each(function() {
		this.onclick = function(){ play('audioPlayer', this.href); return false; };
	});

	$j('a.askAndresAudioLink').each(function() {
		this.onclick = function(){ play(this.id, this.href); return false; };
	});

	$j('li.video-link').each(function() {
		this.onclick = function(){ 
			var clip = this.id.split('-', 1); 
			var objId = this.id.split('-', 2)[1]; 
			swfobject.getObjectById(objId).goto(clip);
			return false;
		};
	});

	$j('.crc-materials li').each(function() {
		this.onclick = function(){ 
			var links = $j(this).children('a');
			location.href = links[0].href;
			return true;
		};
	});

});





