
/* Functions relating to the inntopia embedded booking engine */
booking_width = 195;
booking_height = 210;
booking_name = 'sswindow';
booking_frameborder = 0;
booking_scrolling = "no";

booking_iframe = 'http://vacations.visitrapidcity.inntopia.travel/aspnet/2.0/selector07.aspx?salesid=650555&productcategoryid=%category%&hidetabs=1';

function inntopia_click() {
	var e = function(nodeName) { return $(document.createElement(nodeName)); };
	var t = $(this);
	
	// Remove any other iframes.
	$("div.booking iframe").each(function() {
		var iframe = $(this);
		var query = querystring_from_search(iframe.attr('src'));
		var classid = query['productcategoryid'];
		var newUrl = location.pathname + "?book=" + classid;
		
		var h3 = iframe.prev('h3');
		var text = h3.text();
		
		h3.empty();
		h3.append(e('a')
			.bind("click", inntopia_click)
			.attr('href', newUrl)
			.text(text)
		);
		
		iframe.remove();
	});
	
	// Get the catid from the url.
	var text = t.text();
	var query = querystring_from_url(t.attr('href'));
	var catid = query["book"];
	
	// Remove the link
	var h3 = $(this).closest('h3');
	h3.empty();
	h3.append(
		e('span').text(text)
	);
	
	// Add the Iframe.
	var iframeUrl = booking_iframe.replace('%category%', catid);
	var iframe = e('iframe')
		.attr('src', iframeUrl)
		.attr('width', booking_width)
		.attr('height', booking_height)
		.attr('name', booking_name)
		.attr('id', booking_name)
		.attr('frameborder', booking_frameborder)
		.attr('scrolling', booking_scrolling);
	
	iframe.insertAfter(h3);
	
	return false;
}

$(document).ready(function() {
	$("div.booking a").bind("click", inntopia_click);
});
