﻿

function fnChangeCompare(product, checked) {
	/// <summary>Triggered after pushing add To compare link</summary>
	IPrometeusService.AddToProductCompare(product, checked, lc, wl, onCompareUpdated);
}

var compareResults;
function onCompareUpdated(result) {
	/// <summary>Triggered after adding product to compare</summary>
	compareResults = result;

	if (compareResults.HasProducts == true)
		$('.compareLinks').show();
	else
		$('.compareLinks').hide();
}

function fnToCompare() {
	document.location.replace(compareResults.CompareUrl);
}

function fnGetSelectedItems() {
	/// <summary>Returns an array with product id's that are selected in wishlist</summary>
	var checkboxes = $('#accountWishlist').find('div.compare input');
	var newList = new Array();

	for (var i = 0; i < checkboxes.length; i++) {
		if (checkboxes[i].checked)
			newList[newList.length] = checkboxes[i].value;
	}
	return newList;
}

function fnAddSelectedToBasket() {
	/// <summary>Add the wishlist selected items to the basket</summary>
	var selectedItems = fnGetSelectedItems();
	if (selectedItems.length > 0)
	    document.location.replace('/Handlers/AddToBasket.ashx?pid=' + selectedItems.join('|'));	
}


function fnRemoveItemsFromWishList() {
	var selectedItems = fnGetSelectedItems();

	IPrometeusService.RemoveProductFromWishlist(selectedItems);
	window.location.reload();
}




var addedProduct;
function fnAddToBasket(id, name, brand, price, quantityContainer, imageContainer) {
	var product = {};
	product.Id = id;
	product.Name = name;
	product.Quantity = $(quantityContainer).val();
	addedProduct = new AddedProduct(id, name, brand, $(quantityContainer).val(), $(imageContainer).attr('src'), price);
	IPrometeusService.AddToBasket(product, onProductAdded);
}
function onProductAdded(result) {
	if (result == true) {
		$('#addedImage').attr('src', addedProduct.Image);
		$('#addedImage').attr('alt', addedProduct.Name);
		$('#addedTitle').text(addedProduct.Name);
		$('#addedBrand').text(addedProduct.Brand);
		$('#addedQuantity').text(addedProduct.Quantity);
		$('#addedPrice').html(addedProduct.Price);
		$('#addedToCart, #pageMask').show();
		pageMask();
		layerPosIE();
		addedProduct = null;
	}
	else {	}
}
function AddedProduct(id, name, brand, quantity, image, price) {
	this.Id = id;
	this.Name = name;
	this.Brand = brand;
	this.Quantity = quantity;
	this.Image = image;
	this.Price = price;
}


var addedProductWishlist;
function fnAddToWishlist(id, name, brand, price, imageContainer) {
    var product = {};
    product.Id = id;
    product.Name = name;
    addedProductWishlist = new AddedProductWishlist(id, name, brand, $(imageContainer).attr('src'), price);
    IPrometeusService.AddToWishlist(product, onProductAddedWishlist);
}
function onProductAddedWishlist(result) {
    if (result == true) {
        $('#addedToWishlistImage').attr('src', addedProductWishlist.Image);
        $('#addedToWishlistImage').attr('alt', addedProductWishlist.Name);
        $('#addedToWishlistTitle').text(addedProductWishlist.Name);
        $('#addedToWishlistBrand').text(addedProductWishlist.Brand);
        $('#addedToWishlistPrice').html(addedProductWishlist.Price);
        $('#addedToWishlist, #pageMask').show();
        pageMask();
        layerPosIE();
        addedProductWishlist = null;
    }
    else {    }
}
function AddedProductWishlist(id, name, brand, image, price) {
    this.Id = id;
    this.Name = name;
    this.Brand = brand;
    this.Image = image;
    this.Price = price;
}

function parentExists() {
    alert(parent.location + ' ' + window.location);
    return (parent.location == window.location) ? false : true;
}
