﻿itemCount = 0;
currentItem = null;
maxImageHeight = 525;
productData = null;
effectSpeed = 1000;

function ChangeItem(newItem, whichDirection) {
	var newImage = new Image();
	newImage.onload = function() {
		var leftPosition = Math.floor(($('#jewelryGallery').width() - newImage.width) / 2) + "px";
	
		$('#itemPhoto').css('left', leftPosition);
		$('#itemPhoto').css('top',  Math.floor((maxImageHeight - newImage.height) / 2) + 'px');
		$('#itemPhoto').attr('src', newImage.src);
	
		$('#itemInfo').width(newImage.width);
		$('#itemInfo').css('left', leftPosition);
		$('#itemInfo').html($(newItem).find('desc').text());

		if(whichDirection == 'none') {
			$('#itemPhoto').fadeIn(effectSpeed);
			}
		else {
			$('#itemPhoto').show('drop', { direction: whichDirection }, effectSpeed);
			}
		$('#itemInfo').fadeIn(effectSpeed);
		}
	newImage.src = 'gallery_images/' + $(newItem).find('photo').text();
	}

      
$(document).ready(function() {

	$('#jewelryGallery').prepend('<img id="itemPhoto"/><div id="itemInfo"></div>');

	$.get('catalog.xml', function(data) {
		productData = data;

		currentItem = null;
		queryString = window.location.search.substring(1);
		parts = queryString.split('=');
		if(parts[0] == 'item') {
			currentItem = $(productData).find('item:[id=' + parts[1] + ']');
			}
		if(!currentItem || currentItem.size() < 1) {
			currentItem = $(productData).find('item:first');
			}
		
		ChangeItem(currentItem, 'none');
		});


	$('#leftArrow').click(function() {
		$('#itemInfo').fadeOut(effectSpeed);
		$('#itemPhoto').hide('drop', { direction: 'left' }, effectSpeed, function() {
			currentItem = $(currentItem).prev();
			if(currentItem.size() == 0) {
				currentItem = $(productData).find('item:last');
				}
			ChangeItem(currentItem, 'right');
			});
		});


	$('#rightArrow').click(function() {
		$('#itemInfo').fadeOut(effectSpeed);
		$('#itemPhoto').hide('drop', { direction: 'right' }, effectSpeed, function() {
			currentItem = $(currentItem).next();
			if(currentItem.size() == 0) {
				currentItem = $(productData).find('item:first');
				}
			ChangeItem(currentItem, 'left');
			});
		});

	});      
      

