/**
 * 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'
	});
	
	// 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();
	}
	
	MessageBar.init();
	// Toggle input values
	$('.toggle-value').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');
			});
		});
	}
	
	if($('#order-id')) {
		$('#order-id').bind('change', function(e) {
            var self = this;
    		$.ajax({
			    url: '/uren/findSubProject/'+self.value,
			    cache: false,
		        dataType: 'text',
			    type: 'POST',
			    success: function(result) {
					result = jQuery.parseJSON(result);
					other = $('select#project-id')[0];
					other.options.length = 0;
					opt = new Option();
					opt.text = "Projectnummer";
					opt.value = "";
					other.options.add(opt)
					for(var key in result) {
						res = result[key];
						opt = new Option();
						opt.text = '('+res.projectsubnr+') '+res.projectnaam.substr(0,30) + '...' ;
						opt.value = res.projectsubnr;
						other.options.add(opt);
					}
				}
		    });
		})
	}
		
	// Tabs
	$('#locations').tabs({
		event: 'mouseover'
	});
	
	$('.tabs-wrap').tabs();
	
	// Carousels
	if($('#news-items').length) {
		$('#news-items').jcarousel({
			scroll: 1,
			animation: 'slow',
			wrap: 'both',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(carousel) {
				$('#news-item-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) {
				$('#news-item-controls li:nth-child(' + index + ')').addClass('selected');
			},
			itemVisibleOutCallback:function(carousel, slide, index, state) {
				$('#news-item-controls li:nth-child(' + index + ')').removeClass('selected');
			}
		});
	}
	
	if($('#project-items').length) {
		$('#project-items').jcarousel({
			scroll: 1,
			animation: 'slow',
			wrap: 'both',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(carousel) {
				$('#project-item-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) {
				$('#project-item-controls li:nth-child(' + index + ')').addClass('selected');
			},
			itemVisibleOutCallback:function(carousel, slide, index, state) {
				$('#project-item-controls li:nth-child(' + index + ')').removeClass('selected');
			}
		});
	}
	
	if($('#service-items').length) {
		$('#service-items').jcarousel({
			scroll: 1,
			animation: 'slow',
			wrap: 'both',
			auto: 7,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
	}
	
	// Shadowbox
	Shadowbox.init({
		overlayOpacity: 0.8
	});
	
	// Gallery
	if($('#gallery').length) {
		$('#gallery').galleriffic({
			numThumbs: 5,
			preloadAhead: 2,
			imageContainerSel: '#gallery-image',
			enableBottomPager: false
		});
	}
	
	// Validation
	$('#contact-form').validate();
	
	// Add .focus class to .input-wrapper when the containing input element is focussed
	if($('.input-wrapper input').length) {
		$('.input-wrapper input').focus(function() {
			$(this).parent().addClass('focus');
		});
		$('.input-wrapper input').blur(function() {
			$(this).parent().removeClass('focus');
		});
	}
});

