window.addEvents({
	'domready': function() {
		
		initExternalLinks();
		initSendAFriendLink();

	},
	'load': function() {

		// filtertool
		if ($chk($('filtertool'))) {
			var objProductFilter = new ScrollMenu({
				'elCategoryPane': $('filtertool').getElement('div.categorypane'),
				'elScrollingPane': $('filtertool').getElement('div.scrollingpane'),
				'elScrollButtonUp': $('filtertool').getElement('a.arrows.up'),
				'elScrollButtonDown': $('filtertool').getElement('a.arrows.down'),
				'strDefaultCategory': null 
			});

			// fix footer links
			var footer = $('footer');
			if (!$defined(footer)) return false;
			
			var links = footer.getElement('div.col_2of3').getElements('a');
			
			links.each(function(link) {
			link.removeEvents();
				link.addEvents({
					'click': function(event) {
						event.stop();
						var url = '/'+link.get('href');
						window.location.href = url;
						window.location.reload();
					}
				})				
			});
		}

		// google maps
		if($('routemap')) { 
			loadGoogleMaps(); 
		};
		
		// worldmap
		loadWorldmap(); 
		
		// trendbox
		initTrendBoxSlideshow();
		
		// productdetail carrousel
		initProductDetailCarrousel();
		
		// product detail accordion
		initProductDetailAccordion();
		
		// combined inputs
		intiCombinedInputs();
		
		// IE6 warning
		initIE6Warning();
	},
	'unload': function() {
		
		// google maps unloader
		if($('routemap')) { GUnload(); };
	
	}
});


/**
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @uses <a href="http://www.efocus.nl/" class="external">eFocus</a>
 */
function initExternalLinks() {
	var arrExternalLinks = $$('a.external');
	if (arrExternalLinks.length == 0) return false;
	
	arrExternalLinks.each(function(elExternalLink) {
		elExternalLink.addEvent('click', function(event) {
			event.stop();
			window.open(this.get('href'));
		});
	});
}


/**
 * Replaces the HTML font of a heading with a sIFR variant.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @return void
 */
function replaceHeadingFonts() {
	var museo = {
		src: '/fileadmin/templates/swf/museo.swf'
		,ratios: [8, 1.17, 13, 1.16, 19, 1.18, 23, 1.17, 29, 1.19, 34, 1.18, 57, 1.19, 58, 1.2, 62, 1.19, 63, 1.2, 67, 1.19, 69, 1.2, 70, 1.19, 74, 1.2, 75, 1.19, 79, 1.2, 80, 1.19, 84, 1.2, 85, 1.19, 89, 1.2, 90, 1.19, 94, 1.2, 95, 1.19, 99, 1.2, 100, 1.19, 1.2]
	};
	
	// sIFR.domains = ['naturespride.com'] // Don't check for domains while in development
	sIFR.fitExactly = true;
	sIFR.useStyleCheck = false;
	sIFR.activate(museo);
	
	sIFR.replace(museo, {
		selector: '.heading'
		,wmode: 'transparent'
		,css: [
			'.sIFR-root { color: #241651; }'
			,'a { text-decoration: none; }'
			,'a:link { color: #241651; }'
			,'a:hover { color: #950095; }'
		]
	});
	
	sIFR.replace(museo, {
		selector: '.heading_highlight'
		,wmode: 'transparent'
		,css: [
			'.sIFR-root { color: #339900; }'
			,'a { text-decoration: none; }'
			,'a:link { color: #339900; }'
			,'a:hover { color: #950095; }'
		]
	});
}


/**
 * loads google maps into container and puts custom marker on it
 * 
 * @author: Rocco Janse, <rocco@efocus.nl>
 * @return: void
 */
function loadGoogleMaps() {
	if (GBrowserIsCompatible()) {

		// define map and geocoder
		var map = new GMap2($('routemap'));

		// create marker icon
		var icon = new GIcon();
		icon.image = '/fileadmin/templates/img/icon_nproute.png';
		icon.shadow = '/fileadmin/templates/img/icon_nproute_shadow.png';
		icon.iconSize = new GSize(96, 33);
		icon.shadowSize = new GSize(96, 33);
		icon.iconAnchor = new GPoint(48, 33);
		icon.infoWindowAnchor = new GPoint(5, 1);		

		// create home location
		var point = new GLatLng(51.9583092, 4.2240221);
		map.setCenter(point, 15);
		map.addOverlay(new GMarker(point, icon));
	}
}


/**
 * Loads google maps and puts multiple markers on it.
 * 
 * @author: Ralph Meeuws, <ralph.meeuws{AT}efocus.nl>
 * @author: Richard van Yperen, <richardvy{AT}efocus.nl>
 * @return: void
 */
function loadWorldmap() {
	var elWorldmap = $(document).getElement('.worldmap');
	if (!elWorldmap) return false;
	var elWorldmapHolder = elWorldmap.getElement('.mapholder');
	if (!elWorldmapHolder) return false;
	
	var arrMapLocations = elWorldmap.getElement('.map_locations').getElements('li');
	var arrLocationDetails = new Array;
	var arrLocationMarkers = new Array;
	
	if (GBrowserIsCompatible()) {

		// Initalize map
		var map = new GMap2(elWorldmapHolder);
		map.setCenter(new GLatLng(0, 0), 0);
		var bounds = new GLatLngBounds();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		// create marker icon
		var icon = new GIcon();
		icon.image = '/fileadmin/templates/img/icon_mapmarker.png';
		icon.shadow = '/fileadmin/templates/img/icon_mapshadow.png';
		icon.iconSize = new GSize(20, 34);
		icon.shadowSize = new GSize(37, 34);
		icon.iconAnchor = new GPoint(8, 37);
		icon.infoWindowAnchor = new GPoint(0, 0);
		
		// create locations
		function createMarker(point, index, html, locationUid){
			var marker = new GMarker(point, icon);
			marker.value = index + 1;
			bounds.extend(point);
			
			GEvent.addListener(marker, "click", function() {
				markerEvents(point, index, html, locationUid);
			});
			return marker;
		}
		
		markerEvents = function(point, index, html, locationUid) {
			// goto mapmarker
			map.setCenter(point, 8);
			
			// open infowindow
			map.openInfoWindowHtml(point, html, {
				maxWidth: 400,
				maxHeight: 120
			});
			
			//activate location listitem
			arrMapLocations.each(function(elMapLoc, mapLocIndex){
				if (mapLocIndex == index) {
					elMapLoc.addClass('active');
				} else {
					elMapLoc.removeClass('active');
				}
			});
			
			// update grower specs (using AJAX)
			updateGrowerSpecs(locationUid);
		}
		
		updateGrowerSpecs = function(locationUid){
			// AJAX request:
			tx_np_pi12getGrowerSpecs(locationUid);			
		}
		
		// add all markers as events
		arrMapLocations.each(function(elMapLoc, index){
			var arrCoordinates = elMapLoc.getElement('.coordinates').get('text').split(',');	
			var locationUid = elMapLoc.getElement('.product_uid').get('text');		
			arrLocationDetails = [index, arrCoordinates[0].toFloat(), arrCoordinates[1].toFloat(), elMapLoc.getElement('div.infowindow_holder').get('html')]
			arrLocationMarkers.include(arrLocationDetails);
			elMapLoc.addEvent('click', function(){
				var point = new GLatLng(arrLocationMarkers[index][1], arrLocationMarkers[index][2]);
				markerEvents(point, index, arrLocationMarkers[index][3], locationUid);
			});
		});
		
		// load all markers
		arrMapLocations.each(function(elMapLoc, index){
			var point = new GLatLng(arrLocationMarkers[index][1], arrLocationMarkers[index][2]);
			var locationUid = elMapLoc.getElement('.product_uid').get('text');		
			map.addOverlay(createMarker(point, index, arrLocationMarkers[index][3], locationUid));
		});
		
		// center and fit map by zooming
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}
}


/**
 * Adds an accordion to the productdetail list.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @return void
 */
function initProductDetailAccordion() {
	var elProductDetailList = $(document).getElement('.productdetail_list');
	if (!elProductDetailList) return false;
	
	var ProductDetailAccordion = new Accordion(elProductDetailList.getElements('h2.handle'), elProductDetailList.getElements('div.panel'), {
		show: 0,
		alwaysHide: true,
		onActive: function(elHandle){
			elProductDetailList.getChildren().removeClass('active');
			elHandle.getParent().addClass('active');
		},
		onBackground: function(elHandle){
			if (elHandle.getParent().hasClass('active')) elHandle.getParent().removeClass('active');
		}
	});
}


/**
 * Adds a slideshow to the trendbox.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @return void
 */
function initTrendBoxSlideshow() {
	if (!$(document).getElement('.trendbox')) return false;
	
	var elTrendbox = $(document).getElement('.trendbox');
	
	var objSlideshow = new Carrousel({
		'viewport': elTrendbox.getElement('.trendbox_viewport'),
		'slidesContainer': elTrendbox.getElement('ul.trends_list'),
		'linkNext': elTrendbox.getElement('.carrousel_nav_next'),
		'linkPrevious': elTrendbox.getElement('.carrousel_nav_prev'),
		'slidesToShift': 1,
		'slidesToShow': 1		
	});
}


/**
 * Adds a carousel to the productdetail.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @return void
 */
function initProductDetailCarrousel() {
	if (!$(document).getElement('.productdetail_related_products')) return false;
	
	var elRelatedProducts = $(document).getElement('.productdetail_related_products');
	
	var objRelatedProductsCarrousel = new Carrousel({
		'viewport': elRelatedProducts.getElement('.carrousel_viewport'),
		'slidesContainer': elRelatedProducts.getElement('ul.carrousel_holder'),
		'linkNext': elRelatedProducts.getElement('.carrousel_nav_next'),
		'linkPrevious': elRelatedProducts.getElement('.carrousel_nav_prev'),
		'slidesToShift': 1,
		'slidesToShow': 3		
	});
}


/**
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @uses <a href="http://www.efocus.nl/" class="external">eFocus</a>
 */
function intiCombinedInputs(){
	var arrCombinedInputs = $$('.combined_input');
	if (arrCombinedInputs.length == 0) return false;
	
	arrCombinedInputs.each(function(elCombinedInput){
		if (elCombinedInput.getElement('input').value == '') elCombinedInput.getElement('label').removeClass('hidden');
		
		elCombinedInput.getElement('input').addEvents({
			'focus': function(){
				elCombinedInput.getElement('label').addClass('hidden');
			},
			'blur': function(){
				if (elCombinedInput.getElement('input').value == '') {
					elCombinedInput.getElement('label').removeClass('hidden');
				} else {
					elCombinedInput.getElement('label').addClass('hidden');
				}
			}
		});
	});
}

/**
 * Add url parameter to send-a-friend link
 * 
 * @author Richard van Yperen <richardvy{AT}efocus.nl>
 */
function initSendAFriendLink() {
	if (!$(document).getElement('.sendafriendlink')) return false;
	
	var elSendAFriendLink = $(document).getElement('.sendafriendlink');	
	var href = elSendAFriendLink.get('href');
	elSendAFriendLink.set('href', href+'?tx_np_pi14[url]='+escape(window.location));
}

/*
 * initIE6Warning
 * 
 * checks for IE6 browser then injects warning
 *
 * @author Rou-hun Fan (lowen[AT]efocus.nl)
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 */
 
function initIE6Warning() {
	
	if(!Browser.Engine.trident || Browser.Engine.version > 4 ){
		return false;
	}
	
	var ie6warning = new Element('div', {
		'class'	: 'ie6warning'
	});
	
	if (Cookie.read('ie6warningClosed') != 'warned') {
		var ie6warningInner = '';
		ie6warningInner += '	<div class="inner" style="zoom: 1;">\n';
		ie6warningInner += '\n';	
		ie6warningInner += '		<p>Your browser is outdated. We recommend a newer version of <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer</a>, <a href="http://www.mozilla.com">Firefox</a> or <a href="http://www.apple.com/safari/download/">Safari</a>.</p>\n';
		ie6warningInner += '		<a href="#" class="close">Close</a>\n';
		ie6warningInner += '</div>\n';

		ie6warning.set('html', ie6warningInner);
		ie6warning.inject(document.body, 'top');		
	}	
	
	var closeIE6WarningLink = $(document).getElement('a.close');
	
	if (closeIE6WarningLink) {
		closeIE6WarningLink.addEvent('click', function(event){
			event.stop();
			$(document).getElement('div.ie6warning').destroy();
			var strCookieDomain = window.location.host;
	
			Cookie.write('ie6warningClosed', 'warned',  {duration: false, 'domain': strCookieDomain, 'path': '/'});
		});	
	}
}


