var jqf = jQuery.noConflict();

/**
* preload map images
*/
jqf.getJSON('/ajax/getAllImages.ajax.php', { }, function(json) {
	var type = 'png';
	if (jqf.browser.msie && jQuery.browser.version == '6.0') {
		type = 'gif';
	}
	jqf.each(json, function(i, item) {
		jqf("<img>").attr("src", '/templates/main/images/contact-us/maps/line' + item + '.' + type);
	});
});


/**
* IE6 Dark bullet hack
*/
if (jqf.browser.msie && jQuery.browser.version == '6.0') {
	jqf(".fullCircle").css("background-image", '/templates/main/images/contact-us/greyBulletDark.gif');
}

jqf(document).ready(function() {
	/**
	* show branches in this region
	*/
	jqf('.region').click(function() {
		var thisLink = this;
		if (jqf(thisLink).parent().next().hasClass('active')) {
			return false;
		}
		jqf('#branches ul.active').hide(function() {
			jqf(this).removeClass('active');

			jqf(thisLink).parent().next().show(function() {
				jqf(this).addClass('active');
			});

		});
		return false;
	});

	/**
	* change address and set this branch active
	*/
	jqf('.branch').click(function() {
		// active region left menu //
		jqf('#branches ul li a.active').removeClass('active');
		jqf(this).addClass('active');

		// get city //
		var city = this.href.replace(/.*contactCity=/, '');
		city = city.replace(/&.*/, '');

		// get region //
		var region = this.href.replace(/.*contactRegion=/, '');
		region = region.replace(/&.*/, '');

		// get address //
		jqf.getJSON('/ajax/contactUsAddress.ajax.php', {
				contactRegion: region,
				contactCity: city
			},
			function(json) {
				var header = '';
				if (json.header == 'Tingley') {
					header = 'Headoffice ' + json.header;
				}
				else {
					header = json.header;
				}

				// change postcode for Get directions //
				jqf('input#postcode').attr('value', json.postcode);

				// change title //
				jqf('#address h3 span.title').fadeOut('slow', function() {
					jqf('#address h3 span.title').text(header);
					jqf('#address h3 span.title').fadeIn('slow');
				});

				// change address //
				jqf('#address address').fadeOut('fast', function() {
					jqf('#address address').html(json.address);
					jqf('#address address').fadeIn('fast');
				});

				// change the image (map line) //
				jqf('#mapLink').slideUp('slow', function() {
					if (json.image != '') {
						var type = 'png';
						if (jqf.browser.msie && jQuery.browser.version == '6.0') {
							type = 'gif';
						}

						if(jqf('#mapLink img').attr('src', '/templates/main/images/contact-us/maps/line' + json.image + '.' + type)) {
							jqf('#mapLink').slideDown('slow');
						}
					}
				});
			}
		);
		return false;
	});
});