/**
 * 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();
	
	// Messages
	MessageBar.init();
	
	// 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
	$(document.body).ajaxStart(function() {
		$(this).addClass('loading');
	}).ajaxComplete(function() {
		$(this).removeClass('loading');
	});
	
	// Carousels
	/*if($('#spotlight-visuals').length) {
		$('#spotlight-visuals').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: {
				onBeforeAnimation: function(carousel, slide, index, state) {
					$('#spotlight-controls li:nth-child(' + index + ')').addClass('selected');
				}
			},
			itemVisibleOutCallback: {
				onBeforeAnimation: function(carousel, slide, index, state) {
					$('#spotlight-controls li:nth-child(' + index + ')').removeClass('selected');
				}
			}
		});
	}*/
	
	// Togglers
	$('.accordion').each(function() {
		switch(this.id) {
			case 'contact-accordion':
				new Toggler(this, {allowMultiple: false}).expand($(this).find('li:first-child'));
				break;
			default:
				new Toggler(this);
				break;
		}
	});
	
	// Validation
	$('form').each(function() {
		$(this).validate();
	});

	// Model preview
	if($('#models').length > 0) {		
		modelPreview.initialize();
	}

	// Gallery
	if($('.gallery').length > 0) {
	    var gallery = new Gallery('.gallery');
	}
	
	// Shadowbox
	Shadowbox.init({
		overlayOpacity: 0.8,
		troubleElements: ['select']
	});

	// Model scroller
	if($('.brand-model-scroller-content').length > 0) {
		for(var i = 0; i < $('.brand-model-scroller-content').length; i++) {
			var modelScrollPanel = new HScrollPanel($('.brand-model-scroller-content').eq(i), $('.brand-model-slider-track').eq(i));
		}
	}

	// Tabs
	if($('#brand-cars').length > 0) {
		$('#brand-cars').tabs();
	}

	if($('#model-details').length > 0) {
		$('#model-details').tabs();
	}

	if($('#forms').length > 0) {
		$('#forms').tabs();
	}

	// Dealer selector
	if($('.dealer-select').length > 0) {
		$('.dealer-select').parent().find('.contact, .business-hours').hide();

		$($('#dealer-select-contact').val()).show();
		$($('#dealer-select-business-hours').val()).show();

		$('#dealer-select-contact').change(function() {
			$('#dealer-select-business-hours option').eq($(this).attr('selectedIndex')).attr('selected', 'selected');
			$(this).parent().find('.contact').hide();
			$($('#dealer-select-business-hours')).parent().find('.business-hours').hide();
			$($(this).val()).show();
			$($('#dealer-select-business-hours').val()).show();
		});

		$('#dealer-select-business-hours').change(function() {
			$('#dealer-select-contact option').eq($(this).attr('selectedIndex')).attr('selected', 'selected');
			$(this).parent().find('.business-hours').hide();
			$($('#dealer-select-contact')).parent().find('.contact').hide();
			$($(this).val()).show();
			$($('#dealer-select-contact').val()).show();
		});
	}

	// Department selector
	if($('#department-select').length > 0) {
		$('#department-select').parent().parent().find('.department').hide();
		$($('#department-select').val()).show();
		$('#department-select').change(function() {
			$(this).parent().parent().find('.department').hide();
			$($(this).val()).show();
		});
	}

	// Ticker
	if($('#turbo-deals-ticker li').length > 0) {
		$('#turbo-deals-ticker li').ticker();
	}

	// Dealer-phone-numbers
	if($('#dealer-phone-numbers .dealers a').length > 0) {
		var $dealerPhoneNumbers = $('#dealer-phone-numbers .phone-numbers p');
		var $dealerPhoneNumberLinks = $('#dealer-phone-numbers .dealers a');
		$dealerPhoneNumberLinks.each(function() {
			var $item = $(this);
			var $targetItem = $('#'+$item.attr('href').split('#')[1]);

			$item.click(function(e) {
				e.preventDefault();
				$dealerPhoneNumbers.hide();
				$targetItem.show();
			});
		});
		$dealerPhoneNumbers.hide();
		$dealerPhoneNumbers.eq(0).show();
	}

	// Follow spotlight controls link
	if($('#spotlight-controls li').length > 0) {
		$('#spotlight-controls li').click(function() {
			if($(this).hasClass('selected')) {
				window.location.href = $(this).find('a').attr('rel');
			}
		});
	}

});
