// JavaScript Document
$(document).ready(function() {

	$('div.inv-links a.print').click(function(e) {
		e.preventDefault();
		printSpecial('inv-content-body');
	});
	
	$('a#prettyphoto').prettyPhoto();
	//$('.gallery-icons .icon-video a').prettyPhoto();

	//image click, load image as main image
	$('.inv-thumb a').click(function(event) {
		event.preventDefault();
		
		$(this).toggleClass('active');
		$('.inv-thumb a').not(this).removeClass('active');
		
		// get the image source and load it in the main image
		src = $(this).attr('href');
		href = src.replace(/&w=(\d+)/g, '');
		$('.inv-large img').attr('src', src);
		$('.inv-large a').attr('href', href);
	});
	
	// load the image gallery when large image is clicked
	$('.inv-large a, #inv-content-body [class^=icon-pic]').click(function (event) {
		event.preventDefault();
		
		images = new Array();
		curIndex = '';
		
		curSrc = $(this).attr('href');
		if(curSrc) {
			curSrc = curSrc.replace(/http:\/\/inventory.blusolutions.com\/thumbs\/(\d+)\//g, '');
			curSrc = 'http://inventory.blusolutions.com/' + curSrc;
		}
		
		// load the large image gallery
		$('.inv-thumb a').each(function(index) {
			//build array of images
			src = $(this).attr('href');
			src = src.replace(/http:\/\/inventory.blusolutions.com\/thumbs\/(\d+)\//g, '');
			src = 'http://inventory.blusolutions.com/' + src;
			
			if (src == curSrc) curIndex = index;
			images[index] = src;
		});
		
		$.prettyPhoto.open(images);
		if (curIndex != null) {
			for(var x = 0; x < curIndex; x++) {
				$.prettyPhoto.changePage();
			}
		}
		
	});
	
	// load the tooltip
	$('body').append('<div id="tooltip" style="display: none; position: absolute; background-color:#000; min-height: 20px; min-width: 50px;"><div style="padding: 5px; color:#fff;"></div></div>');
	
	// tool tips			
	
	$.fn.tooltip = function(options) {
		
		var defaults = {
			offset_top: 0,
			offset_left: 0,
			tooltip: '#tooltip',
			tooltipc: 'div'
		};
		
		var options = $.extend(defaults, options); 
		
		//alert('tooltip called');
		
		this.each(function() {
			title = $(this).attr('title');
			$(this).data('title', title);
			$(this).removeAttr('title');
		});

		
		return this.hover(function() {
			obj = $(this);
			
			//alert('hover called');
				
			obj.mousemove(function(e) {
				var offset_top, offset_left, top, left;
				
				$(options.tooltip + ' ' + options.tooltipc).html($(this).data('title'));
				
				pos = $(this).offset();
				
				offset_top = -($(options.tooltip).height() + 5);
				offset_left = ($(this).width() / 2) - ($('#tooltip').width() / 2);
				
				top = pos.top + offset_top;
				left = pos.left + offset_left;					
				
				$(options.tooltip).css('left', left).css('top', top).fadeIn();
			});
			
		}, function(e) {
			// hover out
			$(options.tooltip).stop(false,true).hide();
			$(options.tooltip + ' ' + options.tooltipc).html('');
			$(this).unbind('mousemove');
		});	
		
	};
				
	$('#inventory-wrap a[title]').tooltip();
	
	$('.gallery-icons li[title]').tooltip();
	
	$("a[rel^='blugallery']").click(function(e) {
		e.preventDefault();
		href = $(this).attr('href');
		$.prettyPhoto.open(href);
	});

});