/**
 * 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 erg 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');
	});
	
	// Togglers
	$('.accordion').each(function() {
		switch(this.id) {
			default:
				new Toggler(this);
				break;
		}
	});
	
	// Product tabs
	if($('#product-tabs').length > 0) {
		var tabs = $('#product-tabs').tabs({
			show: function(e) {
				if($('#cart-small').length > 0) {
					smallCartScroller.initialize('#cart-small');		
				}
			}
		});
		$('#button-compile').click(function() {		
			tabs.tabs('select', 1);
			return false;
		});
	}
	
	// Gallery
	if($('.gallery').length > 0) {
		if($('product-details').length > 0) {
			var gallery = new Gallery('.gallery');
		} else {
			var gallery = new Gallery('.gallery', {
				onAfterItemChange: function(el, index) {
					$('.gallery .gallery-image-zoom').attr('href', $(el).attr('rel'));					
					Shadowbox.setup('.gallery .gallery-image-zoom');
				}
			});
		}
	}
	
	// Print button
	if($('.button-print').length > 0) {
		$('.button-print').click(function() {
			print();
		});
	}
	
	// Homepage ticker
	if($('#brand-ticker').length > 0) {
		var brandTicker = new Ticker('brand-ticker');
		setTimeout(function() { brandTicker.initialize(); }, 3000);	
	}
	
	// Validation
	$('form').each(function(i, el) {
		$(el).validate();
	});
	
	//other address box 
	if($('input[name=delivery-address]').length > 0) {
		if($('#other-address').attr('checked')) {
			$('#delivery-address-block').show();
		} else {		
			$('#delivery-address-block').hide();
			$('#delivery-address-block input').attr('disabled', true);
			$('input[name=delivery-address]').change(function(e) {
				if($('#other-address').attr('checked') === true) {
					$('#delivery-address-block').show();
					$('#delivery-address-block input').attr('disabled', false);
				} else {
					$('#delivery-address-block').hide();
					$('#delivery-address-block input').attr('disabled', true);
				}
			});
		}
	}
	
	// Shadowbox
	Shadowbox.init({
		overlayOpacity: 0.8,
		troubleElements: ['select']
	});
	
	// Scroll small cart
	if($('#cart-small').length > 0) {
		smallCartScroller.initialize('#cart-small');		
	}
	
	// Product configurator
	var pc = new Configurator('#product-compile');
	
	// Cart
	var c = new Cart('#cart');
	
	// Product quick search
	$('#product-quick-search select').change(function() {
		var self = this;
		
		$.ajax({
			url: self.form.action,
			cache: false,
			dataType: 'text',
			type: 'POST',
			data: $(self.form).serialize(),
			success: function(result) {
				$('#product-quick-search .result').text(result + ' resultaten');
			}
		});
	});
	
	// Product overview filter
	$('#product-type').change(function() {
		var self = this;
		
		$.ajax({
			url: self.form.action,
			cache: false,
			dataType: 'text',
			type: 'POST',
			data: $(self.form).serialize(),
			success: function(result) {				
				$('#product-overview').html(result);
				$('#filter-bar .result-count').text($('#product-overview li').length + ' resultaten');
			}
		});
	});
});

var smallCartScroller = {
	initialize: function(selector) {		
		var initialOffset = $(selector).position().top;
		$(window).scroll(function() {			
			if($(window).scrollTop() > initialOffset) {
				$(selector).css('position', 'fixed');
			} else {
				$(selector).css('position', 'static');
			}
		});
	}
};

