var Tabs = new Class({
	Implements: [Options, Events],
	options: {
		openTab: '1',
		spanish: false
	},
	initialize: function(tabs, content, options){
		this.setOptions(options);
		this.tabs = tabs;		
		this.content = content;	
		
		this.show(this.options.openTab);		
		this.attach();
	},	
	show: function(tab){
		
		// Add loading indicator
		this.content.empty();
		var loading = new Element('div');
		loading.setProperty('class', 'ajax-loader');
		loading.injectInside(this.content);		
		
		// If VRN spanish, use the url for translated listing jsp
		if(this.options.spanish) {
			var url = '/vegas/es/special-offers/vegas-right-now/vegas-now-listing.jsp?campaign=vegasNow&tab=' + tab + '&date=' + new Date().getTime();
		} else {
			var url = '/vegas/special-offers/vegas-right-now/vegas-now-listing.jsp?campaign=vegasNow&tab=' + tab + '&date=' + new Date().getTime();
		}
		
		new Request.HTML({
			update: this.content,
			onComplete: function(){
				sIFR.replace(ssbold, {
					selector: 'h3.vnitemdate',   
					ratios: [[8,1.41,9,1.33,12,1.35,15,1.3,19,1.28,27,1.27,28,1.25,32,1.26,33,1.24,34,1.25,47,1.24,52,1.23,53,1.24,71,1.23,72,1.22,73,1.23,76,1.22,77,1.23,81,1.22,82,1.23,1.22]],  
					wmode: 'opaque',
					css: [
					  '.sIFR-root { font-size: 14px; background-color: #000000; color: #ffffff; text-transform: uppercase; font-weight: bold; }'
					],
					preventWrap: false,
					fitExactly: true,
					tuneHeight: -6,
					offsetTop: 0
				});
			}
		}).get(url);
		
		// Get tab id
		var tabcurrent = 'vntab' + tab;	

		// Then add/remove highlight
		this.updateStyle($(tabcurrent));

	},
	attach: function(){
		var self = this;
		
		this.tabs.each(function(item, index){			
			item.addEvent('click', function(){	
				// Show content
				self.show(index+1);	
			});			
		});			
	},
	updateStyle: function(currenttab) {	
		// Remove any highlighted tabs
		this.tabs.removeClass('selected');
		
		// Add highlight to the current tab
		currenttab.addClass('selected');
	}
});

/*
 * This is a patch for Mootools Request.HTML
 *
 * If you use Request.HTML to load some data containing any HTML special chars
 * like &nbsp; or &laquo; it won't work with Webkit based browsers.
 */
Request.HTML.implement({
	processHTML: function(text){
		var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
		text = (match) ? match[1] : text;
		
		var container = new Element('div');
		
		return $try(function(){
			var root = '<root>' + text + '</root>', doc;
			if(Browser.Engine.trident){
				doc = new ActiveXObject('Microsoft.XMLDOM');
				doc.async = false;
				doc.loadXML(root);
			}else{
				doc = new DOMParser().parseFromString(root, 'text/html');
			}
			root = doc.getElementsByTagName('root')[0];
			for(var i = 0, k = root.childNodes.length; i < k; i++){
				var child = Element.clone(root.childNodes[i], true, true);
				if(child) container.grab(child);
			}
			return container;
		}) || container.set('html', text);
	}
});