/**
 * Bootstrap.js - Bootstrap for all Javascript functionality
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */

document.observe('dom:loaded', 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';
		}
	}
	
	// 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');
			}
		};
	}
	
	// Homepage carousels
	if($('spotlight-carousel')) {
		new Carousel('spotlight-carousel', $$('#spotlight-carousel .deal-block-item'), $$('a.carousel-control', 'a.carousel-jumper'), { auto: true, frequency: 7, circular: true });
		if($('first-carousel')){setTimeout(function() { new Carousel('first-carousel', $$('#first-carousel .deal-block-item'), $$('a.carousel-control', 'a.carousel-jumper'), { auto: true, frequency: 7, circular: true }); }, 1500)};
		if($('second-carousel')){setTimeout(function() { new Carousel('second-carousel', $$('#second-carousel .deal-block-item'), $$('a.carousel-control', 'a.carousel-jumper'), { auto: true, frequency: 7, circular: true }); }, 2500);}
		if($('third-carousel')){setTimeout(function() { new Carousel('third-carousel', $$('#third-carousel .deal-block-item'), $$('a.carousel-control', 'a.carousel-jumper'), { auto: true, frequency: 7, circular: true }); }, 3500);}
		if($('fourth-carousel')){setTimeout(function() { new Carousel('fourth-carousel', $$('#fourth-carousel .deal-block-item'), $$('a.carousel-control', 'a.carousel-jumper'), { auto: true, frequency: 7, circular: true }); }, 4500);}
	}
	
	if($('occasion-gallery')) {
		var occasionCarousel = new Carousel('gallery-image-scroller', $$('#gallery-image-scroller .slide'), $$('#gallery-navigation .carousel-control', '#gallery-navigation .carousel-jumper'), {
			duration: 0.8,
			selectedClassName: 'selected',
			beforeMove: function(previousIndex, currentIndex) { // Edited this.options.beforeMove so that it passes previousIndex and currentIndex
				var jumpers = $$('#gallery-navigation .carousel-jumper');
				jumpers[previousIndex].removeClassName('selected');
				jumpers[currentIndex].addClassName('selected');
				
				var scrollerX = $('gallery-thumbs-scroller').cumulativeOffset()[0];
				var scrollerWidth = $('gallery-thumbs-scroller').offsetWidth;
				var scrollerDelta = scrollerX + scrollerWidth;
				var jumperX = jumpers[currentIndex].cumulativeOffset()[0];
				
				if(((jumperX + 65) > scrollerDelta) || ((jumperX) < scrollerDelta)) {
					new Effect.SmoothScroll($('gallery-thumbs-scroller'), {
						duration: 0.3,
						x: (jumperX + 65) - scrollerDelta
					}); 
				}
			}
		});
	}
	
	// Contact lightwindow
	if($('contact-lightwindow')) {
		$('contact-lightwindow').observe('click', function(e) {
			Event.stop(e);
			myLightWindow.activateWindow({
				href: this.href,
				title: this.title,
				width: 600,
				height: 570,
				type: 'external'
			});
		});
	}
	
	// Tellafriend lightwindow
	if($('tellafriend-lightwindow')) {
		$('tellafriend-lightwindow').observe('click', function(e) {
			Event.stop(e);
			myLightWindow.activateWindow({
				href: this.href,
				title: this.title,
				width: 600,
				height: 570,
				type: 'external'
			});
		});
	}
	
	// Tellafriend lightwindow
	if($('car-in-mailbox')) {
		$('car-in-mailbox').observe('click', function(e) {
			Event.stop(e);
			myLightWindow.activateWindow({
				href: this.href,
				title: this.title,
				width: 600,
				height: 570,
				type: 'external'
			});
		});
	}	
	if($('save-search')) {
		$('save-search').observe('click', function(e) {
			Event.stop(e);
			myLightWindow.activateWindow({
				href: this.href,
				title: this.title,
				width: 600,
				height: 570,
				type: 'external'
			});
		});
	}		
	
	// Product-table lightwindows
	if($('bijtelling-table')) {
		$$('#bijtelling-table tr, #bijtelling-table .arrow-button').each(function(el) {
				el.observe('click', function(e){
					e.preventDefault();
					if((el.select('.arrow-button')[0])) {
					myLightWindow.activateWindow({
						href: el.select('.arrow-button')[0].href,
						title: el.select('.arrow-button')[0].title,
						width: 600,
						height: 570,
						type: 'external'
					});
					}
				});
		});
		
		/*$$('.arrow-button').each(function(el) {
			el.observe('click', function(e) {
				Event.stop(e);
			})
		});*/
	}
	
	
	// Demo's table
	if($('demos-table')) {
		$$('#demos-table tr, #demos-table .arrow-button').each(function(el) {
				el.observe('click', function(e){
					e.preventDefault();
					if((el.select('.arrow-button')[0])) {
						window.location.href=el.select('.arrow-button')[0].href;
					}
				});
		});
	}
	
	
	// Vestiging kiezen
	if($('establishment-toggle')) {
		WS.Event.addEvent($('establishment-toggle'), 'click', function(e) {
			WS.Event.stopEvent(e);
			WS.hide(this);
			WS.show($('establishment-select'));
		});
	}
	
	if($('establishment-select')) {
		WS.hide($('establishment-select'));
		WS.Event.addEvent($('establishment-select'), 'change', function() {
			establishmentChanged(this);
		});
	}
	
	// Tabs
	var tabElements=WS.DOM.getElementsByClass('tabs');tabElements.forEach(function(el){var tabs=new Tabs(el.id);tabs.initialize();});
	
	// Lightwindow contact form validation
	var contactVal = new WS.Validation('contact-form');
	contactVal.initialize();
	
	// Accordions
	var acc, accOptions = { classNames: { toggle: 'accordion-toggle', toggleActive: 'accordion-toggle-active', content: 'accordion-content' } };
	var pref = 0;
	$$('.accordion-block').each(function(el) {
		acc = new accordion(el.id, accOptions);
		if(getCookie('trace_merk') == "volkswagen"){
			pref = 0;
		}else if(getCookie('trace_merk') == "audi"){
			pref = 1;
		}else if(getCookie('trace_merk') == "seat"){
			pref = 2;
		}else if(getCookie('trace_merk') == "skoda"){
			pref = 3;
		}else if(getCookie('trace_merk') == "vwbedrijfswagens"){
			pref = 4;
		}else{
			pref = 0;
		}
		acc.activate(el.select('.accordion-toggle')[pref]);
	});
	
	var acc2, acc2Options = { classNames: { toggle: 'clip-toggle', toggleActive: 'clip-toggle-active', content: 'clip-content' } };
	$$('.clip-block').each(function(el) {
		acc2 = new accordion(el.id, acc2Options);
		acc2.activate(el.select('.clip-toggle')[0]);
	});
	
	// Alternatives lightwindow + submit
	if($('alternatives-submit')) {
		$('alternatives-submit').observe('click', function(e) {
			Event.stop(e);
			
			var alternativeCheckBoxes = document.getElementsByName('occasion[]');
			var values = [];
			
			for(var i = 0; i < alternativeCheckBoxes.length; i++) {
				if(alternativeCheckBoxes[i].checked) {
					values.push(alternativeCheckBoxes[i].value.split('-')[1]);
				}
			}
			
			myLightWindow.activateWindow({
				href: this.href + '?alternatives=' + values,
				title: this.title,
				width: 600,
				height: 300,
				type: 'external'
			});
		});
	}
});


/**
 * Establishment changed
 */
function establishmentChanged(el) {
	var postBody = 'vestiging=' + el.value;
	
	WS.hide(el);
	WS.show($('establishment-toggle'));
	
	$('establishment-city').innerHTML = el.options[el.selectedIndex].text;
	
	WS.Ajax.request('http://www.huiskes-kokkeler.nl/switch_establishment/', function(response) {
		$('establishment-details').innerHTML = response;
	}, postBody);
}

// IE6 hover fix
if(document.getElementById('navigation') && window.attachEvent) window.attachEvent('onload', function() {
	var sfEls = document.getElementById('navigation').getElementsByTagName('li');
	for(var i = 0; i < sfEls.length; i++) {
		sfEls[i].onmouseover = function() {
			this.className += ' sfhover';
		}
		sfEls[i].onmouseout = function() {
			this.className = this.className.replace(new RegExp(' sfhover\\b'), '');
		}
	}
});

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
