﻿// JScript File
// Class: Image
function Image()
{
	this.Title = "";
	this.Description = "";
	this.Src = "";
	this.Categories = new Array();
}

// Static Method: Parse the given XML Node
Image.ParseXML = function(xm)
{
	var img = new Image();
	
	img.Title = xm.getElementsByTagName("title")[0].firstChild.nodeValue;
	img.Description = xm.getElementsByTagName("description")[0].firstChild.nodeValue
	img.Src = xm.getElementsByTagName("file")[0].firstChild.nodeValue
	
	var cats = xm.getElementsByTagName("category");
	
	for (var i = 0; i < cats.length; i++)
	{
		img.Categories[i] = cats[i].firstChild.nodeValue;
	}
	
	return img;
}

//Instance Method: Cache the current Image
Image.prototype.PreCache = function()
{
	var img = new Image(200,200);
	img.src = "/Thumbnail.ashx?f=" + this.Src + "&w=200";
}

// Class: Gallery
function Gallery()
{
	this.Http = null;
	this.Loaded = false;
	this.Failed = false;
	this.Images = new Array();
	this.Directory = "";
}

// Instance Method: Populate the given container with Image thumbnails
Gallery.prototype.generateThumbnails = function(divId, imgGallery, imgTitle, imgDesc)
{
	// TODO: Generate Thumbnails
	var div = document.getElementById(divId);
	
	while(div.childNodes.length > 0)
		div.removeChild(div.childNodes[0]);
	
		for (var i = 0; i < this.Images.length; i++)
		{
			var smp = this.Images[i];
			smp.PreCache();
			
			var img = document.createElement("img");
			
			img.src = "/Handlers/Thumbnail.ashx?f=" + this.Directory + "/" + smp.Src + "&w=70";
			img.alt = smp.Title;
			img.title = smp.Title;
			img.width = 70;
			img.height = 70;
			//img.style.borderColor = "#0095d9";
			//img.style.borderWidth = "1px";
			//img.style.borderStyle = "solid";
			img.style.margin = "2px";
			img.imgGallery = imgGallery;
			img.imgTitle = imgTitle;
			img.imgDesc = imgDesc;
			img.Image = smp;
			img.Directory = this.Directory;
			img.onclick = function()
			{
				var img = document.getElementById(this.imgGallery);
				var iWidth = parseInt(img.parentNode.offsetWidth - 10, 10);
				img.style.marginTop = "5px";
				img.src = "/Handlers/Thumbnail.ashx?f=" + this.Directory + "/" + this.Image.Src + "&w=" + iWidth;
				img.alt = this.alt;
				img.title = this.title;
				
				var title = document.getElementById(this.imgTitle);
				while(title.childNodes.length > 0)
					title.removeChild(title.childNodes[0]);
				title.appendChild(document.createTextNode(this.Image.Title));
				
				var desc = document.getElementById(this.imgDesc);
				while(desc.childNodes.length > 0)
					desc.removeChild(desc.childNodes[0]);
				desc.appendChild(document.createTextNode(this.Image.Description));
			}
			div.appendChild(img);
			
			if (i == 0)
				img.onclick();
		}
}

// Instance Method: Returns the relevant XMLRequest Object
Gallery.prototype.getHTTPObject = function()
{ 
	if (typeof XMLHttpRequest != 'undefined') 
	{ 
		return new XMLHttpRequest(); 
	} 
	try 
	{ 
		return new ActiveXObject("Msxml2.XMLHTTP"); 
	} 
	catch (e) 
	{ 
		try 
		{ 
			return new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch (e) 
		{
		} 
	} 
	
	return false;
}

// Static Method: Parse the given XML Document
Gallery.ReadXML = function(filename)
{
	
	var sm = new Gallery();
	
	sm.Http = sm.getHTTPObject ();
	
	filename += "?ran=" + Math.random();
	
	sm.Http.open("GET", filename, false);
	sm.Http.send(null);

	if ((sm.Http != null) && (sm.Http.readyState == 4) && (sm.Http.status == 200))
	{		
		var xDoc = sm.Http.responseXML;
		var smx = xDoc.getElementsByTagName("gallery");
		var smn = smx[0].childNodes;
		
		for (var i = 0; i < smx[0].attributes.length; i++)
		{
			if (smx[0].attributes[i].nodeName.toLowerCase() == "fileroot")
				sm.Directory = smx[0].attributes[i].nodeValue;
		}
		var i = 0;

		while (i < smn.length)
		{
			if (smn[i].nodeType != 3)
			{
				sm.Images[sm.Images.length] = Image.ParseXML(smn[i]);
			}
			i++;
		}		
		
		sm.Loaded = true;
		sm.Failed = false;
	}
	
	return sm;	
}