/**
 * Bootstrap.js - JavaScript bootstrapper
 * 
 * @author Webstores <info at webstores dot nl>
 *         Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */
$(document).ready(function() {
	
	// Utilities
	WS.Util.externalLinks();
	WS.Util.rowClick();
	WS.Util.fixPlaceholders();
	
	// IE6 fixes
	if(/msie 6/i.test(navigator.userAgent)) {
		MessageBar.show('warning', '<h1>U gebruikt een zeer oude versie van Internet Explorer</h1><p>Voor een optimale ervaring is het aan te raden om de <a href="http://www.microsoft.com/netherlands/windows/internet-explorer/" title="De laatste versie van Internet Explorer downloaden" rel="external">laatste versie van Internet Explorer</a> te installeren, of te kiezen voor een alternatieve browser zoals <a href="http://www.mozilla.com/firefox/" title="Mozilla Firefox downloaden" rel="external">Firefox</a>, <a href="http://www.google.com/chrome" title="Google Chrome downloaden" rel="external">Chrome</a>, <a href="http://www.apple.com/nl/safari/" title="Apple Safari downloaden" rel="external">Safari</a> of <a href="http://www.opera.com/" title="Opera downloaden" rel="external">Opera</a>.</p>');
		WS.Util.fixIE6HoverList();
	}
	
	// AJAX loading
	/**
	 * TODO: Doesn't work well together with visualwebsiteoptimizer for some reason, needs fix
	 */
	/*$(document.body).ajaxStart(function() {
		$(this).addClass('loading');
	}).ajaxComplete(function() {
		$(this).removeClass('loading');
	});*/
	
	// Togglers
	$('.accordion').each(function() {
		switch(this.id) {
			case 'album-accordion':
				new Toggler(this, {
					allowMultiple: false
				}).expand($(this).find('> li:first-child'));
				break;
			default:
				new Toggler(this);
				break;
		}
	});
	
	var footerToggler = new Toggler('#footer-tools .toolbar', {
		allowMultiple: false,
		onAfterCollapse: function() {
			$('#footer-tools').css('height', 'auto');
		},
		onAfterExpand: function(toggler) {
			$('#footer-tools').css('height', 160);
			$('html, body').animate({
				scrollTop: toggler.offset().top
			}, 1000);
		}
	});
	
	// Carousels
	if($('#spotlight-items').length) {
		$('#spotlight-items').jcarousel({
			scroll: 1,
			animation: 'slow',
			auto: 5,
			wrap: 'both',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(carousel){
				$('#spotlight-controls li').each(function(i){
					$(this).bind('click', function(e){
						e.preventDefault();
						carousel.stopAuto();
						carousel.scroll(i + 1);
						carousel.startAuto();
					});
				});
				
				carousel.clip.hover(function(){
					carousel.stopAuto();
				}, function(){
					carousel.startAuto();
				});
			},
			itemVisibleInCallback: function(carousel, slide, index, state){
				$('#spotlight-controls li:nth-child(' + index + ')').addClass('selected');
			},
			itemVisibleOutCallback: function(carousel, slide, index, state){
				$('#spotlight-controls li:nth-child(' + index + ')').removeClass('selected');
			}
		});
	}
	
	// Tabs
	$('#package-picks').tabs();
	$('#package-details').tabs({
		tabsSelector: '#package-details-tabs'
	});
	
	// Validation
	$('form.generated').validate();
	
	// Sort form
	$('#sort-form button').hide();
	$('#sort-options').change(function() {
		this.form.submit();
	});
	
	// Booking form
	var bf = new BookingForm('#booking-form');
	
	// Datepickers
	var dpp = new DatePickerProxy('#datepicker-row, #arrival-date, #departure-date', '/packages/availability');
	
	// Messages
	MessageBar.init();
	
	// Shadowbox
	Shadowbox.init({
		overlayOpacity: 0.8,
		troubleElements: ['select']
	});
	
	// AddThis
	addthis.init();
});

