var clockInterval, underlineTimeout, menuTimeout;
var currentBackground, nextBackground, backgroundTimer;

var gradients = new Array('bg-sunrise', 'bg-morning', 'bg-day', 'bg-sunset', 'bg-night');

var formActions = {};
var backgroundTime = 15000;

$(document).ready(function() {
	initBackground();
	initClock();
	initNavigation();
	initArchive();
	initVerticalBanners();	
	initBookingCalendars();	
	initDraggableMap();
	
	$("a.subscribe-toggle").attr('href', 'javascript:void(0)').click(function() {
		$("div.subscribe-form").slideToggle();
	});
	
	if (!$.browser.msie || ($.browser.msie && parseInt($.browser.version) > 7))
		$("form input[type=text]").addPlaceholder();
		
	if ($("form#frm-subscribe").length > 0)
		initSubscribeForm();
		
	if ($("form#frm-side-subscribe").length > 0)
		initSideSubscribeForm();
	
	$("a.colorbox").colorbox();
	
	/* FORMS */
	if ($("form#frm-general, form#frm-media, form#frm-venue").length > 0) {
		$("form#frm-general, form#frm-media, form#frm-venue").each(function(index) {
			formActions[$(this).attr('id')] = $(this).attr('action');
			//$(this).removeAttr('action');
		}).submit(function(event) {
			event.preventDefault();
		}).find('a.submit').click(function() {
			var form = $(this).closest('form');
			var selects = true;
			
			form.find('select').each(function(index) {
				if ($(this).val() == '' && !$(this).hasClass('ignore')) {
					$(this).addClass('error');
					selects = false;
				} else {
					$(this).removeClass('error');
				}
			});	
			
			if (form.valid() && selects) {
				$.post(form.attr('action'), form.serialize(), function(data) {
						form.find('div.error-box').fadeOut('fast');
						form.find('div.thank-you').fadeIn('fast');
						form.find('input[type=text], textarea, select').val('');
				});
			} else {
				form.find('div.error-box').fadeIn('fast');
			}
		});
	}
});

initClock = function() {
	var currentTime = new Date();
  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentTimeString = (currentHours > 12 ? currentHours - 12 : currentHours) + ":" + ((currentMinutes < 10 ? "0" : "") + currentMinutes) + " " + (currentHours < 12 ? "AM" : "PM");
	
	$("div#clock div").text(currentTimeString);
	
	if (clockInterval == null)
		clockInterval = setInterval(initClock, 10000);
}

initNavigation = function() {
	$("div#navigation div.underline").css({ left: $("div#navigation").css('left'), opacity: 0, visibility: 'visible' });
	$("div#navigation ul.menu > li > a").each(function(index) {
		$(this).hover(function() {
			if ($(this).next().length > 0) {
				clearTimeout(underlineTimeout);
				clearTimeout(menuTimeout);
				hideSubmenu(true);
				
				$(this).next().css('display', 'block').hover(function() {
					clearTimeout(underlineTimeout);
					clearTimeout(menuTimeout);
				}, function() {
					hideSubmenu(true);
					hideUnderline();
				});
			}
			
			shiftUnderline($(this));
		}, function() {
			menuTimeout = setTimeout(hideSubmenu, 100);
			underlineTimeout = setTimeout(hideUnderline, 100);
		});
	});
}

initArchive = function() {
	$("dl.archive-tree dt a.year").each(function(index) {
		var dd = $(this).parent('dt').next('dd');
		if (!$(this).hasClass('selected'))
			dd.slideUp();
			
		$(this).attr('href', 'javascript:void(0)').click(function() {
			dd.slideToggle('fast');
			
			if ($(this).hasClass('selected'))
				$(this).removeClass('selected');
			else
				$(this).addClass('selected');
		});
	});
}

var activeBanner = null;
initVerticalBanners = function() {
	$("div#vertical-banners div.banner").each(function() {
		$(this).hover(function() {
			$(this).clearQueue().animate({ width: 442 }, 200);
			$(this).find('div.navigation').clearQueue().animate({ width: 410 }, 200);
		}, function() {
			$(this).clearQueue().animate({ width: 190 }, 200);
			$(this).find('div.navigation').clearQueue().animate({ width: 170 }, 200);
		});
	});
}

initBackground = function() {
	getBackground();
	
	$("div#background-1").addClass(currentBackground).fadeIn('slow');
	$("div#background-2").addClass(nextBackground);
	
	//backgroundTimer = setInterval(updateBackground, backgroundTime);
}

getBackground = function() {
	var currentDate = new Date();
	var hour = currentDate.getHours();
	var a, b;
	
	if (hour >= 5 && hour < 8)				// 05:00 - 07:59, sunrise
		a = 0;
	else if (hour >= 8 && hour < 12)	// 08:00 - 11:59, morning
		a = 1;
	else if (hour >= 12 && hour < 17)	// 12:00 - 16:59, day
		a = 2;
	else if (hour >= 17 && hour < 19)	// 17:00 - 18:59, sunset
		a = 3;
	else															// 19:00 - 04:59, night
		a = 4;
		
	//a = randomFromTo(0, 4); // uncomment for random
	b = a == 4 ? 0 : a + 1;
	
	currentBackground = gradients[a];
	nextBackground = gradients[b];
}

updateBackground = function() {
	$("div#background-2").fadeIn('slow', function() {
		$("div#background-1").removeClass().addClass('gradient-background ' + nextBackground).css('display', 'block');
		
		getBackground();
		
		$("div#background-2").css('display', 'none').removeClass().addClass('gradient-background ' + nextBackground); 
	});
}

hideSubmenu = function(quick) {
	clearTimeout(menuTimeout);
	
	$("div#navigation ul.menu > li > a").each(function(index) {
		$(this).next().clearQueue().css('display', 'none');
		$(this).removeClass('selected');
	});
}

shiftUnderline = function(menuItem) {
	var p = menuItem.position();
	
	$("div#navigation div.underline").css({ top: p.top + 15 }).clearQueue().animate({
		left: p.left, 
		width: menuItem.width() + 1 + 'px',
		opacity: 1
	}, 200);
}

hideUnderline = function() {
	$("div#navigation div.underline").animate({
		opacity: 0
	}, 100, function() {
		$(this).css('left', $("div#navigation").css('left'));
	});
}

initSideSubscribeForm = function() {
	$("form#frm-side-subscribe").submit(function(event) {
		event.preventDefault();
	});
	
	$("form#frm-side-subscribe a.subscribe").click(function() {
		$("div.subscribe-error").css('display', 'none');
		var form = $(this).closest('form');
		if (form.valid()) {
			$.post(form.attr('action'), form.serialize(), function(data) {
				form.find('input').val('');
				$("div.subscribe-success").css('display', 'block');
				
				try {
					_gaq.push(['_trackEvent','subscribe_to_e_newsletter','submit','attractions_subscribe']);
				} catch (e) {
					
				}
			});
		} else {
			$("div.subscribe-error").css('display', 'block');
		}
	});
}

initSubscribeForm = function() {
	$("form#frm-subscribe, form#frm-additional").each(function() {
		formActions[$(this).attr('id')] = $(this).attr('action');
		//$(this).removeAttr('action');
	}).submit(function(event) {
		event.preventDefault();
	});
	
	$("form#frm-subscribe a.subscribe").click(function() {
		$("div.subscribe-error").css('display', 'none');
		var form = $(this).closest('form');
		if (form.valid()) {
			$.post(form.attr('action'), form.serialize(), function(data) {
				$("form#frm-additional input#email").val(form.find('input#txtEmail').val());
				form.find('input').val('');
				$.colorbox({ inline: true, href: "#additional-form", transition: "fade" });
				
				try {
					_gaq.push(['_trackEvent','subscribe_to_e_newsletter','submit','home_subscribe']);
				} catch (e) {
					
				}
			});
		} else {
			$("div.subscribe-error").css('display', 'block');
		}
	});
	
	$("form#frm-additional a.submit").click(function() {
		var form = $(this).closest('form');
		var checks = form.find('input:checked').length;
		
		form.find('p.error').text('Please select at least one option below').css('visibility', 'hidden');
		
		if (form.valid()) {
			if (checks > 0) {
				$.post(form.attr('action'), form.serialize(), function(data) {
					form.find('div.thank-you').fadeIn().delay(2000).fadeOut('fast', function() {
						$.colorbox.close();
						form.find('input').val('');
						form.find('input:checkbox').prop('checked', false);
					});
				});
			} else {
				form.find('p.error').text('Please select at least one option below').css('visibility', 'visible');
			}
		} else {
			form.find('p.error').text('Please provide your name').css('visibility', 'visible');
		}
	});
}

showOthers = function(source, target) {
	if ($(source + "option:selected").val().indexOf('other') > -1)
		$(target).slideDown('fast');
	else
		$(target).slideUp('fast').find('input[type=text]').val('');
}

initBookingCalendars = function() {
	var dates = $("input#txtEventStart, input#txtEventEnd").datepicker({
		dateFormat: 'dd-mm-yy',
		minDate: 0,
		maxDate: "+18m",
		numberOfMonths: 1,
		onSelect: function(selectedDate) {
			updateDate(dates, this, "txtEventStart", selectedDate);
		}
	});
	
	var alternate = $("input#txtAlternateStart, input#txtAlternateEnd").datepicker({
		dateFormat: 'dd-mm-yy',
		minDate: 0,
		maxDate: "+18m",
		numberOfMonths: 1,
		onSelect: function(selectedDate) {
			updateDate(alternate, this, "txtAlternateStart", selectedDate);
		}
	});
}

updateDate = function(elements, source, start, selected) {
	var option = source.id == start ? "minDate" : "maxDate";
	var instance = $(source).data("datepicker");
	var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selected, instance.settings);
	
	elements.not(source).datepicker("option", option, date);
}

tab = function(source, name) {
	if (!$(source).hasClass('selected')) {
		$("div.inner-content").each(function(index) {
			if ($(this).css('display') == 'block') {
				$("a#tab-" + this.id.substr(this.id.indexOf('-') + 1)).removeClass('selected');
				$(this).fadeOut('fast', function() {
					$("a#tab-" + name).addClass('selected');
					$("div#content-" + name).fadeIn('fast');
				});
			}
		});
	}
}

initDraggableMap = function() {
	$("div#map-zoom").css({ 'left': -300, 'top': -210 }).draggable({
		cursor: 'move',
		scroll: false,
		stop: stopHandler
	});
}

stopHandler = function(event, ui) {
	var queue = {};
	if (ui.position.left > 0)
		queue.left = 0;
	else if (ui.position.left < -730)
		queue.left = -730;
		
	if (ui.position.top > 0)
		queue.top = 0;
	else if (ui.position.top < -663)
		queue.top = -663;
		
	$("div#map-zoom").animate(queue, '200');
};

randomFromTo = function(from, to){
	 return Math.floor(Math.random() * (to - from + 1) + from);
}

if( self == top ) { document.documentElement.style.display = 'block' ; } else { top.location = self.location ; }
