jQuery.noConflict();
(function($) {
	$(function()
	{
		var galleryIndex=0;
	
		$('.projectscarousel').each(function() {
			galleryInit($(this), galleryIndex);
			galleryIndex++;
		});
	
		function galleryInit(gallery, galleryIndex)
		{
			gallery.wrap('<div class="projectscarousel-inner1"></div>');
			gallery=gallery.parent();
			gallery.wrap('<div class="projectscarousel js" id="projectscarousel_'+galleryIndex+'"></div>');
			gallery=gallery.parent();
			gallery.append('<div class="clearFloats"></div>');

			$('li', gallery).each(function() {
				$(this).replaceWith('<div class="item">'+$(this).html()+'</div>');
			});
			
			$('.item:last', gallery).addClass('last');
			
			$('ol', gallery).each(function() {
				$(this).replaceWith('<div class="projectscarousel-inner2" style="width:'
					+($(this).getItemWidth() * $('.item', $(this)).length)+'px">'+$(this).html()+'</div>');
			});
			
			$('a', gallery).each(function() {
				$(this).append('<span class="arrow">&nbsp;</span>').addClass('projectsCarouselArrowLink');
				
				//set up links inside the h2 and around the image for the item
				var href=$(this).attr('href');
				var parentItem=$(this).closest('.item');
				$('h2', parentItem).replaceWith('<h2><a href="'+href+'">'+$('h2', parentItem).text()+'</a></h2>');
				$('img', parentItem).wrap('<a href="'+href+'"></a>');
			});
			
			$('.projectscarousel-inner1', gallery).bind('jsp-arrow-change', 
					function(event, isAtTop, isAtBottom, isAtLeft, isAtRight)
					{
						if (isAtLeft) $('.jspArrowLeft', gallery).addClass('jspArrowLeftDisabled');
						else $('.jspArrowLeft', gallery).removeClass('jspArrowLeftDisabled');

						if (isAtRight) $('.jspArrowRight', gallery).addClass('jspArrowRightDisabled');
						else $('.jspArrowRight', gallery).removeClass('jspArrowRightDisabled');
					}
				).
				jScrollPane({showArrows: true});
			$('.jspArrowLeft', gallery).addClass('jspArrowLeftDisabled');
			
			
			//stupid IE6 h2 style clash, so change these to h3s
			if($.browser.msie && $.browser.version=='6.0') {
				$('h2', gallery).each(function() {
					$(this).replaceWith('<h3>'+$(this).html()+'</h3>');
				});
			}
		}
	});
	
	$.fn.getItemWidth=function()
	{
		var firstItem=$('.item:first', $(this));
		var width=firstItem.css('width').toInt() +
			firstItem.css('padding-right').toInt() +
			firstItem.css('margin-right').toInt();
		
		return width;
	}
})(jQuery);

String.prototype.toInt = function()
{
    return parseInt(this.replace(/px/,''), 10);
}
