function applyEllipsis() {

   $$('.ellipsis').each( function(e) {

	    var w = e.getWidth(),
	    	t = e.innerHTML;

	    e.innerHTML = "<span>" + t + "</span>";
    	e = e.down();

	    while (t.length > 0 && e.getWidth() >= w) {
	      t = t.substr(0, t.length - 1);
    	  e.innerHTML = t + "&hellip;";
	    }

		if (e.innerHTML.indexOf(' x ') >= 0)
			e.innerHTML = e.innerHTML.substr(0, e.innerHTML.indexOf(' x ') + 3) + '<a href="' + e.up().previous().innerHTML + '">' + e.innerHTML.substr(e.innerHTML.indexOf(' x ') + 3) + '</a>';
	});

}

function resizeContent() {

	var delta = document.viewport.getHeight() - $$('body')[0].getHeight();

	if (delta > 0)
		$('content').setStyle({
			'height': $('content').getHeight() + delta + 'px'
		});
	
}

Event.observe(window, 'resize', resizeContent);

$ready( function() {

	applyEllipsis();

	resizeContent();

	$$('button').invoke('observe', 'click', function(ev) { ev.stop(); resizeContent(); })

	$$('.icon')
		.invoke('observe', 'mouseover', function() { $(this).addClassName('icon-over'); })
		.invoke('observe', 'mouseout', function() { $(this).removeClassName('icon-over'); });

 	$$('a.basket').invoke('observe', 'click', function(ev) {
		
		var type = this.rel.split('|')[0],
			id = this.rel.split('|')[1],
			pub = eval(this.rel.split('|')[2]) || false;

		ev.stop();

		if (pub || confirm('This item has not yet been published. Would you like to add it to your basket anyway?\nWe will deliver it to you as soon as it becomes available.')) {
		
			document.body.style.cursor = 'wait';
		
			new Ajax.Updater('basket-items', '/', {
				parameters: {
					'task': 'basket.add',
					'suppressLayout': 'true',
					'type': type,
					'id': id
				},
				onComplete: function(){
					document.body.style.cursor = 'default';
					applyEllipsis();
					alert('The item has been added to your basket.');
				}
			});
			
		}

	});
	
	if (Prototype.Browser.IE6)
		$$('input[type="file"]', 'input[type="password"]', 'input[type="text"]').each( function(el) {
			el.addClassName('inputText');
		});

	if (Prototype.Browser.IE6 || Prototype.Browser.IE7)
		$('ie-warning')
			.update('You are using an old version of Internet Explorer and therefore you may experience problems.<br />This site will look and work better if you visit <a href="http://www.microsoft.com/ie/">http://www.microsoft.com/ie</a> and upgrade your browser.')
			.show();

	if ($('captcha'))
		Recaptcha.create(
			'6LdM1cISAAAAAEZ5ND3CwRNtuFwtx80NWo9KErxu',
			'captcha',
			{
				theme: 'clean',
				callback: function() {
					$('recaptcha_response_field').setAttribute('required', true);
					$('recaptcha_response_field').setAttribute('message', 'Please enter the words you see in the space provided');
					Validation.setup();
				}
			}
		);

});

