var set = function () {
	
	var tabs = [$('home'), $('vereniging'), $('actueel'), $('contact'), $('forum')];

	if ($('interesses') != null) {
		
		var children = $$('.ivo_right').getChildren('a');
		
		$$(children).addEvent('click', function (e) {
			textarea = this.getParent().get('id').replace('_choice', '');
			$(textarea).set('value', $(textarea).get('value') + this.get('text') + ', ');
			this.setStyle('text-decoration', 'line-through');
		});
		
	}
		
	var checkMouseout = function (element_id) {

		var closeSubmenu = function () {
			if ($('submenu_' + element_id) != null) {
				$('submenu_' + element_id).setStyle('display', 'none');
			}
			$(element_id).removeClass('hover');
		}	
		
		var timer = closeSubmenu.delay(10); 
 		
 		if ($('submenu_' + element_id) != null) {		
			$('submenu_' + element_id).addEvent('mouseenter', function (e) {
				clearTimeout(timer);
			});
		}
		
		$(element_id).addEvent('mouseenter', function (e) {
			clearTimeout(timer);
		});
		
	}

	$$(tabs).addEvents({
		'mouseenter': function (e) {
		
			var tab_id = this.get('id');
			
			this.addClass('hover');
			if ($('submenu_' + tab_id) != null) {
				$('submenu_' + tab_id).setStyle('display', 'block');
				$('submenu_' + tab_id).addEvent('mouseleave', function (e) {
					checkMouseout(this.get('id').replace('submenu_', ''));
				});
			}
			
		},
		'mouseleave': function (e) {
			checkMouseout(this.get('id'))
		}		
	});
	
	var moveonWhatsnew = function (item_id) {
		
		$$('.trigger').removeClass('active');		
		$$('.whatsnew_item').fade('out');
		
		var fadeIn = function () {
			$$('.whatsnew_item').removeClass('current');
			$('whatsnew_' + item_id).fade('in');
			$('whatsnew_' + item_id).addClass('current');
			$('trigger_' + item_id).getParent().addClass('active');
		}
		
		fadeIn.delay(700);
		
	}
	
	var changeWhatsnew = function () {
		
		var whatsnew_items = $$('.trigger').getFirst('a').get('id');
		
		whatsnew_items.each(function(item) {
			if($(item).getParent().hasClass('active')) {
				var active_item_index = whatsnew_items.indexOf(item);
				var next_item = (active_item_index + 1);
				if (next_item == whatsnew_items.length) {
					next_item = 0;
				}
				moveonWhatsnew(whatsnew_items[next_item].replace('trigger_', ''));
			}
		});		
	
	}

	var whatsnew_timer = changeWhatsnew.periodical(10000);
		
	$$('.trigger').getFirst('a').addEvent('click', function(e) {
		moveonWhatsnew(this.get('id').replace('trigger_', ''));
		clearInterval(whatsnew_timer);
	});
	
	if ($('login') != null) {
	
        $('login').addEvent('click', function(e) {

            e.stop();
            $('page_overlay').setStyle('display', 'block');
            
        });

        $$('#overlay_background, #close_login').addEvent('click', function(e) {

            e.stop();
            $('page_overlay').setStyle('display', 'none');
            
        });		
	}
	
	
	if ($('twitter') != null) {
				    
		    $('scroll_down').addEvent('mouseenter', function (e) {
		    	
		    	var addTopmargin = function () {
		    		var margin_top = $('twitter').getStyle('margin-top').replace('px', '');
			    	var new_margin_top = (margin_top - 5);
			    	$('twitter').setStyle('margin-top', new_margin_top + 'px');		    		
		    	}
		    	
		    	var timer = addTopmargin.periodical(100);
		    	
		    	$('scroll_down').addEvent('mouseleave', function (e) {
		    		clearInterval(timer);
		    	});
		    			    	
		    });
		    
	}
		
	if ($('photos') != null) {
				
		var username	= '118056755613002466387';
		
		var thumbsize	= '160c';
		
		var imgmax 		= '800';
		
		var url = (Browser.getQueryStringValue('albumid')) ? 'http://picasaweb.google.com/data/feed/base/user/'+ username +'/albumid/'+ Browser.getQueryStringValue('albumid') +'?category=photo&alt=json&callback=viewPhotoList&thumbsize=' + thumbsize +'&imgmax='+imgmax : 'http://picasaweb.google.com/data/feed/base/user/' + username + '?category=album&alt=json&callback=viewAlbumList&access=public&thumbsize=' + thumbsize;
			
		new Element('script', {'src': url}).inject($('photos'));	
		
	}
	
	if ($('starting') != null) {
				
		var end = new DatePicker('#ending', { 
			pickerClass: 'datepicker_vista', 
			timePicker: true, 
			startDay: 0,
			format: 'Y-m-d H:i',
			inputOutputFormat: 'Y-m-d H:i'
		});
			
		var start = new DatePicker('#starting', { 
			pickerClass: 'datepicker_vista', 
			timePicker: true, 
			startDay: 0,
			format: 'Y-m-d H:i',
			inputOutputFormat: 'Y-m-d H:i',
			onSelect: function () {
				var splitted = $('starting').get('value').split(' ');
				var more_splitted = splitted[1].split(':');
				var four_hours_later = splitted[0] + ' ' + (parseInt(more_splitted[0]) + 5) + ':' + more_splitted[1];
				$('ending').set('value', four_hours_later);
				$('ending').getNext('input').set('value', four_hours_later);
			}
		});
						
	}
	
	if ($('old_calendar_items') != null) {
		
		$('old_calendar_items_trigger').addEvent('click', function (e) {
			
			e.stop();
			
			var current_state = $('old_calendar_items').getStyle('display');
			
			if (current_state == 'block') {
				$('old_calendar_items').setStyle('display', 'none');
				$('old_calendar_items_trigger').set('text', 'Oudere laten zien');
			}
			else {
				$('old_calendar_items').setStyle('display', 'block');
				$('old_calendar_items_trigger').set('text', 'Oudere verbergen');
			}
			
		})
				
	}
	
}

function viewAlbumList(list) {
	
	if ($('lastphoto') == null) {
		
		href  = list.feed.author[0].uri.$t;
		user = list.feed.author[0].name.$t;
		
		$('navigate2').adopt(new Element('a', {'href': href, 'html': 'Naar Picasa', 'title': 'Bekijk de foto\'s van Lux ad Mosam op Picasa', 'class': 'picasalink'}));
	
		list.feed.entry.each(function(item) {
			var title	= item.title.$t;
			var thumb	= item.media$group.media$thumbnail[0].url;
		
			id_begin	= item.id.$t.indexOf('albumid/')+8;
			id_end		= item.id.$t.indexOf('?');
			var id		= item.id.$t.slice(id_begin, id_end);
			var href	= Browser.setQueryStringValues({'albumid': id});
			d			= item.published.$t;
			var date	= d.substr(8,2) + '-' + d.substr(5,2) + '-' + d.substr(0,4);
	
			$('photos').adopt(new Element('div', {'class': 'image-container'}).adopt(new Element('a', {'href': href, 'title': title}).adopt(new Element('img', {'src': thumb, 'alt': title}), new Element('p', {'html': title})), new Element('p', {'html': date, 'class': 'date'})));
		}); 
	
	}
	else {
		
		href  = list.feed.author[0].uri.$t;
		user = list.feed.author[0].name.$t;
		
		var title	= list.feed.entry[0].title.$t;
		var thumb	= list.feed.entry[0].media$group.media$thumbnail[0].url;
		var id		= list.feed.entry[0].id.$t;
		var idsplit = id.split('/');
		var albumid = idsplit[9].replace('?alt=json&hl=nl', '');
		
		$('photos').adopt(new Element('a', {'href': 'fotos'}).adopt(new Element('img', {'src': thumb, 'title': title}))); 
		$('photos').adopt(new Element('span', {'class': 'photo_description','html': 'Een foto uit het album <a href="fotos?&albumid=' + albumid + '">' + title + '</a>'})); 
			
		/*list.feed.entry.each(function(item) {
			var title	= item.title.$t;
			var thumb	= item.media$group.media$thumbnail[0].url;
		
			id_begin	= item.id.$t.indexOf('albumid/')+8;
			id_end		= item.id.$t.indexOf('?');
			var id		= item.id.$t.slice(id_begin, id_end);
			var href	= Browser.setQueryStringValues({'albumid': id});
			d			= item.published.$t;
			var date	= d.substr(8,2) + '-' + d.substr(5,2) + '-' + d.substr(0,4);
	
			$('photos').adopt(new Element('div', {'class': 'image-container'}).adopt(new Element('a', {'href': href, 'title': title}).adopt(new Element('img', {'src': thumb, 'alt': title}), new Element('p', {'html': title})), new Element('p', {'html': date, 'class': 'date'})));
		});*/ 		
		
	}
}

function viewPhotoList(list) {
	
	var album = list.feed.title.$t;
	var href  = Browser.setQueryStringValues({'albumid': ''});
	
	$('navigate').adopt(new Element('a', {'href': href, 'html': 'Terug naar de albums', 'class': 'backlink'}), new Element('h2', {'html': album}));
	
	list.feed.entry.each(function(item, size) {

		var title	= item.title.$t;
		var link	= item.media$group.media$content[0].url;
		size		= item.media$group.media$content[0].width;
		var thumb	= item.media$group.media$thumbnail[0].url;
		
		id_begin	= item.id.$t.indexOf('albumid/')+8;
		id_end		= item.id.$t.indexOf('?');
		var id		= item.id.$t.slice(id_begin, id_end);
		
		$('photos').adopt(new Element('a', {'href': link, 'title': title, 'class': 'lightbox', 'rel': 'lightbox-album'}).adopt(new Element('img', {'src': thumb, 'alt': title})));
	});

	$$("a").filter(function(el) {
		return el.href && el.firstChild && el.firstChild.src;
	}).slimbox({}, function(el) {	
		return [el.href, (el.title || el.firstChild.alt) + '<br /><a href="' + el.href + '">Album: '+ album +' </a>'];
	});
	
	$('photos').adopt(new Element('div', {'class': 'spacer'}));
	
}

window.addEvent('domready', function() {	
	
	set();
	
});
