String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

function addImgSwapLinks()
{
	if (div = document.getElementById('contactpics'))
	{
		var imgs = div.getElementsByTagName('img');
		var i = 0;
		while (img = imgs[i])
		{
			if (img.id != 'big')
			{
				img.style.cursor = 'pointer';
			}
			img.onclick = contactImgClick;
			i++;
			img.order = i;
		}
	}
}

var contactImgClick = function()
{
	if (this.id != 'big')
	{
		this.style.cursor = 'default';
		var big = document.getElementById('big');
		big.id = '';
		big.style.cursor = 'pointer';
		this.id = 'big';
	}
}

$(function() {

	$('table.columns').each(function() {
		var lis = $('li', this);
		var uls = $('ul', this);
		
		var nCols = uls.size();
		var nLis = lis.size();

		var totals = new Array(nLis);
		var total = 0;
		lis.each(function(i) { 
			total += $(this).outerHeight();
			totals[i] = total;
		});

		var div = total / nCols;
		
		var is = new Array(nCols);
		var mins = new Array();
		for (var i = 0; i < nCols-1; i++) {
			is[i] = i;
		}
		is[nCols-1] = nLis-1;
		
		var min = total;

		function recurse (col) {
			if (col == 0) is[col] = 0;
			else is[col] = is[col-1] + 1;
			for (; is[col] < nLis - (nCols - 1 - (col+1)); is[col]++) {
				if (col+2 < nCols) {
					recurse(col+1);
				} else {
					var diff = 0;
					for (var i = 0; i < nCols; i++) {
						if (i == 0) diff += Math.abs(totals[is[0]] - div);
						else diff += Math.abs(totals[is[i]] - totals[is[i-1]] - div);
					}
					if (diff <= min) {
						min = diff;
						mins = is.slice();
					}
				}
			}
		}

		recurse(0);

		var i = 0;
		for (var c = 0; c < nCols; c++) {
			var slice = lis.slice(i, mins[c]+1);
			slice.prependTo(uls.eq(c));
			i = mins[c]+1;
		}
		
	});

	$('div.submitbutton').append('<img class="loading" src="/images/load.gif" alt="Loading...">').find('button').click(function() {
		$('img.loading', this.parentNode).show();
	});

	addImgSwapLinks();
	$("a.aboutimglink").click(function(event) {
		event.preventDefault();
		window.open(this.href, "boffinsphotos", "scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes").focus();
	});

	$('#mainsearch input.text').focus(function() {
		if (this.value == 'title, keyword, author or ISBN') {
			$(this).css('color', '#000').val('');
		}
	}).blur(function() {
		if (this.value == '') {
			$(this).css('color', '#aaa').val('title, keyword, author or ISBN');
		}
	}).blur();
	
	function backToColour() {
		$('img.cclogo').attr('src', function() {
			return $(this).data('colour_img');
		});
	}
	$('img.cclogo').each(function() {
		$(this).data('bw_img', this.src.substring(0, this.src.indexOf('_colour.jpg')) + '_bw.jpg');
		$(this).data('colour_img', this.src);
		
	});

	$('#checkout input[name=CCNO]').keyup(function() {
		if (this.value.length >= 4) {
			var cc = checkCC(this.value);
			if (cc != false) {
				var that = $('#' + (cc + '_logo'));
				that.attr('src', '/images/' + cc + '_colour.jpg');
				that.siblings().each(function() {
					this.src = $(this).data('bw_img');
				});
			} else {
				backToColour();
			}
		} else {
			backToColour();
		}
	});

	$('#shippingselect').change(function() {
		$('#shippingupdate').click();
	});
	
	$('#hmas-perth-vertical-banner a').hover(function() {
		var src = this.firstChild.src;
		this.firstChild.src = src.substring(0, src.length - 4) + '_hover' + '.jpg';
	}, function() {
		var src = this.firstChild.src;
		this.firstChild.src = src.substring(0, src.length - 10) + '.jpg';
	});
});

function checkCC(number) {
	       if (number.match(/^3(4|7)/)) {
		return 'amex';
	} else if (number.match(/^3(6|0[0-5])/)) {
		return 'diners';
	} else if (number.match(/^(3|2131|1800)/)) {
		return 'jcb';
	} else if (number.match(/^5[1-5]/)) {
		return 'mc';
	} else if (number.match(/^4/)) {
		return 'visa';
	} else {
		return false;
	}
}
