
var IpUKRedirectWidget = Class.create({
	markerElementId			: 'ipUKRedirectMarkerId',
	currentOfficeId			: 'ipUKRedirectOfficeId',
	lightWindowContextId	: 'ipUKRedirectLightWindowId',
	lightWindowCloseId	    : 'ipUKRedirectCloseId',
	lightWindowWebSite      : 'ipUKRedirectWebSite',
	lightWindowWebSiteAttr  : 'ipUKRedirectSiteUrl',
	lightWindowRemember     : 'ipUKRedirectRememberId',
	acceptMarkerValue		: 'true',
	redirectCookieName		: 'ukRedirtHomepage',

	addressUK				: 'http://www.SpaFinder.co.uk',
	addressUS				: 'http://www.SpaFinder.com',

	initialize: function(){
		var markerElement = $(this.markerElementId);
		if ( null != markerElement && this.acceptMarkerValue == markerElement.value ){
			Event.observe(this.lightWindowCloseId, 'click', this.closeClickRedirectWindow.bind(this));
			this.showRedirectWindow();
		}
	},

	showRedirectWindow: function(){
		Lightview.show({
			href	: this.lightWindowContextId,
			rel		: 'inline',
			options	: {
				autosize: true,
                closeButton: false
            }
		});
	},

	getRedirectWebSiteValue: function(){
		return Form.getInputs(this.lightWindowContextId, 'radio', this.lightWindowWebSite).find(function(radio){
			return radio.checked;
		}).value;
	},

	getRedirectWebSiteAttribute: function(){
		return Form.getInputs(this.lightWindowContextId, 'radio', this.lightWindowWebSite).find(function(radio){
			return radio.checked;
		}).getAttribute(this.lightWindowWebSiteAttr);
	},

	setCookie: function (name, value, expires, path, domain, secure){
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ){
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+"="+escape( value ) +
			( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) +
			( ( path ) ? ";path=" + path : "" ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
	},

	saveRedirectCookie: function(){
		this.setCookie(this.redirectCookieName, this.getRedirectWebSiteAttribute());
	},

	closeClickRedirectWindow: function(){
		if ( $(this.lightWindowRemember).checked )
			this.saveRedirectCookie();

		if ( this.getRedirectWebSiteValue() != $(this.currentOfficeId).value )
			document.location = this.getRedirectWebSiteAttribute();
	}
});

spafinder.classes.home = Class.create({
	initialize: function() {
		this.carousels();
		this.starbox();
		this.map();
		this.dealdays();
	},
	dealdays: function() {
		try {
			$$('#spafinder.en-US #highlights .highlight .description h5 a').last().setStyle({color: '#FF0000'});
		} catch(e) {
		}
	},
	carousels: function() {
		var promotions = new Carousel($('promotions').down('.carousel'), $('promotions').down('.carousel').select('.slide'), $('promotions').select('.tabs a.jumper'), {
			auto: false,
			circular: false,
			duration: 0,
			frequency: 5,
			wheel: false,
			disabledClassName: 'inactive',
			selectedClassName: 'active',
			jumperClassName: 'jumper',
			afterMove: function() {
				if ( document.images && (promotions.current.id == 'promotion-slide-02') ) {
					var img = new Image(1, 1);
					img.src = 'http://ad.doubleclick.net/ad/hgn.spafinder/swatermicrosite;sz=1x1;ord=123456789?';
				}
				promotions.controls.each(function(element) {
					element.removeClassName(promotions.options.selectedClassName);
				});
				promotions.controls.each(function(element) {
					if (element.rel == promotions.current.id) {
						element.addClassName(promotions.options.selectedClassName);
					}
				});
			}
		});
		var features = new Carousel($('features').down('.carousel'), $('features').down('.carousel').select('.slide'), $('features').select('.tabs a.jumper'), {
			effect: 'fade',
			duration: 0,
			wheel: false,
			disabledClassName: 'inactive',
			selectedClassName: 'active',
			jumperClassName: 'jumper'
		});
		var topspadeals = new Carousel($('top-spa-deals').down('.deck'), $('top-spa-deals').down('.deck').select('.suit'), $$('#top-spa-deals .tabs a.control', '#top-spa-deals .tabs a.play'), {
			disabledClassName: 'inactive',
			selectedClassName: 'active',
			controlClassName: 'control',
			jumperClassName: 'play',
			wheel: false,
			afterMove: function() {
				topspadeals.controls.each(function(element) {
					element.removeClassName(topspadeals.options.selectedClassName);
				});
				topspadeals.controls.each(function(element) {
					if (element.rel == topspadeals.current.id) {
						element.addClassName(topspadeals.options.selectedClassName);
					}
				});
			}
		});
		var toptenspas = new Carousel($('top-ten-spas').down('.deck'), $('top-ten-spas').down('.deck').select('.suit'), $$('#top-ten-spas .tabs a.control', '#top-ten-spas .tabs a.play'), {
			disabledClassName: 'inactive',
			selectedClassName: 'active',
			controlClassName: 'control',
			jumperClassName: 'play',
			wheel: false,
			afterMove: function() {
				toptenspas.controls.each(function(element) {
					element.removeClassName(toptenspas.options.selectedClassName);
				});
				toptenspas.controls.each(function(element) {
					if (element.rel == toptenspas.current.id) {
						element.addClassName(toptenspas.options.selectedClassName);
					}
				});
			}
		});
		var featuredspas = new Carousel($('featured-spas').down('.deck'), $('featured-spas').down('.deck').select('.suit'), $$('#featured-spas .tabs a.control', '#featured-spas .tabs a.play'), {
			disabledClassName: 'inactive',
			selectedClassName: 'active',
			controlClassName: 'control',
			jumperClassName: 'play',
			wheel: false,
			afterMove: function() {
				featuredspas.controls.each(function(element) {
					element.removeClassName(featuredspas.options.selectedClassName);
				});
				featuredspas.controls.each(function(element) {
					if (element.rel == featuredspas.current.id) {
						element.addClassName(featuredspas.options.selectedClassName);
					}
				});
			}
		});
	},
	starbox: function() {
		$$('#top-spa-deals .reviews span.starbox', '#featured-spas .reviews span.starbox').each(function(element) {
			spafinder.Document.stars(element, parseFloat(element.innerHTML.stripTags()));
		});
	},
	map: function() {
		$$('map area').each(function(area) {
			area.observe('mouseover', function(event) { 
				$('world').src = '/assets/images/home/map-' + event.element().id + '.gif';
			});
			area.observe('mouseout', function(event) { 
				$('world').src = '/assets/images/home/map-world.gif';
			});
		});
	}
});
document.observe("dom:loaded", function() {
	new spafinder.classes.home();
});

document.observe("lightview:loaded", function() {
	new IpUKRedirectWidget();
});
