// JavaScript Document//CREATED FEB 09
//AUTHOR: STEPHEN ANDERSON
var modName = "HTML";
var modVer = "2.0";

this.screenX = 0;
this.screeny = 0;

//Select XML doc based on button clicked
var wprod = whichProd(); //function located in scripts/locate.js
var xmlDoc;
//	alert(xmlDoc);
    var xmlURL = wprod + ".xml";
    new Ajax.Request(xmlURL, {
        method: "get",
        asynchronous: false,
//		preserveWhiteSpace: true,
        onSuccess: function(resp, jsonObj) {
            xmlDoc = resp.responseXML;
        }
    });
		
//xmlDoc("flooring.xml"); //file to traverse

//********************************GLOBAL VARIABLES/ARRAYS*********************************/
//var famcount = 0;
//var holcount = 0;


// XML TAG NAMES EXTRACTED FROM STAINES/VIRGINIA XML DOCUMENT
var sJobTitle = xmlDoc.getElementsByTagName("staff");
var sName = xmlDoc.getElementsByTagName("name");
var sMainImage = xmlDoc.getElementsByTagName("picture");
var sBooking = xmlDoc.getElementsByTagName("booking");
var sDescription = xmlDoc.getElementsByTagName("description"); 

/******* CONSOLE: TEST LOG ******/
//console.log(xmlDoc);
//console.log(sJobTitle);
//console.log(sName);
//console.log(sMainImage);
//console.log(sBooking);
//console.log(sDescription);
/********************************/

var aJobTitle = new Array(); 
var aName = new Array(); 
var aMainImage = new Array(); 
var aBooking = new Array();
var aDescription = new Array(); 

/***********Global Product Object**********/
function fncProfile(count, sJobTitle, sName, sMainImage, sBooking, sDescription, nIter){
	this.profileCount = count;
	this.jobTitle = sJobTitle;
	this.profileName = sName;
	this.profileImage = sMainImage;
	this.profileBooking = sBooking;
	this.profileDescription = sDescription;
	this.iteration = nIter;
}
	
//var sImgCount = new Array(); //add total amount of images from XML file
/*************************************************************************************************/

function external(){
	nCount=fncProfile.profileCount;
	return nCount;
}

function verify()
{
  // 0 Object is not initialized
  // 1 Loading object is loading data
  // 2 Loaded object has loaded data
  // 3 Data from object can be worked with
  // 4 Object completely initialized
  if (xmlDoc.readyState != 4)
  {
    return false;
  }
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function checkVersion(IEVersion)
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();
	if ( ver > -1 )
  {
    if ( ver == IEVersion ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  //alert( msg );
	return ver;
}

//node traverse test function
function getElement(node)
{
while(node && node.nodeType !=1)
{
node = node.nextSibling;
}
//return node;
alert("getElement= " + node);
}

// Example:
// var b = new BrowserInfo();
// alert(b.version); 
function BrowserInfo()
{
  this.name = navigator.appName;
  this.codename = navigator.appCodeName;
  this.version = navigator.appVersion.substring(0,4);
  this.platform = navigator.platform;
  this.javaEnabled = navigator.javaEnabled();
  this.screenWidth = screen.width;
  this.screenHeight = screen.height;
}

function convertToNull(elements){
	console.log("converttonull: " + elements);
	}

//xml element values
/*******************************
var aJobTitle = new Array(); [staff:attr(position)]
var aName = new Array(); [name]
var aMainImage = new Array(); [picture]
var aBooking = new Array(); [booking]
var aDescription = new Array(); [description]
********************************/
function eleValue(elements){
	switch(elements){
		case "position":
			break;
		case "name":
			aName.push(sName[i].firstChild.nodeValue);
			break;
		case "picture":
			aMainImage.push(sMainImage[i].firstChild.nodeValue);
			break;
		case "booking":
			aBooking.push(sBooking[i].firstChild.nodeValue);
			break;
		case "description":
			aDescription.push(sDescription[i].firstChild.nodeValue);
			break;
		default:
		break;
	}
}

function createDiv(){//WE HAVE THE ID NOW APPEND THE DIV ELEMENT
	var nCount = 0;
	var bIEVer = "false";
	var buttonProfileDefault = "Show/Hide Profile";
	/*check for IE6*/
	if (checkVersion(6.0) > 0){bIEVer = "true"; chkVer = checkVersion();}

	while (nCount<fncProfile.profileCount){ //COUNTDOWN OF PRODUCTS TO BE DEVELOPED. FINSISHES AT ZERO.
	
		newDiv = document.createElement("div");
		newDiv.setAttribute("id", "frame"+nCount);	
		newDiv.setAttribute((document.all ? 'className' : 'class'), "staffdetails");
		my_div = document.getElementById("content");
		my_div.appendChild(newDiv);

		var stme = aName[nCount].lastIndexOf(" "); //Two names only with dividing space
		var naAnchor = aName[nCount].substr(stme-stme, stme);
		
		if (stme){
			newAnchor = document.createElement("a");//named anchor
			newAnchor.setAttribute("name", naAnchor);	
			newAnchor.setAttribute("title", 'testname');	
			newAnchor.innerHTML = " ";
			my_anchor = document.getElementById("frame"+nCount);
			my_anchor.appendChild(newAnchor);
		}
		
		newDivProduct = document.createElement("div");
		newDivProduct.setAttribute("id", "staffDetails"+nCount);	
		newDivProduct.setAttribute((document.all ? 'className' : 'class'), "toggle staffdetailbox");
		my_divProduct = document.getElementById("frame"+nCount);
		my_divProduct.appendChild(newDivProduct);
		
		newDivDescription = document.createElement("div");
		newDivDescription.setAttribute("id", "staffImage"+nCount);	
		newDivDescription.setAttribute((document.all ? 'className' : 'class'), "foot staffimages");
		my_divDescription = document.getElementById("frame"+nCount);
		my_divDescription.appendChild(newDivDescription);
		
		newDivInfo = document.createElement("div");
		newDivInfo.setAttribute("id", "infoConstraint"+nCount);	
		newDivInfo.setAttribute((document.all ? 'className' : 'class'), "profileInfo");
		my_divInfo = document.getElementById("frame"+nCount);
		my_divInfo.appendChild(newDivInfo);

//CREATION OF <P> TAGS
		newP_Name = document.createElement("p"); //NAME
		newP_Name.setAttribute("id", "name"+nCount);
		my_P = document.getElementById("staffDetails"+nCount);
		my_P.appendChild(newP_Name);
		
		newP_Position = document.createElement("p"); //POSITION
		newP_Position.setAttribute("id", "position"+nCount);
		my_P = document.getElementById("staffDetails"+nCount);
		my_P.appendChild(newP_Position);
	
		newP_Description = document.createElement("p"); //DESCRIPTION
		newP_Description.setAttribute("id", "description"+nCount);
		my_P = document.getElementById("staffDetails"+nCount);
		my_P.appendChild(newP_Description);
		
		newP_Info = document.createElement("p"); //PROFILEINFO
		newP_Info.innerHTML = aDescription[nCount];
		newP_Info.setAttribute((document.all ? 'className' : 'class'), "info");
		newP_Info.setAttribute("id", "profileInfo"+nCount);
		my_P = document.getElementById("infoConstraint"+nCount);
		my_P.appendChild(newP_Info);
		
		newSpan = document.createElement("span");
//		newSpan.setAttribute("id", "proddetails");
		newSpan.innerHTML = aBooking[nCount];
		my_span = document.getElementById("profileInfo"+nCount);
		my_span.appendChild(newSpan);
		
		newP_Bookings = document.createElement("p"); //BOOKINGS
		newP_Bookings.setAttribute("id", "bookings"+nCount);
		my_P = document.getElementById("staffDetails"+nCount);
		my_P.appendChild(newP_Bookings);

// CREATE <SPAN> WITH DATA FROM XML FILE
		newSpan = document.createElement("span");
//		newSpan.setAttribute("id", "proddetails");

//console.log(aName[nCount]);

		newSpan.innerHTML = aName[nCount];
		my_span = document.getElementById("name"+nCount);
		my_span.appendChild(newSpan);

		newSpan = document.createElement("span");
		newSpan.innerHTML = aJobTitle[nCount];
		my_span = document.getElementById("position"+nCount);
		my_span.appendChild(newSpan);
		
		if (bIEVer == "false" || bIEVer == "true" &&  chkVer > '6'){//if not IE6
//		alert("not ie6");
			newIMGVector = document.createElement("img");
			newIMGVector.setAttribute("width", "484px");	
			newIMGVector.setAttribute("height", "317px");
			newIMGVector.setAttribute("src", "images/vector_art.png");
			newIMGVector.setAttribute((document.all ? 'className' : 'class'), "vectorart");
			newIMGVector.setAttribute("id", "vArt");
			my_prevImg = document.getElementById("staffDetails"+nCount);
			
		}else{
			newIMGVector = document.createElement("div");
			newIMGVector.setAttribute("width", "484px");	
			newIMGVector.setAttribute("height", "317px");
			newIMGVector.setAttribute((document.all ? 'className' : 'class'), "IEvectorart");
			newIMGVector.setAttribute("id", "IEvArt");
			my_prevIMGVector = document.getElementById("product"+nCount);
			my_prevIMGVector.appendChild(newIMGVector);
			
			newIEdiv = document.createElement("div");
			newIEdiv.setAttribute("width", "595px");	
			newIEdiv.setAttribute("height", "39px");
			newIEdiv.setAttribute((document.all ? 'className' : 'class'), "IEfoot");
			newIEdiv.setAttribute("id", "IEdescription");
			my_prevnewIEdiv = document.getElementById("frame"+nCount);
			my_prevnewIEdiv.appendChild(newIEdiv);
		}
		
		newProfileImage = document.createElement("img");
		newProfileImage.setAttribute("alt", aName[nCount]);
		newProfileImage.setAttribute("title", aName[nCount]);
		newProfileImage.setAttribute("width", "225px");
		newProfileImage.setAttribute("height", "275px");
		newProfileImage.setAttribute("src", "../"+aMainImage[nCount]);	
		my_newProfileImage = document.getElementById("staffImage"+nCount);
		my_newProfileImage.appendChild(newProfileImage);

			
		if (bIEVer == "true" && chkVer == '6'){//IF IE & version 6 style elements
//			alert( "in IE6: " + checkVersion());
			var newIEDescDiv = document.getElementById("description"+nCount);
			var newIEDiv = document.getElementById("frame"+nCount);
			var newIELogoDiv = document.getElementById("logo"+nCount);
			var newIEImgDiv = document.getElementById("IEvArt");			
			
			if(document.getElementById("frame"+nCount)){
			}
			
			newIEDiv.style.position='relative';
			newIEDiv.style.height='329px';
			newIEDiv.style.marginBottom='2px';
			newIEDiv.style.overflow='hidden';
		
			newIELogoDiv.style.position='absolute';
			newIELogoDiv.style.top='268px';
			newIELogoDiv.style.backgroundColor='white';
			newIELogoDiv.style.display='block';

			newIELogoDiv.style.height='39px';
			newIELogoDiv.style.margin='4px';
			newIELogoDiv.style.	width='119px';
			newIELogoDiv.style.	zIndex='10';
		
		}
		nCount++;

	}
		return function external(nCount){};
}


//CREATE  HREF	
function createAhref(sImgTag, title, url, tiappend){
	newA = document.createElement("a");
//	alert("innerhtml: " + sImgTag[i].firstChild.nodeValue);
	newA.setAttribute("rel","lytebox[vacation]"); 
	newA.setAttribute("href", "..\/" + url);
	newA.setAttribute("title",title);
	newA.innerHTML = title;
	my_Adiv = document.getElementById(tiappend);
	my_Adiv.appendChild(newA);	
}

function fncProfilePics(){
	if (xmlDoc.documentElement){
		var	sJobTitle = (xmlDoc.getElementsByTagName("staff").item(0).getAttribute("position")); //STAFF POSITION/JOB TITLE
	}

	var sImgDir = "images\/";
	var sImgExt = "\.jpg";

	//Traverse XML document & create html elements
	if (xmlDoc.documentElement.childNodes.length > 0){//DO WE HAVE NODES TO TRAVERSE?
		try{
			//get pics count
			//adds unescaped chars to count e.g. carriage return etc		
			//alert("root child count: " + xmlDoc.documentElement.childNodes.length);
			var sTravCount = xmlDoc.getElementsByTagName("staff");

			for (i=0;i<sTravCount.length;i++){//pics length
				fncProfile.profileCount = sTravCount.length;
				fncProfile.iteration = i;
		
			var idgroup = sTravCount[i].getAttribute("position"); //attribute: group name for element creation by count [i]
	
				aJobTitle.push(idgroup); //ADD TITLE TO ARRAY
				
				var gA = aJobTitle.length;
				var sP = sTravCount.length;				
				
				var chNodesLength = sTravCount[i].childNodes.length;	
				
				for (j=0;j<chNodesLength;j++){//traverse childnodes length including  whitespace		
					var chNodesType = sTravCount[i].childNodes[j].nodeType;
					if 	(chNodesType == 1){//only ELEMENT_NODE == 1	
						eleValue(sTravCount[i].childNodes[j].nodeName);  //HERE IS WHERE WE STORE ARRAYS OF XML FILE
					}	
				}
				
				if (gA == sP ){//CHECK COUNT TO SEE IF YOUR READY TO CREATE THE TAGS
					for (var ti in aJobTitle){ //WE HAVE THE COUNT OF GROUPS HERE IN ti OBJECT - THE NUMBER SPECIFIES EACH FILE IN EACH GROUP					
						if (aJobTitle.hasOwnProperty(ti)) { // TEST PROPERTIES OF OBJECT: 
							var tiappend;
							var showhide;

							if (aJobTitle[ti]){
								if (aJobTitle.indexOf(aJobTitle)){
								}
								if(document.getElementById("groupfamily0")){
									tiappend = "lifamily" + ti;								
									createLi('groupfamily0', showhide, tiappend);
								}
							}
						}					
					}
				}
			} //end for loop
		createDiv();

		}
		catch(error){
			alert (error);
		}
		return;
	}

document.write("<br />");
document.write("<br />");
}

var activated = 0; //flag

/*
 * return the 'a:name' attribute holding the employee name - if it matches the show the profile
 * and change the style rules to apply the positioning. Style rule should be [class="staffdetails"]
 */


//Jquery Show/Hide slide profile images into focus and out
jQuery(document).ready(function() {
	//Document load - no need to wait for window to finish.
	//window.onload= function(){ //used to load functions on window (load is complete)
	fncProfilePics();

	
	var divId = jQuery('div#content > div');
	
	jQuery.fn.noSee = function(html) {//Hide all frame[i] elements created dynamically
		return jQuery(divId).each(function(){
			var divIdAttr = jQuery(this).attr('id');
			jQuery(this).addClass('staffdetails').hide();
//			console.log(divIdAttr);		
		});
	};
	
	jQuery.fn.wrappedProfile = function(html) {
		return this.each(function(){
		
			var eleName = linkName();
			var tParent = jQuery(this);
			
			atChild = jQuery(this).children(i);
			atName = atChild.attr('name');//anchor name
			
			if(eleName == atName){
				tParent.fadeIn(2000);
			}			
		});
	};

	jQuery('.profileInfo').hide(); 
	var jANext  = jQuery("span.showfullprofile");
	
	

	
	jQuery(this).noSee();
		
	
		jQuery('#content').children().wrappedProfile();

jQuery('.profileInfo').slideDown(5000); 


	
});
