/*******************************************************************************
____________________________ API DOCUMENTATION BEGIN ___________________________
````````````````````````````````````````````````````````````````````````````````
Used on home page to manage the loading of specialties.

````````````````````````````````````````````````````````````````````````````````
_____________________________ API DOCUMENTATION END ____________________________
*******************************************************************************/

//------------------------------------------------------------------------------
// VARIABLES BEGIN
//------------------------------------------------------------------------------

SPECIALTYMAX = 13; //about of files to preload
specialtyManagerArr = new Array();
userLoadRequest = null;
var xmlreq = null;//xmlURL to load swf

//------------------------------------------------------------------------------
// VARIABLES END
//------------------------------------------------------------------------------

//CLASS CONSTRUCTOR-- creates specialty objects
function Specialty_Object(argPage) {
 this.page = argPage; //page to load
 this.pageContent = ""; //content of page 
 this.loadStatus = "loadMe"; //loadMe | ready
}

//FUNCTION-- build specialty objects
function initSpecialty() {
 for(var j=0;j<SPECIALTYMAX;j++)
 {
  specialtyManagerArr.push(new Specialty_Object("specialty"+(j+1)+".html")); //load each object into array
 }
 setInitialPage();
}

//FUNCTION-- loads initial page
function setInitialPage() {
 if(getCookie("sp")) //if user selected a specialty from a previous visit 
 {
  var spValue = getCookie("sp"); //set user page
	if(spValue <= SPECIALTYMAX) //make sure user page does not exceed amount of available specialities. This is incase the amount of specialities has changed and the user revisits site with a cookie set for a speciality that may not exist anymore.
	{
	 setLoadRequest(spValue);
	 loadIframe(specialtyManagerArr[spValue-1].page); //load user defined page
	}
  else loadIframe(specialtyManagerArr[0].page); //user defined page does not fall within SPECIALTYMAX so we load default page
 }
 else loadIframe(specialtyManagerArr[0].page); //load default page
}

//FUNCTION-- loads page into hidden Iframe
function loadIframe(argPage) {
 //Note: code here fixes IE clicking sound when iframes load. This fix is transparent to other browsers
 var newFrame = document.createElement("iframe"); // create element node
 newFrame.id = "spLoader"; //set element id
 newFrame.src = argPage; //set content
 //build a reference to the existing node to be replaced
 var oldFrame = document.getElementById("spLoader");
 var parentDiv = oldFrame.parentNode;
 parentDiv.replaceChild(newFrame, oldFrame); //replace existing node
 //Note: setSpecialty(N) is automatically invoked from the onload event in each page that is loaded into the Iframe
}

//FUNCTION-- updates specialty object (this function is called from the onload event in the pages that are loaded into the Iframe)
function setSpecialty(argSpecialtyNum) {

 if(pageLoaded)
 {
  specialtyManagerArr[argSpecialtyNum-1].pageContent = frames[0].document.body.innerHTML; //copy HTML from hidden Iframe to property
  specialtyManagerArr[argSpecialtyNum-1].loadStatus = "ready"; //set load status  
  if(userLoadRequest != null)
  	xmlreq = userLoadRequest;
  if(argSpecialtyNum == userLoadRequest)
  {
  userLoadRequest = null; //reset
  loadDiv(argSpecialtyNum); //load requested page
  }
 loadSpecialty(); //invoke loading of the next specialty
 }

}

//Function to load SWF
function  loadSWF()
{


	var xmlurl = "";
	if(null == xmlreq)//for index
	{
		xmlurl = "/levaquin360/assets/levaquin360.xml";
	}
	else if(xmlreq == 1 || xmlreq == 2){
		xmlurl = "/levaquin360/assets/primaryHealthcare.xml";
	}
	else if(xmlreq == 3){
		xmlurl = "/levaquin360/assets/emergencyMed.xml";
	}
	else if(xmlreq == 4){
		xmlurl = "/levaquin360/assets/hospitalists.xml";
	}
	else if(xmlreq == 5 || xmlreq == 6){
		xmlurl = "/levaquin360/assets/infectiousDisease.xml";
	}
	else if(xmlreq == 7){
		xmlurl = "/levaquin360/assets/otolaryngologists.xml";
	}
	else if(xmlreq == 8 || xmlreq == 9){
		xmlurl = "/levaquin360/assets/pulmonologists.xml";
	}
	else if(xmlreq == 10){
		xmlurl = "/levaquin360/assets/urologists.xml";
	}
	else if(xmlreq == 11){
		xmlurl = "/levaquin360/assets/ob-gyn.xml";
	}
	else if(xmlreq == 12){
		xmlurl = "/levaquin360/assets/communityPharm.xml";
	}
	else if(xmlreq == 13){
		xmlurl = "/levaquin360/assets/hospitalPharm.xml";
	}
	var so = new SWFObject("/levaquin360/assets/mic_contentFlipper.swf", "mic_contentFlipper", "236", "207", "8", "#FFFFFF");
	so.addParam("wmode", "transparent");
	so.addVariable("xmlURL", xmlurl); //path to xml file
	so.write("groupContainerDiv");
	
	var so = new SWFObject("/levaquin360/assets/erep_rotator.swf", "erep_rotator", "235", "105", "8", "#FFFFFF"); // file name, instance name, width, height, player, bkg color
so.addParam("wmode", "window");
so.addVariable("clickTag1", "http://www.levaquin360.com/levaquin360/coupon.html");
so.addVariable("clickTag2", "http://www.ortho-mcneil.com/ortho-mcneil/blackbag/blackbag.html");
so.addVariable("clickTag3", "http://www.levaquin360.com/levaquin360/pneumonia-management-tool.html");
so.write("contentRotator");
}

//FUNCTION-- populates div if content is ready
function loadDiv(argSpecialtyNum) {
 setCookie("sp", argSpecialtyNum, 1825);
 xmlreq = argSpecialtyNum;
 document.getElementById("contentContainerDiv").innerHTML = "Loading...";
 if(specialtyManagerArr[argSpecialtyNum-1].loadStatus == "ready") {
 document.getElementById("contentContainerDiv").innerHTML = specialtyManagerArr[argSpecialtyNum-1].pageContent;}//page has been loaded populate div
 else //page not loaded go get it
 {
  setLoadRequest(argSpecialtyNum); //set requested page for loading
  loadIframe(specialtyManagerArr[argSpecialtyNum-1].page);
 }
	loadSWF();

}

//FUNCTION-- loops through array of specialty object check loadStatus property
function loadSpecialty() {
 for(var d=0;d<SPECIALTYMAX;d++)
 {
  if(specialtyManagerArr[d].loadStatus == "loadMe") //if loadStatus is not ready
	{
	 loadIframe(specialtyManagerArr[d].page); //load page into Iframe
	 break;
	}
 }
}

//FUNCTION-- sets load request
function setLoadRequest(argPage) {
 userLoadRequest = argPage;
}

//FUNCTION-- sets a cookie
function setCookie(cookieName, cookieValue, expireDate) { 
 if(expireDate != null)
 {
  //set expiration date for cookie
	var expires = new Date();                                   
	expires.setTime(expires.getTime( ) + (1000 * 60 * 60 * 24 * expireDate)); //days ahead
 }
 document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/" + ((expireDate == null) ? ";" : "; expires=" + expires.toGMTString());
}

//FUNCTION-- retrieves a cookie
function getCookie(cookieName) {
 var cookieNameStr = cookieName + "=";               
 var dc = document.cookie;
	            
 if(dc.length > 0)
 {              
  var beginStr = dc.indexOf(cookieNameStr);       
  if(beginStr != -1) 
  {           
   beginStr += cookieNameStr.length;       
   var endStr = dc.indexOf(";", beginStr);
   if(endStr == -1) {endStr = dc.length;}
   return unescape(dc.substring(beginStr, endStr));
  } 
 }
 return null;
}
