/*@cc_on 
	@if (@_win32)
		try {document.execCommand('BackgroundImageCache', false, true);}catch(e){}
	@end
@*/
var LSR = {
	HomepageCarousel : function(){
		$('#hero .slides').cycle({ 
		    fx:         'fade', 
		    timeout:     5000, 
		    pager:      '.slide-tabs', 
		    pagerEvent: 'click', 
			pauseOnPagerHover: true,
			cleartype:  false
		});
	},
	ExpandClick : function(){
		var box = $('.promo-box, #more-links li, #hero .promo');
		box.each(function(index, elem) {
			$(elem).css('cursor', 'pointer').bind({
				click : function(e) {
					var link = $('a', this)[0];
					window.location.href = link.href;
				}
			});			
		});
	},
	SelectChange : function(){
		var toggler = $('#finder-toggler').find('a'),
		container = $('#tour-finder'),
		sport = container.find('#tfw-sports'),
		experience = container.find('#tfw-experiences'),
		group = container.find('#tfw-package-groups'),
		button = $('#find-tour-button');
		
		// Override the button to navigate via the selected option
		button.bind('click',function(event) {
			event.preventDefault();
			var	link = $(this).attr('href');
			if (link === '#') {
				return false;
			} else {
				window.location	= '/packages/' + link;
			}
		});
		
		// Toggle the tour finder
		toggler.toggle(function() {
			container.slideDown();
		}, function() {
			container.slideUp();
		});
		
		// Configure experience and package group <select>s
		$([experience, group]).each(function(i, el) {
			var select = $(el);
			var label = select.children('option:first-child');
			
			// Cue the user that we're waiting for a selection
			if(select.attr('id') == 'tfw-experiences') {
				label.text('← First choose a sport');
			}
			else {
				label.text('← First choose an experience');
			}
			
			// Disable the <select>
			select.attr({disabled: true}).css('color', '#888');
			
			// Clone and remove all the children.
			// This ensures that keyboard selection won't grab "hidden"
			// options. Also circumvents a display problem stemming
			// from the number of options in the select.
			var stored_options = select.clone();
			select.find('optgroup, optgroup > option').remove();
			
			// Function to execute whenever the previous select changes
			select.bind('changeOptions', function (e, group) {
				
				// Set the label
				var label = $(this).children('option:first-child');
				
				// Restore the select to its empty state
				$(this).attr({disabled: false}).find('optgroup, option').not(label).remove();
			
				// Reset its color
				$(this).css('color', 'inherit');

				// Find the correct <optgroup> and append those options
				var selected_options = stored_options.find('optgroup').filter(group).clone();
				$(this).append(selected_options);

				// If we've got options, update the label to cue the user
				if(selected_options.length > 0) {
					label.text('Make a selection');
				}
				// Otherwise, let them know there are no options
				else {
					label.text('None available');
				}
				// Re-select the label to reset the field's state
				label.show().attr('selected', 'selected');

			});
		});
		sport.change(function(event) {
			var value = $('option:selected', this).val();
			experience.trigger('changeOptions', value);
			group.attr({disabled: true});
			button.attr('href', '#');
		});
		experience.change(function(event) {
			var value = $('option:selected', this).val();
			if (value === null | undefined) {
				group.attr({disabled: true});
			} else{
				group.trigger('changeOptions', value);
			}
			button.attr('href', '#');
		});
		group.change(function(event) {
			var link = $('option:selected', this).val();
			button.attr({href: link});
		});
	},
	EnableOverlays : function(){
		$("a.modal").overlay({
			mask: '#000',
			effect: 'default',
			onBeforeLoad : function() {
				var wrap = this.getOverlay().find(".contentWrap");
				wrap.load(this.getTrigger().attr("href"));	
			}
		});
	},
	Details : function(){
		$('a[rel=accommodation]').colorbox({
			iframe:true, 
			innerWidth:724, 
			innerHeight:671
		});
		$('a[rel=enquiry]').colorbox({
			iframe:true, 
			innerWidth:724, 
			innerHeight:549
		});
		$('.tabs').tabbed();
			
		$(".gallery-nav").scrollable();
		$(".gallery").tabs(".gallery-items > div", {tabs: '.nav-item', effect: 'fade',fadeOutSpeed: "fast"});
		
		//special action for prices tab
		$('#next-steps').find('a').bind('click', function(event){
			event.preventDefault();
			$('.tab-title').last().find('a').click();
		});
	}
};
$(function() {
	$('a[rel=external]').attr('target','_blank');
	if ($('#page-home').length){
		LSR.HomepageCarousel();
		LSR.ExpandClick();
	}
	if ($('#page-resort-detail').length) {
		LSR.Details();
	}
	LSR.SelectChange();
});

