function generateModalDialog(href) {
	var modalclass = href;
	var x = href.lastIndexOf('/');
	if (x != -1) modalclass = href.substring(x + 1);
	x = modalclass.lastIndexOf('.');
	if (x != -1) modalclass = modalclass.substring(0, x);
	if (href.indexOf('#') == 0) modalclass = modalclass.substring(1, href.length);
	$('#modal,#backdrop').remove();
	$('.family-only-product').append('<div id="backdrop"></div><div id="modal" class="' + modalclass + '"><div class="top"></div><div class="middle"></div><div class="bottom"><img class="modal-close" src="http://test.hertzfurniture.com/images/buttons/close.png" /></div></div>');
	$('#modal .modal-close').click(function() {
		closeModalDialog();
	});
	$('#backdrop').click(function() {
		closeModalDialog();
	}).fadeIn(125);
	if ((href.indexOf('/') == 0) || (href.indexOf(location.hostname) != -1)) {
		$('#modal .middle').load(href, function() {
			var marginleft = (($('#modal').width() / 2) * -1);
			var margintop = (($('#modal').height() / 2) * -1);
			$('#modal').css({'margin-left': marginleft,'margin-top': margintop}).fadeIn(250);		
		});
	} else if (href.indexOf('#') == 0) {
		$('#modal .middle').html($(href).html());
		var marginleft = (($('#modal').width() / 2) * -1);
		var margintop = (($('#modal').height() / 2) * -1);
		$('#modal').css({'margin-left': marginleft,'margin-top': margintop}).fadeIn(250);		
	} else {
		location.href = href;
	}
}

function closeModalDialog() {
	$('#modal').fadeOut(250, function() {
		$(this).remove();
	});
	$('#backdrop').fadeOut(125, function() {
		$(this).remove();
	});
}

$(function() {
	if ($(".family-only-product").length == 0)  return;
	if ($("p.cto-number").length != 0) {
		var cp_number;
		if (typeof GetPhoneText == 'undefined') cp_number = '888-793-4999';
		else cp_number = GetPhoneText("NNN-NNN-NNNN");
		$("p.cto-number").html(cp_number);
	}
	//Determine the size of the gallery
	var gallerywidth = $('#photo-gallery li').length * 57;
	$('#photo-gallery ul').width(gallerywidth);
	
	//Set the first image in the gallery to active
	$('#photo-gallery li:first-child a').addClass('current');
	
	//Tell the gallery what to do on click
	$('#photo-gallery li a').click(function() {
		var imageurl = $(this).attr('href');
		var imagetitle = $(this).find('img').attr('title');
		var mediumimage = imageurl.replace(/\/i\/340x340\//g, '/i/340x340/');
		var largeimage = imageurl.replace(/\/i\/340x340\//g, '/i/1024x768/');
		$('#product-photo > img').attr('src',mediumimage);
		$('#photo-caption').html(imagetitle);
		$('#product-photo').attr('href',largeimage);
		$('#photo-gallery li a').removeClass('current');
		$(this).addClass('current');
		return false;
	});
	
	// Tell the gallery how to scroll when there are a lot of images
	$('#photo-gallery').mousemove(function(e) {
		var outerwidth = $(this).width();
		var innerwidth = $(this).find('ul').width();
		if (innerwidth > outerwidth) {
			var scrollpos = ((e.pageX - this.offsetLeft) / outerwidth) * (innerwidth - outerwidth);
			var currentpos = $(this).scrollLeft();
			if (scrollpos > (currentpos + 8) || scrollpos < (currentpos - 8)) {
				$(this).stop().animate({scrollLeft:scrollpos},50);
			} else {
				$(this).scrollLeft(scrollpos);						
			}
		}
	});
	
	//Get the floater to stick to the top of the screen when you scroll down far enough
	var floaterlimit = $('#purchase').offset().top - 5;
	$(window).scroll(function() {
		var windowscroll = $(window).scrollTop();
		if (windowscroll > floaterlimit) {
			$('#purchase').addClass('sticky');
		} else {
			$('#purchase').removeClass('sticky');
		}
	});
	
	//Swatch tooltips and selection markers
	$('#swatches dd').click(function() {
		$(this).siblings().removeClass('current');
		$(this).addClass('current');
	}).hover(
		function() {
			var swatchimage = $(this).find('img').attr('src');
			var largeswatchimage = swatchimage.replace(/\/i\/20x10\//g, '/i/150x75/')
			var swatchname = $(this).find('img').attr('alt');
			$('#swatch-tooltip').remove();
			$('.family-only-product').append('<div id="swatch-tooltip"><img id="swatch-tooltip-image" /><p id="swatch-tooltip-name"></p></div>');
			$('#swatch-tooltip-image').attr('src',largeswatchimage).attr('alt',swatchname);
			$('#swatch-tooltip-name').text(swatchname);
			$('#swatch-tooltip').fadeIn(250);
		}, function () {
			$('#swatch-tooltip').remove();
		}
	).mousemove(function(e) {
		$('#swatch-tooltip').css({'top' : (e.pageY - 10), 'left' : (e.pageX - 194)});
	});

	//Have every link with rel="modal" generate a modal dialog instead of going to the page
	$('a[rel~="modal"]').click(function() {
		var href = $(this).attr('href');
		generateModalDialog(href);
		return false;
	});

	//Live Help text box stretch
	makeElementStretchy($('#questions'),783);
	$(window).resize(function() {
		makeElementStretchy($('#questions'),783);
	});

});
