$(function () {
	
	/*
	----------------------------------------------------------
	Locations page
	----------------------------------------------------------
	*/
	
	var curProvince = "";
	var curShortProvince = "";
	
	$('.columnize').livequery(function () { 
		if($(this).find('ul li').length > 10) {
			$(this).columnize({columns:4});
		}
	});
	
	$('.back-to-area-listings').livequery(function() { 
		$(this).click(function(){
			curCity = $(this).attr('href').split('#')[1].split('|')[0];
			curShortProvince = $(this).attr('href').split('#')[1].split('|')[1];
		
			$('.city-location').fadeOut('fast', function() {
				$('.left-locations').animate({width:'220px'}, 'fast', 'swing', function() {
					$('.right-locations').livequery(function() { 
						$(this).animate({width:'478px'}, 'fast', 'swing', function() {
							$('.city-name').fadeOut('fast', function() {
								$('.back-to-area-listings').livequery(function() { $(this).attr('href', '#' + curCity + '|' + curShortProvince); });
								$('.city-name').html(curCity);
								$('.city-name').fadeIn('fast');
							});
							
							$.post('../includes/php/locations-fetch.php', {province: curShortProvince, city: curCity, cssClass: "locations"}, function(data) { 
								$('.city-location').html(data);
								$('.city-location').fadeIn('fast');	
							});
						});
					});
				});
			});
			
			$('.slide-content-locations').fadeOut('fast', function() {
				$.post('../includes/php/locations-fetch.php', {province: curShortProvince, cssClass: "left-side"}, function(data) { 
					$('.slide-content-locations').html(data).fadeIn();
				});
			});
		});
	});
	
	$('.city-location ul li div.location-content').livequery(function() {
		$(this).mouseenter(
			function() {
				$(this).find('div.details-link').fadeIn('fast');
				$(this).css('borderColor', 'red');
			}
		);
		$(this).mouseleave(
			function() {
				$(this).find('div.details-link').fadeOut('fast');
				$(this).css('borderColor', '#eaeaea');
			}
		);
		
		$('.details-link').livequery(function() {
			$(this).click(
				function() {
					$(this).parent().parent().parent().find('li').not($(this).parent().parent()).slideUp('fast').remove();
					$(this).parent().parent().parent().find('.location-other-stores').slideUp('fast').remove();
					
					$(this).unbind('click');
					$('.city-location ul li div.location-content').unbind('mouseenter');
					$('.city-location ul li div.location-content').unbind('mouseleave');

					$(this).parent().html($(this).parent().html() + '<div style="padding:10px"><div id="map_canvas" style="width: 445px; height: 350px; padding:0;"></div></div>')
						
					$(this).livequery(function () { map_initialize($(this).find('.location-address').html() + ', Canada'); });
					$(this).livequery(function () { $(this).find('.details-link').remove(); });
				}
			);
		});
	});
	
	$('.city-location ul li div.location-content').livequery(function() {
		$(this).html($(this).html() + '<div class="details-link">get map</div><br class="clearboth" />');
	});
	
	$('.stores-link').livequery(function() {
		$(this).click(function(){
			var curCity = $(this).html();

			$('.city-location').fadeOut('fast', function() {
				$('.left-locations').animate({width:'220px'}, 'fast', 'swing', function() {
					$('.right-locations').animate({width:'478px'}, 'fast', 'swing', function() {
						$('.city-name').fadeOut('fast', function() {
							$('.back-to-area-listings').livequery(function() {
								$(this).attr('href', '#' + curCity + '|' + curShortProvince);
							});
							
							$('.city-name').html(curCity);
							$('.city-name').fadeIn('fast');
						});
						
						$.post('../includes/php/locations-fetch.php', {province: curShortProvince, city: curCity, cssClass: "locations"}, function(data) { 
							$('.city-location').html(data);
							$('.city-location').fadeIn('fast');	
						});
					});
				});
			});
			
			$('.slide-content-locations').fadeOut('fast', function() {
				$.post('../includes/php/locations-fetch.php', {province: curShortProvince, cssClass: "left-side"}, function(data) { 
					$('.slide-content-locations').html(data).fadeIn();
				});
			});
		});
	});
	
	$('div.columnize ul.city-items li a').livequery(function() { $(this).click(function(){
			var curCity = $(this).html();

			$('.city-location').fadeOut('fast', function() {
				$('.left-locations').animate({width:'220px'}, 'fast', 'swing', function() {
					$('.right-locations').animate({width:'478px'}, 'fast', 'swing', function() {
						$('.city-name').fadeOut('fast', function() {
							$('.back-to-area-listings').livequery(function() {
								$(this).attr('href', '#' + curCity + '|' + curShortProvince);
							});
							$('.city-name').html(curCity);
							$('.city-name').fadeIn('fast');
						});
						
						$.post('../includes/php/locations-fetch.php', {province: curShortProvince, city: curCity, cssClass: "locations"}, function(data) { 
							$('.city-location').html(data);
							$('.city-location').fadeIn('fast');	
						});
					});
				});
			});
			
			$('.slide-content-locations').fadeOut('fast', function() {
				$.post('../includes/php/locations-fetch.php', {province: curShortProvince, cssClass: "left-side"}, function(data) { 
					$('.slide-content-locations').html(data).fadeIn();
				});
			});
		});
	});
	
	$('div.left-side ul.city-items li a').livequery(function() { $(this).click(function(){
			var curCity = $(this).html();
			$('.city-name').fadeOut('fast', function() {
				$('.back-to-area-listings').livequery(function() {
					$(this).attr('href', '#' + curCity + '|' + curShortProvince);
				});
				$('.city-name').html(curCity);
				$('.city-name').fadeIn('slow');
			});
			$('.city-location').fadeOut('fast', function() {
				$.post('../includes/php/locations-fetch.php', {province: curShortProvince, city: $('.city-name').html(), cssClass: ""}, function(data) { 
					$('.city-location').html(data);
					$('.city-location').fadeIn('slow');
				});
			});
		});
	});
	
	$('#province-overlay').mouseout(function() {
		for(var i = 0; i < $('.province-item').length; i++) {
			if($('.province-item')[i].alt == curProvince) {
				$('#province-overlay').attr('src',$('.province-item')[i].href);
			}
		}
	});
	
	$('.back-to-provincial-selection').livequery(function() {
		$(this).click(function() {
			$('.grey-header').slideUp('fast');
			$('.slide-content').hide();
			$('.slide-content-locations').hide();
			$('.right-locations').hide();
			
			curProvince = '';
			
			$('#province-overlay').attr('src','/images/blank.gif');
		});
	});
	
	$('.province-item').mouseover(function() {
		$('#province-overlay').attr('src',this.href);
	});
	
	$('.province-item').mouseout(function() {
		if(curProvince != this.alt)
		{
			$('#province-overlay').attr('src','/images/blank.gif');
		}
	});
	
	$('.province-item').click(function() {
		curProvince = this.alt;
		curShortProvince = this.title;
		$('.right-locations').hide('fast', function() {
			$('.left-locations').animate({width:'698px'}, 'fast');
			$('.grey-header h3').html(curProvince);
			$('.grey-header').slideDown('fast', function() {
				$('.slide-message').fadeIn('fast');
				$('.slide-content').show();
				$('.slide-content-locations').fadeOut('fast', function() { 
					$(this).html('');
					$.post('../includes/php/locations-fetch.php', {province: curShortProvince, cssClass: "columnize"}, function(data) { 
						$('.slide-content-locations').html(data);
						$('.slide-content-locations').fadeIn('fast', function () {
							$('.slide-message').fadeOut(500);
						});
					});
				});
			});
		});
		
		return false;
	});

	$('ul.locations li a').click(function() {
		$('.province-item[alt=' + $(this).html() + ']').click();
	});
	
	if($('#hdnProvince').attr('value') != '' && $('#hdnCity').attr('value') != '') {
		curCity = $('#hdnCity').attr('value');
		curShortProvince = $('#hdnProvince').attr('value');

		$('#hdnProvince').attr('value', '');
		$('#hdnCity').attr('value', '');

		$('#province' + curShortProvince).click();
		setTimeout(function() {
			$('a[href="#' + curCity + '|' + curShortProvince + '"]').click();
		}, 1000);
	}
	
	$('.location-name').livequery(function() {
		$(this).each(function() {
			if($(this).html() == 'Tip Top Tailors') {
				$(this).wrapInner('<a href=\'tiptop\'></a>');
				return false;
			}
		});
	});

	/*
	----------------------------------------------------------
	Sniff for current location (Locations)
	----------------------------------------------------------
	*/
	
	var curLocation = document.location.toString();
	
	if(curLocation.match('#')) {
		var anchor = curLocation.split('#')[1];
		var locProvince = anchor.split('|')[1];
		var locCity = anchor.split('|')[0];
		
		$('#province' + locProvince).click();
	
		setTimeout(function() {
			$('a[href="#' + locCity + '|' + locProvince + '"]').click();
		}, 1500);
	}
	
	/*
	----------------------------------------------------------
	Contact / Catalogue
	----------------------------------------------------------
	*/
	
	$('#contact-form').validate({
		submitHandler: function(form) {
			$.post('../includes/php/contact-form-collect.php', {
				name: $('#form-your-name').val(),
				email_address: $('#form-email-address').val(),
				address_1: $('#form-address1').val(),
				address_2: $('#form-address2').val(),
				city: $('#form-city').val(),
				province: $('#form-province').val(),
				postal_code: $('#form-postal-code').val(),
				comment: $('#form-comment').val(),
				event_type: $('[name=form-event-type]:checked').val(),
				event_type_other: $('#form-event-type-other').val(),
				date_of_event: $('#form-date').val(),
				contact_type: $('[name=form-contact-type]:checked').val(),
				telephone: $('#form-telephone').val()
			}, 
			function() { 
				$('#form-contact').slideUp('fast', 
					function() { 
						$('#form-contact-complete').slideDown('fast');
					}
				); 
			})
		}
	});
	
	$('#catalogue-form').validate({
		submitHandler: function(form) {
			$.post('../includes/php/catalogue-form-collect.php', {
				name: $('#form-your-name').val(),
				email_address: $('#form-email-address').val(),
				address_1: $('#form-address1').val(),
				address_2: $('#form-address2').val(),
				city: $('#form-city').val(),
				province: $('#form-province').val(),
				postal_code: $('#form-postal-code').val(),
				event_type: $('[name=form-event-type]:checked').val(),
				event_type_other: $('#form-event-type-other').val(),
				date_of_event: $('#form-date').val(),
				catalogue_language: $('[name=form-catalogue-language]:checked').val(),
				telephone: $('#form-telephone').val()
			},
			function() { 
				$('#form-contact').slideUp('fast', 
					function() { 
						$('#form-contact-complete').slideDown('fast');
					}
				); 
			})
		}

	});
	
	$("#form-date").datepicker();
	$("#form-date").datepicker('option', {dateFormat: 'dd/mm/yy'});
	
	$('input[type=radio]').checkBox();
	
	$('input[type=text]').focus(function() { $(this).css('border-color', '#858585'); });
	$('input[type=text]').blur(function() { $(this).css('border-color', '#c0c0c0'); });
	
	$('textarea').focus(function() { $(this).css('border-color', '#858585'); });
	$('textarea').blur(function() { $(this).css('border-color', '#c0c0c0'); });
	
	$('#form-telephone').mask("(999) 999-9999? x 999");
	$('#form-date').mask("99/99/9999");
	$('#form-postal-code').mask("a9a-9a9");
	
	
	$('#submit-form').click(function() { $('#contact-form').submit(); });
	
	$('#submit-form-catalogue').click(function() { $('#catalogue-form').submit(); });

	$('#form-postal-code').change(function() { this.value = this.value.toUpperCase(); });
	
	$('input[name=form-event-type]').change(function() { 
		if ($(this).val() == 'Other') {
			 $(this).parent().parent().parent().find('.hide').slideDown(); 
		} else { 
			$(this).parent().parent().parent().find('.hide').slideUp();
		}
	});
	
	$('input[name=form-contact-type]').click(function() { 
		if ($(this).val() == 'Daytime Telephone') {
			 $(this).parent().parent().find('.hide').slideDown(); 
		} else { 
			$(this).parent().parent().find('.hide').slideUp();
		}
	});
	
	$('#catalogue-frame').hide();
	$('#catalogue-frame').load(function() { $('#catalogue-loading').fadeOut('slow', function() { $('#catalogue-frame').show(); }) });

	/*
	----------------------------------------------------------
	Global
	----------------------------------------------------------
	*/
	
	$('.rounded').corners();
	$('.email-address').deobfuscate();
	$('#container-middle').cycle('fade');
	
	$('.columnize').columnize({ columns: 2, lastNeverTallest:true, buildOnce:true });  
	$('ul.columnize li').addClass("dontsplit");
	
});
