﻿// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject()
var pages=0;
var codes=0;
var imgs=0;
var subs=0;
var teams=0;
var firsts=0;
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
																		"MSXML2.XMLHTTP.5.0",
																		"MSXML2.XMLHTTP.4.0",
																		"MSXML2.XMLHTTP.3.0",
																		"MSXML2.XMLHTTP",
																		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for(i=0;i<XmlHttpVersions.length && !xmlHttp;i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

// read a file from the server
function process(code,page,img,sub,team,first)
{
	// only continue if xmlHttp isn't void
	if(xmlHttp)
	{
		try
		{
			// initiate reading a file from the server
			if(page!=0)
			{				
				pages=page;
				codes=code;
				imgs=img;				
				subs=sub;
				teams=team;
				firsts=first;
				if(sub==1)
				{
					PicBig = document.getElementById("SideShowPicBig");
					PicBig.innerHTML="";
					xmlHttp.open("GET","./SideShow.php?code=" + code +"&page="+page+"&statu=1",true);
				}
				else
				{
					xmlHttp.open("GET","./SideShow.php?code=" + code +"&page="+page+"&statu=0",true);
				}
				xmlHttp.onreadystatechange = handleRequestStateChange;
				xmlHttp.send(null);			
			}
		}
		// display the error in case of failure
		catch(e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

function handleRequestStateChange()
{
	// when readyState is 4, we are ready to read the server response
	if(xmlHttp.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if(xmlHttp.status == 200)
		{
			// do something with the response from the server
			handleServerResponse();
		}
		else
		{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function handleServerResponse()
{
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
	throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror")
	throw("Invalid XML structure");
	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	// obtain arrays with image name
	nameArray = xmlRoot.getElementsByTagName("name");
	titleArray = xmlRoot.getElementsByTagName("title");
	// generate HTML output
	var html = "";
	// iterate through the arrays and create an HTML structure
	
	for(i=0;i<nameArray.length;i++)
	{
		if(i==0)
		{			
			num=nameArray.item(i).firstChild.data;
			page=num/4;			
			page=parseInt(page);
			if(num%4!=0)
			{
				page++;
			}	
			
			if(pages==1)
			{				
				cnlPre = document.getElementById("controlPre");
				if(subs==1)
				{
					cnlPre.innerHTML="<div onmouseup='process(\"" + codes + "\",0,1,1)' style='margin-left:10px;'><img src='./images/previous.gif' /></div>";
				}
				else
				{
					cnlPre.innerHTML="<div onmouseup='process(\"" + codes + "\",0,1)' style='margin-left:10px;'><img src='./images/previous.gif' /></div>";
				}				
			}	
			else
			{
				cnlPre = document.getElementById("controlPre");
				page1=pages-1;				
				if(subs==1)
				{
					cnlPre.innerHTML="<div onmouseup='process(\"" + codes + "\"," + page1 + ",1,1)' style='margin-left:10px;'><img src='./images/previous.gif' /></div>";
				}
				else
				{
					cnlPre.innerHTML="<div onmouseup='process(\"" + codes + "\"," + page1 + ",1)' style='margin-left:10px;'><img src='./images/previous.gif' /></div>";
				}
				
			}
			if(pages==page)
			{		
				
				cnlNext = document.getElementById("controlNext");				
				if(subs==1)
				{
					cnlNext.innerHTML="<div onmouseup='process(\"" + codes + "\",0,1,1)' style='margin-right:10px;'><img src='./images/next.gif' /></div>";
				}
				else
				{
					cnlNext.innerHTML="<div onmouseup='process(\"" + codes + "\",0,1)' style='margin-right:10px;'><img src='./images/next.gif' /></div>";
				}
				
			}
			else			
			{				
				cnlNext = document.getElementById("controlNext");
				page1=pages+1;				
				if(subs==1)
				{
					cnlNext.innerHTML="<div onmouseup='process(\"" + codes + "\"," + page1 + ",1,1)' style='margin-right:10px;'><img src='./images/next.gif' /></div>";
				}
				else
				{
					cnlNext.innerHTML="<div onmouseup='process(\"" + codes + "\"," + page1 + ",1)' style='margin-right:10px;'><img src='./images/next.gif' /></div>";
				}
				
			}
		
		}		
		else if(i==imgs)
		{
			opts="";
			if(subs==1)
			{
				opts=",1,\""+opts+"\"";
				if(titleArray.item(i).firstChild.data!="Null")
				{
					Title = document.getElementById("SideShowTitle");
					Title.innerHTML=titleArray.item(i).firstChild.data;
				}
			}								
			PicBig = document.getElementById("SideShowPicBig");
			PicBig.innerHTML="<img src='includes/image.php?size=345&file=../directories/pictures/" + nameArray.item(i).firstChild.data + "'  />";
			html +="<a><img src='includes/image.php?size=70&file=../directories/pictures/" + nameArray.item(i).firstChild.data + "'  onmouseup='process(\""+codes+"\"," + pages + "," + i +opts+ ");'  style='opacity:1;filter:alpha(opacity=100)' /></a>" ;
			
		}
		else
		{
			opts="";
			if(subs==1)
			{
				opts=",1,\""+opts+"\"";
			}
			html +="<a><img src='includes/image.php?size=70&file=../directories/pictures/" + nameArray.item(i).firstChild.data + "'  onmouseup='process(\""+codes+"\"," + pages + "," + i +opts+ ");'  style='opacity:0.4;filter:alpha(opacity=40)' /></a>" ;
		}		
	}
	if(subs==1)
	{		
		if(teams=='News')
		{
			PicBig = document.getElementById("News");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពថ្ចាក់ដឹកនាំ ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("News");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}				
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពថ្ចាក់ដឹកនាំ ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);
		}
		if(teams=='Network')
		{
			PicBig = document.getElementById("Network");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពហេដ្ឋារចនាសម្ព័ន្ធ ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Network");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}				
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពហេដ្ឋារចនាសម្ព័ន្ធ ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);		
		}
		if(teams=='Vehicle')
		{
			PicBig = document.getElementById("Vehicle");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពក្រុមការងារយានយន្ដ ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Vehicle");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}		
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពក្រុមការងារយានយន្ដ ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);
		}
		if(teams=='Resident')
		{
			PicBig = document.getElementById("Resident");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពក្រុមការងារស្ថិតិប្រជាជន ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Resident");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}			
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពក្រុមការងារស្ថិតិប្រជាជន ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);
		}
		if(teams=='Realestate')
		{
			PicBig = document.getElementById("Realestate");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពក្រុមការងារដីថ្លី ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Realestate");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}		
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពក្រុមការងារដីថ្លី ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);
		}
		if(teams=='Human')
		{
			PicBig = document.getElementById("Human");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពបណ្ដុះបណ្ដាលធនធានមនុស្ស ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Human");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}		
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពបណ្ដុះបណ្ដាលធនធានមនុស្ស ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);	
		}
		if(teams=='Camsert')
		{
			PicBig = document.getElementById("Camsert");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពសន្ដិសុខបច្ចេកវិទ្យាព័ត៌មាន ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Camsert");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}				
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពសន្ដិសុខបច្ចេកវិទ្យាព័ត៌មាន ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);			
		}
		if(teams=='Policy')
		{
			PicBig = document.getElementById("Policy");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពក្រុមការងារគោលនយោបាយយុទ្ធសាស្រ្ដ ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Policy");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}			
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពក្រុមការងារគោលនយោបាយយុទ្ធសាស្រ្ដ ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;			
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);
		}
		if(teams=='Interprise')
		{
			PicBig = document.getElementById("Interprise");
			if(PicBig.innerHTML.substr(0,1)!='<')
			{
				msg="សកម្មភាពសហគ្រាស ";
				leng=PicBig.innerHTML.length-msg.length;
				PicBig.innerHTML="<span class='selectedlibrary'>"+msg+PicBig.innerHTML.substr(msg.length,leng)+"</span>";
			}
		}
		else
		{
			backspace=0;
			fronspace=0;
			PicBig = document.getElementById("Interprise");
			if(PicBig.innerHTML.substr(0,1)=='<')
			{
				backspace=7;
				if(BrowserDetect.browser=="Firefox")
				{
					if(firsts==1)
					{
						fronspace=28;
					}
					else
					{
						fronspace=29;
					}				
				}	
				else
				{
					fronspace=28;
				}
			}
			msg="សកម្មភាពសហគ្រាស ";
			leng=PicBig.innerHTML.length-msg.length-backspace-fronspace;
			max=msg.length+fronspace;		
			PicBig.innerHTML=msg+PicBig.innerHTML.substr(max,leng);		
		}
	}
	// obtain a reference to the <div> element on the page
	myDiv = document.getElementById("SideShowPicSmall");
	// display the HTML output
	myDiv.innerHTML = html;
}// holds an instance of XMLHttpRequest