/*----------------------------------------------------------------------------
	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');
					}
				).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');
			}


			$("ol.inline li:not(:first-child), 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');

		}

	});

/*----------------------------------------------------------------------------
	While DOM is loading
----------------------------------------------------------------------------*/

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


/*----------------------------------------------------------------------------
	What's On
----------------------------------------------------------------------------*/

	$(document).ready(function() {

		$(".scrollable").scrollable({size: 3}).circular().autoscroll({
		    autuplay:true,
			steps: 3,
		    interval: 6000
		});

	});

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

	// Filter Stores
	$(document).ready(function() {

		var root = $('#header h2 a').attr('href');
		var loader = $("<img />");
		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();

					$('.store-results li').each(function(){
						var relatedCat = $(this).attr("rel");

						$('.store-search li').removeClass('active');
						$('.store-search li[rel=' + relatedCat + ']').addClass('active');
					});
				});

			});

		}

		//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').removeClass("active");
			});

		});

		showAll.wrap("$(<p></p>)");
		showAll.insertAfter(".store-search fieldset");

		// Load the correct Map when an Store is Hovered
		attachHovers();

	});

	function attachHovers() {

		$('.icon, #map-pointer').hide();

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

		//Store Letters as Numbers
		var letters = "abcdefghijklmnopqrstuvwxyz".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<letters.length;i++) {
					if(letters[i] == yCoord) {
						y = (i+1);
						break;
					}
				}

				$("#map-pointer").css({
					"left": (x * 20 -30) + "px",
					"top": (y * 20 -30) + "px"
				}).show();
			}

			return false;

		});
	}

/*----------------------------------------------------------------------------
	Centre Directory Homepage
----------------------------------------------------------------------------*/
	$(document).ready(function() {

		$("#cat").attr("disabled", "");

		$("#store-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").attr("disabled", "disabled");
			}
		});

	});
