//GLOBALS
var galleryButtonImage = '/wp-content/themes/fjallgarden/images/imagegallery_play.png';

var defaultIGSettings = {
	phpFile: '/wp-content/themes/fjallgarden/js/imagegallery.php',
	mouseInfoDiv: {
		previousCSS: {
			'background': 'url(/wp-content/themes/fjallgarden/images/imagegallery_previous.png) center center no-repeat',
			'opacity': .6
		},
		nextCSS: {
			'background': 'url(/wp-content/themes/fjallgarden/images/imagegallery_next.png) center center no-repeat',
			'opacity': .6
		},
		closeCSS: {
			'background': 'url(/wp-content/themes/fjallgarden/images/imagegallery_close.png) center center no-repeat',
			'opacity': .6
		},
		mouseDistance: {
			top: 7,
			left: 0
		}
	}
}

//JQUERY

$(document).ready(function() {
	$('#header-bookings select').css('opacity', 0.0);
	
	$('#header-bookings select').each(function(item, key) {
		$(this).parent().find('.value').text($(this).find(':selected').val());
	}).change(function() {
		$(this).parent().find('.value').text($(this).find(':selected').val());
	});
	
	$(document).pngFix();

	$('div#body.afterski').css('opacity', 0.8);
	$('div#body.afterski *').css('opacity', 1);
	
	if($('body.home #flash').length > 0) {
		swfobject.embedSWF(template_directory +"/home.swf", "flash", "100%", "100%", "8.0.0", "", {base_url: url}, {wmode: "transparent", scale: "noScale"});
	}
	
	$('ul.languages').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	var dates = new Array();
	
	$.ajax({
		url: template_directory +'/proxy.php',
		success: function(xml, status) {
			if(status != "success") {
				alert("Couldn't load the file!");
				return;
			}
			
			$(xml).find('dates').each(function(key, item) {
				var nights = new Array();
				$(item).find('nights').find('length').each(function(key, item) {
					nights.push($(item).text());
				});
			
				dates.push({"date": $(item).find('date').text(), "nights": nights});
			});
			
			$('#input-arrival').datepicker({
				showOn: 'both',
				buttonImage: template_directory +'/images/calendar.gif',
				buttonImageOnly: true,
				dateFormat: 'yy-mm-dd',
				showAnim: 'slideDown',
				minDate: new Date(),
				beforeShowDay: function(date) {
					return [checkDates(date) == true];
				},
				onSelect: function(dateText) {
					var nights = get_nights_by_date(dateText);
					
					if(nights !== false) {
						// remove old options
						$('#select-nights').children().remove();
						
						// add new
						for(var i = 0; i < nights.length; i++) {
							$('#select-nights').append('<option>'+ nights[i] +'</option>');
						}

						// set display number
						$('.select.nights .value').text($('#select-nights option:first').val());
					}
				}
			});
		}
	});

	// Image gallery
	
	$('#body').imageGallery($.extend({}, defaultIGSettings, {ignore: '.child-preview, .image-gallery'}));
	
	
	// FUNCTIONS
	
	function get_nights_by_date(date) {
		if(typeof date == "undefined")
			return false;
	
		for(var i = 0; i < dates.length; i++) {
			if(dates[i].date == date)
				return dates[i].nights;
		}
		
		return false;
	}
	
	function checkDates(date) {
		for(var i = 0; i < dates.length; i++)
			if($.PHPDate("Y-m-d", date) == dates[i].date)
				return true;
		
		return false;
	}
	
	
	if(locale != "sv_SE")
		return;
	
	/* Swedish initialisation for the jQuery UI date picker plugin. */
	/* Written by Anders Ekdahl ( anders@nomadiz.se). */
	jQuery(function($){
		$.datepicker.regional['sv'] = {
			closeText: 'Stäng',
			prevText: '&laquo;Förra',
			nextText: 'Nästa&raquo;',
			currentText: 'Idag',
			monthNames: ['Januari','Februari','Mars','April','Maj','Juni',
			'Juli','Augusti','September','Oktober','November','December'],
			monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun',
			'Jul','Aug','Sep','Okt','Nov','Dec'],
			dayNamesShort: ['Sön','Mån','Tis','Ons','Tor','Fre','Lör'],
			dayNames: ['Söndag','Måndag','Tisdag','Onsdag','Torsdag','Fredag','Lördag'],
			dayNamesMin: ['Sö','Må','Ti','On','To','Fr','Lö'],
			dateFormat: 'yy-mm-dd', firstDay: 1,
			isRTL: false};
		$.datepicker.setDefaults($.datepicker.regional['sv']);
	});
});

function find_rooms() {		
	var date = $('#input-arrival').val();
	
	if(date == ""){
		window.location = "http://www.shecenter.com/prosp/Fjallgarden/SelectCalendar.aspx";
		return false;
	}
	
	$('#bookings-form').submit();
}