var _containDiv;
var _dataProvider;
var _data;
var _width;
var _height;
var _thumbContainer;
var _picContainer;
var _picTextContainer;
var _thumbCoverA;
var _thumbCoverB;
var _textContainer;
var _textCover;
var _textOpacity = 100;
var _thumbCoverOpacity = 60;
var _allPics;
var _allThumbs;
var _picWidth;
var _picHeight;
var _slideInterval;
var _thumbControlInterval;
var _thumbCoverInterval;
var _playInterval;
var _textOpacityInterval;
var _picTextInterval;
var _picTextHeight = 0;
var _picPosition = -100;
var _thumbPosition = 0;
var _waitTillSlideTime = 3000;
var _thumbWidth = 60;
var _thumbHeight = 45;
var _currentPic = 0;
var _backgroundColor;
var _textClass;
var _secondTextClass;
var _showText;
var _slideSpeed = .1;


var _thumbControl = false;

function gallery()
{
	this.initGallery = initGallery;
	this.setDimensions = setDimensions;
	this.setBackgroundColor = setBackgroundColor;
	this.setTextClass = setTextClass;
	this.setSecondTextClass = setSecondTextClass;
	this.setSlideSpeed = setSlideSpeed;
	this.setWaitTime = setWaitTime;
}

function setWaitTime(time) {
	_waitTillSlideTime = time;
}

function setSlideSpeed(speed) {
	_slideSpeed = speed;
}

function setSecondTextClass(textclass)
{
	_secondTextClass = textclass;	
}

function setTextClass(textclass)
{
	_textClass = textclass;	
}

function setBackgroundColor(color)
{
	_backgroundColor = color;	
}

function setDimensions(width,height,showText)
{
	_width = width;
	_height = height;
	
	_showText = showText;
	
	_picWidth = _width * .95;
	_picHeight = _picWidth * .75;
	
	var measureHeight;
	if (_showText){
		measureHeight = _height - 130;		
	} else {
		measureHeight = _height - 80;
	}
	
	
	
	if (_picHeight > measureHeight)
	{
		_picHeight = measureHeight;
		_picWidth = _picHeight * 1.33;
	}
	
	_currentThumbPosition = _picWidth;
	_otherThumbPosition = _currentThumbPosition + _thumbWidth + 20;
}


function initGallery(containerDiv, dataProvider)
{
	_containDiv = containerDiv;
	_dataProvider = dataProvider;
	
	loadData()
}

function createLayout()
{
	_thumbCoverA = document.createElement('div');
	_thumbCoverA.setAttribute('id', 'thumbCoverA');
	_thumbCoverA.style.position = "absolute";
	_thumbCoverA.style.width = _width/2 - _thumbWidth/2 - 2 + 'px';
	_thumbCoverA.style.height = 60 + 'px';
	_thumbCoverA.style.left = '0px';
	_thumbCoverA.style.top = '0px';
	_thumbCoverA.style.zIndex = '5';
	_thumbCoverA.style.backgroundColor = _backgroundColor;
	_thumbCoverA.style.filter = 'alpha(opacity=60)';
	_thumbCoverA.style.opacity = '.6';
	
	_thumbCoverB = document.createElement('div');
	_thumbCoverB.setAttribute('id', 'thumbCoverB');
	_thumbCoverB.style.position = "absolute";
	_thumbCoverB.style.width = _width/2 - _thumbWidth/2 - 5 + 'px';
	_thumbCoverB.style.height = 60 + 'px';
	_thumbCoverB.style.left = _width/2 + _thumbWidth/2 + 'px';
	_thumbCoverB.style.top = '0px';
	_thumbCoverB.style.zIndex = '5';
	_thumbCoverB.style.backgroundColor = _backgroundColor;
	_thumbCoverB.style.filter = 'alpha(opacity=60)';
	_thumbCoverB.style.opacity = '.6';
	
	_textContainer = document.createElement('div');
	_textContainer.setAttribute('id', 'textContainer');
	_textContainer.style.position = "absolute";
	_textContainer.style.width = _width + 'px';
	_textContainer.style.height = '45px';
	_textContainer.style.left = '0px';
	_textContainer.style.top = '5px';
	_textContainer.style.overflow = "hidden";
	
	_textCover = document.createElement('div');
	_textCover.setAttribute('id', 'textCover');
	_textCover.style.position = "absolute";
	_textCover.style.width = _width + 'px';
	_textCover.style.height = '45px';
	_textCover.style.left = '0px';
	_textCover.style.top = '5px';
	_textCover.style.overflow = "hidden";
	_textCover.style.filter = 'alpha(opacity=100)';
	_textCover.style.opacity = '1.0';
	_textCover.style.zIndex = '5';
	_textCover.style.backgroundColor = _backgroundColor;
	
	_thumbContainer = document.createElement('div');
	_thumbContainer.setAttribute('id', 'thumbContainer');
	_thumbContainer.style.position = "absolute";
	_thumbContainer.style.width = _width - 10 + 'px';
	_thumbContainer.style.height = 60 + 'px';
	_thumbContainer.style.left = '5px';
	_thumbContainer.style.top = _height - 60 + 'px';
	_thumbContainer.style.overflow = "hidden";
	_thumbContainer.onmouseover = controlThumbnails;
	_thumbContainer.onmouseout = releaseThumbnailControl;

	_picTextContainer = document.createElement('div');
	_picTextContainer.setAttribute('id', 'picTextContainer');
	_picTextContainer.style.position = "absolute";
	_picTextContainer.style.width = _picWidth + 'px';
	_picTextContainer.style.height = '0px';
	_picTextContainer.style.left = '0px';
	_picTextContainer.style.top = _picHeight + 'px';
	_picTextContainer.style.zIndex = '20';
	_picTextContainer.style.backgroundColor = _backgroundColor;
	_picTextContainer.style.filter = 'alpha(opacity=70)';
	_picTextContainer.style.opacity = '.7';
	
	_picContainer = document.createElement('div');
	_picContainer.setAttribute('id', 'picContainer');
	_picContainer.style.position = "absolute";
	_picContainer.style.width = _picWidth + 'px';
	_picContainer.style.height = _picHeight + 'px';
	_picContainer.style.left = _width/2 - _picWidth/2 + 'px';
	if (_showText){
		_picContainer.style.top = '50px';
	} else {
		_picContainer.style.top = '0px';
	}
	_picContainer.style.overflow = "hidden";
	_picContainer.onmouseover = function() {	
		setPictureText();
		if (_data.getElementsByTagName("object")[_currentPic].getAttribute('link') != null)
			document.body.style.cursor='pointer';
	}
	_picContainer.onmouseout = function() {
		removePictureText()
		if (_data.getElementsByTagName("object")[_currentPic].getAttribute('link') != null)
			document.body.style.cursor='default';
	}
	_picContainer.onclick = function() {
		if (_data.getElementsByTagName("object")[_currentPic].getAttribute('link') != null)
			window.location = _data.getElementsByTagName("object")[_currentPic].getAttribute('link');
		createCookie("currentPic",_currentPic,10);
		clearInterval(_playInterval);
	}
	
	_picDiv1 = document.createElement('div');
	_picDiv1.setAttribute('id', 'picDiv1');
	_picDiv1.style.position = "absolute";
	_picDiv1.style.width = _picWidth + 'px';
	_picDiv1.style.height = _picHeight + 'px';
	_picDiv1.style.left = _picPosition + 'px';
	_picDiv1.style.top = '0px';
	
	_allPics = new Array();
	_allThumbs = new Array();
	
	for (var a = 0; a<_data.getElementsByTagName("object").length + 1; a++)
	{
		_allPics[a] = document.createElement('div');
		_allPics[a].setAttribute('id', 'picDiv' + a);
		_allPics[a].style.position = "absolute";
		_allPics[a].style.width = _picWidth + 'px';
		_allPics[a].style.height = _picHeight + 'px';
		_allPics[a].style.left = _picPosition - (a*_picWidth) + 'px';
		_allPics[a].style.top = '0px';
		_picContainer.appendChild(_allPics[a]);
		
		if (a < _data.getElementsByTagName("object").length)
		{
			_allThumbs[a] = document.createElement('div');
			_allThumbs[a].setAttribute('id', a);
			_allThumbs[a].style.position = "absolute";
			_allThumbs[a].style.width = _thumbWidth + 'px';
			_allThumbs[a].style.height = _thumbHeight + 'px';
			_allThumbs[a].style.left = _width/2 - _thumbWidth/2 - (a*(_thumbWidth + 20)) + 'px';
			_allThumbs[a].style.top = '0px';
			_allThumbs[a].onmouseover = function() {
				document.body.style.cursor='pointer';	
			}
			_allThumbs[a].onmouseout = function() {
				document.body.style.cursor='default';	
			}
			_allThumbs[a].onclick = function() {
				_currentPic = parseInt(this.id);
				createCookie("currentPic",_currentPic,10);
				//if (_currentPic > _data.getElementsByTagName("object").length)
					//_currentPic =
				clearInterval(_playInterval);
				clearInterval(_slideInterval);
				_slideInterval = setInterval("slideToCurrentPic()",10);
				clearInterval(_thumbControlInterval);
				_thumbControlInterval = setInterval('thumbnailControl(true)', 10);
				clearInterval(_textOpacityInterval);
				_textOpacityInterval = setInterval('textFadeOut(true)', 10);
			}
			_thumbContainer.appendChild(_allThumbs[a]);
		}
	}
	
	
	
	_containDiv.appendChild(_picContainer);
	_picContainer.appendChild(_picTextContainer);
	_containDiv.appendChild(_thumbContainer);
	_thumbContainer.appendChild(_thumbCoverA);
	_thumbContainer.appendChild(_thumbCoverB);
	if (_showText) {
		_containDiv.appendChild(_textContainer);
		_containDiv.appendChild(_textCover);
	}
	
	initLoadImages();
	initLoadThumbs();
}

function revealPicText() {
	_picTextHeight += 10;
	_picTextContainer.style.height = _picTextHeight + 'px';
	_picTextContainer.style.top = _picHeight - _picTextHeight + 1 + 'px';
	if (_picTextHeight >= _picHeight / 3) {
		_picTextHeight = _picHeight / 3;
		_picTextContainer.style.height = _picTextHeight + 'px';
		_picTextContainer.style.top = _picHeight - _picTextHeight + 1 + 'px';
		clearInterval(_picTextInterval);
	}
}

function hidePicText() {
	_picTextHeight -= 10;
	_picTextContainer.style.height = _picTextHeight + 'px';
	_picTextContainer.style.top = _picHeight - _picTextHeight + 1 + 'px';
	if (_picTextHeight <= 10) {
		_picTextHeight = 0;
		_picTextContainer.style.height = _picTextHeight + 'px';
		_picTextContainer.style.top = _picHeight - _picTextHeight + 1 + 'px';
		clearInterval(_picTextInterval);
	}
}

function controlThumbnails(e) {
	if (!e) var e = window.event;
	var relTarg = e.relatedTarget || e.fromElement;
	
	if (relTarg.nodeName == 'DIV') {
		if (relTarg.id == "thumbCoverA" || relTarg.id == "thumbCoverB" || relTarg.id == "thumbContainer")
			return;
	} else {
		return;
	}
	
	_thumbControl = true;
	clearInterval(_thumbControlInterval);
	clearInterval(_thumbCoverInterval);
	_thumbCoverInterval = setInterval('thumbCoverFadeOut()', 5);
}

function releaseThumbnailControl(e) {
	if (!e) var e = window.event;
	var relTarg = e.relatedTarget || e.toElement;
	
	if (relTarg.nodeName == 'DIV') {
		if (relTarg.id == "thumbCoverA" || relTarg.id == "thumbCoverB" || relTarg.id == "thumbContainer")
			return;
	} else {
		return;
	}
	
	_thumbControl = false;	
	clearInterval(_thumbCoverInterval);
	_thumbCoverInterval = setInterval('thumbCoverFadeIn()', 5);
	clearInterval(_thumbControlInterval);
	_thumbControlInterval = setInterval('thumbnailControl(true)', 10);
}

function setTextField(text) {
	_textContainer.innerHTML = '<div align="center" class="' + _textClass + '">' + text + '</div>';
}

function setPictureText() {
	if (_data.getElementsByTagName("object")[_currentPic].getAttribute('html') != null) {
		var html = _data.getElementsByTagName("object")[_currentPic].getAttribute('html');

		_picTextContainer.innerHTML = '<div class="' + _secondTextClass + '">' + html + '</div>';
		
		clearInterval(_playInterval);
		clearInterval(_picTextInterval);
		_picTextInterval = setInterval('revealPicText()',10);
	}
}

function removePictureText() {
	if (_data.getElementsByTagName("object")[_currentPic].getAttribute('html') != null) {
		clearInterval(_picTextInterval);
		_picTextInterval = setInterval('hidePicText()',10);
		clearInterval(_playInterval);
		_playInterval = setInterval('waitForSlide()', _waitTillSlideTime);
	}
}

function thumbCoverFadeOut() {
	_thumbCoverOpacity -= 10;
	_thumbCoverA.style.filter = 'alpha(opacity=' + _thumbCoverOpacity + ')';
	_thumbCoverB.style.filter = 'alpha(opacity=' + _thumbCoverOpacity + ')';
	_thumbCoverA.style.opacity = '0.' + _thumbCoverOpacity;
	_thumbCoverB.style.opacity = '0.' + _thumbCoverOpacity;
	_thumbCoverA.style.display = "none";
	_thumbCoverB.style.display = "none";
	if (_thumbCoverOpacity <= 0)
		clearInterval(_thumbCoverInterval);
}

function thumbCoverFadeIn() {
	_thumbCoverOpacity += 10;
	_thumbCoverA.style.filter = 'alpha(opacity=' + _thumbCoverOpacity + ')';
	_thumbCoverB.style.filter = 'alpha(opacity=' + _thumbCoverOpacity + ')';
	_thumbCoverA.style.opacity = '0.' + _thumbCoverOpacity;
	_thumbCoverB.style.opacity = '0.' + _thumbCoverOpacity;
	_thumbCoverA.style.display = "block";
	_thumbCoverB.style.display = "block";
	if (_thumbCoverOpacity >= 60)
		clearInterval(_thumbCoverInterval);
}

function textFadeOut(useCurrentPic) {
	_textOpacity += 10;
	_textCover.style.filter = 'alpha(opacity=' + _textOpacity + ')';
	if (_textOpacity >= 100) {
		clearInterval(_textOpacityInterval);
		_textCover.style.opacity = '1.0';
		_textOpacity = 100;
		var currentPic = _currentPic;
		if (useCurrentPic == false) {
			if (_currentPic + 1 > _data.getElementsByTagName("object").length - 1)
				currentPic = -1;
			if (_data.getElementsByTagName("object")[_currentPic + 1].getAttribute('text') != null)
				setTextField(_data.getElementsByTagName("object")[currentPic + 1].getAttribute('text'))
		} else {
			if (_data.getElementsByTagName("object")[_currentPic].getAttribute('text') != null)
				setTextField(_data.getElementsByTagName("object")[currentPic].getAttribute('text'))
		}
		_textOpacityInterval = setInterval('textFadeIn()', 10);
	} else {
		_textCover.style.opacity = '0.' + _textOpacity;
	}
}
function textFadeIn() {
	_textOpacity -= 10;
	_textCover.style.filter = 'alpha(opacity=' + _textOpacity + ')';
	_textCover.style.opacity = '0.' + _textOpacity;
	if (_textOpacity <= 0) {
		clearInterval(_textOpacityInterval);
	}
}

function initLoadThumbs()
{
	for (var a = 0; a<_data.getElementsByTagName("object").length; a++)
	{
		if (_data.getElementsByTagName("object")[a].getAttribute('thumb') != null)
		{
			_allThumbs[a].innerHTML = "<img src='inventory/" + _data.getElementsByTagName("object")[a].getAttribute('thumb') + "' width='" + _thumbWidth + "px' height='" + _thumbHeight + "px' />" 
		} else 
		{
			_allThumbs[a].innerHTML = "<img src='inventory/" + _data.getElementsByTagName("object")[a].getAttribute('image') + "' width='" + _thumbWidth + "px' height='" + _thumbHeight + "px' />" 
		}
		
	}
	
}

function initLoadImages()
{
	for (var a = 0; a<_data.getElementsByTagName("object").length; a++)
	{
		_allPics[a].innerHTML = "<img src='inventory/" + _data.getElementsByTagName("object")[a].getAttribute('image') + "' width='" + _picWidth + "px' height='" + _picHeight + "px' />" 
	}
	_allPics[_data.getElementsByTagName("object").length].innerHTML = "<img src='inventory/" + _data.getElementsByTagName("object")[0].getAttribute('image') + "' width='" + _picWidth + "px' height='" + _picHeight + "px' />" 
	
	if (_data.getElementsByTagName("object")[_currentPic].getAttribute('text') != null)
		setTextField(_data.getElementsByTagName("object")[_currentPic].getAttribute('text'));
	
	_slideInterval = setInterval("initSlide()", 10);
	_textOpacityInterval = setInterval('textFadeIn()', 10);
}

function initSlide()
{
	_picPosition += ((_picWidth * (_currentPic)) - _picPosition)*_slideSpeed;
	
	if (_picPosition > -1)
	{
		clearInterval(_slideInterval);
		_playInterval = setInterval('waitForSlide()', _waitTillSlideTime);
		_picPosition = (_picWidth * (_currentPic));
	}
	
	for (var a = 0; a<_data.getElementsByTagName("object").length + 1; a++)
	{
		_allPics[a].style.left = _picPosition - (a*_picWidth) + 'px';
	}

}

function thumbnailControl(resetControl) {
	var thumbSlide;
	if (resetControl == false)
	 	thumbSlide = _currentPic + 1;
	else
		thumbSlide = _currentPic;
		
	if (thumbSlide > _data.getElementsByTagName("object").length - 1)
		thumbSlide = 0;
	_thumbPosition += ((thumbSlide*(_thumbWidth + 20)) - _thumbPosition)*_slideSpeed;
	
	if (_thumbPosition > ((thumbSlide*(_thumbWidth + 20)) - 1) && _thumbPosition < ((thumbSlide*(_thumbWidth + 20)) + 1)) {
		clearInterval(_thumbControlInterval);				   
	}
	
	for (var a = 0; a<_data.getElementsByTagName("object").length; a++)
	{
		_allThumbs[a].style.left = _thumbPosition + _width/2 - _thumbWidth/2 - (a*(_thumbWidth + 20)) + 'px';
	}
}

function slideToCurrentPic()
{
	_picPosition += ((_picWidth * (_currentPic)) - _picPosition)*_slideSpeed;
	
	
	if (_picPosition > (_picWidth * (_currentPic)) - 1 && _picPosition < (_picWidth * (_currentPic)) + 1)
	{
		_picPosition = (_picWidth * (_currentPic));
		clearInterval(_slideInterval);
	}
	
	for (var a = 0; a<_data.getElementsByTagName("object").length + 1; a++)
	{
		_allPics[a].style.left = _picPosition - (a*_picWidth) + 'px';
	}
}

function slidePicsOneSpace()
{
	_picPosition += ((_picWidth * (_currentPic + 1)) - _picPosition)*_slideSpeed;
	
	
	if (_picPosition > (_picWidth * (_currentPic + 1)) - 1)
	{
		_picPosition = (_picWidth * (_currentPic + 1));
		_currentPic ++;
		if (_currentPic == _data.getElementsByTagName("object").length)
		{
			_currentPic = 0;
			_picPosition = 0;
			clearInterval(_thumbControlInterval);
			_thumbControlInterval = setInterval('thumbnailControl(true)', 10);
		}
		clearInterval(_slideInterval);
	}
	
	for (var a = 0; a<_data.getElementsByTagName("object").length + 1; a++)
	{
		_allPics[a].style.left = _picPosition - (a*_picWidth) + 'px';
	}
	
	createCookie("currentPic",_currentPic,10);
}

function waitForSlide()
{
	clearInterval(_slideInterval);
	_slideInterval = setInterval("slidePicsOneSpace()",10);
	if (_thumbControl == false) {
		clearInterval(_thumbControlInterval);
		_thumbControlInterval = setInterval('thumbnailControl(false)', 10);
	}
	clearInterval(_textOpacityInterval);
	_textOpacityInterval = setInterval('textFadeOut(true)', 10);
}

function onLoadData() {
	var picCookie = readCookie('currentPic');
	
	if (picCookie != null) {
		_currentPic = parseInt(picCookie);
		_thumbControlInterval = setInterval('thumbnailControl(true)', 10);
	}
	createLayout();
}

function loadData()
{
	var xmlhttp;
	try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");    // Trying Internet Explorer 
	}
	catch(e)    // Failed 
	{
	  xmlhttp = new XMLHttpRequest();    // Other browsers.
	}
	xmlhttp.open('POST', _dataProvider, true);
	
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200)
				{
					_data = stringToXml(xmlhttp.responseText);
					//alert(_data.getElementsByTagName("object")[1].getAttribute('text'));
					onLoadData()
				}
			}
		}
		xmlhttp.send(null);
}

function stringToXml(xmlData) {
		if (window.ActiveXObject) {
			//for IE
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(xmlData);
			return xmlDoc;
		} else if (document.implementation && document.implementation.createDocument) {
			//for Mozila
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(xmlData,"text/xml");
			return xmlDoc;
		}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

