/**
 * Bootstrap.js - Bootstrap for all Javascript functionality
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */

var ytfr, interval;

jQuery.noConflict();    
jQuery(document).ready(function(){
	if (jQuery("a.colorbox, a[rel='page-gallery']").length) {
		jQuery("a.colorbox, a[rel='page-gallery']").colorbox({
			opacity: 0.8,
			current: 'Afbeelding {current} van {total}',
			previous: 'Vorige',
			next: 'Volgende',
			close: 'Sluiten',
			slideshowStart: 'Diavoorstelling starten',
			slideshowStop: 'Diavoorstelling stoppen'
		});
	}
	
	if(jQuery("a.webcallback").length) {
		jQuery("a.webcallback").colorbox({
			width: 665,
			height: 480,
			opacity: 0.8,
			iframe: true
		});
	}
});	

WS.Event.addEvent(window, 'load', function() {	
	
	// External links
	var a = document.getElementsByTagName('a');
	for(var i = 0; i < a.length; i++) {
		if(/external/.test(a[i].rel)) {
			a[i].target = '_blank';
		}
	}
	
	if($('more-brand')){
		$('more-brand').observe('click', function(event){
			var element = Event.findElement(event, 'option');			
			if(element){
				document.location.href = element.value;
			}
		});
	}
	
	if($('contacts')) {
		if($$('.contacts-item').length > 1) {
			var contactsCarousel = new Carousel($('contacts-viewport'), $$('.contacts-item'), $$('a.contacts-control'), {
				controlClassName: 'contacts-control'
			});
			contactsCarousel.start();
		}
		else {
			$$('.contacts-control').each(function(el) {
				el.addClassName('hidden');
			});
		}
	}
	
	if($('faq-accordion')) {
		var faqAccordion = new accordion('faq-accordion', {
			classNames: {
				toggle: 'accordion-toggle',
				toggleActive: 'accordion-toggle-active',
				content: 'accordion-content'
			}
		});
		faqAccordion.activate($$('#faq-accordion .accordion-toggle')[0]);
	}
	
	if($('jobs-accordion')) {
		var jobsAccordion = new accordion('jobs-accordion', {
			classNames: {
				toggle: 'accordion-toggle',
				toggleActive: 'accordion-toggle-active',
				content: 'accordion-content'
			}
		});
		//jobsAccordion.activate($$('#jobs-accordion .accordion-toggle')[0]);
	}
	
	// Homepage YouTube player
	/*if($('visualplayer')) {
		ytfr = new YTFeedReader('visualplayer', {
			videoId: 'nWfpbZIgbt0',
			autoplay: false,
			width: 470,
			height: 290,
			playStart: function(p) {
				interval = setInterval(function() {
					$('elapsed-bar').style.width = Math.round((100 * p.getCurrentTime()) / p.getDuration()) + '%';
				}, 1000);
			},
			stateChangeCallback: function(s) {
				if(s === 0) {
					WS.addClass($('visualplayer-wrap'), 'paused');
					$('elapsed-bar').style.width = '100%';
					clearInterval(interval);
				}
				if(s === 1) {
					WS.removeClass($('visualplayer-wrap'), 'paused');
				}
				else {
					WS.addClass($('visualplayer-wrap'), 'paused')
				}
			}
		});
		
		WS.Event.addEvent('toggle-play', 'click', function(e) { WS.Event.stopEvent(e); ytfr.togglePlay(); });
	}
	
	// Sidebar YouTube player
	if($('player')) {
		ytfr = new YTFeedReader('player', {
			videoId: 'crYyvH62juE',
			autoplay: false,
			width: 230,
			height: 170,
			playStart: function(p) {
				interval = setInterval(function() {
					$('elapsed-bar').style.width = Math.round((100 * p.getCurrentTime()) / p.getDuration()) + '%';
				}, 1000);
			},
			stateChangeCallback: function(s) {
				if(s === 0) {
					WS.addClass($('player-wrap'), 'paused');
					$('elapsed-bar').style.width = '100%';
					clearInterval(interval);
				}
				if(s === 1) {
					WS.removeClass($('player-wrap'), 'paused');
				}
				else {
					WS.addClass($('player-wrap'), 'paused')
				}
			}
		});
		
		WS.Event.addEvent('toggle-play', 'click', function(e) { WS.Event.stopEvent(e); ytfr.togglePlay(); });
	}*/
	
	// Tell-a-friend button/form
	if($('tellafriend-form')) {
		var tellafriendVal = new WS.Validation('tellafriend-form');
		tellafriendVal.initialize();
		
		WS.hide('tellafriend-form');
		WS.Event.addEvent('tellafriend-toggle', 'click', function() { WS.toggle('tellafriend-form'); });
	}
	
	// offerte-form
	if($('offerte-form')) {
		var offerteVal = new WS.Validation('offerte-form');
		offerteVal.initialize();
		
		WS.hide('offerte-form');
		if($('order-button')) {
			WS.Event.addEvent('order-button', 'click', function() { WS.toggle('offerte-form'); });
		}
		WS.Event.addEvent('quote-button', 'click', function() { WS.toggle('offerte-form'); });
	}	
	
	// Tell-a-friend button/form
	if($('vacancy-form')) {
		var vacancyVal = new WS.Validation('vacancy-form');
		vacancyVal.initialize();
	}
	
	// Brands ticker
	if($('brand-ticker')){
		var brandTicker = new Ticker('brand-ticker');
		setTimeout(function() { brandTicker.initialize(); }, 3000);
		
		var btItems = $('brand-ticker').getElementsByTagName('a');
		for(var i = 0; i < btItems.length; i++) {
			WS.Event.addEvent(btItems[i], 'mouseover', function() {
				WS.DOM.getChild(this, 1).src = WS.DOM.getChild(this, 1).src.replace(/\/blackandwhite\//, '/homepage/');
			});
			WS.Event.addEvent(btItems[i], 'mouseout', function() {
				WS.DOM.getChild(this, 1).src = WS.DOM.getChild(this, 1).src.replace(/\/homepage\//, '/blackandwhite/');
			});
		}
	}
	
	// Product tabs
	/*if($('product-tabs')) {
		var productTabs = new Tabs('product-tabs');
		productTabs.initialize();
	}*/
	
	// Product carousel
	if($('carousel-block')) {
		if($$('#carousel-block img').length > 1) {
			var contactsCarousel = new Carousel($('block-viewport'), $$('#block-viewport img'), $$('#carousel-block a.icon'), {
				controlClassName: 'icon'
			});
		}
		else {
			$$('#carousel-block .block-foot')[0].hide();
		}
	}	
	
	//Footer service navigation
	var serviceNav = new Toggler();
	serviceNav.initialize();
	
	//Search toggle
	if($('search')) {
		WS.Util.toggleValue('search', 'focus');
	}
});