/**
 * Bootstrap.js - JavaScript bootstrapper
 * 
 * @author Webstores <info at webstores dot nl>
 *         Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */
$(document).ready(function() {
	
	var fs = new FontSizer('#font-size', {
		applyTo: ['#main']
	});
	
	// External links
	$('a[rel="external"]').each(function() {
		this.target = '_blank'
	});
	
	// Input placeholders
	$('.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');
			}
		});
	});
	
	// IE6 hover
	if(/msie 6/i.test(navigator.userAgent)) {
		$('#navigation li').each(function() {
			$(this).hover(function() {
				$(this).addClass('iehover');
			},
			function () {
				$(this).removeClass('iehover');
			});
		});
	}
	
	// Carousels
	if($('.gallery-slides li').length > 1) {
		$('.gallery-slides').jcarousel({
			scroll: 1,
			animation: 'slow',
			wrap: 'both',
			initCallback: function(carousel) {
				$('.gallery-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) {
				$('.gallery-controls li:nth-child(' + index + ')').addClass('selected');
			},
			itemVisibleOutCallback:function(carousel, slide, index, state) {
				$('.gallery-controls li:nth-child(' + index + ')').removeClass('selected');
			}
		});
	}
	
	if($('#news-slides li').length > 1) {
		$('#news-slides').jcarousel({
			scroll: 1,
			animation: 'slow',
			auto: 7,
			wrap: 'both',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(carousel) {
				carousel.clip.hover(
					function() {
						carousel.stopAuto();
					},
					function() {
						carousel.startAuto();
					}
				);
			}
		});
	}
	
	// Accordions
	$('.accordion').each(function() {
		new Accordion(this);
	});
	
	// Appointment banner
	$('#appointment-banner').click(function(e) {
		e.preventDefault();
		$('#appointment-window').toggle();
	});
	
	$('#appointment-window-close').click(function(e) {
		e.preventDefault();
		$('#appointment-window').hide();
	});
	
	// Validation
	$('form').each(function() {
		$(this).validate({
			highlight: function(el, errorClass, validClass) {
				var errorEl = null;
				
				switch(el.type) {
					case 'radio':
						errorEl = $(el).parents('ul.options');
						break;
					case 'checkbox':
						errorEl = $(el).parent();
						break;
					default:
						errorEl = $(el);
						break;
				}
				
				errorEl.removeClass(validClass).addClass(errorClass);
			},
			unhighlight: function(el, errorClass, validClass) {
				var errorEl = null;
				
				switch(el.type) {
					case 'radio':
						errorEl = $(el).parents('ul.options');
						break;
					case 'checkbox':
						errorEl = $(el).parent();
						break;
					default:
						errorEl = $(el);
						break;
				}
				
				errorEl.removeClass(errorClass).addClass(validClass);
			}
		});
	});
	
	// Truncate
	/*var contentTruncator = new Truncator('.section-news #content', {
		maxHeight: 560,
		truncateElements: ['p', '#addthis'],
		togglerClass: 'more truncator-toggler'
	});*/
	
	// Make table rows clickable/hoverable
	rowClick();
	rowHover();
});


/**
 * 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');
				}
			);
		}
	});
};

