(function($) { 
	$(document).ready(function() {
		$('.OCVFeaturesComponent').each(function() {
			var $component = $(this);
			
			$component.find('.features li').hide().each(function() {
				$(this).attr('height', $(this).height() + 10);
				$(this).attr('width', $(this).width() + 10);
				$(this).attr('top', $(this).css('top'));
				$(this).attr('left', $(this).css('left'));
				
				$(this).css({
					width: 0,
					height: 0,
					padding: 0,
					top: $component.find('.markers li.' + $(this).attr('class')).position().top + 17 + 'px',
					left: $component.find('.markers li.' + $(this).attr('class')).position().left + 17 + 'px'
				});
			});
			
			$component.find('.markers li').click(function() {
				var $feature = $component.find('.features li.' + $(this).attr('class'));

				if ($feature.hasClass('animating')) {
					$feature.removeClass('animating').stop().show().animate({
						padding: '0px',
						height: '0px',
						width: '0px',
						top: $component.find('.markers li.' + $(this).attr('class')).position().top + 17 + 'px',
						left: $component.find('.markers li.' + $(this).attr('class')).position().left + 17 + 'px'
					}, 'fast');
				}
				
				else {
					$feature.addClass('animating').stop().animate({
						padding: '5px',
						height: $feature.attr('height') + 'px',
						width: $feature.attr('width') + 'px',
						top: $feature.attr('top'),
						left: $feature.attr('left')
					}, 'fast');
				}
			})
		});
	
	
		// OCV tabs
		if ($('.horizontal-tabs').hasClass('OCVFeaturesComponent')) {
			
			$('.horizontal-tabs, ul.tabs').wrapAll('<div class="OCVFeaturesSet"></div>');
			$('.OCVFeaturesSet').width($('.OCVFeaturesComponent .background-image').width());
			
			// initiate tabs
			$('.OCVFeaturesSet').tabs();
			
			Cufon.replace('.OCVFeaturesSet ul.tabs a');

			$('.OCVFeaturesSet ul.tabs li').width(($('.OCVFeaturesSet').width() / $('.OCVFeaturesSet ul.tabs li').length - 15));
			
			// gallery buttons
			$('.OCVFeaturesSet .GalleryComponent').append($('<img src="roadcraft/images/prev.png" alt="" class="prev" /><img src="roadcraft/images/next.png" alt="" class="next" />'));
			
			var images = $('.OCVFeaturesSet .GalleryComponent a');
			var index = 0;
			$('.OCVFeaturesSet .GalleryComponent .next').click(function() {
				index = (images.get(index+1)) ? index + 1 : 0;
				$(this).parent('.GalleryComponent').find('.placeholderimage img').attr('src', images.get(index).href);
			});
			$('.OCVFeaturesSet .GalleryComponent .prev').click(function() {
				index = (images.get(index-1)) ? index - 1 : images.length - 1;
				$(this).parent('.GalleryComponent').find('.placeholderimage img').attr('src', images.get(index).href);
			});
			
			images.each(function() {
				$.preloadImage($(this).attr('href'));
			});
		}
	});
})(jQuery);

jQuery.preloadImage = function() {
	return jQuery("<img>").attr("src", arguments[0]);
} 