﻿// Make sure these global variables are placed in an Init function!!!

var bIsMoviePlaying    = false;		// Boolean for whether or not the movie is playing
var oNonMovieHTML      = null;		// Global for patient photo

  
function redisplayCurrentPhoto() {
	
	var elem = document.getElementById('flash_movie');

	if (elem !== null) {
	
		Dom.setStyle('flashcontent1',"display","none"); //hide flash movie
				    
	    if (bIsMoviePlaying === true && oNonMovieHTML) {
	    	var flashMovie = getFlashMovieObject('flashmovie1'); // Retrieving the Flash Movie Object; <object> tag id   
			flashMovie.stopTheVideo();	//Stopping the current video; stopTheVideo is a Flash method defined in videoplayer.swf

		   	elem.appendChild(oNonMovieHTML); //display patient photo on top of hidden flash movie
		    oNonMovieHTML = null;
	  	    bIsMoviePlaying = false;
	    }
  	}	
	return false;
}


function displayPatientImage( id, photofld, photosrc, photodescription, quotefld, quotesrc, quotedescription, filename ) {	
	redisplayCurrentPhoto();  
		
    //display new patient photo
    var elem = document.getElementById(photofld);
	elem.src = "/images/Bariatric/" + photosrc;
	elem.alt = photodescription;

	//display new play video or quote box
	var quoteElem = document.getElementById(quotefld);
	quoteElem.src = "/images/Bariatric/" + quotesrc;
	quoteElem.alt = quotedescription;
   
    var elem2 = document.getElementById('quote_box');
    if (filename !== '') {
		elem2.onclick = function(){
			playVideo(filename,id,photosrc)};	
	} else { 	
        //disable onclick for quote_box by not calling playVideo
		elem2.onclick = function() { }
	}

	// cycle through each patient name; set all to gray color
	var a = document.getElementById("patient_names").getElementsByTagName('a');
	
    for (var i = 0; i < a.length; i++) {
		if (a[i].id != id) {
			a[i].style.color = "#d0c7d2";
		} else {
			a[i].style.color = "#ffffff";
		}
	}

	return false;
}

function getFlashMovieObject(movieName) {
    if (window.document[movieName]) {
	    return window.document[movieName];
    }
  
    if (navigator.appName.indexOf("Microsoft Internet")== -1) {
    	if (document.embeds && document.embeds[movieName]) {
    	  return document.embeds[movieName];} 
    } else {
        return document.getElementById(movieName);
    }
}

function playVideo(videoTitle, id, photosrc) {
	var elem = document.getElementById('flash_movie'),
		strURL,
		dateTime = new Date(); // Unique value appended to AJAX request to ensure the request is unique every time; resolves IE caching issue
		
	if (videoTitle.length > 0) { 
		strURL = '/norwood.asp?AJAX=True&videotitle=' + encodeURIComponent(videoTitle) + '&id=' + encodeURIComponent(id) + '&photosrc=' + encodeURIComponent(photosrc);
		strURL += '&DateTimeNow=' + encodeURIComponent(dateTime.valueOf());
	}

	var responseSuccess = function(o) {
		if (bIsMoviePlaying === false) {
			oNonMovieHTML  = document.createDocumentFragment();
			while (elem.hasChildNodes()) 
			{ oNonMovieHTML.appendChild(elem.firstChild); }
		}

        bIsMoviePlaying = true;
		elem.innerHTML = o.responseText;
	};

    var responseFailure = function(o) {
		//alert('System Problem');
	};
	

	var callback = {
		success: responseSuccess,
		failure: responseFailure
	};
	
	var transaction = YAHOO.util.Connect.asyncRequest('GET', strURL, callback, null);
	return false;
}

