/**
 * Bootstrap.js - JavaScript bootstrapper
 * 
 * @author Webstores <info at webstores dot nl>
 *         Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */
$(document).ready(function() {
	
	// External links
	$('a[rel="external"]').each(function() {
		this.target = '_blank'
	});
	
	// Toggle input values
	$('.placeholder').each(function() {
		var v = this.value;
		
		$(this).focus(function() {
			if(this.value == v) {
				this.value = '';
			}
			$(this).addClass('focus');
		});
		
		$(this).blur(function() {
			if(this.value == '') {
				this.value = v;
				$(this).removeClass('focus');
			}
		});
		
		$(this.form).submit(function() {
			$(this).find('.placeholder').each(function() {
				if(this.value == v) {
					this.value = '';
				}
			});
		});
	});
	
	// IE6 hover
	if(/msie 6/i.test(navigator.userAgent)) {
		$('#navigation li').each(function() {
			$(this).hover(function() {
				$(this).addClass('iehover');
			},
			function () {
				$(this).removeClass('iehover');
			});
		});
	}
	
	// AJAX loading
	$(document.body).ajaxStart(function() {
		$(this).addClass('loading');
	}).ajaxComplete(function() {
		$(this).removeClass('loading');
	});
	
	// Accordions
	$('.accordion').each(function() {
		switch(this.id) {
			/*case 'info-accordion':
				new Toggler(this).expand($(this).find('li:first-child').get(0));
				break;*/
			default:
				new Toggler(this);
				break;
		}
	});
	
	var filterAccordion = new Toggler('#content-filter ul', {
		togglerClass: 'more',
		genericBehavior: true
	});
	
	// Show search form
	var ssf = new ShowSearchForm('#show-search-form');
	
	// Carousels
	if($('#spotlight-items').length) {
		$('#spotlight-items').jcarousel({
			scroll: 1,
			animation: 'slow',
			auto: 7,
			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');
			}
		});
	}
	
	// Programme per page
	$('#per-page-form button').hide();
	$('#per-page-form select').change(function() {
		$(this).parents('form').submit();
	});
	
	// Hoverable/clickable table rows
	rowClick();
	rowHover();
	
	// Booking form
	var cf = new CheckoutForm('#ticket-checkout');
	
	// Shadowbox
	Shadowbox.init({
		overlayOpacity:0.8,
		troubleElements: ['select']
	});
	
	// Validation
	//$('.generated-form').validate();
});


/**
 * Make table rows clickable
 */
function rowClick(table) {
	$('.tclick tr').each(function() {
		var anchor = $('a:first-child', this);
		
		if(anchor.length) {
			$(this).click(function() {
				switch(anchor.attr('rel')) {
					case 'external':
						window.open(anchor.attr('href'));
						break;
					default:
						window.location.href = anchor.attr('href');
						break;
				}
			});
			
			$(this).attr('title', anchor.attr('title'));
		}
	});
};


/**
 * Give table rows a hover state
 */
function rowHover(table) {
	$('.thover tr').each(function() {
		if($('th', this).length <= 1) {
			$(this).hover(
				function() {
					$(this).addClass('hover');
				},
				function() {
					$(this).removeClass('hover');
				}
			);
		}
	});
};

