$(function() {
	$('.scrollable').jScrollPane();
	$('.timeago').timeago();
	$('.fancybox').fancybox({ overlayColor: '#000', overlayOpacity: 0.6, frameHeight: 300 });
	// increase the z-indexes of fancybox, as the css for the site has too large values otherwise
	//$('*[id^=fancy]').each(function() {
	//    $(this).css('z-index', 2000 + $(this).css('z-index'));
	//});
	$('#subscribe-form input[type=text]').defaultValue({ value: 'enter e-mail' });
	
});

function disabledcombo(id, disabled) {
	document.getElementById(id).disabled = disabled;
	//custom function
	if(document.getElementById(id).refresh!=undefined)
			document.getElementById(id).refresh();
}

$(function() {
	$('form.product select').msDropDown();
	$('form.product select').unbind('change');
	
	var updateAttributes = function() {
		$(this).closest('form').ajaxSubmit({
			success: function() {
				setTimeout("setupTextField()", 100);
				$('form.product select, form.product input, form.product.textarea').attr('disabled', false);
				$('form.product select').each(function() { disabledcombo($(this).attr('id'), false); });
				$('form.product select').msDropDown();
			}
		});
		$('form.product select, form.product input, form.product.textarea').attr('disabled', true);
		$('form.product select').each(function() { disabledcombo($(this).attr('id'), true); });
		$('form.product .img-select').addClass('disabled');
	}
		
	$('form.product select:not(.align)').livequery('change', updateAttributes);
	$('form.product input[type=radio]:not(.font, .ink)').livequery('change', updateAttributes);
	
	//var mainImg = $('.large-img img').attr('src');
	$('.img-select img').livequery(function() {
		$(this).hover(
			function() {
				var thisImg = $(this).attr('src');
				var offset  = $(this).offset();
				var img = $('<img class="hoverImg" style="position:absolute;z-index:100;border:1px solid #000" />')
					.attr('src', thisImg.replace(/rp:\d+:\d+/, 'rc:175:175'))
					.css('top', offset.top)
					.css('left', offset.left + $(this).width() + 5);
				//$(this).closest('label').find('input').show();
				$('body').append(img);
			},
			function() {
				//$('.large-img img').attr('src', mainImg);
				$('.hoverImg').remove();
			}
		);
		$(this).click(function() {
			$(this).closest('label').find('input[type=radio]').show().click().change();
		});
	});
});

/* 
 * Tabbify the stationary product page and make 
 * any other links to the tabs trigger the switch.
 * 
 */
$(function() {
	$tabs = $('.add-nav').tabs();
	$tabs.bind("tabsshow", function(event, ui) { 
	    window.location.hash = ui.tab.hash;
	})
	$('.add-nav a').livequery(function() {
	    $('a[href="' + $(this).attr('href') + '"]').click(function() {
	        $tabs.tabs('select', $(this).attr('href'));
	        return false;
	    });
	});
});


var setupTextField;

$(function() {
	
	var updateTextField = function() {
		var product = $(this).closest('.stationary-text');
		var input   = product.find('.field');
		//input.val('');
		input.val("[align: " + product.find('.align').val() + "]\n");
		
		product.find('.row').each(function() {
			input.val(input.val()
				+ '['
				+ 'font: ' + $(this).find('input.font:checked').val() + ', '
				+ 'ink: '  + $(this).find('input.ink:checked').val()
				+ '] '
				+ $(this).find('input[type=text]').val()
				+ "\n"
			);
		});
	};
	$('.stationary-text .row input').livequery('change', updateTextField);
	$('.stationary-text input.field').each(function() {
		if (!$(this).val()) {
			updateTextField();
		}
	});
	
	$('.align').livequery('change', function() {
		$(this).closest('.stationary-text').find('.row input[type=text]').css('text-align', $(this).val());
		$(this).closest('.stationary-text').each(updateTextField);
	});
	
	$('input.font').livequery('click', function() {
		if ($(this).val() == 2) {
			$(this).closest('.row').find('input[type=text]').css('font-size', 20);
		} else {
			$(this).closest('.row').find('input[type=text]').css('font-size', 12);
		}
	//	$(this).closest('.row').find('input[type=text]').css('font-size', $(this).val()*12);
	//	$(this).closest('.row').find('input[type=text]').css('font-family', $(this).val() == 2 ? 'Georgia, "Times New Roman", Times, serif' : 'Helvetica');
	});
	//
	$('input.ink').livequery('click', function() {
		var row = $(this).closest('.row');
		
		// remove all current colour classes
		$('.simpleattribute-inkcolour option, .simpleattribute-secondinkcolour option').each(function() {
			row.find('input[type=text]').removeClass('ink_' + $(this).attr('value').replace(/[^a-zA-Z0-9]*/g,'').toLowerCase());
		});
		
		// find the colour classes for the selected inks
		var ink = [];
		ink[1] = 'ink_' + $('.simpleattribute-inkcolour').val().replace(/[^a-zA-Z0-9]*/g, '').toLowerCase();
		if ($('.simpleattribute-secondinkcolour').length) ink[2] = 'ink_' + $('.simpleattribute-secondinkcolour').val().replace(/[^a-zA-Z0-9]*/g, '').toLowerCase();
		$(this).closest('.row').find('input[type=text]').addClass(ink[$(this).val()]);
		
	});
	
	setupTextField = function() {
		if ($('.simpleattribute-secondinkcolour').length) {
			$('.ink2').attr('disabled', false);
		} else {
			$('.ink2').attr('disabled', true);
		}
		
		$('.stationary-text').each(function() {
			var lines = $(this).find('.field').val().split(/\n/);
			
			var data = lines[0].match(/^\[align: (left|center|right)\]$/);
			if (data) {
				$(this).find('.align').val(data[1]).change();
				$('form.product select').msDropDown();
			}
			for(var i=1;i<lines.length;i++) {
				if (lines[i]) {
					var data = lines[i].match(/^\[font: ([12]), ink: ([12])\] (.*)$/);
					$(this).find('.text_'+i).val(data[3]);
					$(this).find('.text_' + i + '_font_'  + data[1]).click();
					$(this).find('.text_' + i + '_ink_'   + data[2]).click();
				}
			}
			//$(this).find('input[type=radio]:checked').click();
			
		});
		
	};
	setupTextField();
	
});



$(function() {
	
	$('a.thumbnail').click(function(e) {
		e.preventDefault();
		$('.large-img img').attr('src', $(this).attr('href'));
		$('.large-img a').attr('href', $(this).attr('href').replace('rp:249:254', 'm:500'));
		$('p.enlarge-link a').attr('href', $(this).attr('href').replace('rp:249:254', 'm:500'));
	});
	
	// flickr
	$('.flickr-gallery .btn-next-black').click(function(e) {
		e.preventDefault();
		var ul = $('.flickr-gallery ul');
		var li = ul.find('li:first');
		li.clone().appendTo(ul);
		li.remove();
	});
	$('.flickr-gallery .btn-back-black').click(function(e) {
		e.preventDefault();
		var ul = $('.flickr-gallery ul');
		var li = ul.find('li:last');
		li.clone().prependTo(ul);
		li.remove();
	});
	
	// get around target not being valid xhtml
	$('.external').attr('target', '_blank');
	
});
