
//implement mootools more form
Element.implement({

	tidy: function(){
		this.set('value', this.get('value').tidy());
	},

	getTextInRange: function(start, end){
		return this.get('value').substring(start, end);
	},

	getSelectedText: function(){
		if (Browser.Engine.trident) return document.selection.createRange().text;
		return this.get('value').substring(this.getSelectionStart(), this.getSelectionEnd());
	},

	getIERanges: function(){
		this.focus();
		var range = document.selection.createRange();
		var re = this.createTextRange();
		var dupe = re.duplicate();
		re.moveToBookmark(range.getBookmark());
		dupe.setEndPoint('EndToStart', re);
		return { start: dupe.text.length, end: dupe.text.length + range.text.length, length: range.text.length, text: range.text };
	},

	getSelectionStart: function(){
		if (Browser.Engine.trident) return this.getIERanges().start;
		return this.selectionStart;
	},

	getSelectionEnd: function(){
		if (Browser.Engine.trident) return this.getIERanges().end;
		return this.selectionEnd;
	},

	getSelectedRange: function(){
		return {
			start: this.getSelectionStart(),
			end: this.getSelectionEnd()
		};
	},

	setCaretPosition: function(pos){
		if (pos == 'end') pos = this.get('value').length;
		this.selectRange(pos, pos);
		return this;
	},

	getCaretPosition: function(){
		return this.getSelectedRange().start;
	},

	selectRange: function(start, end){
		this.focus();
		if (Browser.Engine.trident){
			var range = this.createTextRange();
			range.collapse(true);
			range.moveStart('character', start);
			range.moveEnd('character', end - start);
			range.select();
			return this;
		}
		this.setSelectionRange(start, end);
		return this;
	},

	insertAtCursor: function(value, select){
		var start = this.getSelectionStart();
		var end = this.getSelectionEnd();
		this.set('value', this.get('value').substring(0, start) + value + this.get('value').substring(end, this.get('value').length));
		if ($pick(select, true)) this.selectRange(start, start + value.length);
		else this.setCaretPosition(start + value.length);
		return this;
	},

	insertAroundCursor: function(options, select){
		options = $extend({
			before: '',
			defaultMiddle: 'SOMETHING HERE',
			after: ''
		}, options);
		value = this.getSelectedText() || options.defaultMiddle;
		var start = this.getSelectionStart();
		var end = this.getSelectionEnd();
		if (start == end){
			var text = this.get('value');
			this.set('value', text.substring(0, start) + options.before + value + options.after + text.substring(end, text.length));
			this.selectRange(start + options.before.length, end + options.before.length + value.length);
			text = null;
		} else {
			text = this.get('value').substring(start, end);
			this.set('value', this.get('value').substring(0, start) + options.before + text + options.after + this.get('value').substring(end, this.get('value').length));
			var selStart = start + options.before.length;
			if ($pick(select, true)) this.selectRange(selStart, selStart + text.length);
			else this.setCaretPosition(selStart + text.length);
		}
		return this;
	}

});

window.addEvent('domready', function() {	
	
	//resolution check 
	if(screen.width < 1280 && !Cookie.read('notoptimal')){ 
		
		overlay 	= new Element('div', { 'id': 'notoptimaloverlay', 'events' : { 'click': function(){ overlay.destroy(); notoptimal.destroy(); } } });
		notoptimal	= new Element('div', { 'id': 'notoptimal' });
		inner 		= new Element('div', { 'class': 'inner' });
		contents	= new Element('div', { 'class': 'content' });
		p			= new Element('p', { 'html': 'Vanwege een lage schermresolutie is uw weergave van deze site momenteel niet optimaal.' });
		a			= new Element('a', { 'html': 'Lees meer over het gebruik van schermresoluties.', 'href': 'http://blog.innovatio.nl/browser' });
		
		contents.adopt(p,a);
		inner.adopt(contents);
		notoptimal.adopt(inner);
		
		$$('body').adopt(overlay,notoptimal);
		var myCookie = Cookie.write('notoptimal', 'true');
		
	}		
	
	if(!Browser.Platform.mac) { $$('input[type="submit"]').addClass('nomacsubmit'); }
									 
	//$$('a').addEvent('focus',function(item){ if(item.blur()) item.blur(); });									 
	
	$$('input[type=text],textarea').addEvents({
		'mouseenter':function(){ 
			this.addClass('hover');
		},				
		'mouseleave':function(){ 
			this.removeClass('hover');
		},				
		'click':function(){ 
			this.addClass('focus');
			this.selectRange(0,this.get('value').length);
		},
		'focus':function(){ 
			this.addClass('focus');
			this.selectRange(0,this.get('value').length);
		},				
		'blur':function(){ 
			//this.removeClass('focus');
		}
	});
	
	
	////////////////////
	// Kolomen
	///////////////////////////
	
	$$('#content ul.cols-2 li:nth-child(2n)').setStyle('width', 350); 
	
	$$('ul.cols-2 li:nth-child(2n)').setStyle('margin-right',0); 
	$$('ul.cols-3 li:nth-child(3n)').setStyle('margin-right',0); 
	$$('ul.cols-4 li:nth-child(4n)').setStyle('margin-right',0); 
	$$('ul.cols-5 li:nth-child(5n)').setStyle('margin-right',0); 

	$$('ul.cols-2 li:nth-child(2n+1)').setStyle('clear','left'); 
	$$('ul.cols-3 li:nth-child(3n+1)').setStyle('clear','left'); 
	$$('ul.cols-4 li:nth-child(4n+1)').setStyle('clear','left'); 
	$$('ul.cols-4 li:nth-child(5n+1)').setStyle('clear','left'); 

	$$('ul.cols-2,ul.cols-3,ul.cols-4,ul.cols-5').each(function (item,index){
		var br = new Element('br', { 'styles': {'clear':'left'} }); 
		br.inject(item, 'after');								   
	});


	////////////////////
	// subcontent drop
	///////////////////////////

	if($type($('subheader')) == 'element') {
		if($('subheader').getStyle('display') != 'none') {
		
			//var myFx = new Fx.Slide('subcontent',{duration: 500});			
			//myFx.hide();
			
			//$$('body').setStyle('padding-bottom',100);
			$$('body').setStyle('height','auto');

			//$$('#subcontent .content').setStyle('visibility','hidden');
			$$('#subcontent').setStyle('display','none');
			//$$('#subcontent .content').set('opacity',0);


			$$('#subheader h2').addEvents({
				'mouseenter':function(){ 
					var o = this.getStyle('margin-left');
					this.tween('margin-left', [o,25]);
				},				
				'mouseleave':function(){ 
					var o = this.getStyle('margin-left');
					this.tween('margin-left', [o,0]);
				},				
				'click':function(){ 
					$('subheader').destroy();	
					$('subcontent').setStyle('display','block');		
					$('bottombar').setStyle('border',0);		
					$('bottombar').setStyle('margin-top',0);		
					//myFx.toggle();
					//function() { var scrollFx = new Fx.Scroll(window).toElement('subcontent').delay(1000,this); }
					var scrollFx = new Fx.Scroll(window, { duration: 1500 }).toElement('subcontent');
					//$$('#subcontent .content').setStyle('visibility','visible');
					//$$('#subcontent .content').fade('in');
					$$('body').setStyle('padding-bottom',0);
				}
				
			});
		
			//function test() { 
	
			//}
		
		}
	}	

	////////////////////
	// Drop down menu
	///////////////////////////

	if($type($('menu')) == 'element') {

		$$('#more h2').addEvent('mouseenter',function(){ 
			
			$('menu').set('morph', {duration: 1000, transition: 'bounce:out'});
			$('menu').morph({top: 0});
		
		});
		
		$('menu').addEvent('mouseleave',function(){ 
			
			$('menu').set('morph', {duration: 400, transition: 'quad:in'});
			$('menu').morph({top: -140});	
			
		});
		
		
		$$('#menu li a').addEvents({
			'mouseenter': function(e){
				
				this.set('morph',{duration: 400, transition: 'quart:out'});
				this.morph({ 
					'background-color': '#FFF',
					'color': '#FF006E',
					'margin-left': 10,
					'font-size': 24
				})
				
			},
			'mouseleave': function(e){
				
				this.set('morph',{duration: 400, transition: 'quart:out'});
				this.morph({ 
					'background-color': '#FF006E',
					'color': '#FFF',
					'margin-left': 0,
					'font-size': 16
					
				})
	
			}
		})
		
		}
		
					
});