

	/*----------------------------------------------------------------------------
		Equal Heights
	----------------------------------------------------------------------------*/

	(function($) {
		$.fn.equalHeights = function(minHeight, maxHeight) {
			tallest = (minHeight) ? minHeight : 0;
			this.each(function() {
				if($(this).height() > tallest) {
					tallest = $(this).height();
				}
			});
			if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
			return this.each(function() {
				$(this).height(tallest).css("overflow","visible");
			});
		}
	})(jQuery);


/*----------------------------------------------------------------------------
	After DOM is Loaded
----------------------------------------------------------------------------*/

	$(document).ready(function() {

		var root = $('#header h2 a').attr('href');
		
	/*------------------------------------------------------------------------
		Image Faders
	------------------------------------------------------------------------*/
		
		$('.image-fader').imageFader();
		
	/*------------------------------------------------------------------------
		Fixes for Internet Explorer
	------------------------------------------------------------------------*/
		
		if ($.browser.msie) {
			if (parseFloat($.browser.version) <= 7) {
				// Hover states:
				$('li, input[type=submit]').hover(
					function() {
						$(this).addClass('hover');
					},
					function() {
						$(this).removeClass('hover');
					}
				);
				$('li, input[type=submit]').focus(
					function() {
						$(this).addClass('hover');
					},
					function() {
						$(this).removeClass('hover');
					}
				);
			}
			
			// IE6 Can't have this, since it gets confused by the number of classes:
			if (parseFloat($.browser.version) != 6) {
				// First/last child classes:
				$('*:first-child').addClass('first-child');
				$('*:last-child').addClass('last-child');
			}
			
			$('#content .column.store-results ol li:last-child').addClass('last-child');
			

			$("ol.inline li:not(:first-child)").each(function(){
				$(this).html("| " + $(this).html());
			});
			$("ul.inline li:not(:first-child)").each(function(){
				$(this).html("| " + $(this).html());		
			});
			
			// Nth Child Classes
			$('*:nth-child(1n)').addClass('nth-child-1');
			$('*:nth-child(2n)').addClass('nth-child-2');
			$('*:nth-child(3n)').addClass('nth-child-3');
			$('*:nth-child(4n)').addClass('nth-child-4');

		}

	/*----------------------------------------------------------------------------
		Open external links in a new tab/window
	----------------------------------------------------------------------------*/
		$('a.external, a[rel=external]').live("click", function() {
			window.open($(this).attr('href'));
			return false;
		})
	});
	
	

/*----------------------------------------------------------------------------
	Homepage Specials
----------------------------------------------------------------------------*/

	$(document).ready(function() {
	
		$("#home-specials li .info").equalHeights();

	});
	
	
	$(document).ready(function() {
	
		$(".home-scrollable").scrollable({size: 4}).navigator().circular().autoscroll({ 
		    autoplay:true,
			steps: 4, 
		    interval: 6000         
		});;
		
		$("#home-specials li:first-child").addClass("first-child");
			
	});
	
/*----------------------------------------------------------------------------
	Homepage Centre Directory Box
----------------------------------------------------------------------------*/	

		$(document).ready(function() {
		
			if($("body").hasClass("home")) {
				
				$('.home #centre-directory input[type="text"]').autocomplete(names, {matchContains: true});	
			}
			
		});

	
/*----------------------------------------------------------------------------
	Specials Page - Featured
----------------------------------------------------------------------------*/	
	
	$(document).ready(function() {
	
		$(".specials-scrollable").scrollable({size: 3}).circular().autoscroll({ 
		    autoplay:true,
			steps: 3, 
		    interval: 6000         
		});	
	
	});	

	
/*----------------------------------------------------------------------------
	Centre Directory 
----------------------------------------------------------------------------*/	

	// Filter Stores 
	$(document).ready(function() {
		
		var root = $('#header h2 a').attr('href');	
		var loader = $("<img />");
		loader.addClass("loader");
		loader.attr("src", root + "/workspace/assets/images/ajax-loader.gif");
		
		if($('.store-search input[type="text"]').length > 0) 
		{
			
			$('.store-search input[type="submit"]').hide();	
			
			//Initialize Centre Directory
			$('.store-search input[type="text"]').autocomplete(names, {matchContains: true});	


			$('.store-search input[type="text"]').result(function(event, data, formatted) {

				$(".store-results .copy").html(loader);

				var url = root + '/centre-directory/ajax-stores/?store-name=' + data;
				$.get(url, function(filteredData) {
					$(".store-results .copy").html(filteredData);
					attachHovers();
					updateCategories();
				});

			});
	
		}	
		
		//Create Show All link
		var showAll = $("<a href='#'>Show all stores</a>");
		showAll.click(function(){
			
			$(".store-results .copy").html(loader);
			var url = root + '/centre-directory/ajax-stores/';
			$.get(url, function(filteredData) {
				$(".store-results .copy").html(filteredData);
				attachHovers();
				$('.store-search li').each(function(){					
					$(this).removeClass("active");
				});
			});
			
		});
		
		showAll.wrap("$(<p></p>)");
		showAll.insertAfter(".store-search fieldset");
						
	});
	
	
	// Load the correct Map when an Store is Hovered
	$(document).ready(function() {
		
		attachHovers();

	});
	
	function attachHovers() {
		
		$(".icon").hide();
		
		$("#map-pointer").hide();		
		
		$('.store-results li').hover(
			
			function()
			{	
				$(this).children(".icon").show()
			},
			function(){
				
				$(".icon").hide();

			}	
		);	
		
		
		//Store Letters as Numbers
		var letters = "abcdefghijklmnopqrstuvwxyz";
		var lettersArray = letters.split("");

		$('.store-results li .icon').click( 
	
			function(){	
											
				if($(this).attr("rel") != "")
				{
					var x = $(this).attr("rel").split("-")[1];
					var yCoord = $(this).attr("rel").split("-")[0];		
					var y;
					
					for (i=0;i<lettersArray.length;i++) {
						
						if(lettersArray[i] == yCoord) {
							y = (i+1);
							break;
						}
					}
	
					$("#map-pointer").css("left", ((x * 25) - 9) + "px");
					$("#map-pointer").css("top", ((y * 25) - 34) + "px");
					$("#map-pointer").show();
									
				}
				
				return false;		
					
			}
		);
	}
	
	function updateCategories() {
		
		$('.store-results li').each(function(){
			
			var relatedCat = $(this).attr("rel");
			
			$('.store-search li').each(function(){
				
				if($(this).attr("rel") == relatedCat) {
					
					$(this).addClass("active")
				}
				else
				{
					$(this).removeClass("active")
				}
				
			});		
			
		});
		
	}

/*----------------------------------------------------------------------------
	Centre Directory Homepage
----------------------------------------------------------------------------*/
	$(document).ready(function() { 
		
		$("#cat").attr("disabled", "");	
		
		$("#centre-directory form").submit(function(){
			
			if($("#cat").val() != "all" &&  $("#store").val() == "") {
				
				var root = $('#header h2 a').attr('href');	
				window.location = root + "/centre-directory/" + $("#cat").val();
				return false;

			}
			else
			{
				$("#cat").val('all');		
			}
					
		});
		
	});
