/**
 * Bootstrap.js - Bootstrap for all Javascript functionality
 * 
 * @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');
			});
		});
	}
	
	// Accordions
	$('.accordion').each(function() {
		new Accordion(this);
	});

	// Spotlight carousel
	if($.browser.msie && $.browser.version=="6.0") {	
	}
	else {
		if($('#spotlight-items').length) {
			$('#spotlight-items').jcarousel({
				scroll: 1,
				animation: 'slow',
				auto: 4,
				wrap: 'both',
				buttonNextHTML: null,
				buttonPrevHTML: null,
				initCallback: function(carousel) {
					$('#spotlight-next').bind('click', function(e) {
					    carousel.next();
					    e.preventDefault();
					});

					$('#spotlight-prev').bind('click', function(e) {
					    carousel.prev();
					    e.preventDefault();
					});
				
					$('#spotlight-controls li').each(function(i) {
						$(this).bind('click', function(e) {
							e.preventDefault();
							carousel.stopAuto();
							carousel.scroll(i + 1);
							carousel.startAuto();
						});
					});
					
					$('#spotlight-jumper 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(instance, element, index) {
					$('#spotlight-jumper li').removeClass('selected');
					$('#spotlight-jumper li').eq(index - 1).addClass('selected');
				}
			});		
		}
	}
	
	// Brands Ticker
	if($('#brands-ticker').length > 0) {
		var brandTicker = new Ticker('brands-ticker');
		setTimeout(function() { brandTicker.initialize(); }, 2000);	
	}
	
	/*if($.browser.msie && $.browser.version=="6.0") {	
	} else {
		if($('#brands-ticker-items').length) {
			$('#brands-ticker-items').jcarousel({
		    	wrap: 'both',
				auto: 3,
				scroll: 4,
				animation: 1000,
				visible: null,
				buttonNextHTML: null,
				buttonPrevHTML: null,
				initCallback: function(carousel) {
					$('#brands-ticker-next').bind('click', function(e) {
					    carousel.next();
					    e.preventDefault();
					});

					$('#brands-ticker-prev').bind('click', function(e) {
					    carousel.prev();
					    e.preventDefault();
					});
			
					carousel.clip.hover(
						function() {
							carousel.stopAuto();
						},
						function() {
							carousel.startAuto();
						}
					);
				}
		    });
		};
	}*/
	

			
	//Footer tabs
	$("#footer .tabs").idTabs("tabs-footer"); 

	$("#footer .tabs").idTabs(function(id,list,set){ 
		$("a",set).removeClass("selected") 
		.filter("[@href='"+id+"']",set).addClass("selected"); 
		for(i in list) 
		$(list[i]).hide(); 
		$(id).fadeIn(); 
		return false; 
	 });
	
	
	//Remove collapsed state after first button click	
	$("#footer .tabs li a").click(function(e){
	    $("#footer .tabs li").removeClass("collapsed");
		window.location.href="#footer";
		e.preventDefault();
	});
	

	//Locations tabs
	$("#locations .tabs").idTabs("tabs-locations"); 

	$("#locations .tabs").idTabs(function(id,list,set){ 
		$("a",set).removeClass("selected") 
		.filter("[@href='"+id+"']",set).addClass("selected"); 
		for(i in list) 
		$(list[i]).hide(); 
		$(id).fadeIn(); 
		return false; 
	 });	

	if( !!Shadowbox ) {
		Shadowbox.init();
	}
	
	// Clickable table rows	
	rowClick();
	rowHover();
	
	// Initiate Cufon
	Cufon.now();
});

//cufon regular
Cufon.replace('#main .overview .product-category h2 a', { 
	fontFamily: 'DIN-Engschrift',
	fontWeight: 400,
	hover: true
});

//cufon regular
Cufon.replace('#main h1, #main h2, #news-date', { 
	fontFamily: 'DIN-Engschrift',
	fontWeight: 400,
	hover: false
});

	/**
	 * Make table rows clickable
	 */
	function rowClick(table)
	{
		$('.tclick tr').each(function()
		{
			var anchor = $('a:first-child', this);
			if (!!anchor.length) {
				switch (anchor.attr('rel')) {
				
					default:
						$(this).click(function()
						{
	                        window.location.href = anchor.attr('href');
	                    });
						break;
						
					case 'external':
						$(this).click(function()
						{
							window.open(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');
					}
				);
			}
		});
	};
	
