﻿// JavaScript Document

// Instantiated in every page. Contains session specific data.
function ShopController(properties) {
	this.ids					= properties.ids;
	this.urls					= properties.urls;
	this.codes				= properties.codes;
	this.texts				= properties.texts;
}

// Returns the base url for the current session (like /nl-nl)
ShopController.prototype.getBaseUrl = function() {
	return this.urls.baseShop;
}

// Returns the base url for product flag images (like sale, new, etc)
ShopController.prototype.getProductLabelImageBaseUrl = function() {
	return this.urls.baseProductLabelImage;
}

// Returns the image url for a given image ID.
// Available formatnames are: Detail, Full, Icon, Teaser, Thumbnail
ShopController.prototype.getImageUrl = function(idImage, formatName) {
	return "/_AppData/Images/" + formatName + "/" + idImage + ".jpg";
}

// Returns an array of Product objects from the given data.
// The %%TopProducts%% tag is converted into JSON code that has the exact format for this method.
ShopController.prototype.getProductsArrayFromData = function(data) {
	var products			= [];
	for(var i=0; i<data.length; i++) {
		products.push(this.getProductFromData(data[i]));
	}
	return products;
}

// Returns a Product object from the given data
ShopController.prototype.getProductFromData = function(data) {
	return new Product(data, this);
}

// Returns an array of Item objects from the given data
// The %%TopItems%% tag is converted into JSON code that has the exact format for this method.
ShopController.prototype.getItemsArrayFromData = function(data) {
	var items			= [];
	for(var i=0; i<data.length; i++) {
		items.push(this.getItemFromData(data[i]));
	}
	return items;
}

// Returns an Item object from the given data
ShopController.prototype.getItemFromData = function(data) {
	return new Item(data, this);
}






// Product wrapper class
function Product(properties, shopController) {
	this.ids					= properties.ids;
	this.urls					= properties.urls;
	this.texts				= properties.texts;
	this.flags				= properties.flags;
	this.price				= properties.price;
	
	this.controller		= shopController;
}

// Returns image url for main product image in given format
// Available formatnames are: Detail, Full, Icon, Teaser, Thumbnail
Product.prototype.getImageUrl = function(formatName) {
	return this.controller.getImageUrl(this.ids.image, formatName);
}

// Returns array of image urls for the flags that this product has.
Product.prototype.getFlagImages = function() {
	var urls		= [];
	var baseUrl	= this.controller.getProductLabelImageBaseUrl();
	
	if(this.flags.isEndOfLife) 	urls.push(baseUrl + "WarehouseClearance.png");
	if(this.flags.isNew) 				urls.push(baseUrl + "New.png");
	if(this.flags.isPromotion) 	urls.push(baseUrl + "Promotion.png");

	return urls;
}

// Returns html with price information in the current language.
Product.prototype.getPriceHtml = function() {
	if(this.price.requireSignIn) {
		return this.controller.texts.signInToViewPrices;
	} else if(this.price.onRequest) {
		return this.controller.texts.priceOnRequest;
	} else if(this.price.visible) {
		if(this.price.oldAmount.length) {
			return 	'<span class="OldAmount">' + this.price.oldAmount + '</span>'
						+	'<span class="NewAmount">' + this.price.currentAmount + '</span>';
		} else {
			return '<span class="Amount">' + this.price.currentAmount + '</span>';
		}
	}
	return '&nbsp;';
}








// Item wrapper class
function Item(properties, shopController) {
	this.ids					= properties.ids;
	this.urls					= properties.urls;
	this.texts				= properties.texts;
	this.flags				= properties.flags;
	this.price				= properties.price;
	
	this.controller		= shopController;
}

// Returns image url for main item image in given format
// Available formatnames are: Detail, Full, Icon, Teaser, Thumbnail
Item.prototype.getImageUrl = function(formatName) {
	return this.controller.getImageUrl(this.ids.image, formatName);
}

// Returns array of image urls for the flags that this item has.
Item.prototype.getFlagImages = function() {
	var urls		= [];
	var baseUrl	= this.controller.getProductLabelImageBaseUrl();
	
	if(this.flags.isEndOfLife) 	urls.push(baseUrl + "WarehouseClearance.png");
	if(this.flags.isNew) 				urls.push(baseUrl + "New.png");
	if(this.flags.isPromotion) 	urls.push(baseUrl + "Promotion.png");

	return urls;
}

// Returns the 'x variants' text in the current language
Item.prototype.getProductsVariantsHtml = function() {
	return this.texts.products + ' ' + this.controller.texts.variants;
}

// Returns html with price information in the current language.
Item.prototype.getPriceHtml = function() {
	if(this.price.requireSignIn) {
		return this.controller.texts.signInToViewPrices;
	} else if(this.price.onRequest) {
		return this.controller.texts.priceOnRequest;
	} else if(this.price.visible) {
		if(this.price.oldAmount.length) {
			return 	this.controller.texts.startsAt + ' '
						+ '<span class="OldAmount">' + this.price.oldAmount + '</span>'
						+	'<span class="NewAmount">' + this.price.currentAmount + '</span>';
		} else {
			return 	this.controller.texts.startsAt + ' '
						+	'<span class="Amount">' + this.price.currentAmount + '</span>';
		}
	}
	return '&nbsp;';
}

