var status = 0;
var strip = null;
var stripi = [];
var stripp = 1;
var stripml = 0;
var slides = null;
var fwd = null;
var rew = null;
var close = null;
var lasts = null;

$(document).ready(function(){
	strip = $($('div.imgstrip'));
	strips = $($('div#slides'));
	centre();
	if ($('div.calnav').size() == 0 && strip.size() != 0) {
		slides = $($('div.slides'));
		fwd = $($('a#fwd'));
		if (strip.find('img').size() > 2) {
			strip.find('img').each(function(cnt){
				stripi[cnt] = $($(this));
			});
		
			fwd.click(function(){
				if (status == 0) {
					showStrip();
				} else {
					move(1);
				}
			});
		} else {
			fwd.remove();
		}
		
	
		if (strip!=null) {
			$('img#backgroundimg').fadeTo(1, 0);
		}
	} 
	$('#calendar').find('a').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	
	if ($('form#bookstepone').length > 0) {
		// booking step one calendar date selection
		
		var cal = $($('form#bookstepone'));
		cal.find('div.row a').each(function(){
			var href = $(this).attr('href'); 
			if (href == "#") {
				$(this).click(function(me){
					me.preventDefault();
					return false;
				});
			} /*else {
				$(this).click(function(me){
					me.preventDefault();
					$('input#date').val(href);
					if (lasts != null) {
						lasts.removeClass('selected');
					}
					$(this).addClass('selected');
					lasts = $($(this));
					return false;
				});
			}*/
		});
		
	}
	
	

});

$(window).load(function(){
	if (strip!=null) {
		setTimeout("fistImageFade()",1500);
	}
});

$(window).resize(function(){
	centre();
});

function centre() {
	var leftm = (getW()-655)/2;
	if (leftm < 150) {
		// margin auto;
		$('div#canvas').css("marginLeft",'auto');
	} else {
		// we have enough room to adjust....
		$('div#canvas').css("marginLeft",(leftm - 150));
	}

}

function getW() {
	return document.documentElement.scrollWidth;
}

function fistImageFade() {
	// check if more than one image if yes it's fine otherwise first duplicate first image
	var firstimg = $(strip.find('img').eq(0));
	if (strip.find('img').size() < 2) {
		firstimg.after(firstimg.clone());
	}
	firstimg.fadeTo(500,0,function(){
		// fading background image
		firstimg.remove();
		$('img#backgroundimg').fadeTo(1200, .99);	
	});

}

function showStrip() {
	if ($.browser.msie && parseFloat($.browser.version)<7) {
		
		strip.animate({marginLeft:-407},200,function(){
			strip.css("width",654);
		});
	} else {
		strip.animate({marginLeft:-407},300,function(){
			strip.css("width",654);
			//strip.animate({width:654},100);
		});
	}
	fwd.before('<a href="#" id="rew"></a>');
	fwd.after('<a href="#" id="close"></a>');
	rew = $($('a#rew'));
	close = $($('a#close'));
	
	close.click(function(){
		hideStrip();
	});
	rew.click(function(){
		move(-1);
	});
	status=1;
}

function move(dir) {

	if (dir == 1 && stripp != stripi.length-1) {
		stripml -= stripi[stripp].width() + 6;
		stripp++;
		strips.animate({marginLeft:stripml},300);
	}
	if (dir == -1 && stripp != 1) {
		stripp--;
		stripml += stripi[stripp].width() + 6; 
		strips.animate({marginLeft:stripml},300);
	}
	
}

function hideStrip() {
	stripp = 1;
	stripml = 0;
	strips.animate({marginLeft:stripml},50);
	close.remove();
	rew.remove();
	strip.css("width",247);
	strip.animate({marginLeft:0},300);
	status = 0;
}