﻿function product(productID) {
	this.productID = productID;
}

product.prototype.populateFeaturedGallery = function() {
	var product = this;
	jQuery("#product_thumbs img").each(function(idx) {
		jQuery(this).click(function() {
			product.displayLargeFeaturedImg(idx + 1);
		});
	});
};
product.prototype.displayMedFeaturedImg = function(imageID) {
	var product = this;
	var newImg = jQuery("<img />");
	var imgFilename = "images/Prod_" + this.productID + "_" + imageID + ".jpg";
	newImg.attr({
		"src": imgFilename,
		"alt": ""
	});
	newImg.click(function() {
		product.displayLargeFeaturedImg(imageID);
	});
	jQuery("#larger_image_btn").click(function() {
		product.displayLargeFeaturedImg(imageID);
	});
	
	jQuery("#image_showcase img").remove();
	newImg.prependTo("#image_showcase");
};

product.prototype.largeFeaturedImageActions = function() {
	var winWidth = jQuery(window).width();
	var winHeight = jQuery(window).height();
	var winXCenter = winWidth / 2;
	var winYCenter = winHeight / 2;
	var winTop = jQuery(window).scrollTop();

	jQuery("#product_lightbox_container, #product_lightbox_backdrop, #product_lightbox_image").width(winWidth).height(winHeight + winTop).css("top", 0);
};

product.prototype.displayLargeFeaturedImg = function(imageID) {
	var product = this;
	var newImg = jQuery("<img />");
	var imgFilename = "images/LargeProd_" + this.productID + "_" + imageID + ".jpg";
	newImg.attr({
		"src": imgFilename,
		"alt": ""
	});
	jQuery("#product_lightbox_image img").remove();

	jQuery("#product_lightbox_backdrop").fadeTo(0, .85, function() {
		newImg.prependTo("#product_lightbox_image");
		jQuery("#product_lightbox_container").show();
	});
	jQuery(window).bind("resize.productLightBoxEvents", function() {
		product.largeFeaturedImageActions();
	});
	jQuery(window).bind("scroll.productLightBoxEvents", function() {
		product.largeFeaturedImageActions();
	});
	jQuery("#product_lightbox_image, #product_lightbox_close").bind("click", function() {
		jQuery("#product_lightbox_container").fadeOut(200, function() {
			jQuery(window).unbind(".productLightBoxEvents");
		});
	});
	this.largeFeaturedImageActions();
};


product.prototype.populateTechGallery = function() {
	var product = this;
	jQuery("#product_tech_thumbnails img").each(function(idx) {
		var description = jQuery(this).next("p").html();
		jQuery(this).next("p").remove();
		jQuery(this).click(function() {
			product.displayTechSection(idx + 1, description);
		});
	});
	jQuery("#product_tech_thumbnails img:first").trigger("click");
};

product.prototype.displayTechSection = function(imageID, description) {
	jQuery("#product_tech_showcase *").remove();
	var newImg = jQuery("<img />");
	var imgFilename = "images/ProdTech_" + this.productID + "_" + imageID + ".jpg";
	newImg.attr({
		"src": imgFilename,
		"alt": ""
	});

	var newDesc = jQuery("<p />");
	newDesc.html(description);

	jQuery("#product_tech_showcase").prepend(newDesc).prepend(newImg);
};

product.prototype.init = function() {
	this.populateFeaturedGallery();
	this.displayMedFeaturedImg(1);
	this.populateTechGallery();
	jQuery("#dialog").before(jQuery("#product_lightbox_container"));
	product.displayMedFeaturedImg(1);
};
