﻿/* Fusion.Store Front End */

var popup_audio = function(url) {
	window.open('http://www.alien-languages.com/playaudio.aspx?url=' + url, 'AudioSample', 'width=320,height=240,menubar=no,scrollbars=no,status=no');
}

Store = {};

Store.setBasketTotals = function(basketSummary)
{
	if(basketSummary.TotalQuantity > 0)
		$("#uxBasketTotals").html("You have<br/>" + basketSummary.TotalQuantity + " Item(s) | £" + $.numberToString(basketSummary.TotalPrice, 2, 0) + "<br/>in your <a href='/basket.aspx'>basket&gt;&gt;</a>"); 
	else
		$("#uxBasketTotals").html("Your basket<br/>is empty.");	  
}

Store.updateBasketTotals = function()
{
	$.call({
		url: "/WebServices/BasketWebService.asmx/GetCurrentSummary",
		data: {},
		success: function(response)
		{
			Store.setBasketTotals(response.d);
		}
	});
}

Store.addProduct = function(productId) {
	var ctrl = document.getElementById("q_" + productId);
	var quantity = parseInt(ctrl.value, 10);
	if (isNaN(quantity) || quantity <= 0) {
		alert("Please enter a valid quantity.");
		return;
	}

	// Extract attribute values
	var attributes = [];
	var attributeValues = [];
	var cancel = false;
	$(".a_" + productId).each(function(index, element) {
		if (element.value == '') {
			alert("There are options/variations you need to choose from before this product can be added to your basket.");
			cancel = true;
			return;
		}
		var value = parseInt(element.value);
		if (!isNaN(value)) {
			attributeValues.push(value);
		}
	});
	if (cancel)
		return;

	$.call({
		url: "/WebServices/BasketWebService.asmx/AddProduct",
		data: { productId: productId, quantity: quantity, attributeValues: attributeValues },
		success: function(response) {
			$("#itemsAddedMessage_" + productId).show();
			$(".checkoutconfirm").show();
			scroll(0, 0);
			Store.setBasketTotals(response.d);
		}
	});
}

Store.searchForLanguage = function(ddlId)
{
	var ctrl = document.getElementById(ddlId);
	if(!ctrl)
		return;
	window.location = "/searchresults.aspx?q=" + ctrl.value;
}
