
/* Google Analytics boilerplate */

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19893233-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();



/* preload rollover images */

function image_preload (src) {
	var image = new Image ();
	image.src = src;
}

image_preload ('/images/icons/social_media/24px/facebook.png');
image_preload ('/images/icons/social_media/24px/twitter-2.png');
image_preload ('/images/icons/social_media/24px/tumblr.png');
image_preload ('/images/icons/social_media/24px/youtube.png');
image_preload ('/images/icons/social_media/24px/rss.png');



/* login/logout links at bottom of page */

function update_login_logout_links () {
	if ($.cookie ('user_email')) {
		$('#login-register-link').hide ();
		$('#logout-link').prepend (' &mdash; ');
		$('#logout-link').prepend ($('<span>').text ($.cookie ('user_email')));
	}
	else {
		$('#logout-link').hide ();
	}
}

$(document).ready (update_login_logout_links);



/* shopping cart indicator */

function update_shopping_cart_summary () {
	var cart_items = $.cookie ('cart_items') || 0;
	var cart_value = $.cookie ('cart_value') || 0;

	$('#shopping-cart-summary').html (
		'<b>' + cart_items + '</b> '
		+ (1 == cart_items ? 'item' : 'items')
		+ ' in cart &mdash; <b>$'
		+ cart_value
		+ ' CAD</b>');
}

$(document).ready (update_shopping_cart_summary);



/* search box */

function search_submit (form) {
	window.location = '/search/' + escape (form.keywords.value);
	return false;
}



/* product page sections */

$(document).ready (
	function () {

		$('#sections .section').hide ()

		$('#section-navigation a').click (
			function () {
				$('#section-navigation a').removeClass ('active');
				$(this).addClass ('active');

				$('#sections .section').hide ()
				$(this.hash).show ();

				return false;
			});

		$('#product-description-link').click ();

	});



/* product page whitewash */

function whitewash_hide () {
	$('#add-to-cart').attr ('disabled', null);

	$('#whitewash').fadeOut (
		500,
		function () { $(this).remove () });

	$('#added-to-cart').fadeOut (
		500,
		function () { $(this).remove () });
}

function whitewash_show () {
	$('#add-to-cart').attr ('disabled', 'disabled');

	$('<div id="whitewash">')
		.hide ()
		.click (whitewash_hide)
		.appendTo ('#header')
		.fadeIn (500);
}



/* product page add-to-cart */

function add_to_cart_success (data, textStatus, jqXHR) {
	$.cookie ('cart_items', data.cart_items, { path: '/', domain: '.lark.me' });
	$.cookie ('cart_value', data.cart_value, { path: '/', domain: '.lark.me' });
	update_shopping_cart_summary ();

	$('<div id="added-to-cart">')
		.hide ()
		.append ($('<h1>').text ('Added to cart'))
		.append ($('<img>').attr ({ width: 100, height: 100, src: $('#product-full-image').attr ('src') }) )
		.append ($('<div>')
			.append ($('<h2>')
				.append ($('<span class="product-brand">').text (data.product_brand))
				.append (' ')
				.append ($('<span class="product-name">').text (data.product_name))))
		.appendTo ('#header')
		.fadeIn (500);
}

function add_to_cart (form) {
	whitewash_show ();
	$.post ('/checkout/product-add', $(form).serialize (), add_to_cart_success);
	return false;
}



/* product page product stock */

function check_stock () {
	$('#add-to-cart').attr ('disabled', 'disabled');
	$('#product-status-throbber').css ('display', 'inline');
	$('#product-status-text').text ('Checking product stock...');

	$.get (
		window.location.pathname + '/stock',
		$('#product-attributes-form').serialize (),
		function (data) {
			$('#product-status-throbber').css ('display', 'none');

			var text;

			if (data.error) {
				switch (data.error) {
					case 'No matching product':
						text = 'Sorry, this product is not available. Please try another size or colour.';
						break;

					default:
						text = data.error;
						break;
				}
			}
			else if (data.in_stock) {
				$('#add-to-cart').removeAttr ('disabled');
				text = '';
			}
			else {
				text = 'Sorry, this product is not in stock. Please try another size or colour.';
			}

			$('#product-status-text').text (text);
		});
}

$(document).ready (
	function () {

		if ($('#product-page').length) {
			$('#product-page input[type=radio]').change (check_stock);
			$('#product-page #product-status').append ($('<p><img id="product-status-throbber" src="/images/icons/throbber.gif" width="16" height="16" alt="Loading animation"><span id="product-status-text"></span></p>'));
			check_stock ();
		}

		function update_heights () {
			$('.submenu').each (function () {
				$(this).css ('max-height', '');
				if ($(this).height () > $(window).height () - 150)
					$(this).css ('max-height', $(window).height () - 150);
			});
		}

		$(window).resize (update_heights);
		update_heights ();
	});


