paintFinder = new function PaintFinder() {
	this.selectAll = function(item) {
//		item.parent().parent().siblings().addClass('active');
		item.parent().parent().parent().find('.paint-finder-option-item').addClass('active');
	}
	
	this.deselectAll = function(item) {
//		item.parent().parent().siblings().removeClass('active');
		item.parent().parent().parent().find('.paint-finder-option-item').removeClass('active');
	}	
	
	this.setNoColourMatches = function() {
		$('#paint-finder-colours .inner').html('<span>No matching colours</span>');
		this.setColourMatchCount(0);
	}
	
	this.setNoPaletteMatches = function() {
		$('#paint-finder-colour-groups .inner').html('<span>No matching colours</span>');
		this.setNoColourSelection();
	}	
	
	this.setColourNavigation = function() {
		var VISIBLE_COLOURS = 5;	
			
		var colourCount = $('.paint-finder-colour-match .paint-finder-colour-match-info').length;		
		if (colourCount != 0) {
			$('#paint-finder-colours-up').show();
			$('#paint-finder-colours-down').show();
			$('#paint-finder-colours-up').addClass('inactive');
			if (colourCount > VISIBLE_COLOURS) {
				$('#paint-finder-colours-down').removeClass('inactive');
			} else {
				$('#paint-finder-colours-down').addClass('inactive');				
			}
		} else {
			$('#paint-finder-colours-up').hide();
			$('#paint-finder-colours-down').hide();			
		}								
						
		$('#paint-finder-colours-up').unbind('click');			
	}	
	
	this.colourMoveUp = function() {
		var COLOUR_HEIGHT = 56;
		var VISIBLE_COLOURS = 5;	
		var objUp = $('#paint-finder-colours-up');	
		var newPosition;
		
		clearTimeout(this.timeoutColourUp);		
		
		objUp.unbind('mouseover');
		newPosition = parseInt($('#paint-finder-colour-matches').css('top')) + COLOUR_HEIGHT + 'px';
		$('#paint-finder-colour-matches').css('top', newPosition);
		this.setColourNavigationEvents();						
		if (parseInt(newPosition) == 0) {
			objUp.addClass('inactive');
			objUp.unbind('mouseover');
		} else {
//			this.setColourNavigationEvents();				
			this.timeoutColourUp = setTimeout('paintFinder.colourMoveUp()', 100);		
		}
		
		$('#paint-finder-colours-down').removeClass('inactive');		
	}
	
	this.colourMoveDown = function() {
		var COLOUR_HEIGHT = 56;
		var VISIBLE_COLOURS = 5;	
		var objDown = $('#paint-finder-colours-down');	
		var newPosition;
		
		clearTimeout(this.timeoutColourDown);	
			
		objDown.unbind('mouseover');
		newPosition = parseInt($('#paint-finder-colour-matches').css('top')) - COLOUR_HEIGHT + 'px';
		$('#paint-finder-colour-matches').css('top', newPosition);
		$('#paint-finder-colours-up').removeClass('inactive');
		
		var colourCount = $('.paint-finder-colour-match:visible').children(':visible').length;		
													
		this.setColourNavigationEvents();								
		if (parseInt(newPosition) == 0-((colourCount*COLOUR_HEIGHT)-(VISIBLE_COLOURS*COLOUR_HEIGHT))) {
			$('#paint-finder-colours-down').addClass('inactive');
			objDown.unbind('mouseover');
		} else {
//			this.setColourNavigationEvents();				
			this.timeoutColourDown = setTimeout('paintFinder.colourMoveDown()', 100);
		}
		
		$('#paint-finder-colours-up').removeClass('inactive');
	}	
	
	this.setColourNavigationEvents = function() {
		var COLOUR_HEIGHT = 56;
		var VISIBLE_COLOURS = 5;
		var newPosition;
				
		$('#paint-finder-colours-up').unbind('mouseover');			
		$('#paint-finder-colours-up').mouseover(function() {
			paintFinder.timeoutColourUp = setTimeout('paintFinder.colourMoveUp()', 100);		
		});	
		
		$('#paint-finder-colours-down').unbind('mouseover');				
		$('#paint-finder-colours-down').mouseover(function() {
			paintFinder.timeoutColourDown = setTimeout('paintFinder.colourMoveDown()', 100);
		});	
	
		$('#paint-finder-colours-up').mouseout(function() {
			clearTimeout(paintFinder.timeoutColourUp);
		});
		
		$('#paint-finder-colours-down').mouseout(function() {
			clearTimeout(paintFinder.timeoutColourDown);
		});			
	}
	
	this.setColourEvents = function() {

		$('#paint-finder .paint-finder-colour-add').unbind('click');
		$('#paint-finder .paint-finder-colour-add').click(function() {					
			var id = $(this).parent().parent().attr('id').substring(31);
			
			paintFinder.addColour(id);			
			paintFinder.setSessionData();
	
			return false;	
		});		
		
		this.setColourNavigationEvents();	
	}		
	
	this.setColourSelectionCount = function(selectionCount) {
		$('#paint-finder-colour-selection-matches strong').html(selectionCount);
	}	
	
	this.setColourMatchCount = function(matchCount) {
		$('#paint-finder-colours-matches strong').html(matchCount);
	}
	
	this.colourSelectionMoveUp = function() {
		var COLOUR_HEIGHT = 65;
		var VISIBLE_COLOURS = 4;	
		var objUp = $('#paint-finder-colour-selection-up');	
		var newPosition;
		
		clearTimeout(this.timeoutColourSelectionUp);		
		
		objUp.unbind('mouseover');
		newPosition = parseInt($('#paint-finder-colour-selection-items').css('top')) + COLOUR_HEIGHT + 'px';
		$('#paint-finder-colour-selection-items').css('top', newPosition);
		this.setColourSelectionNavigationEvents();						
		if (parseInt(newPosition) == 0) {
			objUp.addClass('inactive');
			objUp.unbind('mouseover');
		} else {
//			this.setColourSelectionNavigationEvents();				
			this.timeoutColourSelectionUp = setTimeout('paintFinder.colourSelectionMoveUp()', 100);		
		}
		
		$('#paint-finder-colour-selection-down').removeClass('inactive');		
	}
	
	this.colourSelectionMoveDown = function() {		
		var COLOUR_HEIGHT = 65;
		var VISIBLE_COLOURS = 4;	
		var objDown = $('#paint-finder-colour-selection-down');	
		var newPosition;
		
		clearTimeout(this.timeoutColourSelectionDown);	
			
		objDown.unbind('mouseover');
		newPosition = parseInt($('#paint-finder-colour-selection-items').css('top')) - COLOUR_HEIGHT + 'px';
		$('#paint-finder-colour-selection-items').css('top', newPosition);
		$('#paint-finder-colour-selection-up').removeClass('inactive');
		
		var colourCount = $('#paint-finder-colour-selection-items .paint-finder-colour-selection-info').length;									
		
		this.setColourSelectionNavigationEvents();						
															
		if (parseInt(newPosition) == 0-((colourCount*COLOUR_HEIGHT)-(VISIBLE_COLOURS*COLOUR_HEIGHT))) {
			$('#paint-finder-colour-selection-down').addClass('inactive');
			objDown.unbind('mouseover');
		} else {
			this.timeoutColourSelectionDown = setTimeout('paintFinder.colourSelectionMoveDown()', 100);
		}
		
		$('#paint-finder-colour-selection-up').removeClass('inactive');							
	}			
	
	this.setColourSelectionNavigationEvents = function() {
		var COLOUR_HEIGHT = 65;
		var VISIBLE_COLOURS = 4;
		var newPosition;
		
		var colourCount = $('#paint-finder-colour-selection-items .paint-finder-colour-selection-info').length;													
				
		if (colourCount > VISIBLE_COLOURS) {				
			if (parseInt($('#paint-finder-colour-selection-items').css('top')) != 0) {
				$('#paint-finder-colour-selection-up').unbind('mouseover');			
				$('#paint-finder-colour-selection-up').mouseover(function() {
					paintFinder.timeoutColourSelectionUp = setTimeout('paintFinder.colourSelectionMoveUp()', 100);		
				});	
	
				$('#paint-finder-colour-selection-up').mouseout(function() {
					clearTimeout(paintFinder.timeoutColourSelectionUp);
				});				
			}						
		
			// Make sure down is disabled after removing a colour at the end	
			if (parseInt($('#paint-finder-colour-selection-items').css('top')) > parseInt(0-((colourCount*COLOUR_HEIGHT)-(VISIBLE_COLOURS*COLOUR_HEIGHT)))) {
				$('#paint-finder-colour-selection-down').unbind('mouseover');				
				$('#paint-finder-colour-selection-down').mouseover(function() {
					paintFinder.timeoutColourSelectionDown = setTimeout('paintFinder.colourSelectionMoveDown()', 100);
				});			
				
				$('#paint-finder-colour-selection-down').mouseout(function() {
					clearTimeout(paintFinder.timeoutColourSelectionDown);
				});	
			
			}
				
		
		}
	}	
	
	this.setColourSelectionEvents = function() {
		$('#paint-finder .paint-finder-colour-remove').unbind('click');
		
		$('#paint-finder .paint-finder-colour-remove').click(function() {
			var id = $(this).parent().parent().attr('id').substring(35);
			$(this).hide();
			paintFinder.removeColour(id);
			paintFinder.setSessionData();			
			
			return false;
		});
		
		$('#paint-finder .paint-finder-colour-order-tester').unbind('click');

		$('#paint-finder .paint-finder-colour-order-tester').click(function() {		
			var colourId = $(this).parent().parent().attr('id').substring(35);
			paintFinder.showTesterPots(colourId);
			return false;
		});		
	
		this.setColourSelectionNavigationEvents();
	}	
	
	this.setColourNavigationEvents = function() {		
		var COLOUR_HEIGHT = 56;
		var VISIBLE_COLOURS = 5;
		var newPosition;
		
		var colourCount = $('.paint-finder-colour-match:visible').children(':visible').length;
				
		if (colourCount > VISIBLE_COLOURS) {	
			if (parseInt($('#paint-finder-colour-matches').css('top')) != 0) {			
				$('#paint-finder-colours-up').unbind('mouseover');			
				$('#paint-finder-colours-up').mouseover(function() {
					paintFinder.timeoutColourUp = setTimeout('paintFinder.colourMoveUp()', 100);		
				});	
				
				$('#paint-finder-colours-up').mouseout(function() {
					clearTimeout(paintFinder.timeoutColourUp);
				});						
			}
			
			if (parseInt($('#paint-finder-colour-matches').css('top')) > parseInt(0-((colourCount*COLOUR_HEIGHT)-(VISIBLE_COLOURS*COLOUR_HEIGHT)))) {				
				$('#paint-finder-colours-down').unbind('mouseover');				
				$('#paint-finder-colours-down').mouseover(function() {
					paintFinder.timeoutColourDown = setTimeout('paintFinder.colourMoveDown()', 100);
				});		
				
				$('#paint-finder-colours-down').mouseout(function() {
					clearTimeout(paintFinder.timeoutColourDown);
				});	
			
			}
		}
	}
	
	this.removeColour = function(id) {
		var COLOUR_HEIGHT = 56;
		var VISIBLE_COLOURS = 5;	
		var newPosition;
		var colourCount;
		
		$('#paint-finder-colour-selection-info-' + id).remove();
		
		// Display add link on colour
		var addLinkHtml = '<a class="paint-finder-colour-add" href="#">Add to my colours</a>';
		$('#paint-finder-colour-match-info-' + id + " .control").prepend(addLinkHtml);
		this.setColourEvents();
		
		colourCount = $('#paint-finder-colour-selection-items .paint-finder-colour-selection-info').length;
			
		if (colourCount == 0) {
//			$('#paint-finder-colour-selection-up').hide();
//			$('#paint-finder-colour-selection-down').hide();
			this.showColourSelectionProductsButton(false);
			this.showColourSelectionTesterPotButton(false);			
		}
		
		this.setColourSelectionCount(colourCount);
		
		this.setColourSelectionNavigationEvents();
	}
	
	this.addColour = function(id) {
		var colourObj;
		var colourObjSpan;
		var colourObjColour;
		var colourObjAddLink;
		var colour;
		var VISIBLE_COLOUR_SELECTIONS = 4;
		
		if ($('#paint-finder-colour-match-info-' + id).length != 0) {
			colourObj = $('#paint-finder-colour-match-info-' + id);
			colourObjSpan = colourObj.find('span');
			colourObjColour = colourObj.find('.paint-finder-colour-match-info-colour');
			
			// Remove add link
			colourObjAddLink = colourObj.find('.control').children().eq(0);
			colourObjAddLink.remove();

			colour = {'id': id, 'code': colourObjColour.css('background-color'), 'name': colourObjSpan.html()}						
			
			
		} else {
			return false;
		}
		

		if ($('#paint-finder-colour-selection .paint-finder-colour-selection-info').length == 0) {
			$('#paint-finder-colour-selection span').remove();
			
			$('#paint-finder-colour-selection .inner').append('<div id="paint-finder-colour-selection-items"></div>');
			
			$('#paint-finder-colour-selection-up').show();
			$('#paint-finder-colour-selection-down').show();				
		}
		
		var colourHtml = '<div id="paint-finder-colour-selection-info-' + colour.id + '" class="paint-finder-colour-selection-info">';
		colourHtml += '<div class="paint-finder-colour-selection-info-colour" style="background-color: ' + colour.code + ';"></div>';
		colourHtml += '<span>' + colour.name + '</span>';				
		colourHtml += '<div class="control"><a class="paint-finder-colour-remove" href="#">Remove</a><a class="paint-finder-colour-view" href="home.php?col=' + colour.id + '&find=1">Matching products</a><a class="paint-finder-colour-order-tester" href="#">Order tester</a></div>';	
		colourHtml += '</div>';
			
		$('#paint-finder-colour-selection-items').append(colourHtml);
		
		
		
		
		var colourCount = $('#paint-finder-colour-selection-items .paint-finder-colour-selection-info').length;		
		if (colourCount != 0) {			
			$('#paint-finder-colour-selection-up').show();
			$('#paint-finder-colour-selection-down').show();
			if (colourCount > VISIBLE_COLOUR_SELECTIONS) {
				$('#paint-finder-colour-selection-down').removeClass('inactive');
			} else {
				$('#paint-finder-colour-selection-down').addClass('inactive');				
			}
		} else {
			$('#paint-finder-colour-selection-up').hide();
			$('#paint-finder-colour-selection-down').hide();			
		}									
		
		
		this.setColourSelectionCount(colourCount);		
//		this.setColourNavigationEvents();		
		this.setColourSelectionEvents();
		
		$('#paint-finder-colour-selection-info-' + colour.id).show();						
				
		this.showColourSelectionProductsButton(true);
		this.showColourSelectionTesterPotButton(true);											
	}
			
	this.setPaletteEvents = function() {
		var id;
		var VISIBLE_COLOURS = 7;
		
		$('#paint-finder .paint-finder-palette').unbind('click');
		$('#paint-finder .paint-finder-palette').click(function() {
			var objPalette = $(this);
			$('#paint-finder .paint-finder-palette').each(function() {
				if (objPalette != $(this)) {
					$(this).removeClass('active');
				}
			});
			
			objPalette.addClass('active');
						
			id = objPalette.attr('id').substring(21);									

			paintFinder.refreshColours();
			
			paintFinder.setSessionData();
				
		});			
	}
	
	this.setSessionData = function() {
		var manufacturers = [];
		var types = [];
		var applications = [];
		var finishes = [];
		var colours = [];
		var palettes = [];			
		
		$('#paint-finder-manufacturers .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {
				manufacturers[manufacturers.length] = $(this).attr('id').substring(26);
			}
		});
		
		$('#paint-finder-type .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {		
				types[types.length] = $(this).find('.title').html();
			}
		});	
		
		$('#paint-finder-application .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {		
				applications[applications.length] = $(this).find('.title').html();
			}
		});	
		
		$('#paint-finder-finish .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {		
				finishes[finishes.length] = $(this).find('.title').html();
			}
		});		
				
		$('#paint-finder-colour-selection .paint-finder-colour-selection-info').each(function() {
			colours[colours.length] = $(this).attr('id').substring(35);
		});				
		
		$('#paint-finder-colour-groups .paint-finder-palette.active').each(function() {
			var palette_obj = $(this);
			
			palette_id = palette_obj.attr('id').substring(21);	
			
			palettes[palettes.length] = palette_id;
		});				
		
		$.getJSON('set-paint-finder-filters.php', {'manufacturers[]': manufacturers, 'types[]': types, 'applications[]': applications, 'finishes[]': finishes, 'colours[]': colours, 'palettes[]': palettes}, function(data) {			
//			if (data.success == true) {
//				var url = location.href;
//	    		var baseUrl = url.substring(0, url.indexOf('/', 14));
//				
//				document.location = baseUrl + '/home.php?find=1';
//			}
		});			
		
	}	
	
	this.clearColours = function() {
		$('#paint-finder-colours .inner').html('');
	}
		
	this.getColours = function() {
		var html;
		var manufacturers = [];
		var types = [];
		var applications = [];
		var finishes = [];
		var palettes = [];
		var VISIBLE_PALETTES = 7;
		
		$('#paint-finder-loader').fadeIn('slow');
		
		$('#paint-finder-manufacturers .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {
				manufacturers[manufacturers.length] = $(this).attr('id').substring(26);
			}
		});
		
		$('#paint-finder-type .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {		
				types[types.length] = $(this).find('.title').html();
			}
		});	
		
		$('#paint-finder-application .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {		
				applications[applications.length] = $(this).find('.title').html();
			}
		});	
		
		$('#paint-finder-finish .paint-finder-option-item.active').each(function() {
			if (!$(this).hasClass('select-all')) {		
				finishes[finishes.length] = $(this).find('.title').html();
			}
		});		
				
		$('#paint-finder-colour-groups .paint-finder-palette.active').each(function() {
			var palette_obj = $(this);
			
			palette_id = palette_obj.attr('id').substring(21);				
			
			palettes[palettes.length] = palette_id;
		});		
		
		$.getJSON('get-colours.php', {'manufacturers[]': manufacturers, 'types[]': types, 'applications[]': applications, 'finishes[]': finishes, 'palettes[]': palettes}, function(data) {			
			var coloursHtml = '<div id="paint-finder-colour-matches">';
			var colourCount = 0;
			
			coloursHtml += '<div class="paint-finder-colour-match" id="paint-finder-colour-match-' + palette_id + '">';
			
			$.each(data.main_colours, function(colourIndex, colour) {								
				colourCount++;
				coloursHtml += '<div id="paint-finder-colour-match-info-x' + colour.id + '" class="paint-finder-colour-match-info">';
				coloursHtml += '<div class="paint-finder-colour-match-info-colour" style="background-color: ' + colour.code + ';"></div>';
				coloursHtml += '<span>' + colour.name + '</span>';				
				coloursHtml += '<div class="control">';
				if (colour.selected == false) {
					coloursHtml += '<a href="#" class="paint-finder-colour-add">Add to my colours</a>';
				}
				coloursHtml += '<a href="home.php?col=x' + colour.id + '&find=1" class="paint-finder-colour-view">Matching products</a>';
				coloursHtml += '</div>';
				coloursHtml += '</div>';
			});						
			
			$.each(data.colours, function(colourIndex, colour) {								
				colourCount++;
				coloursHtml += '<div id="paint-finder-colour-match-info-' + colour.id + '" class="paint-finder-colour-match-info">';
				coloursHtml += '<div class="paint-finder-colour-match-info-colour" style="background-color: ' + colour.code + ';"></div>';
				coloursHtml += '<span>' + colour.name + '</span>';				
				coloursHtml += '<div class="control">';
				if (colour.selected == false) {
					coloursHtml += '<a href="#" class="paint-finder-colour-add">Add to my colours</a>';
				}
				coloursHtml += '<a href="home.php?col=' + colour.id + '&find=1" class="paint-finder-colour-view">Matching products</a>';
				coloursHtml += '</div>';
				coloursHtml += '</div>';
			});			
			
			coloursHtml += '</div>';							
			
			coloursHtml += '</div>';				
									
			if (colourCount == 0) {
				paintFinder.showColourProductsButton(false);
				paintFinder.setNoColourMatches();				
			} else {
				paintFinder.showColourProductsButton(true);				
				$('#paint-finder-colours .inner').html(coloursHtml);
				$('.paint-finder-colour-match').show();
				paintFinder.setColourEvents();
				paintFinder.setColourNavigation();
				paintFinder.setColourMatchCount(colourCount);
				
			}			
			
			$('#paint-finder-loader').hide();			
		});					
	}
	
	this.refreshColours = function() {
		if ($('#paint-finder-manufacturers').find('.paint-finder-option-item.active').length != 0 &&
				$('#paint-finder-type').find('.paint-finder-option-item.active').length != 0 &&
				$('#paint-finder-application').find('.paint-finder-option-item.active').length != 0 &&			
				$('#paint-finder-finish').find('.paint-finder-option-item.active').length != 0 &&
				$('#paint-finder-colour-groups').find('.paint-finder-palette.active').length != 0) {	

			// SCO 20120119 fix case
			//this.GetColours();
			this.getColours();  
			this.showColourProductsButton(true);			
		} else {
			this.showColourProductsButton(false);
			this.setNoColourMatches();
		}		
		
//		this.setColourEvents();
		this.setColourSelectionEvents();
	}
	
	this.showTesterPots = function(colourId) {
		var colours = new Array();
		var mainColours = new Array();
		
		var html = '<h2>Tester Pot Selection</h2>';
		html += '<p>You are ordering tester pots for colours which have multiple manufacturers. Please choose your preferred manufacturer for each of these colours to continue.</p>';
		html += '<table cellpadding="2" cellspacing="2">';
		
		if (colourId == '') {		
			$('.paint-finder-colour-selection-info').each(function() {
				var id = $(this).attr('id').substring(35);
				html += '<tr>';
				html += '<td class="tester-pot-selector-col1"><div class="tester-pot-selector-colour" style="background-color: ' + $(this).children().eq(0).css('background-color') + '"></div></td>';
				html += '<td class="tester-pot-selector-col2">' + $(this).children().eq(1).html() + '</td>';
				
				if (id.substring(0, 1) == 'x') {
					mainColours[mainColours.length] = id.substring(1);
					
					html += '<td class="tester-pot-selector-col3">';
					html += '<select id="tester-pot-selector-manufacturers-' + id + '" class="tester-pot-selector-manufacturers">';
					html += '<option value="">Select a manufacturer</option>';
					html += '</select>';
					html += '</td>';
				} else {
					colours[colours.length] = id;
					
					html += '<td class="tester-pot-selector-col3">Added to cart</td>';
				}
				
				html += '</tr>';
			});		
		} else {
			var id = colourId;
			var colourItem = $('#paint-finder-colour-selection-info-' + colourId);
			
			html += '<tr>';
			html += '<td class="tester-pot-selector-col1"><div class="tester-pot-selector-colour" style="background-color: ' + colourItem.children().eq(0).css('background-color') + '"></div></td>';
			html += '<td class="tester-pot-selector-col2">' + colourItem.children().eq(1).html() + '</td>';
			
			if (id.substring(0, 1) == 'x') {
				mainColours[mainColours.length] = id.substring(1);
				
				html += '<td class="tester-pot-selector-col3">';
				html += '<select id="tester-pot-selector-manufacturers-' + id + '" class="tester-pot-selector-manufacturers">';
				html += '<option value="">Select a manufacturer</option>';
				html += '</select>';
				html += '</td>';
			} else {
				colours[colours.length] = id;
				
				html += '<td class="tester-pot-selector-col3">Added to cart</td>';
			}
			
			html += '</tr>';			
		}
		
		html += '</table>';
		
		if (mainColours.length != 0) {		
			$('#tester-pot-selector').html(html).show();		
		}
		
		$.getJSON('get-manufacturers-by-colour.php', {'colours[]': mainColours}, function(data) {			
			$.each(data, function(colourid, manufacturers) {
				$.each(manufacturers, function(testpotid, manufacturer) {
					$('#tester-pot-selector-manufacturers-x' + colourid).append('<option value="' + testpotid +  '">' + manufacturer + '</option>');
				});
			});
			
			$('.tester-pot-selector-manufacturers').change(function() {
				var dropDown = $(this);
				$.post('cart.php?ajax=1', {'productid': dropDown.val(), 'amount': 1, 'price': 2.99, 'mode': 'add'}, function(data) {
					dropDown.replaceWith('Added to cart');
					if ($('.tester-pot-selector-manufacturers').length == 0) {
						$('#tester-pot-selector').html('').hide();
						document.location = 'cart.php';						
					}										
				});				
			});
		});
		
		$.getJSON('get-tester-pot-data.php', {'colours[]': colours}, function(data) {
			$.each(data, function(testerpotindex, testerpotid) {
				$.post('cart.php?ajax=1', {'productid': testerpotid, 'amount': 1, 'price': 2.99, 'mode': 'add'}, function(data) {
					if (mainColours.length == 0) {
						document.location = 'cart.php';
					}
				});								
			});
		});
	}
	
	this.showColourProductsButton = function(show) {
		if (show) {
			$('#paint-finder-colours-matches-button').show();
		} else {
			$('#paint-finder-colours-matches-button').hide();			
		}
	}
	
	this.showColourSelectionProductsButton = function(show) {
		if (show) {
			$('#paint-finder-colour-selection-matches-button').show();
		} else {
			$('#paint-finder-colour-selection-matches-button').hide();			
		}		
	}
	
	this.showColourSelectionTesterPotButton = function(show) {
		if (show) {
			$('#paint-finder-colour-selection-tester-button').show();
		} else {
			$('#paint-finder-colour-selection-tester-button').hide();			
		}		
	}		
};

$(function() {	
//	$('.paint-finder-show-results button').click(function() {
//		paintFinder.setSessionData();
//	});
	
	$('.paint-finder-select .select-all').click(function() {
		var item = $(this);
		paintFinder.selectAll(item);
		paintFinder.refreshColours();
		paintFinder.setSessionData();
		return false;
	});
	
	$('.paint-finder-select .select-none').click(function() {
		var item = $(this);
		paintFinder.deselectAll(item);
		paintFinder.refreshColours();
		paintFinder.setSessionData();		
		return false;
	});
	
	$('.paint-finder-option-item').click(function() {
		var item = $(this);
				
		// .item is already selected
		if (item.hasClass('active')) {
			item.removeClass('active');
		} else {
			item.addClass('active');
		}								
		
		paintFinder.refreshColours();
		
		paintFinder.setSessionData();		
		
	});	
	
	$('.paint-finder-option-item').mouseover(function() {	
		var item = $(this);
		item.addClass('over');
	});
	
	$('.paint-finder-option-item').mouseout(function() {	
		var item = $(this);
		item.removeClass('over');
	});
	
	$('#paint-finder-colours-matches-button button').click(function() {
		var url = location.href;
		var baseUrl = url.substring(0, url.indexOf('/', 14));
		var paletteId = $('.paint-finder-palette.active').attr('id').substring(21);		
		var newUrl = baseUrl + '/home.php?find=1&palette=' + paletteId;
		
		document.location = newUrl;
		
		return false;	
	});
	
	$('#paint-finder-colour-selection-matches-button button').click(function() {
		var url = location.href;
		var baseUrl = url.substring(0, url.indexOf('/', 14));
		var newUrl = baseUrl + '/home.php?find=1';
		
		document.location = newUrl;
		
		return false;			
	});
	
	$('#paint-finder-colour-selection-tester-button button').click(function() {		
		paintFinder.showTesterPots('');
	});
	
	$('.paint-finder-colour-order-tester').click(function() {		
		var colourId = $(this).parent().parent().attr('id').substring(35);
		paintFinder.showTesterPots(colourId);
		return false;
	});
	
	paintFinder.setPaletteEvents();
	
	paintFinder.refreshColours();	
	
});

