$(document).ready(function() {
	// fancybox
	$("a.fancy_gallery").fancybox({
		'zoomOpacity': true,
		'overlayShow': false,
		'zoomSpeedIn': 500,
		'zoomSpeedOut': 500
	});

	// image gallery
	for (i=1; i <= 4; i++) {
		if ($('#ithumb'+i+' a img').length > 0) {
			$('#ithumb'+i).css('height', parseInt($('#ithumb'+i+' a img').height())+'px');
		}
		else {
			$('#ithumb'+i).css('height', 'auto');
		}
	}

	if ($('#priceContainer').length > 0 && typeof(priceData) != 'undefined') {
		var api = new jGCharts.Api();
		jQuery('<img>')
			.attr('src', api.make(priceData))
			.appendTo("#priceContainer");
	}

	// bubble tooltip init
	$('.bubbleInfo').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		var trigger = $('.trigger', this);
		var info = $('.bubblepopup', this).css('opacity', 0);

		$([trigger.get(0), info.get(0)]).hoverIntent({
			sensitivity: 2,
			interval: time,
			timeout: time,
			over: function() {
				if (info.is(':animated, :visible')) {
					// don't trigger the animation again
					return;
				}
				else {
					// reset position of info box
					var newtop = parseInt($(this).parent().children('.bubblepopup').outerHeight()) - 10;

					if (newtop > 0) {
						newtop *= -1;
					}

					ou1 = parseInt($(this).parent().children('.trigger').outerWidth());
					ou2 = parseInt($(this).parent().children('.bubblepopup').outerWidth());

					var newleft = null;

					if (ou1 > ou2) {
						newleft = parseInt((ou1 - ou2) / 2);
					}
					else {
						newleft = parseInt((ou2 - ou1) / 2) * -1;
					}

					leftadjust = parseInt($(this).parent().children('.trigger').css('left'));

					if (!isNaN(leftadjust)) {
						newleft += leftadjust;
					}

					info.css({
						top: newtop,
						left: newleft,
						display: 'block'
					}).animate({
						top: '-=' + distance + 'px',
						opacity: 1
					}, time, 'swing', function() { });
				}
			},
			out: function() {
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					info.css('display', 'none');
				});
			}
		});
	});

});

function goGal(img, thumb) {
	$('#picholder a img').attr({'src': 'watermark.php?mark=' + img + ''});
	//$('#picholder a img').attr({'src': img});
	$('#picholder a').attr({'href': img});

	buf = new Image();
	buf.src = img;

	sw = buf.width;
	sh = buf.height;



	if (sw >= 200 && sw >= sh) {
		r = sw / 200;
		sh = sh / r;

		$('#picholder a img').attr({'width': 200, 'height': sh});
	}

	else if (sh >= 200 && sh >= sw) {
		r = sh / 200;
		sw = sw / r;

		$('#picholder a img').attr({'width': sw, 'height': 200});
	}

	else {
		$('#picholder a img').attr({'width': sw, 'height': sh});
	}
}
