/**
 * @copyright ©2011 Spinitron All rights reserved.
 */
'';
/*global
	$
*/
$(document).ready(function() {
	'use strict';
	var searching 
		= '<h2 class="merch-head">Searching...<img src="/ajax-loader.gif"></h2>',

	setup = function () {
		$('a.merch.buyit:not([target])').click(function(e) {
			e.preventDefault();
			var params 
					= $(this)
						.attr('href')
						.replace(/^.+\.php\?/, '')
						.replace(/#.+$/, ''),
				label = params.match(/merch=label/);
			$('#merchpanel').show();
			if (!label) {
				$('#merchpanel #slot1').load(
					'/click.php?' + params + '&vendor=itms', 
					function () {
						$.scrollTo('#slot1');
					}
				);
				$('#merchpanel #slot1').html(searching);
			}
			$('#merchpanel #slot2').load(
				'/click.php?' + params + '&vendor=amazon', 
				function () {
					if (label) $.scrollTo('#slot2');
				}
			);
			$('#merchpanel #slot2').html(searching);
		});

		$('a.merch.wide:not([target])').click(function(e) {
			e.preventDefault();
			var params 
					= $(this)
						.attr('href')
						.replace(/^.+\.php\?/, '')
						.replace(/#.+$/, ''),
				slot = $(this).parent().parent();
			slot.load(
				'/click.php?' + params, 
				function () {
					$.scrollTo(slot);
				}
			);
			slot.html(searching);
		});
	},

	reload = function () {
		$('#merchpanel a.merch:not([target])').click(function(e) {
			e.preventDefault();
			var params 
				= $(this)
					.attr('href')
					.replace(/^.+\.php\?/, '')
					.replace(/#.+$/, ''),
				slot = $(this).parent().parent();
			slot.load(
				'/click.php?' + params, 
				function () {
					$.scrollTo(slot);
				}
			);
			slot.find('h2').replaceWith(searching);
		});

	};

	$(window).ajaxStop(function() {
		reload();
	});

	setup();

});


