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('#requestinfoXXreferer').change( function() {
	if( $j('#requestinfoXXreferer option:selected').text() == 'Other' || $j('#requestinfoXXreferer option:selected').text() == "Otra" ) {
		$j('#requestinfoXXreferer_other_field_wrapper').show();
		$j('#requestinfoXXreferer_other').focus();
	}
	else {
		$j('#requestinfoXXreferer_other_field_wrapper').hide();
		$j('#requestinfoXXhow_did_you_hear').val( $j('#requestinfoXXreferer option:selected').text() );
	}
});

$j('#requestinfoXXreferer_other').change( function() {
	$j('#requestinfoXXhow_did_you_hear').val( $j('#requestinfoXXreferer_other').val() );
});

$j(document).ready(function() {
	
	/*Dynamically load CRC videos into GUX player*/
	$j(".video-link").click(function(e){
		e.preventDefault();
		var videoId = $j(e.target).attr("id");
		loadAndPlayVideo(videoId);
	});

	$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('.crc-materials li').each(function() {
		this.onclick = function(){ 
			var links = $j(this).children('a');
			location.href = links[0].href;
			return true;
		};
	});

	$j('#requestinfoXXreferer_other_field_wrapper').hide();
	
	// Check to see if they're using a mobile device, and if not, disable click to call anchors
	if(!isMobileDevice()){
		$j('.c2c').attr('href','javascript:;').css('cursor','default');
	}

	

});

function loadAndPlayVideo(videoId){
	andresResources_ytplayer_video_object.load_video_by_id(videoId);
	andresResources_ytplayer_video_object.play_loaded_video();
}

function isMobileDevice() {
	var blnTouchDevice	= false;
	
	if(navigator.userAgent.match(/iPhone|iPod|iPad|Android/)) {
		blnTouchDevice	= true;
	}
	
	return blnTouchDevice;
}
