/**
 * A class that makes fetching additional items easy
 *
 * @since Tue Jun 01 2010
 * @author Giso Stallenberg
 * @package 
 **/
var fetch = Class.create({
	/**
	 * initialize
	 *
	 * Creates a new fetch
	 *
	 * @since Tue Jun 01 2010
	 * @access public
	 * @param string module
	 * @param string button
	 * @param integer count
	 * @param integer total
	 * @param integer offset
	 * @return fetch
	 **/
	initialize: function(module, button, count, total, offset, additional) {
		if (count == 0) {
			return; // no fetching needed
		}
		this._button = $(button);
		if (!this._button) {
			return;
		}
		this._started = true;
		this._url = new WJUrl();
		this._url.setCt("wmdynamic");
		this._url.setDt("moreitems");
		this._url.addParameter("module", module);
		this._url.addParameter("wmtrigger[]", "requestufts");
		this._url.addParameter("uft[]", "moreitems");
		this._url.addParameter("__type", "");
		for (key in additional) {
			this._url.addParameter(key, additional[key] );
		}
		this._spin = new WJSpin();
		
		this.onload(count, total, offset);
	},

	/**
	 * _handleElement
	 *
	 * Hides or shows the element and adds an observer
	 *
	 * @since Tue Jun 01 2010
	 * @access protected
	 * @return void
	 **/
	_handleElement: function() {
		if (this._count + this._offset >= this._total) {
			this._button.hide();
		}
		else if (this._started) {
			this._button.show();
			this._button.previous(".fetchmore_washingline").ajaxUpdateType = "appendChild";
			this._button.observe("click", this.load.bindAsEventListener(this) );
			this._started = false;
		}
	},

	/**
	 * load
	 *
	 * Loads additional items
	 *
	 * @since Tue Jun 01 2010
	 * @access public
	 * @param Event event
	 * @return void
	 **/
	load: function(event) {
		this._url.addParameter("count", this._count);
		this._url.addParameter("offset", this._offset+this._count);
		this._spin.content(this._url, [this._button.previous("div") ]);
	},

	/**
	 * onload
	 *
	 * Should be called when new items are loaded
	 *
	 * @since Tue Jun 01 2010
	 * @access public
	 * @param integer count
	 * @param integer total
	 * @param integer offset
	 * @return void
	 **/
	onload: function(count, total, offset) {
		this._count = count;
		this._total = total;
		this._offset = offset || 0;
		this._handleElement();
	}
});
/**
 * A class that makes a contentblocks collection slidable
 *
 * @since Mon Nov 08 2010
 * @author Nick van der Zwet
 * @package
 **/
var CvdCarousel = Class.create({
	initialize: function() {
	},

	/**
	 * initCarouselMenu
	 *
	 * Init's the carousel menu
	 *
	 * @since Wed Nov 10 2010
	 * @access public
	 * @return void
	 **/
	initCarouselMenu: function() {
		Effect.toggle('carousel-jumpers', 'Slide', {duration: 0.75, delay: 2});
		$('carousel-wrapper').observe("mouseenter", function() {
			Effect.toggle('carousel-jumpers', 'Slide',{duration: 0.75});
		});
		$('carousel-wrapper').observe("mouseleave", function() {
			Effect.toggle('carousel-jumpers', 'Slide', {duration: 0.75, delay: 0.25});
		});
	},

	/**
	 * initCarousel
	 *
	 * Init's a carousel
	 *
	 * @since Fri May 28 2010
	 * @access public
	 * @param string container
	 * @return void
	 **/
	initCarousel: function(container) {
		var carouselcontent = $(container);
		var container = $(container).up(".carousel");
		var carouselslides = container.select(".carousel-slide");
		var firstslide = carouselslides.first().addClassName("carousel-slide-active");
		var lastslide = carouselcontent.appendChild(firstslide.cloneNode(true) );
		carouselslides.push(lastslide);
		lastslide.id = null;
		lastslide.select("*[id]").each(function(el) {
			el.id = null;
		} );
		
		var options = {};
		if (container.hasAttribute("rel") ) {
			options = container.readAttribute("rel").evalJSON();
		}
		
		carouselcontent.style.width = ( (carouselslides.size() * carouselslides.first().getDimensions().width) + 10) + "px";

		var controls = container.select("a.carousel-control");
		
		if (container.select("a.carousel-jumper").size() > 0) {
			var jumpers = container.select("a.carousel-jumper");
			jumpers.first().addClassName("carousel-jumper-active");
			options["selectedClassName"] = "carousel-jumper-active";
			controls = (controls.size() > 0) ? controls.concat(jumpers) : jumpers;
		}

		var slidableid = container.down(".carousel-content").identify();
		var carousel = new Carousel(slidableid, carouselslides, controls, options);

		var jumperbuttons = container.down(".carousel-jumpers");
		if (jumperbuttons) {
			jumperbuttons.setStyle( {
				visibility: "visible"
			} );

			carousel.options.afterMove = function(container, jumpers, carouselslides) {
				var id = container.identify();
				jumpers.invoke("removeClassName", this.options.selectedClassName);
				carouselslides.invoke("removeClassName", "carousel-slide-active");
				var index = this.current._index;
				var cindex = (typeof(jumpers[index] ) == "undefined") ? 0 : index;
				jumpers[cindex].addClassName(this.options.selectedClassName);
				this.current.addClassName("carousel-slide-active");
			}.bind(carousel, container, jumpers, carouselslides);
		}
		this.carousel = carousel;
	}
});
var cvdcarousel = new CvdCarousel();
/*
Copyright (c) 2009 Victor Stanciu - http://www.victorstanciu.ro

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

Carousel = Class.create(Abstract, {
	initialize: function (scroller, slides, controls, options) {
		this.scrolling	= false;
		this.scroller	= $(scroller);
		this.slides		= slides;
		this.controls	= controls;

		this.options    = Object.extend({
            duration:           1,
            auto:               false,
            frequency:          3,
            visibleSlides:      1,
            controlClassName:   'carousel-control',
            jumperClassName:    'carousel-jumper',
            disabledClassName:  'carousel-disabled',
            selectedClassName:  'carousel-selected',
            circular:           false,
            wheel:              true,
            effect:             'scroll',
            transition:         'sinoidal'
        }, options || {});
        
        if (this.options.effect == 'fade') {
            this.options.circular = true;
        }

		this.slides.each(function(slide, index) {
			slide._index = index;
        });

		if (this.controls) {
            this.controls.invoke('observe', 'click', this.click.bind(this));
        }
        
        if (this.options.wheel) {            
            this.scroller.observe('mousewheel', this.wheel.bindAsEventListener(this)).observe('DOMMouseScroll', this.wheel.bindAsEventListener(this));;
        }

        if (this.options.auto) {
            this.start();
        }

		if (this.options.initial) {
			var initialIndex = this.slides.indexOf($(this.options.initial));
			if (initialIndex > (this.options.visibleSlides - 1) && this.options.visibleSlides > 1) {               
				if (initialIndex > this.slides.length - (this.options.visibleSlides + 1)) {
					initialIndex = this.slides.length - this.options.visibleSlides;
				}
			}
            this.moveTo(this.slides[initialIndex]);
		}

		this.scroller.observe("mouseenter", function() {
			if (this.timer != null) {
				clearTimeout(this.timer);
			}
		}.bind(this) );
		this.scroller.observe("mouseleave", function() {
			this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 1000);
		}.bind(this) );
	},

	click: function (event) {
		var element = event.findElement('a');

		if (!element.hasClassName(this.options.disabledClassName)) {
			if (element.hasClassName(this.options.controlClassName)) {
				eval("this." + element.rel + "()");
			}
		}
		this.deactivateControls();
	},

	moveTo: function (element) {
		if (this.options.beforeMove && (typeof this.options.beforeMove == 'function')) {
			this.options.beforeMove();
        }

		this.previous = this.current ? this.current : this.slides[0];
		this.current  = $(element);

		var scrollerOffset = this.scroller.cumulativeOffset();
		var elementOffset  = this.current.cumulativeOffset();

		if (this.scrolling) {
			this.scrolling.cancel();
		}

        switch (this.options.effect) {
            case 'fade':               
                this.scrolling = new Effect.Opacity(this.scroller, {
                    from:   1.0,
                    to:     0,
                    duration: this.options.duration,
                    afterFinish: (function () {
                        this.scroller.scrollLeft = elementOffset[0] - scrollerOffset[0];
                        this.scroller.scrollTop  = elementOffset[1] - scrollerOffset[1];

                        new Effect.Opacity(this.scroller, {
                            from: 0,
                            to: 1.0,
                            duration: this.options.duration,
                            afterFinish: (function () {
                                if (this.controls) {
                                    this.activateControls();
                                }
                                if (this.options.afterMove && (typeof this.options.afterMove == 'function')) {
                                    this.options.afterMove();
                                }
                            }).bind(this)
                        });
                    }
                ).bind(this)});
            break;
            case 'scroll':
            default:
                var transition;
                switch (this.options.transition) {
                    case 'spring':
                        transition = Effect.Transitions.spring;
                        break;
                    case 'sinoidal':
                    default:
                        transition = Effect.Transitions.sinoidal;
                        break;
                }

                this.scrolling = new Effect.SmoothScroll(this.scroller, {
                    duration: this.options.duration,
                    x: (elementOffset[0] - scrollerOffset[0]),
                    y: (elementOffset[1] - scrollerOffset[1]),
                    transition: transition,
                    afterFinish: (function () {
                        if (this.controls) {
                            this.activateControls();
                        }
                        if (this.options.afterMove && (typeof this.options.afterMove == 'function')) {
                            this.options.afterMove();
                        }                        
                        this.scrolling = false;
                    }).bind(this)});
            break;
        }

		return false;
	},

	prev: function () {
		if (this.timer != null) {
			clearTimeout(this.timer);
		}
		if (this.current) {
			var currentIndex = this.current._index;
			var prevIndex = (currentIndex == 0) ? (this.options.circular ? this.slides.length - 1 : 0) : currentIndex - 1;
        } else {
            var prevIndex = (this.options.circular ? this.slides.length - 1 : 0);
        }

		if (prevIndex == (this.slides.length - 1) && this.options.circular && this.options.effect != 'fade') {
			this.scroller.scrollLeft =  (this.slides.length - 1) * this.slides.first().getWidth();
		//	this.scroller.scrollTop =  (this.slides.length - 1) * this.slides.first().getHeight();
			prevIndex = this.slides.length - 2;
        }

		this.moveTo(this.slides[prevIndex]);
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 1000);
	},

	next: function () {
		if (this.timer != null) {
			clearTimeout(this.timer);
		}
		if (this.current) {
			var currentIndex = this.current._index;
			var nextIndex = (this.slides.length - 1 == currentIndex) ? (this.options.circular ? 0 : currentIndex) : currentIndex + 1;
        } else {
            var nextIndex = 1;
        }

		if (nextIndex == 0 && this.options.circular && this.options.effect != 'fade') {
			this.scroller.scrollLeft = 0;
			this.scroller.scrollTop  = 0;
			nextIndex = 1;
        }

		if (nextIndex > this.slides.length - (this.options.visibleSlides + 1)) {
			nextIndex = this.slides.length - this.options.visibleSlides;
		}		

		this.moveTo(this.slides[nextIndex]);
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 1000);
	},

	first: function () {
		this.moveTo(this.slides[0]);
    },

	last: function () {
		this.moveTo(this.slides[this.slides.length - 1]);
    },

	toggle: function () {
		if (this.previous) {
			this.moveTo(this.slides[this.previous._index]);
        } else {
            return false;
        }
    },

	stop: function () {
		if (this.timer) {
			clearTimeout(this.timer);
		}
	},

	start: function () { 
        this.periodicallyUpdate();
    },

	pause: function () {
		this.stop();
		this.activateControls();
    },

	resume: function (event) {
		if (event) {
			var related = event.relatedTarget || event.toElement;
			if (!related || (!this.slides.include(related) && !this.slides.any(function (slide) { return related.descendantOf(slide); }))) {
				this.start();
            }
        } else {
            this.start();
        }
    },

	periodicallyUpdate: function () {
		if (this.timer != null) {
			this.next();
		}
		else {
			this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 1000);
		}
    },
    
    wheel: function (event) {
        event.cancelBubble = true;
        event.stop();
        
		var delta = 0;
		if (!event) {
            event = window.event;
        }
		if (event.wheelDelta) {
			delta = event.wheelDelta / 120; 
		} else if (event.detail) { 
            delta = -event.detail / 3;	
        }        
       
        if (!this.scrolling) {
            this.deactivateControls();
            if (delta > 0) {
                this.prev();
            } else {
                this.next();
            }            
        }
        
		return Math.round(delta); //Safari Round
    },

	deactivateControls: function () {
		this.controls.invoke('addClassName', this.options.disabledClassName);
    },

	activateControls: function () {
		this.controls.invoke('removeClassName', this.options.disabledClassName);
    }
});


Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
	initialize: function (element) {
		this.element = $(element);
		var options = Object.extend({ x: 0, y: 0, mode: 'absolute' } , arguments[1] || {});
		this.start(options);
    },

	setup: function () {
		if (this.options.continuous && !this.element._ext) {
			this.element.cleanWhitespace();
			this.element._ext = true;
			this.element.appendChild(this.element.firstChild);
        }

		this.originalLeft = this.element.scrollLeft;
		this.originalTop  = this.element.scrollTop;

		if (this.options.mode == 'absolute') {
			this.options.x -= this.originalLeft;
			this.options.y -= this.originalTop;
        }
    },

	update: function (position) {
		this.element.scrollLeft = this.options.x * position + this.originalLeft;
		this.element.scrollTop  = this.options.y * position + this.originalTop;
    }
});
/**
 * WJContactlocation handles the Javascript interactions for the contactlocation form
 *
 * @since Fri Oct 22 2010
 * @author Nick van der Zwet
 * @package Windmill.Javascript.Cvd
 **/
var WJContactForm = Class.create({
	
	/**
	 * initialize
	 *
	 * Creates a new WJContactForm
	 *
	 * @since Fri Oct 22 2010
	 * @access public
	 * @param Form form
	 * @return WJContactForm
	 **/
	initialize: function() {
		this.spin = new WJSpin();
	},

	/**
	 * addObserver
	 *
	 * Adds an obersver for the submit button
	 *
	 * @since Mon Dec 13 2010
	 * @access public
	 * @return void
	 **/
	addObserver: function() {
		if ($("contactformsubmit") ) {
			$("contactformsubmit").observe("click", this.submit.bindAsEventListener(this) );
		}
	},

	/**
	 * submit
	 *
	 * Spin/submit the content for this form
	 *
	 * @since Fri Oct 22 2010
	 * @access public
	 * @return void
	 **/
	submit: function(event) {
		var spin = new WJSpin();
		var url = new WJUrl($(document.forms.locationcontactbannerform).serialize(true) );
		url.setCt("wmdynamic");
		url.setDt("Wmcontactformcvd");
		url.addParameter("module", "Wmcontactformcvd");
		spin.content(url, [$("contactformwrapper"), this.addObserver.bindAsEventListener(this)] );
	}

});

var cvdcontactform = new WJContactForm();var WJFontSizer = Class.create({
	/**
	 * initialize
	 *
	 * 
	 *
	 * @since Fri Oct 16 2009
	 **/
	initialize: function() {
		cookie = new WJCookie();

		this.currentcookie = cookie.get("fontsize");

		if (this.currentcookie && this.currentcookie != "fontnormal"){
			this.switchFontTo(this.currentcookie);
		}
		else if ($("fontnormal") && typeof($("fontnormal").addClassName) !== "undefined") {
			$('fontnormal').addClassName("active");
		}
		
		if( $('fonttoggler') ) {
			$('fonttoggler').observe('click', function(event){
				this.switchFontTo(Event.element(event).identify() );
			}.bindAsEventListener(this) );
		}
	},
 
	/**
	 * switchFontTo
	 *
	 * Call changeFontTo based on it's condition
	 *
	 * @since Fri Oct 16 2009
	 * @access public
	 * @param size
	 * @return
	 **/
	switchFontTo: function(size) {
		var grootte = "middel";
		switch(size) {
			case "fontlarge":
				this.changeFontTo(1);
				if( $('fonttoggler') ) {
					$('fontnormal').removeClassName("active");
					$('fontlarger').removeClassName("active");
					$('fontlarge').addClassName("active");
				}
				cookie.set("fontsize", "fontlarge");
				grootte = "groot";
				break;
			case "fontlarger":
				this.changeFontTo(1.25);
				if( $('fonttoggler') ) {
					$('fontnormal').removeClassName("active");
					$('fontlarge').removeClassName("active");
					$('fontlarger').addClassName("active");
				}
				cookie.set("fontsize", "fontlarger");
				grootte = "groter";
				break;
			case "fontnormal":
			default:
				this.changeFontTo(0.75);
				if( $('fonttoggler') ) {
					$('fontlarger').removeClassName("active");
					$('fontlarge').removeClassName("active");
					$('fontnormal').addClassName("active");
				}
				cookie.set("fontsize", "fontnormal");
				grootte = "middel";
				break;
		};
	},
 
	/**
	 * changeFontTo
	 *
	 *	Changes the font to a given value
	 *
	 * @since Mon Oct 19 2009
	 * @access public
	 * @param size
	 * @return
	 **/
	changeFontTo: function(size) {
		this.fontSize = size;
		this.setFontSize();
	},

	/**
	 * setFontSize
	 *
	 *	sets the fontsize on elements
	 *
	 * @since Wed Nov 11 2009
	 * @access public
	 * @param size
	 * @return
	 **/
	setFontSize: function() {
		if(typeof this.fontSize != 'undefined'){
			$$('div.richtext > p, div.richtext > ul > li > p, div.richtext > table > tbody > tr > td > p, div.cbmiddle > ul > li > *, ul.nav_breadcrumbs > li > *, div.middle > ul.pos_menu > li > a, a.articlenewsoverview, a.moduleinmodeoverview, div.newspager > span, div.newspager > div.more, ul.bottom > li > *, div.cbmiddle > p, div.stl_inputtitle > p,div.locationsubcategorydescription > p, div.locationitem > p, a.backtotop, div.articleindexcolumn > p, ul.articleindexoverview > li > a, ul.vacancyarticles > li > a, ul.remainingnewsarticles > li > a, ol.googlesearchresults > li > p, div.locationsubcategory > *, div.locationsubcategory > div > a, div.partnerlocation > p, div.contentblock > ul > li > a, ol > li > a, div.searchpager span, div.searchpager div.more, p.error').each(function(el) {
				el.setStyle({fontSize: this.fontSize + 'em'});
			}.bind(this));
		}
	}
});

var fontSizer = new WJFontSizer();

