var products = [],
	fadeTime = 750,
	leftColumn = null,
	mainContentsFull = null,
	slideshowTimer = 0,
	slideshowCurrent = -1,
	slideshowObjects = null,
	slideshowFadeTime = 500,
	slideshowTimeout = 5000;

function ajax(url) {
	try {
		var req = new XMLHttpRequest();
		req.open('GET', url, false);
		req.send(null);
		return req.responseText;
	} catch(err) {
		return false;
	}
}

function showForm(formName) {
	var f = document.getElementsByTagName('fieldset');
	var i = f.length;
	while (i--) {
		if (f[i].id == formName) {
			f[i].style.display = 'block';
		} else {
			f[i].style.display = 'none';
		}
	}
}

function checkCustomerForm() {
	if (document.getElementById('oldPassword').value == '') {
		document.getElementById('oldPassword').focus();
		return false;
	}
	return true;
}

function fadeMeIn(elm) {
	$(elm).css('display', 'none').css('visibility', 'visible');
	if ($(elm).is(':hidden')) {
		$(elm).fadeIn(fadeTime, 'swing');
	} else {
		$(elm).fadeOut(fadeTime, 'swing', function(e) {
			$(this).fadeIn(fadeTime, 'swing');
		});
	}
}

function switchBigImage(url) {
	$('#bigImage').attr('newSrc', url).fadeOut(fadeTime, function(e) {
		$(this).attr('src', $(this).attr('newSrc'));
	});
}

function slideshowNext() {
	slideshowCurrent = (slideshowCurrent + 1 >= slideshowObjects.length) ? 0 : slideshowCurrent + 1;
	if ($('#slideshow1').css('opacity') == 0) {
		$('#slideshow1').attr('src', slideshowObjects[slideshowCurrent].url);
	} else {
		$('#slideshow2').attr('src', slideshowObjects[slideshowCurrent].url);
	}
	$('#captionContents').animate({'opacity':0}, slideshowFadeTime, 'linear', function(e) {
		$(this).html(slideshowObjects[slideshowCurrent].caption);
	});
}

function slideshowPrevious() {
	slideshowCurrent = (slideshowCurrent - 1 < 0) ? slideshowObjects.length - 1 : slideshowCurrent - 1;
	if ($('#slideshow1').css('opacity') == 0) {
		$('#slideshow1').attr('src', slideshowObjects[slideshowCurrent].url);
	} else {
		$('#slideshow2').attr('src', slideshowObjects[slideshowCurrent].url);
	}
	$('#captionContents').animate({'opacity':0}, slideshowFadeTime, 'linear', function(e) {
		$(this).html(slideshowObjects[slideshowCurrent].caption);
	});
}

function stopSlideshow() {
	clearInterval(slideshowTimer);
}

function startSlideshow() {
	slideshowNext();
	slideshowTimer = setInterval(slideshowNext, slideshowTimeout);
}

function initSlideshow() {

	if (document.getElementById('caption') && document.getElementById('slideshow')) {
	
		var caption		= document.getElementById('caption');
		var slideshow	= document.getElementById('slideshow');
		var parentElm	= slideshow.parentNode;
		
		var width	= slideshow.getAttribute('width'),
			height	= slideshow.getAttribute('height'),
			pos		= findPos(slideshow);
			
		parentElm.removeChild(slideshow);
		
		var slideWrapper = document.createElement('div');
		slideWrapper.id				= 'slideWrapper';
		slideWrapper.style.width	= width + 'px';
		slideWrapper.style.height	= height + 'px';
		
		/*
		* Add the two new images
		*/
		var i1					= document.createElement('img');
		i1.id					= 'slideshow1';
		i1.style.width			= width + 'px';
		i1.style.height			= height + 'px';
			
		var i2					= document.createElement('img');
		i2.id					= 'slideshow2';
		i2.style.width			= width + 'px';
		i2.style.height			= height + 'px';
		
		slideWrapper.appendChild(i1);
		slideWrapper.appendChild(i2);
		parentElm.insertBefore(slideWrapper, caption);
		
		caption.style.width = (slideshow.width - 20) + 'px';
		
		var captionContents = document.createElement('span');
		captionContents.id = 'captionContents';
		
		var play = document.createElement('img');
		play.src = '/gfx/play.png';
		play.id = 'play';
		play.style.display = 'none';
		play.onclick = function() {
			document.getElementById('pause').style.display = '';
			document.getElementById('play').style.display = 'none';
			startSlideshow();
		};
		
		var pause = document.createElement('img');
		pause.src = '/gfx/pause.png';
		pause.id = 'pause';
		pause.onclick = function() {
			document.getElementById('play').style.display = '';
			document.getElementById('pause').style.display = 'none';
			stopSlideshow();
		};
		
		var back = document.createElement('img');
		back.src = '/gfx/back.png';
		back.id = 'back';
		back.onclick = function() {
			slideshowPrevious();
		};
		
		var forward = document.createElement('img');
		forward.src = '/gfx/forward.png';
		forward.id = 'forward';
		forward.onclick = function() {
			slideshowNext();
		};
		
		caption.appendChild(back);
		caption.appendChild(play);
		caption.appendChild(pause);
		caption.appendChild(forward);
		caption.appendChild(captionContents);
		
		$('#slideshow1').load(function() {
			$(this).animate({'opacity':1}, slideshowFadeTime, 'linear', function() {
				$('#slideshow2').css({'zIndex':1, 'opacity':0}, slideshowFadeTime, 'linear');
				$('#slideshow1').css({'zIndex':-1});
			});
			$('#captionContents').animate({'opacity':1}, slideshowFadeTime, 'linear');
		});
		
		$('#slideshow2').load(function() {
			$(this).animate({'opacity':1}, slideshowFadeTime, 'linear', function() {
				$('#slideshow1').css({'zIndex':1, 'opacity':0}, slideshowFadeTime, 'linear');
				$('#slideshow2').css({'zIndex':-1});
			});
			$('#captionContents').animate({'opacity':1}, slideshowFadeTime, 'linear');
		});
		
	}
	
	startSlideshow();
	
}

function findPos(elm) {
	var curleft = curtop = 0;
	if (elm.offsetParent) {
		do {
			curleft	= elm.offsetLeft;
			curtop	= elm.offsetTop;
		} while (elm = elm.offsetParent);
	}
	return {
		'left': curleft,
		'top': curtop
	};
}

window.onscroll = function() {
	if (!leftColumn) {
		try {
			leftColumn = document.getElementById('leftColumn');
		} catch (err) {}
	}
	if (!mainContentsFull) {
		try {
			mainContentsFull = document.getElementById('mainContentsFull');
		} catch (err) {}
	}
	if (mainContentsFull) {
		if (window.pageYOffset >= 146 || document.documentElement.scrollTop >= 146) {
			leftColumn.style.position = 'fixed';
			leftColumn.style.top = '29px';
			mainContentsFull.style.marginLeft = '212px';
		} else {
			leftColumn.style.position = '';
			leftColumn.style.top = '0px';
			mainContentsFull.style.marginLeft = '0px';
		}
	}
};

