jqZoomOptions = {
    zoomType: "standard",
    lens: true,
    preloadImages: true,
    alwaysOn: false,
    title: false,
	zoomWidth: 500,
	zoomHeight: 450
}

thumbPos	  = 3;
thumbCount	  = 1;
thumbAnimated = true;


$(document).ready(function() {
	// grab the parent level attributes and display
	var parentAttrs = getAttributeAJAX(itemID, 0, 0);
	
	// check to see if valid HTML returned from AJAX call (synchronous) then add to DOM and show default items
	if ($(parentAttrs)) {
		var insertedParentAttrs = $('#attributesContainer').html(parentAttrs);
		showDefaultItems(insertedParentAttrs);
	}
	else {
		$(".jqzoom").jqzoom(jqZoomOptions);
	}
	
	// when a product attribute is changed then look for child attributes to display
	// bind select elements
	$("#attributesContainer").delegate("select.prodAttr", "change", function() {
		var attrContainer = $(this).parents('.attributeContainer');
		var attrIDs = $(this).val();
		displayChildAttrs(attrIDs, attrContainer);
	});
	// bind input elements
	$("#attributesContainer").delegate("input", "change", function() {
		var attrContainer = $(this).parents('.attributeContainer');
		// get attribute ids from checked checkboxes and radios
		var attrIDs = $(this + ':checked').val();
		displayChildAttrs(attrIDs, attrContainer);
	});
	
	
	// product thumbnail count for recently viewed products:
	thumbCount = $("input[name=ThumbCount]").val();
	
	// on recently viewed product mouseenter/mouseleave, change opacity:
	$("div.thumbnailContainer div.thumbnails img").mouseenter(function() {
		$(this).animate({opacity: "0.99"}, 200);
	});
	
	$("div.thumbnailContainer div.thumbnails img").mouseleave(function() {
		$(this).animate({opacity: "0.80"}, 200);
	});
	
	// on recently viewed product scroll arrow click, move as needed:
	$("div.move").click(function() {
		if ($.browser.mozilla)   { $(this).css('MozUserSelect','none'); }
		else if ($.browser.msie) { $(this).bind('selectstart', function() { return false; }); }
		else					 { $(this).mousedown(function() { return false; }); }
		
		if (thumbAnimated) {
			thumbAnimated = false;
			
			var thisID = $(this).attr("id");
			var currentPos = $("div.recentlyViewedProducts div.thumbnailContainer div.thumbnails").css("margin-left");
			
			if (thisID.search("Back") > -1 && thumbPos > 3) {
				var newPos = parseInt(currentPos) + 80;
				thumbPos = thumbPos - 1;
			}
			else if (thisID.search("Next") > -1 && thumbPos < thumbCount) {
				var newPos = parseInt(currentPos) - 80;
				thumbPos = thumbPos + 1;
			}
			
			$("div#moveBack").css({visibility: "visible"});
			$("div#moveNext").css({visibility: "visible"});
			
			if (thumbPos == 3) { $("div#moveBack").css({visibility: "hidden"}); }
			else if (thumbPos == thumbCount) { $("div#moveNext").css({visibility: "hidden"}); }
			
			$("div.recentlyViewedProducts div.thumbnailContainer div.thumbnails").animate({marginLeft: newPos+"px"}, 350, function() {
				thumbAnimated = true;
			});
		}
	});
});

function showDefaultItems(attrs) {
	// process select elements
	$(attrs).find('select').each(function(){
		var attrContainer = $(this).parents('.attributeContainer');
		var attrIDs = $(this).val();
		displayChildAttrs(attrIDs, attrContainer);
	});
	// process checkbox and radio input elements
	$(attrs).find('input:checked').each(function(){
		var attrContainer = $(this).parents('.attributeContainer');
		var attrIDs = $(this).val();
		displayChildAttrs(attrIDs, attrContainer);
	});
	if( $(attrs).find('select').length == 0 && $(attrs).find('select').length == 0 )
		$(".jqzoom").jqzoom(jqZoomOptions);
}

function displayChildAttrs(attrIDs, attrContainer){
	// figure out what the next hierarchy id should be
	var hierarchy =  $(attrContainer).data('hierarchy');
	var nextHierarchy =  hierarchy + 1;
	
	// remove all child attributes to be replaced with new attributes
	$(attrContainer).nextAll('.attributeContainer').each(function(index, Element){
		if ($(this).data('hierarchy') <= hierarchy) {
			return true;
		}
		else {
			$(this).remove();
		}
	});
	
	// add any found attributes to the display
	if (attrIDs && attrIDs.length) {
		// display product image if defined by selected attribute detail
		findProductPhotos(attrIDs);
		
		var childAttrs = getAttributeAJAX(itemID, attrIDs, nextHierarchy);
		// check to see if valid HTML then add to DOM and show default items
		if ($(childAttrs)) {
			$(attrContainer).after(childAttrs);
			showDefaultItems(childAttrs);
		}
	}
}

function updateProductPhotos(data) {
	// if images where returned, then update the main photo and thumbnail
	if(data.length > 0) {
		// remove all existence of jqzoom so we can reassign it correctly w/o error
		$(".jqzoom").unbind().removeData('jqzoom').empty();
		$(".zoomPad").remove();
		
		// remove all the current thumbnails
		$('#thumblist').empty();
		
		// put back all the new thumbnails and main product image
		$(data).each(function(i,objImg) {
			// if this is the first image, replace the main image with it
			if (i==0) {
				var mainProdImgElem = $('<img id="mainProdImg" width="350" />');
				$(mainProdImgElem).attr('src',objImg.srcHiRes);
				$(mainProdImgElem).appendTo('.jqzoom');
				$('.jqzoom').attr('href',objImg.srcHiRes);
				$('.zoomWrapperImage').find('img').attr('src', objImg.srcHiRes);
			}
			var elemThumb = $('<li><a href="javascript:void(0);"><img width="75" border="0" /></a></li>');
			if (i==0) {
				$(elemThumb).find('a').addClass('zoomThumbActive');
			}
			$(elemThumb).find('img')
				.attr('src', objImg.srcThumb)
				.attr('rel', '{gallery: \'gal1\', smallimage: \'' + objImg.srcHiRes + '\', largeimage: \'' + objImg.srcHiRes + '\'}');
			$(elemThumb).appendTo('#thumblist');
		});
		
		// rebind jqzoom
		$(".jqzoom").jqzoom(jqZoomOptions);
	}
}

function findProductPhotos(attributeDetailID) {
	if( typeof attributeDetailID != "undefined" && attributeDetailID > 0 ) {
		$.ajax({
			cache: false,
			data: {
				method: 'getAttributeDetailImages',
				productAttributeDetailID: attributeDetailID
			},
			dataType: 'json',
			success: function(data) {
				updateProductPhotos(data);
			},
			traditional: true,
			type: "GET",
			url: "/com/ei/image/ImageProxy.cfc"
		});
	}
}

function getAttributeAJAX(productid,productattributedetailid,returnHierarchy) {
	var rtnHTML = 'no data';
	$.ajax({
		async: false,
		cache: false,
		data: {
			productattributedetailid: productattributedetailid,
			item_id: productid,
			returnHierarchy: returnHierarchy
		},
		success: function(data){
			rtnHTML = $(data).find("data_div").text();
		},
		traditional: true,
		type: "GET",
		url: "/ajax/attributesAjax.cfm"
 	});
	return rtnHTML;
}
