﻿$(document).ready(function() {
	$('#pageName').data('orig', $('#pageName').text());
	$('div.jqmWindow').jqm({modal: true, trigger: false, overlay: 25 });
	var categoryId = $(parentCategory).val();
	// turn on JS view for supported browsers
	$(supportsJS).val("1");
	$('.yesJS').show();
	// hide variations if empty
	if ($(ddlVariations).children().size() < 2) {
		$(ddlVariations).parent().hide();
	}
	$('.noJS').hide();
	
	var ie6 = ($.browser.msie && $.browser.version < 7);
	
	var fadeInLargePhoto = function() {
					$('#content').hide();
					unhover();
					notLoading();
					// extra callback for IE 6
					$('#popup').fadeIn('normal', function() {
							$('#popup img').each(function() {
								var self = $(this);
								if (self.width() > 740) {
									self.width(740);
								}
							});
						});
					$('#popup img').unbind();
				};
	var setupFilmstrip = function() {
		if (ie6) {
			$('#filmStrip').hide();
			return;
		}
		if ($('#previewPane li').length) {
			$('#filmStrip').show();
			$('#previewPane').jCarouselLite({ btnNext: ".next", btnPrev: ".prev"});
			$('#previewPane img').click(function(event) {
				event.preventDefault();
				var href = $(this).parent().attr('href');
				if (href.lastIndexOf("/") == (href.length - 1)) return; // no image
				$('#pageName').text($(this).attr('title'));
				$('#popup div:last').html($('#productright .productInfo').html());
				var productName = $('select.productname').children(':selected').text();
				if (productName) {
					$('#popup div:last').prepend('<label>Item Name:</label> ' + productName + '<br/>');
				}
				loading();
				$('#popup img').load(fadeInLargePhoto).attr('src', $(this).parent().attr('href'));
			});
		} else {
			$('#filmStrip').hide();
		}
	};
	
	var copyProductData = function(data) {
		if (!data.isOK) return;
		$(itemNumber).text(data.sku);
		$('.uom').text(data.unit);
		$(weight).text(data.weight);
		$(price).text(data.fullPrice);
		$(youPay).text(data.discountPrice);
		if (data.fullPrice != data.discountPrice) {
			$('.discounted').show();
		} else {
			$('.discounted').hide();
		}
	};
	
	// click handlers for drop downs
	$('select.productname').change(function() {
		loading();
		$.jmsajax({
			type: "POST", url: "../ProductDataService.asmx", method: "GetProductData", dataType: "msjson",
			data: { 'categoryId': categoryId, productId: $(this).children(':selected').val() },
			success: function(data) {
				notLoading();
				if (!data.isOK) return; // todo: show error
				$('#viewPane > img').attr('src', '../ProductImages/' + data.photo);
				if (data.largePhoto) {
					$('a.largePhoto').attr('href', '../ProductImages/' + data.largePhoto);
				} else {
					$('a.largePhoto').attr('href', '');
				}
				copyProductData(data);
				
				// reset variations
				$(ddlVariations).empty();
				if (data.items.length > 1) {
					$.each(data.items, function(i,val) {
						$(ddlVariations).append('<option value="' + val.uid + '">' + val.label + '</option>');
					});
					$(ddlVariations).parent().show();					
				} else {
					$(ddlVariations).parent().hide();
				}
				$(currentItem).val(data.items[0].uid); // currentItem
				
				// preview pane
				$('#previewPane ul').remove();
				if (data.relatedPhotos && data.relatedPhotos.length) {
					$('#previewPane').append("<ul></ul>");
					var ul = $('#previewPane ul');
					$.each(data.relatedPhotos, function(i,val) {
						ul.append('<li><a href="../ProductImages/' 
							+ val.largePhoto +  '"><img alt="' + val.title + '" title="' + val.title 
							+ '" src="../ProductImages/' + val.thumbnail + '" /></a></li>');
					});					
				}
				setupFilmstrip();
			}
		});
		$(currentProduct).val($(this).children(':selected').val()); // currentProduct		 
	});
	
	$('select.variation').change(function() {
		loading();
		$.jmsajax({
			type: "POST",
			url: "../ProductDataService.asmx",
			method: "GetItemData",
			dataType: "msjson",
			data: { itemId: $(this).children(':selected').val() },
			success: function(data) {
				notLoading();
				if (!data.isOK) return; // todo: show error
				copyProductData(data);
			}
		});
		$(currentItem).val($(this).children(':selected').val()); // currentItem		
	});	
		
	var hover = function() {
		if ($(largePhoto).attr('href')) {
			$('#viewPane #magnify').show();
			$('#viewPane > img').addClass('hot');
		}
	};
	var unhover = function() {
		$('#viewPane > img').removeClass('hot');
		$('#viewPane #magnify').hide();
		$('#viewPane > img').show();
	};

	$('#viewPane').live('mouseover', hover);
	$('#viewPane').live('mouseout', unhover);
	$('#viewPane').live('click', function() {
		if (!$(largePhoto).attr('href')) return;
		
		$('#popup div:last').html($('#productright .productInfo').html());
		var productName = $('select.productname').children(':selected').text();
		if (productName) {
			$('#popup div:last').prepend('<label>Item Name:</label> ' + productName + '<br/>');
		}
		loading();
		$('#popup img').load(fadeInLargePhoto).attr('src', $(largePhoto).attr('href'));
	});
	$('#popup a').click(function(event) {
		event.preventDefault();
		notLoading();
		$('#popup').hide();
		$('#pageName').text($('#pageName').data('orig'));
		$('#content').fadeIn();
	});
	
	setupFilmstrip();
	
});
