var AjaxWishlist = function()
{
	this.Timer;
	this.Timer2;
	
	// functions called by buttons etc
	
	this.add = function(StringData)
	{
		var scope = this;
		$.ajax({
			type : 'GET',
			url : '/ajaxWishlist.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 : '/ajaxWishlist.php',
			data : StringData + '&action=remove',
			success : function(response) { scope.removeSuccess(json_decode(response)); },
			error : function(response) { scope.removeFailure(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.addFailure(response); return false; }
		if(typeof this.removeSuccessCallback == 'function') { this.removeSuccessCallback(response); }
	};
	
	this.removeFailure = function(response)
	{
		if(typeof this.removeFailureCallback == 'function') { this.removeFailureCallback(response); }
	};
	
	// helper functions
	
	this.contains = function(stcode)
	{
		var found = false;
		$('#basket .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

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

AjaxWishlist.prototype.removeSuccessCallback = function(response)
{
	if ( this.Timer ) {
		clearTimeout(this.Timer);
	}

	var PanelString = "Your favourites list has been updated<br/>";
	PanelString += response.stockdesc + " Removed<br/>"
//	$('.wishlistind' + response.stockcode).html('<form id="addtowishlist' + response.stockcode + '" class="addtowishlist" method="post" action="/addtowishlist.php"><input type="hidden" name="returnto" value="' + document.URL + '"><input type="hidden" name="addstockcode1" value="' + response.stockcode + '" ><input id="wishlistsubmitinput" class="wishlistsubmitinput" type="image" value="Submit" alt="Add" title="Click to add item to your list of favourites" src="/themes/bowak/images/wishlistblank.png"></form>');
	$('.wishlistind' + response.stockcode).html('<img class="wishlistadd" src="/themes/bowak/images/wishlistblank.png" title="Add/remove item from your list of favourites" onClick="myAjaxWishlist.add(\'returnto=&addstockcode1=' + response.stockcode + '&category=' + response.category +'\')">');

	if ( response.wishlistfound == "true" ) {
		$('#wishlistind' + response.category).html('<a href="#wishlist' + response.category + '" rel="facebox"><img src="/themes/bowak/images/wishlistblank.png" title="Add/remove item from your list of favourites"></a>');
	}

	$("#accountpanelinner").html(PanelString);
	$("div#accountpanel").slideDown("slow");
	$("#accountcontent").css("background-color", '#fff4a8');
	$("#accountcontent").css("color", '#000');
	$("div#accountpanel").css("color", '#000');
	this.Timer = setTimeout(function(){$("div#accountpanel").slideUp("slow");$("#accountcontent").css("background-color", '#7a7a7a');$("#accountcontent").css("color", '#fff');$("div#accountpanel").css("color", '#fff');},3000);

        /* Calls add to wishlist ajax function when button is selected */
        $('*[id*=addtowishlist]').submit(function() {
            var WishlistAddString = $(this).serialize();
            myAjaxWishlist.add(WishlistAddString);
            return false;
        });
        
        $('a[rel*=facebox]').facebox();

	// make sure the controls are visible
	this.showControls();

	// make sure all remove links have events
	this.addRemoveEvents();
};

AjaxWishlist.prototype.addSuccessCallback = function(response)
{
	if ( this.Timer ) {
		clearTimeout(this.Timer);
	}

	var PanelString = "Your favourites list has been updated<br/>"; 
	PanelString += response.stockdesc + " Added<br/>"
	// $('.wishlistind' + response.stockcode).html('<a href="/wishlist.php"><img src="/themes/bowak/images/wishlist.png" title="View Favourites"></a>');
//	$('.wishlistind' + response.stockcode).html('<form id="removefromwishlist' + response.stockcode + '" class="removefromwishlist" method="post" action="/removefromwishlist.php"><input type="hidden" name="returnto" value="' + document.URL + '"><input type="hidden" name="addstockcode1" value="' + response.stockcode + '" ><input id="wishlistsubmitinput" class="wishlistsubmitinput" type="image" value="Submit" alt="Add" title="Click to add item to your list of favourites" src="/themes/bowak/images/wishlist.png"></form>');
	$('.wishlistind' + response.stockcode).html('<img class="wishlistadd" src="/themes/bowak/images/wishlist.png" title="Add/remove item from your list of favourites" onClick="myAjaxWishlist.remove(\'returnto=&addstockcode1=' + response.stockcode + '&category=' + response.category +'\')">');

	if ( response.category ) {
		$('#wishlistind' + response.category).html('<a href="#wishlist' + response.category + '" rel="facebox"><img src="/themes/bowak/images/wishlist.png" title="Add/remove item from your list of favourites"></a>');
	}

	$("#accountpanelinner").html(PanelString);
	$("div#accountpanel").slideDown("slow");
	$("#accountcontent").css("background-color", '#fff4a8');
	$("#accountcontent").css("color", '#000');
	$("div#accountpanel").css("color", '#000');
	this.Timer = setTimeout(function(){$("div#accountpanel").slideUp("slow");$("#accountcontent").css("background-color", '#7a7a7a');$("#accountcontent").css("color", '#fff');$("div#accountpanel").css("color", '#fff');},3000);

        /* Calls remove from wishlist ajax function when button is selected */
        $('*[id*=removefromwishlist]').submit(function() {
            var WishlistAddString = $(this).serialize();
            myAjaxWishlist.remove(WishlistAddString);
            return false;
        });
        
        $('a[rel*=facebox]').facebox();

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

AjaxWishlist.prototype.addFailureCallback = function(response)
{
};


