/**
 *	@author			Stewart Smith
 *	@email			stewart.smith@beamland.com
 *	@version		1.0
 *
 *	@history
 * 	04.24.2009		ssmith		Initial Version 
 *	
 *	embed code using swfobject v2.1
 */
	var AthenaVideoPlayer = {
		// variables for swfobject
		swfUrlStr		:'/_swf/VideoShell.swf',
		replaceElemIdStr:'flashModule',
		widthStr		:'694',
		heightStr		:'390',
		swfVersionStr	:'9.0.0',
		xiSwfUrlStr		:'/_swf/expressInstall.swf',
		flashvarsObj	:{
			sourceID			:'1',
			regStatus			:'false',
			trackAccount		:'athenahealthtest',
		playlistURL			:'',
		defaultVideoID		:'',
		autoPlayDefault		:'false',
			defaultPlaylistOpen	:'true',
			defaultCornersRound	:'false',
			trackNetPath		:'/_inc/video_tracking.php', 
			swfPath				:'/_swf/AthenaVideoPlayer.swf'
		},
		paramsObj		:{
			scale				:'noScale',
			salign				:'lt',
			menu				:'false',
			quality				:'high',
			bgcolor				:'#cccccc',
			wmode				:'transparent',
			allowFullScreen		:'true',
			allowScriptAccess	:'sameDomain'
		},
		attributesObj	:{
			id					:'athenaVideoPlayer',
			name				:'athenaVideoPlayer'
		},
		
		// functions
		embed:function()
		{
			// embed/re-embed the swf
			// this.widthStr = '694';
			// this.heightStr = '390';
		swfobject.embedSWF( this.swfUrlStr + '?rand=' + new Date().getTime(), this.replaceElemIdStr, this.widthStr, this.heightStr, this.swfVersionStr, this.xiSwfUrlStr, this.flashvarsObj, this.paramsObj, this.attributesObj );
		},
		embedDemo:function()
		{
			// embed/re-embed the swf
			this.widthStr = '850';
			this.heightStr = '520';
		swfobject.embedSWF( this.swfUrlStr + '?rand=' + new Date().getTime(), this.replaceElemIdStr, this.widthStr, this.heightStr, this.swfVersionStr, this.xiSwfUrlStr, this.flashvarsObj, this.paramsObj, this.attributesObj );
		},
		defaultPlaylistURL:function( $filePath )
		{
			// if param passed - update var
			if( $filePath != undefined ) this.flashvarsObj.playlistURL = $filePath;
			this.embed();
		},
		defaultPlaylistOpen:function( $bool )
		{
			// if called without a param - make it open at start
			if( $bool == undefined ) $bool = true;
			
			// update var
			if( $bool || $bool == 'true' )
			{
				this.flashvarsObj.defaultPlaylistOpen = 'true';
			}
			else if( !$bool || $bool == 'false' )
			{
				this.flashvarsObj.defaultPlaylistOpen = 'false';
			}
			
			// re-embed the swf
			this.embed();
		},
		defaultCornersRound:function( $bool )
		{
			// if called without a param - make rounded corners
			if( $bool == undefined ) $bool = true;
			
			// update var
			if( $bool || $bool == 'true' )
			{
				this.flashvarsObj.defaultCornersRound = 'true';
			}
			else if( !$bool || $bool == 'false' )
			{
				this.flashvarsObj.defaultCornersRound = 'false';
			}
			
			// re-embed the swf
			this.embed();
		},
		// from swfaddress
		
		_swfChange:function()
		{
			for (var i = 0, id, obj, value = SWFAddress.getValue(), setter = 'setSWFAddressValue'; id = _ids[i]; i++)
			{
				obj = document.getElementById(id);
				if (obj)
				{
					if (obj.parentNode && typeof obj.parentNode.so != UNDEFINED)
					{
						obj.parentNode.so.call(setter, value);
					}
					else
					{
						if (!(obj && typeof obj[setter] != UNDEFINED))
						{
							var objects = obj.getElementsByTagName('object');
							var embeds = obj.getElementsByTagName('embed');
							obj = ((objects[0] && typeof objects[0][setter] != UNDEFINED) ? 
								objects[0] : ((embeds[0] && typeof embeds[0][setter] != UNDEFINED) ? 
									embeds[0] : null));
						}
						if (obj)
							obj[setter](value);
					} 
				}
				else if (obj = document[id])
				{
					if (typeof obj[setter] != UNDEFINED)
						obj[setter](value);
				}
			}
		}
	};
	
	var isInIFrame = ( window.location != window.parent.location ) ? true : false;

	loadFullReg = function( sourceID, videoID ) {	
		tb_show(null,'/forms/leadform.php?src='+sourceID+'&type=gateway&height=700&width=946&TB_iframe=true',false);
	}
	
	loadDemoReg = function( sourceID, videoID ) {
		// for safety, convert source ID to string so no switch statements below break
		sourceID = sourceID.toString();

		// check whether the users already registered
		// if yes, then show the demo
		if ($.cookie('revisit') || $.cookie('dfex')) {

			switch(sourceID) {
				case "47": // collector
					var vidID = "6";
					break;

				case "48": // clinicals
					var vidID = "2";
					break;
				
				case "56":
				case "74": // communicator
					var vidID = "9";
					break;
			}

			if ($('#lyr_video').length > 0) {
				window.location.href = '/video.php?vid='+vidID;
				self.parent.sizeThickboxToVideoPlayer();
			}
			else {
				// alert( 'loadContactUs( ' + sourceID + ' )' );
				tb_show(null,'/video-player.php?vid='+vidID+'&height=1300&width=890&TB_iframe=true',false);
			}
		}
		// if no, then show the form
		else {

			switch(sourceID) {
				case "47": // collector
					var srcID = "49";
					break;

				case "48": // clinicals
					var srcID = "50";
					break;
				
				case "56":
				case "74": // communicator
					var srcID = "74";
					break;
					
			}

			if ($('#lyr_video').length > 0) {
				window.location.href = '/forms/leadform.php?src='+srcID+'&type=demo&height=700&width=946&TB_iframe=true';
				self.parent.sizeThickboxToContact();
			}
			else {
				// alert( 'loadContactUs( ' + sourceID + ' )' );
				tb_show(null,'/forms/leadform.php?src='+srcID+'&type=demo&height=700&width=946&TB_iframe=true',false);
			}
		}
			
		/*
		// The videoID could be null if the call is form a postroll 'Watch Demo' 
		if( AthenaVideoPlayer.flashvarsObj.regStatus != 'true' )
		{
			alert( 'loadDemoReg( ' + sourceID + ', ' + videoID + ' )' );
			AthenaVideoPlayer.flashvarsObj.regStatus = 'true';
		}
		if( videoID != undefined && videoID != '' )
		{
			AthenaVideoPlayer.flashvarsObj.defaultVideoID = videoID;
		}
		else
		{
			AthenaVideoPlayer.flashvarsObj.defaultVideoID = '';
			AthenaVideoPlayer.flashvarsObj.playlistURL = 'lang/en/playlist_demo.xml';
		}
		AthenaVideoPlayer.flashvarsObj.defaultCornersRound = 'true';
		AthenaVideoPlayer.embedDemo();
		*/
	}

	cancelDemo = function( sourceID ) {
		// alert( 'cancelDemo( ' + sourceID + ' )' );
		self.parent.tb_remove();
	}

	loadContactUs = function( sourceID, formType ) {
		if (typeof(formType) == 'undefined') {
			formType = 'contact';	
		}
		
		if ($('#lyr_video_demo').length > 0) {
			$('#lyr_video_demo').css({display:'none'});
			window.location.href = '/forms/leadform.php?src='+sourceID+'&type='+formType;
			self.parent.sizeThickboxToContact();
		}
		else if ($('#lyr_video').length > 0 || $('#lyr_video_player').length > 0) {
			window.location.href = '/forms/leadform.php?src='+sourceID+'&type='+formType;
			self.parent.sizeThickboxToContact();
		}
		else {
			// alert( 'loadContactUs( ' + sourceID + ' )' );
			tb_show(null,'/forms/leadform.php?src='+sourceID+'&type='+formType+'&height=700&width=946&TB_iframe=true',false);
		}
	}
	
	loadCollector = function(fromLayer) {
		var destinationURL = '/our-services/athenaCollector/medical-billing.php';

		if (isInIFrame) {
			self.parent.location = destinationURL;
			self.parent.tb_remove();
		}
		else {
			window.location.href = destinationURL;
		}
	}
	loadClinicals = function(fromLayer) {
		var destinationURL = '/our-services/athenaClinicals/EMR.php';
		
		if (isInIFrame) {
			self.parent.location = destinationURL;
			self.parent.tb_remove();
		}
		else {
			window.location.href = destinationURL;
		}
	}
	loadCommunicator = function(fromLayer) {
		var destinationURL = '/our-services/athenaCommunicator/patient-communication.php';
		
		if (isInIFrame) {
			self.parent.location = destinationURL;
			self.parent.tb_remove();
		}
		else {
			window.location.href = destinationURL;
		}
	}
	loadCoordinator = function(fromLayer) {
		var destinationURL = '/our-services/athenaCoordinator/care-coordination.php';
		
		if (isInIFrame) {
			self.parent.location = destinationURL;
			self.parent.tb_remove();
		}
		else {
			window.location.href = destinationURL;
		}
	}
	loadGuarantee = function() {
		tb_show(null,'/our-company/hitech-guarantee.php?height=1300&width=724&TB_iframe=true',false);
	}
	loadCollectorDemo = function(fromLayer) {
		var destinationURL = '/our-services/athenaCollector.php?open=vid51';

		if (isInIFrame) {
			self.parent.location = destinationURL;
			self.parent.tb_remove();
		}
		else {
			window.location.href = destinationURL;
		}
	}
	loadCommunicatorDemo = function(fromLayer) {
		var destinationURL = '/our-services/athenaCommunicator.php?open=vid52';
		
		if (isInIFrame) {
			self.parent.location = destinationURL;
			self.parent.tb_remove();
		}
		else {
			window.location.href = destinationURL;
		}
	}
	
	loadHITECH = function(fromLayer) {
		var destinationURL = '/meaningful-use.php';

		if (isInIFrame) {
			self.parent.location = destinationURL;
			self.parent.tb_remove();
		}
		else {
			window.location.href = destinationURL;
		}
	}

	
	sizeToVideo = function( videoID ) {
		AthenaVideoPlayer.flashvarsObj.defaultVideoID = videoID;
		AthenaVideoPlayer.flashvarsObj.defaultCornersRound = 'false';
		AthenaVideoPlayer.embed();
	}

	sizeToDemo = function( videoID ) {
		AthenaVideoPlayer.flashvarsObj.defaultVideoID = videoID;
		AthenaVideoPlayer.flashvarsObj.defaultCornersRound = 'true';
		AthenaVideoPlayer.embedDemo();
	}

	playVideo = function( videoID ) {
		document.getElementById('athenaVideoPlayer').playVideo( videoID );
		return false;
	}
	playParentVideo = function( videoID ) {
		self.parent.document.getElementById('athenaVideoPlayer').playVideo( videoID );
		self.parent.tb_remove();
		return false;
	}
	
	
	switchVideo = function(videoObj) {
		if ($(videoObj).parent().hasClass('video')) {
			var swapObj = $(videoObj).parent();
		}
		else {
			var swapObj = $(videoObj).parent().parent();	
		}
		
		// get the id of the new video to play
		var newVideoPlaylistID = $(videoObj).attr('rel');
		
		// if this is a demo, then check if the revisit cookie is set
		// if it's not, then pop the form instead
		if ((newVideoPlaylistID == 'athenaClinicals Software Demo' || newVideoPlaylistID == 'athenaClinicals Software Demo - Product Page' || newVideoPlaylistID == 'athenaCollector Software Demo' || newVideoPlaylistID == 'athenaCollector Software Demo - Product Page' || newVideoPlaylistID == 'athenaCommunicator Software Demo' || newVideoPlaylistID == 'athenaCommunicator Software Demo - Product Page') && !$.cookie('revisit')) {

			switch(newVideoPlaylistID) {
				case "athenaClinicals Software Demo":
				case "athenaClinicals Software Demo - Product Page":
					window.location.href = '/forms/leadform.php?src=50&type=demo&height=700&width=946&TB_iframe=true';
					self.parent.sizeThickboxToContact();
					break;
					
				case "athenaCollector Software Demo":
				case "athenaCollector Software Demo - Product Page":
					window.location.href = '/forms/leadform.php?src=49&type=demo&height=700&width=946&TB_iframe=true';
					self.parent.sizeThickboxToContact();
					break;
					
				case "athenaCommunicator Software Demo":
				case "athenaCommunicator Software Demo - Product Page":
					window.location.href = '/forms/leadform.php?src=74&type=demo&height=700&width=946&TB_iframe=true';
					self.parent.sizeThickboxToContact();
					break;
			}

		}
		else {
		
			// hide transcript if it's showing
			$('#transcriptContainer').hide();
		
			// remove "last" from last related element
			$('.related div').removeClass('last');
			
			// move current video to the spot where we just clicked
			$('.current .video').clone().insertBefore($(swapObj));
			$('.current .video').remove();
			
			// move clicked video to current position
			$(swapObj).clone().appendTo($('.current'));
			$(swapObj).remove();
	
			// add in last class
			$('.related div:last-child').addClass('last');
	
			// change page title and description
			for (var i=0; i<videoJSON.videos.length; i++) {
				if (videoJSON.videos[i].id == newVideoPlaylistID) {
					if (videoJSON.videos[i].transcript.length) {
						var videoDescription = videoJSON.videos[i].description + ' ' + '<a id="toggleTranscript" href="#">View Transcript</a>';
					}
					else {
						var videoDescription = videoJSON.videos[i].description;
					}
					$('#videoTitle').html(videoJSON.videos[i].title);
					$('#videoDescription').html(videoDescription);
					$('#transcript').html(videoJSON.videos[i].transcript);
					break;
				}
			}
	
			// reapply click handler
			$('.current .video a').unbind('click');
			$('.related .video a').unbind('click').click(function(event) {
				event.preventDefault();
				switchVideo($(this));										 
			});
			
			// reapply show/hide transcript toggle
			if ($("#toggleTranscript").length) {
				$("#toggleTranscript").toggle(
					function() {
						$("#transcriptContainer").fadeIn(300);
						$("#toggleTranscript").html("Hide Transcript");
					}, 
					function() {
						$("#transcriptContainer").fadeOut(100);
						$("#toggleTranscript").html("Show Transcript");
					}
				);
			}
			
			// play the video
			playVideo(newVideoPlaylistID);
			
		}
	}
