function Gallery()
{
	this.imgTitle = $("imgTitle");
	this.imgDesc = $("imgDesc");
	this.thumbNavig = $("ThumbNavig");
	this.anchors = this.thumbNavig.getElementsByTagName("a");
	this.targetFile;
	this.browser;
	this.xmlDoc=new Object();
	this.projectsArray=new Array();
	this.tempImg=new Image();
	this.selectedSection;
	this.currSectionSel;
	this.firstElement;
	this.localroot=this;
	
	this.loadLibrary=function(target)
	{
		var localroot=this;
		this.targetFile=target;
		
		var localroot=this;
		
		var browser;
		if (window.ActiveXObject) {
			 browser = "ie";
			 localroot.browser="ie";
		}else{
			 browser = "ff";
			 localroot.browser="ff";
		}
		
		new Ajax.Request(this.targetFile,
		{
			method:'get',
			onSuccess: function(transport){
			      //alert(transport.responseText)
			  	  var response = transport.responseXML  || "no response text";
				  localroot.xmlDoc=response;
				 if(browser=="ie"){
					localroot.parseDataIE(response);
				 }else{
					 localroot.parseData(response);
				 }
			},
			onFailure: function(){ alert('Something went wrong...') }
		});
	}
	
	this.parseDataIE=function(xml){
		
		var projectsXML=xml.documentElement;
		for(var i=0;i<projectsXML.childNodes.length;i++)
		{
			var sectionObject=new Object();
			sectionObject.label=projectsXML.childNodes[i].getAttribute("label");
			var sectionProjects=new Array(); // images (objects) array 
			for(var j=0;j<projectsXML.childNodes[i].childNodes.length;j++)  // all <image> tags in the section
					{
							var tempObj=new Object();
							tempObj.url=projectsXML.childNodes[i].childNodes[j].getAttribute("href");
							tempObj.thumbpath=projectsXML.childNodes[i].childNodes[j].getAttribute("thumb");
							tempObj.label=projectsXML.childNodes[i].childNodes[j].getAttribute("label");
							tempObj.desc=projectsXML.childNodes[i].childNodes[j].firstChild.nodeValue;
							sectionProjects.push(tempObj);
					}
			sectionObject.projects=sectionProjects;
			this.localroot.projectsArray.push(sectionObject)
		}
		this.localroot.selectedSection=this.localroot.projectsArray[0];
		this.localroot.constructThumbs();
	}
	
	this.parseData=function(xml)
	{
		var projectsXML=xml.documentElement;
		if(this.localroot.browser=="ff")
		{
			for(var i=0;i<projectsXML.childNodes.length;i++)
			{
				//var tempArray=new Array();
				
				if(projectsXML.childNodes[i].attributes!=null)
				{
					var sectionObject=new Object();
					sectionObject.label=projectsXML.childNodes[i].attributes["label"].value;  //  all <section> tags
					
					var sectionProjects=new Array(); // images (objects) array 
					for(var j=0;j<projectsXML.childNodes[i].childNodes.length;j++)  // all <image> tags in the section
					{
						if(projectsXML.childNodes[i].childNodes[j].attributes!=null)
						{
							var tempObj=new Object();
							tempObj.url=projectsXML.childNodes[i].childNodes[j].attributes["href"].value;
							tempObj.thumbpath=projectsXML.childNodes[i].childNodes[j].attributes["thumb"].value;
							tempObj.label=projectsXML.childNodes[i].childNodes[j].attributes["label"].value;
							tempObj.desc=projectsXML.childNodes[i].childNodes[j].firstChild.nodeValue;
							sectionProjects.push(tempObj);
						}
					}
					
					sectionObject.projects=sectionProjects;
					this.localroot.projectsArray.push(sectionObject)
					
				}
	        }
		   	this.localroot.selectedSection=this.localroot.projectsArray[0];
			this.localroot.constructThumbs();
     	}
	}
	
	this.constructThumbs=function()
	{
		//preloading thumbnails
		var imgthumb = new Image();
		var thumb_url = new Array();
		var listElements = new Array();
		var thumbNavig = this.thumbNavig;
		var newUl = document.createElement("ul");
		for(var j=0;j < this.selectedSection.projects.length;j++)
		{
			thumb_url[j] = this.selectedSection.projects[j].thumbpath;
			imgthumb.src = thumb_url[j];
			listElements[j] = document.createElement("li");
			newUl.appendChild(listElements[j])
			var anchorTag = document.createElement("a");
			anchorTag.href = "javascript:void(0)";
			if(anchorTag.addEventListener)
			{
				anchorTag.setAttribute("onclick","activeTh("+j+"),showImage("+j+")");
				anchorTag.className = "imgTagInactive";
				anchorTag.setAttribute("id","id_"+j+";");
			}
			else
			{
				anchorTag.j=j;
				//anchorTag.setAttribute("class","imgTagInactive");
				anchorTag.className = "imgTagInactive";
				anchorTag.onclick = function()
				{
					showImage(this.j);	
					activeTh(this.j);
					return false;
				}
				anchorTag.id = "id_"+j;
			}
			listElements[j].appendChild(anchorTag);
			this.showImage(0);
			this.firstElement = listElements[0].firstChild; 
			this.firstElement.className = "imgTagActive";
			var imgTag = document.createElement("img");
			anchorTag.appendChild(imgTag);
			imgTag.src = imgthumb.src;
			imgTag.width = 29;
			imgTag.height = 29;
		}
		
		var delUl=thumbNavig.getElementsByTagName("ul")[0];
		if(delUl!=null)
		{
			delUl.parentNode.removeChild(delUl);
		}
		
		thumbNavig.appendChild(newUl);
	}
	
	this.showImage=function(index) // !-------------IE BUGs
	{	
		var localroot=this;
					
		this.tempImg.src=this.selectedSection.projects[index].url;
		$("PortfolioImageScreen").innerHTML = "<div class='LoaderDiv'><img src='images/ajax-loader.gif' alt='loader'/></div>";
		this.tempImg.onload=function() 
		{
			$("PortfolioImageScreen").innerHTML = "<img src='"+localroot.selectedSection.projects[index].url+"' alt=''/>";
			//$("screen").src=localroot.selectedSection.projects[index].url;
			//localroot.imgTitle.innerHTML = '<a href="'+localroot.selectedSection.projects[index].label+'" target="_blank">'+localroot.selectedSection.projects[index].label+'</a>';
			localroot.imgTitle.innerHTML = localroot.selectedSection.projects[index].label;
			localroot.imgDesc.innerHTML = localroot.selectedSection.projects[index].desc;
		}
    }
	
	this.activeTh = function(index) 
	{
		for(var i = 0; i < this.anchors.length; i++)
		{
			this.anchors[i].className = "imgTagInactive";	
		}
		this.anchors[index].className = "imgTagActive";
	}
	
	this.setGallery=function(index,target)
	{
		this.selectedSection=this.projectsArray[index];

		if(this.currSectionSel!=target)
		{
			if(this.currSectionSel!=null)
			{
				this.currSectionSel.style.opacity="0.8";
			}
			this.currSectionSel=target;
			this.currSectionSel.style.opacity ="1.0";
		}
		this.showImage(0);
		this.constructThumbs();
	}
}