
/* Functions relating to the inntopia embedded booking engine */
booking_width = 210;
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() {
	// Remove any other iframes.
	$("div.booking h3").each(function() {
		if(this.firstChild.nodeName != 'A') {
			// Find the iframe and remove it.
			var iframe = this.nextSibling;
			while(iframe != null && iframe.nodeName != "IFRAME")
				iframe = iframe.nextSibling;
			if(iframe == null)
				return;
			
			// Grab the class ID
			var query = querystring_from_search(iframe.getAttribute('src'));
			var classid = query['productcategoryid'];
			var newUrl = location.pathname + "?book=" + classid;
			
			iframe.parentNode.removeChild(iframe);
			
			// Make this a link.
			var text = this.firstChild.nodeValue;
			this.removeChild(this.firstChild);
			var a = this.appendChild(document.createElement('a'));
			a.setAttribute('href', newUrl);
			a.appendChild(document.createTextNode(text));
			$(a).bind("click", inntopia_click);
		}
	});
	
	// Get the catid from the url.
	var text = this.firstChild.nodeValue;
	var query = querystring_from_url(this.getAttribute('href'));
	var catid = query["book"];
	
	// Remove the link
	var h3 = this.parentNode;
	while(h3 != null && h3.nodeName != "H3")
		h3 = h3.parentNode;
	while(h3.firstChild != null)
		h3.removeChild(h3.firstChild);
	h3.appendChild(document.createTextNode(text));
	
	// Add the Iframe.
	var iframeUrl = booking_iframe.replace('%category%', catid);
	var iframe = document.createElement('iframe');
	iframe.setAttribute('src', iframeUrl);
	iframe.setAttribute('width', booking_width);
	iframe.setAttribute('height', booking_height);
	iframe.setAttribute('name', booking_name);
	iframe.setAttribute('id', booking_name);
	iframe.setAttribute('frameborder', booking_frameborder);
	iframe.setAttribute('scrolling', booking_scrolling);
	
	insertAfter(iframe, h3);
	
	return false;
}

function insertAfter(node, afterNode) {
	if(afterNode.nextSibling != null)
		afterNode.parentNode.insertBefore(node, afterNode.nextSibling);
	else
		afterNode.parentNode.appendChild(node);
}

$(document).ready(function() {
	$("div.booking a").bind("click", inntopia_click);
});
