/**
 * Bootstrap.js - Bootstrap for all Javascript functionality
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */

WS.Event.addEvent(window, 'load', 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';
		}
	}
	
	// Fix IE6 png
	if(WS.browser.IE6) {
		var packages = WS.DOM.getElementsByClass('package', $('main'), 'a');
		for(var i = 0; i < packages.length; i++) {
			WS.Util.fixPngBackground(packages[i], 'crop');
		}
		WS.Util.fixPngBackground($('logo'));
		WS.Util.fixPngBackground($('visual-caption'));
		WS.Util.fixPngBackground($('content-head'), 'crop');
		WS.Util.fixPngBackground($('zoover'));
		WS.Util.fixPngBackground($('footer'));
	}
	
	// More photo's button
	if($('more-photos')) {
		WS.Event.addEvent('more-photos', 'click', function(e) {
			WS.Event.stopEvent(e);
			myLightWindow.activate(null, WS.DOM.getElementsByClass('thumb', $('thumb-wrap'), 'a')[0]);
		});
	}
	
	// Set the current dates
	if($('book-form')) {
		// Booking form validation
		var v = new WS.Validation('book-form');
		v.initialize();	
		
		//var date = new Date();
		//setDate('start', date, 1);
		//setDate('end', date, 2);
	}
	

});


/**
 * Sets the date for a set of three select elements
 * 
 * @param {String} id The ID of the select elements
 * @param {Date} date The date object to use
 */
function setDate(id, date, dateOffset) {
	date = new Date(date); // Make a clone instead of using a reference
	
	if(dateOffset) {
		date.setDate(date.getDate() + dateOffset);
	}
	
	$(id + '-day').selectedIndex = date.getDate();
	$(id + '-month').selectedIndex = getIndexByValue($(id + '-month'), date.getMonth() + 1);
	$(id + '-year').selectedIndex = getIndexByText($(id + '-year'), date.getFullYear());
};


/**
 * Get an option's index by its text
 * 
 * @param {Object} select The select element
 * @param {String} text The option's text to look for
 */
function getIndexByText(select, text) {
	for(var i = 0; i < select.options.length; i++) {
		if(select.options[i].text == text) {
			return select.options[i].index;
		}
	}
};


/**
 * Get an option's index by its value
 * 
 * @param {Object} select The select element
 * @param {String} text The option's text to look for
 */
function getIndexByValue(select, value) {
	for(var i = 0; i < select.options.length; i++) {
		if(select.options[i].value == value) {
			return select.options[i].index;
		}
	}
};

