var AjaxBasket = function()
{
	this.Timer;
	this.Timer2;
	
	// functions called by buttons etc
	
	this.add = function(StringData)
	{
		var scope = this;
		$.ajax({
			type : 'GET',
			url : '/ajaxBasket.php',
			data : StringData + '&action=add',
			success : function(response) { scope.addSuccess(json_decode(response)); },
			error : function(response) { scope.addFailure(json_decode(response)); }
		});
	};
	
	this.remove = function(StringData)
	{
		var scope = this;
		$.ajax({
			type: 'GET',
			url : '/ajaxBasket.php',
			data : StringData + '&action=remove',
			success : function(response) { scope.removeSuccess(json_decode(response)); },
			error : function(response) { scope.removeFailure(json_decode(response)); }
		});
	};

        this.adjust = function(StringData)
        {
                var scope = this;
                $.ajax({
                        type: 'GET',
                        url: '/ajaxBasket.php',
                        data: StringData + '&action=adjust',
                        success: function(response) { scope.adjustSuccess(json_decode(response)); },
                        error: function(response) { scope.adjustFailure(json_decode(response)); }
                });
        };
	
	
	// called by ajax object
	
	this.addSuccess = function(response)
	{
		if(response.type == 'error') { this.addFailure(response); return false; }
		if(typeof this.addSuccessCallback == 'function') { this.addSuccessCallback(response); }
	};
	
	this.addFailure = function(response)
	{ 
		if(typeof this.addFailureCallback == 'function') { this.addFailureCallback(response); }
	};
	
	this.removeSuccess = function(response)
	{
		if(response.type == 'error') { this.removeFailure(response); return false; }
		if(typeof this.removeSuccessCallback == 'function') { this.removeSuccessCallback(response); }
	};
	
	this.removeFailure = function(response)
	{
		if(typeof this.removeFailureCallback == 'function') { this.removeFailureCallback(response); }
	};

        this.adjustSuccess = function(response)
        {
                if(response.type == 'error') { this.adjustFailure(response); return false; }
                if(typeof this.adjustSuccessCallback == 'function') { this.adjustSuccessCallback(response); }
        };

        this.adjustFailure = function(response)
	{
		if(typeof this.adjustFailureCallback == 'function') { this.adjustFailureCallback(response); }
	};
	
	// helper functions
	
	this.contains = function(stcode)
	{
		var found = false;
		$('#basketdetail .items li').each(function(){ if(this.className == 'basket_' + stcode) { found = true; } });
		if(found) { return true; }
		return false;
	};
	
	this.addRemoveEvents = function()
	{
		var scope = this;
		$('#basket .items li a.remove').each(function(){
			$(this).unbind().click(function(){
				var stcode = $(this).parent().find('.stcode').html();
				$(this).unbind();
				scope.remove(stcode);
			})
		})
	};
	
	this.showControls = function(instant)
	{
		if($('#basket .items li').length > 1)
		{
			if(instant) { $('#basket .controls').show(); }
			else { $('#basket .controls').slideDown(); }
		}
	};
	
	this.addRemoveEvents();
	this.showControls(true);
	
}

/***************************************************
*
*	customise these if needed

****************************************************/

AjaxBasket.prototype.removeSuccessCallback = function(response)
{
	// remove the row
	$('#basketdetail .items .basket_' + response.laststockcode[1]).slideUp('fast', function() { $(this).remove(); });
	
	$('#basketind' + response.laststockcode[1]).html('');
	
	// set the new subtotal
	$('#basketinner .basketitems').html(response.items);
	$('#basketinner #gdstot').html('&pound;' + response.gdstotal);
	$('#basketinner #vattot').html('&pound;' + response.vattotal);
	$('#basketinner #nettot').html('&pound;' + response.total);
	
	// if it was the last item, hide the controls
	if(response.items == 0) { $('#basketdetail .controls').slideUp(); }
};

AjaxBasket.prototype.adjustSuccessCallback = function(response)
{
        // Update the quantity
        var thisRow = $('#basketdetail .items .basket_' + response.laststockcode);
        // thisRow.stop(true,true).effect("highlight", { color : '#019052' }, 200);
        var quanspan = thisRow.find('.quantity');
        var quan = quanspan.html();
        quan = Number(quan) + Number(response.lastquantity);
        quanspan.html(response.lastquantity);
        //var reducequan = Number(response.lastquantity) - 1;
        //var addquan = Number(response.lastquantity) + 1;
        //var downarrow = thisRow.find('.downarrow')
        //downarrow.unbind('click').click(function() { myAjaxBasket.adjust('addstockcode1=' + response.laststockcode + '&quantity1=' + reducequan ); });
        //var uparrow = thisRow.find('.uparrow')
        //uparrow.unbind('click').click(function() { myAjaxBasket.adjust('addstockcode1=' + response.laststockcode + '&quantity1=' + addquan ); });
        //thisRow.find('.uparrow').click('myAjaxBasket.adjust("addstockcode1=' + response.laststockcode + '&quantity1=' + addquan + ')"');

	// set the new subtotal
	$('#basketinner .basketitems').html(response.items);
	$('#basketinner #gdstot').html('&pound;' + response.gdstotal);
	$('#basketinner #vattot').html('&pound;' + response.vattotal);
	$('#basketinner #nettot').html('&pound;' + response.total);

}

AjaxBasket.prototype.addSuccessCallback = function(response)
{
	if ( this.Timer ) {
		clearTimeout(this.Timer);
	}
	if ( this.Timer2 ) {
		clearTimeout(this.Timer2);
	}
	for(count=0;count<=30;count++) {
		if (response.lastquantity[count] > 0) {
			if(this.contains(response.laststockcode[count]))
			{
				// update row
				var thisRow = $('#basketdetail .items .basket_' + response.laststockcode[count]);
				thisRow.stop(true,true).effect("highlight", { color : '#019052' }, 200);
				var quanspan = thisRow.find('.quantity');
				var quan = quanspan.html();
				quan = Number(quan) + Number(response.lastquantity[count]);
				quanspan.html(quan);
				
			}
			else
			{
				// add a new row
				var newRow = $('#basketdetail .master').clone();
				newRow.removeClass().addClass('basket_' + response.laststockcode[count]);
				newRow.hide();
				
				$(newRow).find('.stockdesc').html(response.laststockdesc[count]);
				$(newRow).find('.quantity').html(response.lastquantity[count]);
				$(newRow).find('#removestockcode').value = response.laststockcode[count];
				
				$('#basketdetail .items').append(newRow);
				$(newRow).find('#removestockcode').val(response.laststockcode[count]);
				newRow.slideDown();

                                $('.uparrow').unbind('click').click(function() { basketincrease(this,1) });
                                $('.downarrow').unbind('click').click(function() { basketincrease(this,-1) });
				
			}
		}
	}
	$('#basketdetail .items #remove').submit(function() {
		var BasketRemoveString = $(this).serialize();
		myAjaxBasket.remove(BasketRemoveString);
		return false;
	});
	$(document).trigger('close.facebox')
	var PanelString = "Your basket has been updated<br/>";
	for(count=0;count<=30;count++) {
		if ( response.lastquantity[count] && response.lastquantity[count] != "" ) { 
			PanelString += response.lastquantity[count] + " x " + response.laststockunit[count] + " " + response.laststockdesc[count] + "<br/>"
			$('#basketind' + response.laststockcode[count]).html('<a href="/basket.php"><img src="/themes/bowak/images/basketind.png" title="' + response.lastquantity[count] + ' in your basket"></a>');
		}
	}
        if ( response.category != "" ) {
            $('#basketind' + response.category).html('<a href="/basket.php"><img src="/themes/bowak/images/basketind.png" title="' + response.lastquantity[count] + ' in your basket"></a>');
        }

	$("#panelinner").html(PanelString);
	$("div#panel").slideDown("slow");
	$("#basketcontent").css("background-color", '#019052');
	this.Timer = setTimeout(function(){$("div#panel").slideUp("slow");$("#basketcontent").css("background-color", '#7a7a7a');},3000);
	//this.Timer2 = setTimeout(function(){$("#basketcontent").css("background-color", '#006b3d');},2000);
	
	// set the new subtotal
	$('#basketinner .basketitems').html(response.items);
	$('#basketinner #gdstot').html('&pound;' + response.gdstotal);
	$('#basketinner #vattot').html('&pound;' + response.vattotal);
	$('#basketinner #nettot').html('&pound;' + response.total);

	// make sure the controls are visible
	this.showControls();
	
	// make sure all remove links have events
	this.addRemoveEvents();
};

AjaxBasket.prototype.addFailureCallback = function(response)
{
	$(document).trigger('close.facebox')
};


