// Created by Sander Spies / om|vorm (except in_array() and the <hoteliers.com> part, that's just somewhat cleaned up)
// Uses Prototype (prototype.js) and Scriptaculous (scriptaculous.js + effects.js)

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) <=6; // Check for IE version 6 or older (we won't be using some stylish effects for these old browsers)
var curDate = new Date();

document.observe("dom:loaded", function() {
	page = $$('body').first().readAttribute('id').toString(); // Which page are we on?
	topLevelPages = new Array('debosrand', 'hotel', 'arrangementen', 'vergaderen', 'evenementen', 'meeting');
	if ( page.include('_') ) {
		arr_page = new Array();
		arr_page = page.split('_');
		page	= arr_page[0];
		subpage	= arr_page[1];
	}
	if ( !in_array(page, topLevelPages) ) {
		page = 'debosrand'; // Used to display the correct active mainNav element after the mouse leaves the #nav
	}

	$('nav').childElements().each(function(el) { // First level within the #nav list (mainNav)
		// mainNav onMouseOver
		Event.observe(el, 'mouseover', function(event) {
			el.firstDescendant().setStyle({backgroundPosition: 'left 0'});			// Make the A-tag within the hovered item (LI) active
			el.siblings().each(function(el) {										// Style all other buttons (A-tags within LI's) on the same level...
				el.firstDescendant().setStyle({backgroundPosition: 'left -35px'});	// ...as inactive
			});

			if ( el.descendants().size() > 1 ) {					// Is there a subNav item for this button? (Which is a DIV with an UL in it in this case). Size must be bigger than 1 because there's also the A-tag
				$('a_'+page).next().setStyle({display: 'none'});	// Hide the 'default' subNav for the page we're on at the moment
				el.descendants()[1].setStyle({display: 'block'});	// Show the subNav of the mainNav item that is hovered (which is a DIV containing an UL)
			}
		});

		// mainNav onMouseOut
		Event.observe(el, 'mouseout', function(event) {
			el.firstDescendant().setStyle({backgroundPosition: 'left -35px'});		// Style the button the mouse just left as inactive
			el.siblings().each(function(el) {										// Style all other buttons on the same level...
				el.firstDescendant().setStyle({backgroundPosition: 'left -35px'});	// ...as inactive too (if a new button is hovered the mouseover event will kick in again and display it as active)
			});

			if ( el.descendants().size() > 1 ) {					// The subNav item of this button... (size must be bigger than 1 because there's also the A-tag)
				el.descendants()[1].setStyle({display: 'none'});	// ...hide it! If a new button is hovered the mouseover event will kick in again and display the appropriate subNav item
			}

			$('a_'+page).setStyle({backgroundPosition: 'left 0'});	// Reset active mainNav button and...
			d = typeof(subpage) == 'undefined' ? 'none' : 'block';	// ...if we are on a subpage...
			$('a_'+page).next().setStyle({display: d});				// ...the active subNav dropdown too, if the mouse isn't anywhere over the navigation
		});
	});

	photos = $('photo').select('img').size();
	if ( photos > 1 ) {
		$('mask').update('<img src="/images/loading.gif" id="loading">');

		var aPhotos = new Array();
		$('photo').select('img').each(function(el) {	// All images within #photo...
			aPhotos.push( el.readAttribute('src') );	// ...collected in an array
		});
		$('photo').update();							// Empty #photo

		for (var i=0; i<aPhotos.size(); i++) {			// Put all photos back in #photo with an onload event and make them invisible by default
			$('photo').insert({bottom: '<img src="'+aPhotos[i]+'" id="img'+(i+1)+'" alt="" onload="imageLoaded();">'});
			$('img'+(i+1)).setStyle({opacity: 0, display: 'block'});
		}
	}

//	adjustHeight();

	// Pagination for Special Offers page, watch click events for navigation
	if ( $('pagination') != null ) {
		// Previous
		$('pPrev').observe('click', function(event) {
			Event.stop(event);
			if (currentPage > 1) {
				currentPage--;
				displayPage(currentPage);
			}
		});

		// Next
		$('pNext').observe('click', function(event) {
			Event.stop(event);
			if (currentPage < totalPages) {
				currentPage++;
				displayPage(currentPage);
			}
		});

		// Numbered pages
		$('pagination').select('a.pageNum').each(function(el) {
			Event.observe(el, 'click', function(event) {
				Event.stop(event);
				el.blur();
				newPage = parseInt( $(el).readAttribute('id').sub('p', '') );
				if ( newPage != currentPage) {
					currentPage = newPage;
					displayPage(currentPage);
				}
			});
		});
	}

	// Arrangementen (expand/collaps and fade in/out list items)
	if ( $('arrangementen') != null ) {
		$('arrangementen').select('a.arrangement').each(function(el) {
			Event.observe(el, 'click', function(event) {
				Event.stop(event);
				if ( el.up(0).hasClassName('active') ) {
					new Effect.BlindUp(el.nextSiblings()[0], {
						duration: 0.5,
						fps: 50,
						queue: { position: 'end', scope: 'blind' },
						afterFinish: function() {
							el.up(0).removeClassName('active');
						}
					});

					new Effect.Opacity(el.nextSiblings()[0], {
						duration: 0.5,
						from: 1.0,
						to: 0.0
					});
				} else {
					el.up(0).addClassName('active');
					new Effect.BlindDown(el.nextSiblings()[0], {
						duration: 0.5,
						fps: 50,
						queue: { position: 'end', scope: 'blind' }
					});

					new Effect.Opacity(el.nextSiblings()[0], {
						duration: 0.5,
						from: 0.0,
						to: 1.0
					});
				}
			});
		});
	}

	// Hoteliers
	if ( $('hcomForm') != null ) {
		$('hSubmit').observe('click', function(event) {
			if ( !Prototype.Browser.IE6 ) {					// Browsers other than IE<=6: Form gets posted to the iframe in the 'lightbox' overlay
				$$('html').first().addClassName('scroll');	// Disable vertical scrolling
				$$('body').first().insert({bottom: '<div id="overlay"></div>'});
				$('overlay').setStyle({ opacity: '0' });

				new Effect.Opacity('overlay', {					// Fade in overlay
					duration: 0.5,
					from: 0.0,
					to: 0.8,
					afterFinish: function() {
						$$('body').first().insert({bottom: '<div id="lightbox"><div id="content"><iframe name="hcomTarget" id="hcomTarget" frameborder="0" height="480" width="640"></iframe></div><div>'});
						removeLightbox(); // Start observing the click ('close lightbox') action on the #overlay div now that the div #overlay exists
						$('hcomForm').target = 'hcomTarget';	// Target is iframe id
						$('hcomForm').submit();					// Post the form
					}
				});

			} else { // IE<=6: Form gets posted to a new window
				$('hcomForm').target = '_blank';
				$('hcomForm').submit();
			}
		});

		$('ad').selectedIndex = curDate.getDate();
		$('am').selectedIndex = curDate.getMonth();
		getNDay();
	}

	// Insert two images of leaves behind the navigation and sidebar
	$('container').insert({bottom: '<img src="/images/leaf_top.png" id="leaf_top" alt=""><img src="/images/leaf_bottom.png" id="leaf_bottom" alt="">'});
});

function removeLightbox() {
	$('lightbox').observe('click', function(event) {
		$('lightbox').stopObserving('click'); // Remove click handler
		$('lightbox').remove();
		new Effect.Opacity( 'overlay', {
			duration: 0.5,
			from: 0.8,
			to: 0.0,
			afterFinish: function() {
				$('overlay').remove();							// Take the #overlay 'layer' out of the page
				$$('html').first().removeClassName('scroll');	// Enable vertical scrolling again
			}
		});
	});
}

function adjustHeight() {
	// Adjust height of page if content is not 100% height
//	containerHeight = $('container').getHeight()+40+40; // 40+40 = margin-top and border-bottom of the body element
	containerHeight = $('container').getHeight()+40+68; // 40+68 = margin-top and border-bottom of the body element
	screenHeight = window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
	if ( screenHeight != null && containerHeight < screenHeight ) {
//		newHeight = screenHeight-350-40-40;  // -350 = padding-top and -bottom of the container element, -40-40 = margin-top and border-bottom of the body element
		newHeight = screenHeight-350-40-68;  // -350 = padding-top and -bottom of the container element, -40-68 = margin-top and border-bottom of the body element
		$('container').setStyle({minHeight: newHeight+'px'});
	}
}

function imageLoaded() {
	nCalled = typeof(nCalled) == 'undefined' ? 1 : ++nCalled;
	if (nCalled == photos) {
		pe = new PeriodicalExecuter(function() {
			new Effect.Opacity( $('loading'), {
				duration: 1.0,
				from: 1.0,
				to: 0.0,
				afterFinish: function() {
					$('loading').remove();
				}
			});
			new Effect.Opacity( $('photo').firstDescendant(), {
				duration: 2.0,
				from: 0.0,
				to: 1.0
			});
			curImg = 1;
			startSlideshow();
		}, 0.5);
	}
}

function startSlideshow() {
	pe.stop();
	pe = new PeriodicalExecuter(function() {
		new Effect.Opacity( $('img'+curImg), {
			duration: 2.0,
			from: 1.0,
			to: 0.0
		});
		curImg = curImg == photos ? 1 : ++curImg;
		new Effect.Opacity( $('img'+curImg), {
			duration: 2.0,
			from: 0.0,
			to: 1.0
		});
	}, 6);
}


function in_array(needle, haystack, argStrict) {
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
	// *     returns 1: true

	var found = false, key, strict = !!argStrict;

	for (key in haystack) {
		if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			found = true;
			break;
		}
	}
	return found;
}

function displayPage(page)
{
	$('pagination').select('a.pageNum').each(function(el) {
		if ( el.hasClassName('current') ) {
			el.removeClassName('current');
		}
		$('p'+page).addClassName('current');
	});

	var prev = (page == 1) ? '<span>vorige</span>' : '<a href="/nl/arrangementen/aanbiedingen/?p='+(page-1)+'">vorige</a>';
	$('pPrev').update(prev);

	var next = (page == totalPages) ? '<span>volgende</span>' : '<a href="/nl/arrangementen/aanbiedingen/?p='+(page+1)+'">volgende</a>';
	$('pNext').update(next);

	var pageWidth = 666;
	var move = -((page-1)*pageWidth);
	new Effect.Move( $('aanbiedingenContainer'), {
		x: move,
		y: 0,
		duration: 1.2,
		mode: 'absolute',
		transition: Effect.Transitions.sinoidal,
		queue: { position: 'end', scope: 'scroll' } });
}

// <hoteliers.com>
function dInM(mon) {
	if (mon == 2) {
		Year = curDate.getFullYear();
		if(Math.round(Year/4) == Year/4) {
			if(Math.round(Year/100) == Year/100) {
				if(Math.round(Year/400) == Year/400) {
					return 29;
				} else {
					return 28;
				}
			} else {
				return 29;
			}
		}
		return 28;
	} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
		return 30;
	}
	return 31;
}

function getNDay() {
	theDay = $('ad').selectedIndex + 1;
	theMonth = $('am').selectedIndex + 1;

	dInMonth = dInM(theMonth);
	if (theDay > dInMonth) {
		theDay = dInMonth;
		$('ad').selectedIndex = theDay - 1;
	}
	newDay = theDay + 1;
	newMonth = theMonth;
	if (newDay > dInMonth) {
		newDay = 1;
		newMonth = theMonth + 1;
		if (newMonth > 12) {
			newMonth = 1;
		}
	}
	$('dd').selectedIndex = newDay - 1;
	$('dm').selectedIndex = newMonth - 1;
}

function checkDDate() {
	theDay = $('dd').selectedIndex + 1;
	theMonth = $('dm').selectedIndex + 1;
	dInMonth = dInM(theMonth);

	if (theDay > dInMonth) {
		theDay = dInMonth;
		$('dd').selectedIndex = dInMonth - 1;
	}
}
// </hoteliers.com>