$(function() {
	var root = $('#header h1 a').attr('href');
	
	// Replace select elements:
	$('select')
		.uniform();
	
	// External links:
	$('a[rel = external]')
		.live('click', function() {
			window.open($(this).attr('href'));
			
			return false;
		});
	
	// Various image faders:
	$('.image-fader')
		.imageFader();
	
	// Featured events scroller:
	$('#feature-events div.scrollable')
		.scrollable({
			size: 3
		})
		.circular()
		.autoscroll({
			autuplay: true,
			steps: 3,
			interval: 6000
		});
	
	// Contact form:
	$('#contact-form')
		.each(function() {
			var $wrapper = $(this);
			var $field = $('<div />')
				.addClass('field')
				.insertBefore($('form:first', this));
			var $label = $('<label />')
				.appendTo($field)
				.text('Enquiry Type');
			var $select = $('<select />')
				.appendTo($field);
			var height = 0;
			
			$('form', this).each(function() {
				var $form = $(this).hide();
				var text = $('legend', $form)
					.hide().text();
				
				if ($form.height() > height) {
					height = $form.height();
				}
				
				$('<option />')
					.text(text)
					.appendTo($select);
			})
			
			$wrapper
				.css({
					'height':	$wrapper.height() + height + 'px'
				});
			
			$select
				.uniform()
				.bind('change', function() {
					var $forms = $wrapper.find('form').hide();
					var index = $('option:selected', this).index();
					
					$($forms.get(index)).show();
				});
			
			$('p.error', this)
				.closest('form')
				.each(function() {
					$select.val($('legend', this).text());
				})
			
			$select
				.trigger('change');
		});
	
		$('input#general-sign-up, input#leasing-sign-up').live('click', function() {
	        if ($(this).attr("checked") == true) {
				$("div.postcode").css('visibility', 'visible');
	        } else {
				$("div.postcode").css('visibility', 'hidden');
	        }
		});
        if ($('input#general-sign-up, input#leasing-sign-up').attr("checked") == true) {
			$("div.postcode").css('visibility', 'visible');
        } else {
			$("div.postcode").css('visibility', 'hidden');
        }
	
	// Placeholders:
	if (!Modernizr.input.placeholder) {
		$('[placeholder]')
			.live('change', function() {
				$(this).removeClass('placeholder');
			})
			
			.live('focus', function() {
				var input = $(this);

				if (input.is('.placeholder')) {
					input
						.removeClass('placeholder')
						.val('');
				}
			})
			
			.live('blur', function() {
				var input = $(this);
				
				if (input.val() == '') {
					input.addClass('placeholder');
				}
				
				if (input.is('.placeholder')) {
					input.val(input.attr('placeholder'));
				}
			})
			
			.trigger('blur')
			.parents('form')
			
			.live('submit', function() {
				$('[placeholder]', this)
					.each(function() {
						var input = $(this);
						
						if (input.is('.placeholder')) {
							input.val('');
						}
					})
			});
	}
	
	// Centre directory search:
	$(document).ready(function() {
		
		// Create Show All link
		$('<a />')
			.bind('click', function() {
				var url = root + '/centre-directory/ajax-stores/';
				
				$('#store-results .copy').empty();
				
				$.get(url, function(data) {
					$('#store-results')
						.html(data);
					$('#store-search li')
						.removeClass('active');
				});
				
				return false;
			})
			
			.attr('href', '#')
			.text('Show all stores')
			.insertAfter('#store-search form')
			.wrap('<p></p>');
		
		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) {
					var url = root + '/centre-directory/ajax-stores/?store-name=' + data;
					
					$('#store-results').empty();

					$.get(url, function(filteredData) {
						$('#store-results').html(filteredData);

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

							$('#store-search li').removeClass('active');
							$('#store-search li[rel=' + relatedCat + ']').addClass('active');
						});
					});
				});
		}
	});
	
	// Centre directory map:
	(function() {
		var map = $('#map');
		var width = map.width();
		var height = map.height();
		var x_grid = width / 40;
		var y_grid = height / 80;
		
		// Tweak the final position:
		var x_adjust = 6;
		var y_adjust = 6;
		
		$('#store-results li')
			.live('mouseover', function() {
				$(this).children('.icon').show()
			})
			
			.live('mouseout', function() {
				$('.icon').hide();
			});
		
		$('#store-results li .icon')
			.live('click', function() {
				if ($(this).attr('rel') != '') {
					var bits = $(this).attr('rel').split('x');
					var x = parseInt(bits[1]);
					var y = parseInt(bits[0]);
					
					x = Math.max(1, Math.min(x, 40)) - 1;
					y = Math.max(1, Math.min(y, 80)) - 1;
					
					$("#map-pointer")
						.css({
							'margin-left': '-' + (width - (x * x_grid + x_adjust)) + 'px',
							'margin-top': '-' + (height - (y * y_grid + y_adjust)) + 'px'
						})
						.show();
				}

				return false;
			});
	})();
	
	// Homepage search:
	(function() {
		$("#store-directory form")
			.bind('submit', function() {
				if ($('#cat').val() != 'all' &&  $('#store').val() == '') {
					window.location = root + '/centre-directory/' + $('#cat').val() + '/';
					
					return false;
				}
				
				else {
					$('#cat').attr('disabled', 'disabled');
				}
			});
	})();
	
	// Shadow replacement:
	$(document).ready(function() {
		$('.column[data-shadow-enabled != no]', '#content')
			.each(function() {
				var $self = $(this);
				var url = '/workspace/image-jit/?filter=column-shadow';
				var image = $self.css('background-image');
				var query = '&width='
					+ $self.outerWidth()
					+ '&height='
					+ $self.outerHeight();
				
				if (/^url\(/.test(image)) {
					query += '&file=' + image.split('/workspace/')[1].replace(/["']?\)$/g, '');
				}
				
				$self
					.css({
						'background': 'url(' + root + url + query + ')'
					});
			});
	});
});
