/**
 * Lamped eCommerce: Client Library
 * Copyright 2011 Lamped.co.uk
 *
 * @version 2011-04-14
 * @author Marc Gray support@lamped.co.uk
 */
lecClient = {
	isIE: false,
	countryData: {
		AR: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // Argentina
		AU: { region: 'State', postCode: 'Post Code', requiresPostCode: true }, // Australia
		CA: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // Canada
		CL: { region: 'Region', postCode: 'Postal Code', requiresPostCode: false }, // Chile
		CN: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // China
		CZ: { postCode: 'Postal Code', requiresPostCode: true }, // Czech Republic
		DE: { postCode: 'Postal Code', requiresPostCode: true }, // Germany
		DK: { postCode: 'Postal Code', requiresPostCode: true }, // Denmark
		FI: { postCode: 'Postal Code', requiresPostCode: true }, // Finland
		FR: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // France
		GB: { region: 'County', postCode: 'Postcode', requiresPostCode: true }, // United Kingdom
		HK: { postCode: 'Postal Code', requiresPostCode: false }, // Hong Kong
		HU: { postCode: 'Postal Code', requiresPostCode: true }, // Hungary
		IE: { region: 'County', postCode: 'Postcode', requiresPostCode: false }, // Ireland
		IN: { postCode: 'PIN Code', requiresPostCode: false }, // India
		IS: { postCode: 'Postal Code', requiresPostCode: true }, // Iceland
		IT: { region: 'Province', postCode: 'Postcode', requiresPostCode: true }, // Italy
		ID: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // Indonesia
		IQ: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // Iraq
		IR: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // Iran
		JP: { region: 'Prefecture', postCode: 'Postal Code', requiresPostCode: true }, // Japan
		KP: { region: 'Province / Ward', postCode: 'Postal Code', requiresPostCode: true }, // Korea, Democratic People's Republic of
		KR: { region: 'Province / Ward', postCode: 'Postal Code', requiresPostCode: true }, // Korea, Republic of
		LK: { postCode: 'Postcode', requiresPostCode: true }, // Sri Lanka
		MY: { region: 'State', postCode: 'Postcode', requiresPostCode: true }, // Malaysia
		NZ: { postCode: 'Postcode', requiresPostCode: true }, // New Zealand
		NL: { postCode: 'Postal Code', requiresPostCode: true }, // Netherlands
		NO: { postCode: 'Postal Code', requiresPostCode: true }, // Norway
		PH: { region: 'Province', postCode: 'ZIP Code', requiresPostCode: false }, // Philippines
		PL: { postCode: 'Postal Code', requiresPostCode: true }, // Poland
		SG: { postCode: 'Postcode', requiresPostCode: true }, // Singapore
		SK: { postCode: 'Postal Code', requiresPostCode: true }, // Slovakia
		SI: { postCode: 'Postal Code', requiresPostCode: true }, // Slovenia
		SE: { postCode: 'Postal Code', requiresPostCode: true }, // Sweden
		TR: { region: 'Province', postCode: 'Postal Code', requiresPostCode: true }, // Turkey
		US: { region: 'State', postCode: 'ZIP Code', requiresPostCode: true } // United States
	},

	init: function() {
		// Extend jQuery to provide find() and closest() combo
		jQuery.fn.findClosest = function(selector) {
			var match;
			var node = $(this).parent();
			while (node.length) {
				match = $(node).find(selector);
				if (match.length)
					return match;
				node = node.parent();
			}
			return null;
		}

		// Handle auto-submit selections
		$('.lec-js-autosubmit').change(this.onAutoSubmitChange);

		// Handle single submission clicks
		var singleSubmits = $('.lec-js-singlesubmit')
		if (singleSubmits.length) {
			var singleSubmitsPreload = new Image(16, 16).src = '/inc/img/lec-loading.gif';
			singleSubmits.click(this.onSingleSubmitClick);
		}

		// Handle submit on load
		$('.lec-js-submitonload').closest('form').submit();

		// Handle country select changes
		$('.lec-js-country')
			.change(this.onCountryChange)
			.keypress(this.onCountryChange);
		this.onCountryChange();

		// Handle "use billing address as delivery"
		$('.lec-js-copyaddress')
			.change(this.onCopyAddressChange);
		if ($('.lec-js-copyaddress').is(':checked'))
			$('.lec-js-deliveryaddress').css('display', 'none');

		// Allow and handle saluation "Other"
		$('.lec-js-salutation')
			.append('<option>Other...</option>')
			.change(this.onSalutationChange);

		// Detect problematic Internet Explorer
		var ie = navigator.userAgent.match(/^Mozilla\/4\.0 \(compatible; MSIE ([1-8])\.[0-9]/);
		if (ie != null)
			this.isIE = ie[1];

		// Remove any items marked as "no-js"
		$('.no-js').remove();
	},


	/*** General Forms *************************************************/

	onAutoSubmitChange: function() {
		$(this).closest('form').submit();
	},

	onSingleSubmitClick: function(e) {
		$(this)
			.css('display', 'none')
			.after(
				$('<div><img src="/inc/img/lec-loading.gif" height="16" width="16" /> <span style=" font-weight: bold; vertical-align: middle;">Please wait...</span></div>')
					.fadeIn(1000)
			);
	},


	/*** Checkout Forms ************************************************/

	onCountryChange: function() {
		$('.lec-js-country').each(function() {
			var country, postCode;

			// Overwrite defaults with country specific data if available
			var country = { region: 'Region / Province / State', postCode: 'Postal Code / ZIP Code', requiresPostCode: false };
			if (lecClient.countryData[$(this).val()])
				$.extend(country, lecClient.countryData[$(this).val()]);

			// Update region text
			$(this).findClosest('.lec-js-region').text(country.region);

			// Update postcode text, remove required markers
			postCode = $(this).findClosest('.lec-js-postcode');
			postCode.text(country.postCode).parent().find('.lec-required-field').remove();

			// Add required marker if necessary
			if (country.requiresPostCode)
				postCode.parent().find('input[name=postcode]').after('<span class="lec-required-field">*</span>');
		});
	},

	onCopyAddressChange: function() {
		if ($('.lec-js-copyaddress').is(':checked')) {
			$('.lec-js-deliveryaddress').attr('disabled', 'disabled').stop(true, true).fadeOut(1000);
		} else {
			$('.lec-js-deliveryaddress').removeAttr('disabled').stop(true, true).fadeIn(1000);
		}
	},

	onSalutationChange: function() {
		if ($(this).val() == 'Other...') {
			$(this)
				.after('<input type="text" name="salutation" />')
				.remove();
		}
	},


	/*** Document Ready ************************************************/

	readyHandlers: [],
	readyDone: false,
	flashHandlers: [],
	flashDone: false,
	flashPaused: 0,

	ready: function() {
		for(var i=0; i<lecClient.readyHandlers.length; i++) {
			lecClient.readyDone = true;
			try { lecClient.readyHandlers[i][0].call(lecClient.readyHandlers[i][1]); } catch (e) {}
		}
		lecClient.readyHandlers = [];

		if (lecClient.flashPaused == 0) {
			lecClient.flashResume();
		}
	},

	onReady: function(callback, scope) {
		scope = scope || window;
		if (this.readyDone) {
			callback.call(scope);
		} else {
			this.readyHandlers.push([callback, scope]);
		}
	},

	onFlashReady: function(callback, scope) {
		scope = scope || window;
		if (this.flashDone) {
			callback.call(scope);
		} else {
			this.flashHandlers.push([callback, scope]);
		}
	},

	flashPause: function() {
		this.flashPaused++;
	},

	flashResume: function() {
		if (this.flashPaused > 0) {
			this.flashPaused--;
		}

		if (this.flashPaused === 0) {
			for(var i=0; i<this.flashHandlers.length; i++) {
				this.flashDone = true;
				try { this.flashHandlers[i][0].call(this.flashHandlers[i][1]); } catch(e) {}
			}
			this.flashHandlers = [];
		}
	}
}

lecClient.onReady(lecClient.init, lecClient);

$(lecClient.ready);

