/**
 * Bootstrap.js - Bootstrap for all Javascript functionality
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */


jQuery.noConflict();
jQuery(document).ready(function(){
	if($$('.datepicker')){
		jQuery('.datepicker').datepicker({
			'dateFormat'		: 	'dd-mm-yy',
			'changeMonth'		: 	true,
			'changeYear'		: 	true,
			'yearRange'			:	'1900:2020',
			'dayNames'			: 	['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
			'dayNamesMin'		:	['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
			'dayNamesShort'		:	['Zon', 'Maa', 'Din', 'Woe', 'Don', 'Vri', 'Zat'],
			'monthNames'		:	['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
			'monthNamesShort' 	:	['Jan', 'Feb', 'Maa', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec']
		});
	}
	
	// Datepickers
	var dpp = new DatePickerProxy('#calendar .calendar-ui-wrap', '/calendars/homepage');
	
	if($('.autocomplete')){
		jQuery('.autocomplete').autocomplete({
			source: '/extranet/zorgaanbieders',
			select: function(event, ui) {
				jQuery('#big-input').val(ui.item.label);
				jQuery('#big-search').submit();
			}
		});
	}
	
	jQuery('#quicklinks a').tooltip({ 
	    delay: 5, 
	    showURL: false, 
	    bodyHandler: function() {
			var title = jQuery(this).text().toUpperCase();
			var ul = jQuery(this).next().clone().show();
			return '<h4>' + title + '</h4><ul class="quicklinks-hover-list">' + ul.html() + '</ul>';
		}
	});
	
});

WS.DOM.ready(function() {
	
	if($('font-sizer')) {
		var fs = new FontSizer('font-sizer', ['main']);
		WS.Event.addEvent('fontsmall', 'click', function(e) { WS.Event.stopEvent(e); fs.setFontSize(this) });
		WS.Event.addEvent('fontmedium', 'click', function(e) { WS.Event.stopEvent(e); fs.setFontSize(this) });
		WS.Event.addEvent('fontlarge', 'click', function(e) { WS.Event.stopEvent(e); fs.setFontSize(this) });
	}
	
	// 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';
		}
	}
	
	// Toggle input values
	var inputs = WS.DOM.getElementsByClass('toggle-value');
	for(var j = 0; j < inputs.length; j++) {
		var v = inputs[j].value;
		inputs[j].onfocus = function() {
			if(this.value == v) {
				this.value = '';
			}
			WS.addClass(this, 'focus');
		};
		inputs[j].onblur = function() {
			if(this.value == '') {
				this.value = v;
				WS.removeClass(this, 'focus');
			}
		};
	}
	
	// Cart visual price update
	if($('cart-products')) {
		var id = 0;
		var lastamount = 0;
		WS.Event.addEvent($('cart-products'), 'click', function(e) {
			var clicked = WS.Event.getTarget(e);
			var split = clicked.id.split('-');
			if(split[1] == 'add'){
				var amount = $('amount-' + split[2]);
				var price = $('price-' + split[2]);
				var subtotal = $('subtotal-' + split[2]);

				var newvalue = parseInt(amount.value);
				
				subtotal.value = (newvalue * price.value);
				var showprice = $('show-price-' + split[2]);
				showprice.innerHTML = '&euro; ' + WS.Util.number_format(subtotal.value, 2, ',', '.');

				var subtotalvalue = $('subtotal');
				subtotalvalue.value = parseFloat(subtotalvalue.value) + parseFloat(price.value);
				
				var showsubtotal = $('showsubtotal');
				showsubtotal.innerHTML = '&euro; ' + WS.Util.number_format(subtotalvalue.value, 2, ',', '.');
				
				var totalvalue = $('total');
				totalvalue.value = parseFloat(totalvalue.value) + parseFloat(price.value);
				var showtotal = $('showtotal');
				showtotal.innerHTML = '&euro; ' + WS.Util.number_format(totalvalue.value, 2, ',', '.');
				
				id = split[2];
				lastamount = newvalue;				
				
			}else if(split[1] == 'subtract'){
				var amount = $('amount-' + split[2]);
				var price = $('price-' + split[2]);
				var subtotal = $('subtotal-' + split[2]);
				
				var newvalue = amount.value;
				var pass = false;
				
				if(newvalue > 1){
					pass = true;
				}
				
				if(id == split[2] && lastamount - 1 > 0){
					pass = true;
				}
					
				if(pass){				
					var subtotalvalue = $('subtotal');
					subtotalvalue.value -= price.value;
					
					var showsubtotal = $('showsubtotal');
					showsubtotal.innerHTML = '&euro; ' + WS.Util.number_format(subtotalvalue.value, 2, ',', '.');
					
					var totalvalue = $('total');
					totalvalue.value -= price.value;
					var showtotal = $('showtotal');
					showtotal.innerHTML = '&euro; ' + WS.Util.number_format(totalvalue.value, 2, ',', '.');
				}			
	
				subtotal.value = (newvalue * price.value);
				var showprice = $('show-price-' + split[2]);
				showprice.innerHTML = '&euro; ' + WS.Util.number_format(subtotal.value, 2, ',', '.');
				
				id = split[2];
				lastamount = newvalue;			
			}	
		});
	}
	
	// Region block mouseover
	if($('region-map')) {
		var areas = WS.DOM.getElementsByClass('region-area');
		for(var i = 0; i < areas.length; i++) {
			WS.Event.addEvent(areas[i], 'mouseover', function() { $('region-map').src = '/img/region-map-' + this.id.substring(12) + '.png'; });
			WS.Event.addEvent(areas[i], 'mouseout', function() { $('region-map').src = '/img/region-map.png'; });
		}
	}
	
	// News agenda tabs
	var naTabs = new Tabs(WS.DOM.getElementsByClass('tabs', $('news-agenda-block'), 'ul')[0]);
	naTabs.initialize();
	
	// Accordions
	if(WS.DOM.getElementsByClass('accordion').length) {
		var acc, accOptions = { classNames: { toggle: 'accordion-toggle', toggleActive: 'accordion-toggle-active', content: 'accordion-content' } };
		
		$$('.accordion').each(function(el) {
			var activateEl = el.select('.accordion-toggle')[0];
			
			if(window.location.hash != '' && $(window.location.hash.substring(1))) {
				activateEl = $(window.location.hash.substring(1)).previous('.accordion-toggle');
			}
			
			acc = new accordion(el.id, accOptions);
			acc.activate(activateEl);
		});
	}
	
	// Toggle blocks
	if(WS.DOM.getElementsByClass('toggle-block').length) {
		var tb, tbOptions = { classNames: { toggle: 'toggle-block-toggle', toggleActive: 'toggle-block-toggle-active', content: 'toggle-block-content' } };
		
		$$('.toggle-block').each(function(el) {
			var activateEl = null;
			
			if(window.location.hash != '' && $(window.location.hash.substring(1))) {
				activateEl = $(window.location.hash.substring(1)).previous('.toggle-block-toggle');
			}
			
			tb = new accordion(el.id, tbOptions);
			if(activateEl) {
				tb.activate(activateEl);
			}
		});
	}
	
	// Carousels
	if($('album-carousel')) {
		var albumCarousel = new Carousel('album-carousel', $$('#album-carousel .album-carousel-item'), $$('a.carousel-control', 'a.carousel-jumper'), {
			auto: true,
			frequency: 5,
			circular: true
		});
	}
	
	if($('theme-menu')) {
		var menuTimeout;
		
		$$('#menu-care, #menu-living, #menu-identity, #menu-income').each(function(el) {
			if(!el.hasClassName('selected')) {
				var submenu = el.down('.submenu').hide();
				el.observe('mouseover', function() {
					menuTimeout = setTimeout(function() {
						if(!this.hasClassName('menu-active')) {
							Effect.BlindDown(submenu, { duration: 0.25 });
						}
						el.addClassName('menu-active');
					}.bind(this), 100);
				});
				el.observe('mouseout', function() {
					clearTimeout(menuTimeout);
				});
			}
		});
	}
	
	//initialize checkout-form
	if($('checkout-form')) {
		var loginFormVal = new WS.Validation('checkout-form');
		loginFormVal.initialize();	
	};
	
	// Photo album
	if($('album-image')) {
		var interval, playing = false;
		var album = new PhotoAlbum('album-image', $('album-images').getElementsByTagName('a'), { initial: 0 });
		WS.Event.addEvent('album-prev', 'click', function(e) { WS.Event.stopEvent(e); album.prev(); });
		WS.Event.addEvent('album-next', 'click', function(e) { WS.Event.stopEvent(e); album.next(); });
		//WS.Event.addEvent(document, 'keydown', function(e) { WS.Event.stopEvent(e); if(e.keyCode == 37) album.prev(); else if(e.keyCode == 39) album.next(); });
		WS.Event.addEvent('slideshow-toggle', 'click', function(e) {
			WS.Event.stopEvent(e);
			if(playing) {
				clearInterval(interval);
				WS.removeClass('lightwindow-wrapper', 'playing');
				playing = false;
			}
			else {
				interval = setInterval(album.next, 5000);
				WS.addClass('lightwindow-wrapper', 'playing');
				playing = true;
			}
		});
	}
	
	// Album Lightwindows
	var alw = WS.DOM.getElementsByClass('album-lightwindow');
	for(var i = 0; i < alw.length; i++) {
		WS.Event.addEvent(alw[i], 'click', function(e) {
			WS.Event.stopEvent(e);
			myLightWindow.activateWindow({
				href: this.href,
				title: this.title,
				width: 800,
				height: 520,
				type: 'external'
			});
		});
	}
	
	// Form validation
	var questionVal = new WS.Validation('question-form');
	questionVal.initialize();
	
	var questionVal = new WS.Validation('register-form');
	questionVal.initialize();
	
	// Profile form
	var i = 0;
	$$('#profile-form a.toggler').each(function(el) {
		if(i == 0){
			$(el.href.split('#')[1]).addClassName('expanded');	
		}else{
			$(el.href.split('#')[1]).addClassName('collapsed');
		}
		
		el.observe('click', function(e) {
			Event.stop(e);
			
			var toggleEl = $(el.href.split('#')[1]);
			
			if(toggleEl.hasClassName('collapsed')) {
				toggleEl.removeClassName('collapsed');
				toggleEl.addClassName('expanded');
			}
			else if(toggleEl.hasClassName('expanded')) {
				toggleEl.removeClassName('expanded');
				toggleEl.addClassName('collapsed');
			}
		});
		i++;
	});
	
	if($('disability-self')){
		$$('.disability-self-sub:not(.expanded), .disability-other-sub:not(.expanded)').each(function(el) {
			el.hide();
		});
		
		$('disability-self').observe('click', function(e) {
			if(this.checked) {
				$$('.disability-self-sub').invoke('show');
			}
			else {
				$$('.disability-self-sub').invoke('hide');
			}
		});
	
		if($('disability-other')){
			$('disability-other').observe('click', function(e) {
				if(this.checked) {
					$$('.disability-other-sub').invoke('show');
				}
				else {
					$$('.disability-other-sub').invoke('hide');
				}
			});
		}
	}
});
