$(document).ready( function(){	

	// Only init history if SliceDice is available
	if ( SliceDice.init() ){
		TBhistory.init();	
	}

});

/*
 * TBHistory runs the show here. Its 'pageload' function is called after: 
 * 		- a pageload (of course) 
 * 		- calling the add function 
 * 		- clicking the browsers back or forward button
 */ 
var TBhistory = function(){

	function init(){
		$.historyInit(pageload);
	}
	
	function pageload(hash){
		SliceDice.doAction( hash );
	}
	
	function add( hash ){
		$.historyLoad(hash);
	}
	return {
		init:init,
		add:add
	};
	
}();

/*******************************************************************************
 * 
 * SliceDice contains all functionality for filtering of searchresults
 * 
 * Predefined arrays in html template:
 * 
 * ResultArray/ TotalArray 
 * 		0 allotmentgroupid 
 * 		1 bit1 
 * 		2 bit2 
 * 		3 bit3 
 * 		4 minPrice /totalPrice
 * 		5
 * 		6 
 * 		7 OriginalPrice
 * 
 * facilities 
 * 		0 facilitygroupid 
 * 		1 name 
 * 		2 bit1 
 * 		3 bit2 
 * 		4 bit3 
 * 		5 number found in current resultarray
 * 		6 in current filterset
 * 
 * facilitygroups 
 * 		0 exclusive 
 * 		1 name 
 * 		2 show because at least 1 of the facilities has available allotments 
 * 		3 show because it is not exclusive, or exclusive and non selected
 * 
 * filters
 * 
 * 
 ******************************************************************************/ 
var SliceDice = function(){
	
	// Keep a reference to some DOM Elements, to minimize DOM access
	var searchResultsEl = null;	
	var searchContainer = null; // contains everyting associated with search
	var viewContainer	= null;	// contains everyting associated with viewing the group
	var mapContainer	= null;	// contains everyting associated with viewing result on the map
	var favouritesContainer	= null;	
	var sideSearch = null;
	var sideView = null;
	
	var sideBanner = null;
	var detailEl = null;
	var appliedFilterList = null;
	var appliedFiltersContainer = null; 
	var detailViewAction = 'ag';
	
	// Current viewstate: Possible values at the moment: search - view - map
	var currentView = '';  
	// Store the request string here to prevent making the exacte same request twice
	var lastRequestString = '';
	
	// Obj that contains search properties like limit & searchType
	var searchProperties = {};
	var currentPage = 1;
	
	// Store id of the ag we're currently viewing ( added so we can determine which one is next)
	var currentResultId = 0; 
	
	// If used more than once, store ids of elements here please
	var conf = {
		filterlist : '#filterlist'		// Where to append the selectable filters UL to
	};

	var buildCompleted = false;

	function init(){

		setSearchProperties();

		if ( typeof(totalArray) === 'undefined' || !totalArray.length  || parseInt( searchProperties.disableSlicedice) ){
			// When totalArray is empty we show some things that are normally only shown with javascript
			// Slice & dice won't be enabled though
			$('#search').css('visibility', 'visible');
			$('#side-search').show();
			return false;
		}
	
		// Allotmentgroups and companies get their results from different urls: adjust that here if needed
		if (searchProperties.searchType == 'company'){
			detailViewAction = 'comp';	
		}

		// Define some elements we'll use a lot
		searchResultsEl		= $('#search-results');
		detailEl 			= $('#single-allotmentgroup');
		searchContainer 	= $('#search');
		viewContainer 		= $('#view');
		mapContainer 		= $('#map');
		favouritesContainer = $('#favourites');
		sideView			= $('#side-view');
		sideSearch			= $('#side-search');
		sideBanner			= $('.side-banner');
		initTabs();
		
		// While in slice & dice, span.current in breadcrumbs will change the view 'to search'
		$('#breadcrumbs span.current').click( function(){
			setView('search');
			return false;
			
		}).css('cursor', 'pointer');
		return true;
	}
	
	function build( filter1, filter2, filter3, page){

		if ( filter1 == undefined || filter2 == undefined || filter3 == undefined ){
			buildFilterList();
		}else{
			setFilters(filter1, filter2, filter3);
			applyfilters( );
			updateResultSet( page )
		}

		attachResultBoxEvents();
		buildPagination();
		buildCompleted = true;
	}

	function attachResultBoxEvents(){
		
		$('div.resultbox h3 a').live('click', function(e){
			// In case of a right click, allow user to open link in a new tab
			if (e.button == 2){	return true; }
			
			var anchor = $(this);
			var url = cleanUrl( anchor.attr('href') );
			
			currentResultId = anchor.attr('id').replace( 'res-', '');
			goTo( 'view-' + url);
			return false;
		
		});
		
		// Bind click and mouseover events in such a way that dynamically loaded results will also get this behaviour
		$('div.resultbox').live('click', function(e){

			// If this is a right click it probably bubbled up from the link don't do anyting
			if (e.button == 2){ return true; }

			// Also let links go 
			if (e.target.tagName == 'A' && $(e.target).hasClass('link') ){
				return true;
			}

			// Hotels have multiple allotmentgroups, they have a specifix link we should follow 
			if( e.target.tagName == 'A' && e.target.className == 'roomlink' ){
				
				var anchor = $(e.target);
				var url = cleanUrl ( anchor.attr('href') );			
				goTo( 'view-' + url);
				return false;	
			}
			// Just trigger the click event of the link (in h3)
			$(this).find('h3').eq(0).children('a').eq(0).click();
			return false;
			
		}).live('mouseover', function(){
			$(this).addClass('hover');
		}).live('mouseout', function(){			
			$(this).removeClass('hover');	
		}).css('cursor', 'pointer');
		
		// Favourites
		$('div.resultbox a.favourites, #overview a.favourites').live( 'click',  function(){
			var anchor = $(this);
			var allotmentgroupId = anchor.attr('id').replace('fav-add-', '');
			addToFavourites( allotmentgroupId, anchor);
			return false;
		});
		
		$('div.resultbox a.del-favourites').live( 'click',  function(){
			var anchor = $(this);
			var allotmentgroupId = anchor.attr('id').replace('fav-del-', '');
			removeFromFavourites( allotmentgroupId, anchor);
			return false;
		});

		$('ul#favourites-list li a').live('click', function(){
			var url = $(this).attr('href');
			goTo( 'view-' + url);
			return false;
		});
	
		$('#prev-result').live( 'click', function(){
			viewPrevious();
			return false;
		} );
		$('#next-result').live( 'click', function(){
			viewNext();
			return false;
		} );

		$('#back-to-search').live('click', function(){
			var filterHash = createCurrentFilterHash();
			goTo('search-P' + currentPage + filterHash );
			return false;
		});
		
	}
	
	// Fill the searchProperties obj based on values provided in the form
	function setSearchProperties(){

		var inputs = $('#search-properties').find('input[type=hidden]');
		for ( var i = 0, j=inputs.length; i<j; i++ ){
			var input = inputs.eq(i);
			searchProperties[input.attr('name')] = input.val();  
		}
	}
	
	var filters = [];
	
	function setFilters(filter1, filter2, filter3 ){
		filters = [];
		for (var i=0; i<facilities.length; i++){
			if ( ( (facilities[i][2] & filter1) == facilities[i][2]) && ((facilities[i][3] & filter2) == facilities[i][3]) && ((facilities[i][4] & filter3) == facilities[i][4]) ){
				facilities[i][6] = true;
				filters.push(i);
			} else {
				facilities[i][6] = false;
			}
		}
	}
	
	function addfilter(iFilter){
		
		if (iFilter!=undefined){
		
			// Track the event
			var igroup = facilities[iFilter][0];
			var name = facilities[iFilter][1];
			var groupname = facilitygroups[igroup][1];
			TexelTracker.trackEvent("Add filter", groupname, name );
			
			// Add the filter
			filters.push(iFilter);
			facilities[iFilter][6] = true;

		}
		updateCurFilters();
	}

	function removefilter(iFilter){

		if (iFilter!=undefined){
			
			// Track the event
			var igroup = facilities[iFilter][0];
			var name = facilities[iFilter][1];
			var groupname = facilitygroups[igroup][1];
			TexelTracker.trackEvent("Remove filter", groupname, name );

			for (var i=0; i<filters.length; i++){
				if (filters[i]==iFilter){
					break;
				}
			}
			if (i<filters.length-1){
				filters = filters.slice(0,i).concat(filters.slice(i+1));
			} else if (i==filters.length-1){
				filters = filters.slice(0,-1);
			}
			facilities[iFilter][6] = false;
		}
		updateCurFilters();
	}

	function createCurrentFilterHash(){
		var tmp = []; // faster string concat
		tmp.push( 'A', curFilter1, 'B', curFilter2, 'C', curFilter3, 'S', sortMinPrice);
		return tmp.join('');
	}
	
	// Define filters here, so that they are available within other functions
	var curFilter1 = 0; 
	var curFilter2 = 0;
	var curFilter3 = 0;
	var sortMinPrice = 0;
	
	function applyfilters(){

		updateCurFilters();
		resultArray = new Array();
		for (var i=0; i<totalArray.length; i++){
			if ( ((totalArray[i][1] & curFilter1) == curFilter1) && ((totalArray[i][2] & curFilter2) == curFilter2)  && ((totalArray[i][3] & curFilter3) == curFilter3) ){
				resultArray.push(totalArray[i]);
			}
		}

		if( sortMinPrice ){
			resultArray.sort( function(a,b){
				return a[4] - b[4]; 
				
			});		
		}
		
		buildFilterList();
	}

	function updateCurFilters(){
		
		curFilter1 = 0;
		curFilter2 = 0;
		curFilter3 = 0;
	
		for (var i=0; i<filters.length; i++){
			curFilter1 += facilities[filters[i]][2];
			curFilter2 += facilities[filters[i]][3]; 
			curFilter3 += facilities[filters[i]][4]; 
		}
	}
	
	function count(){
		for (var j=0; j<facilities.length; j++){
			facilities[j][5]=0; 
			for (var i=0; i<resultArray.length; i++){
			    if ( ((resultArray[i][1] & facilities[j][2]) == facilities[j][2]) && ((resultArray[i][2] & facilities[j][3]) == facilities[j][3]) && ((resultArray[i][3] & facilities[j][4]) == facilities[j][4]) ){
					facilities[j][5]++;
				}
			}
		}
	}

	function buildFilterList(){

		count();
		var filterlist = $(conf.filterlist);
		if (!filterlist.length) { 
			return false; 
		}
	
		filterlist.html('');
		
		var groupContainer = null;
		var currentGroupId = -1;
		
		// Reset show criteria for facilitygroup
		for (var a in facilitygroups){
			facilitygroups[a][2] = false; // Show only when at least one member has allotments
			facilitygroups[a][3] = false; // Exclusive group : user can only select one of this facilityGroup
		}

		for (var i=0, j=facilities.length; i<j; i++){
			
			if (facilities[i][0] != currentGroupId){
				if (groupContainer){

					if (facilitygroups[currentGroupId][0] && facilitygroups[currentGroupId][3]  ){
						// This in an exlusive group with one item selected. We have to adjust the display and remove the click event				
						groupContainer.find('li:not(.applied) ').children('a').unbind('click').addClass('exclusive');
					}
					if ( facilitygroups[currentGroupId][2] ) {
						filterlist.append(groupContainer);
					}
				}
				currentGroupId = facilities[i][0];
				groupContainer = $('<div><h4>' + facilitygroups[currentGroupId][1] + '</h4></div>');
				var ul = $('<ul></ul>');
				groupContainer.append(ul);
			}

			if (!facilities[i][6]){ 
				
				// If it's not in current filterset
				var listItem = $('<li></li>');
	
				if (facilities[i][5]> 0){ 
					// so there are allotments with this facility, so this facilitygroup has at least 1 member with allotments
					facilitygroups[currentGroupId][2] = true;
					var link = $('<a></a>').bind( 'click', i, function(e){
						addfilter(e.data);
						var filterHash = createCurrentFilterHash();
						goTo(currentView +'-P' + currentPage + filterHash );
						
					});
					link.html( '<span>('+facilities[i][5]+')</span>' + facilities[i][1] );
					listItem.append(link);
				}else{
					// No remaining results with this facility
					listItem.html( '<span>(' + facilities[i][5]+')</span>' + facilities[i][1] );
				}
				ul.append(listItem);

			} else {
				
				// so the facility is in the current filter set
				// always add the filter list if it is already in the filter list
				facilitygroups[currentGroupId][2] = true;
				if (facilitygroups[currentGroupId][0] == 1){
					// if the group is exclusive don't show it
					facilitygroups[currentGroupId][3] = true;
				}
				
				var listItem = $('<li class="applied"><a>' + facilities[i][1] + '</a></li>').bind( 'click', i, function(e){
					removefilter(e.data);
					goTo('search-P' + currentPage + createCurrentFilterHash() );
				});
				ul.append(listItem)
			}			
		}
			
		if (groupContainer){
			filterlist.append(groupContainer);
		}
	}

	function buildAppliedFilterList(){
		
		if(!appliedFilterList){
			appliedFiltersContainer = $('#applied-filters');
			appliedFilterList = $('<ul></ul>').appendTo( appliedFiltersContainer );
		}else{
			appliedFilterList.html('');
		}
		
		if (filters.length){
			// Add all applied filters to a list,and attach function on click event to remove it
			for (var i = 0, j=filters.length; i < j; i++ ){
				$('<li>' + facilities[filters[i]][1] + '</li>').bind( 'click', i, function(e){
					removefilter( filters[e.data] );
					var filterHash = createCurrentFilterHash();
					goTo('search-P' + currentPage + filterHash );
				}).appendTo(appliedFilterList);
			}
			appliedFiltersContainer.show();
		}else{
			appliedFiltersContainer.hide();	
		}
	}

	var companyResults = null;
	
	function updateResultSet( p ){

		var data = searchProperties;
		
		// Set pagination values
		var page = p || 1;
		var limit = parseInt(searchProperties.limit) || 1000 ;
		
		// Make sure the page is in range
		totalPages = resultArray.length % limit == 0 ?  resultArray.length / limit : Math.floor(resultArray.length/limit) + 1;
		if ( page > totalPages ){ page = totalPages; }
		
		var rangeStart = (page-1) * limit;
		var rangeEnd = rangeStart + limit;
		
		switch ( searchProperties.searchType ){
		
			case 'company':
				
				var ids = [];
				// Simply get all allotmentgroups in ResultArray and in range
					
				for ( var i=rangeStart, j=resultArray.length; i<j && i < rangeEnd ; i++){
					ids.push( 'C', resultArray[i][0]);
				}
				data.ids = ids.join('');	

				break;
		
		
			case 'house':
				
				var ids = []
				// Simply get all allotmentgroups in ResultArray and in range
				
				for ( var i=rangeStart, j=resultArray.length; i<j && i < rangeEnd ; i++){
					ids.push(
							'A', resultArray[i][0],
							'MP', resultArray[i][4],
						    'OP', resultArray[i][7],
						    'CNF', resultArray[i][5] // ConfigurationId
					);
				}
				data.ids = ids.join('');	
				break;
				
			case 'hotel':
								
				var orgObj = buildOrgObj();
				
				// Build the string that represents the orgs we want to fetch
				var ids = [];
				
				for( orgId in orgObj ){
					ids.push( 'ORG', orgId ); // Set the orgId
		
					// For each ag, provide min- and original price
					for ( var k = 0, l= orgObj[orgId].allotmentGroups.length; k<l; k++){
						
						ids.push(
							'A', orgObj[orgId].allotmentGroups[k][0], 	// AllotmentgroupId
							'MP', orgObj[orgId].allotmentGroups[k][4], 	// MinPrice 
							'OP' , orgObj[orgId].allotmentGroups[k][7]	// OriginalPrice
						);
					}
				}
				
				data.ids = ids.join('');

				break;


			default:	
				break;
		}

		if ( lastRequestString == data.ids && !sortMinPrice ){
			// No need to refetch the results
			setView('search');
			setLoading(false);
			return false;
		}

		lastRequestString = data.ids;
		setLoading(true);
		
		var action = 'ags/';
		if( searchProperties.searchType == 'company'){
			action = 'comps';
		}
		Ajax.doRequest( action , data, handleUpdateResult );
		currentPage = page;
	}
	
	function handleUpdateResult( resp ){

		searchResultsEl.html( resp.data.html );	

		// When user filters while viewing the map, we stay in mapview and refresh the map
		if ( currentView == 'map' ){
			SearchMap.refresh();
		}else{
			setView('search');		
		}
		setLoading(false);	
	}
	
	
	// Build an array of organisations that have an array of allotmentGroups within that org
	function buildOrgArray(){
		
		// Function is deprecated; buildOrgObj is now the preferred method
		
		var orgArray = new Array();
		var currentOrg = null;
		
		for (var i = 0, j = resultArray.length; i<j; i++){
			
			if ( !currentOrg || (resultArray[i][6] != currentOrg.id ) || (resultArray[i][6]== 0 ) ){
				// If this is the first allotmentgroup with this orgId, create an organisation obj which we can append ags to
				// Add the previous org to the orgArr
				if (currentOrg){
					orgArray.push(currentOrg);
				}

				currentOrg = {
					id: resultArray[i][6],
					allotmentGroups : new Array()
				};
			}
			currentOrg.allotmentGroups.push( resultArray[i] );
		}
		// Add the last one
		if (currentOrg){
			orgArray.push(currentOrg);
		}

		return orgArray;
	}
	
	
	// Build an array of organisations that have an array of allotmentGroups within that org
	function buildOrgObj(){
		
		var orgs = {};
		
		for (var i = 0, j = resultArray.length; i<j; i++){
			var orgId = resultArray[i][6];
			if( !orgs[orgId] ){
				orgs[orgId] = {
					allotmentGroups : []		
				}
			}
			orgs[orgId].allotmentGroups.push( resultArray[i] ); 
		}
		return orgs;
	}

	
	// Tabs to switch between search, map and favourites
	function initTabs(){
		
		$('#search-tabs li a').click( function(){
			
			var a = $(this);
			var id = a.attr('id');
			
			switch (id){
			
				case 'show-results' : 
					setView('search');
					return false;
					
				case 'show-map' :
					setView('map');			
					return false;
					
				case 'show-favourites':
					setView('favourites');
					return false;		
			}
		});			
	}
	

	
	function viewDetails( hash ){

		setLoading(true);
		Ajax.doRequest( detailViewAction + hash, '', handleviewDetails);
		// Track pageview event
		TexelTracker.trackPageView( hash );
	}
	
	function viewNext(){
		
		if (currentResultId){
			// Find the next resultbox in the dom and trigger the click
			var result = $( '#res-' + currentResultId ).parents('div.resultbox:first').next();
			if ( result.length ){
				// We can just trigger the click event
				result.click();
		
			}else if( total > currentPage ){
				setLoading(true);
				// There are some more results we can fetch, so update resultset
				goTo( 'search-P' + (currentPage+1) + createCurrentFilterHash() );
			}
		}

	}

	function viewPrevious(){
		
		if (currentResultId){
			// Find the netx resultbox in the dom and trigger the click
			var result = $( '#res-' + currentResultId ).parents('div.resultbox:first').prev();
			if ( result.length ){
				result.click();
			}else if( currentPage != 1 ){
				// There are some more results we can fetch, so update resultset
				goTo( 'search-P' + (currentPage-1) + createCurrentFilterHash() );
			}else if ( currentPage == 1){
				setView('search');
			}
		}
	}

	function handleviewDetails( resp ){
		
		// Show picture of next and previous result. We get these from dom.
		var currentResultBox = $( '#res-' + currentResultId ).parents('div.resultbox:first');
		
		var prevResultImageSource  = currentResultBox.prev().children('img').attr('src');
		log(prevResultImageSource);	
		if ( prevResultImageSource != ''){
			$('#prev-result-img').show().attr('src', prevResultImageSource );
		}else{
			$('#prev-result-img').hide();
		}
		
		var nextResultImageSource  = currentResultBox.next().children('img').attr('src');
		if( nextResultImageSource  != '' ){
			$('#next-result-img').show().attr('src', nextResultImageSource ); 	
		}else{
			$('#next-result-img').hide();
		}
		
		
	
		
/*
			
		if (currentResultId){
			// Find the next resultbox in the dom and trigger the click
			var 
			if ( result.length ){
				// We can just trigger the click event
				result.click();
		
			}else if( total > currentPage ){
				setLoading(true);
				// There are some more results we can fetch, so update resultset
				goTo( 'search-P' + (currentPage+1) + createCurrentFilterHash() );
			}
		}
		*/
		
		
		
		detailEl.html( resp.data.html );
		Tabs.init();
		setView('view');
		setLoading(false);

	}

	var paginationInited = false;
	var nextLink = null;
	var prevLink = null;
	
	var total = 0;
	var paginationContainers = null;
	function buildPagination(){
	
		if (!paginationContainers){
			paginationContainers = $('div.pagination'); 
		}

		paginationContainers.show();
		
		// Calculate pagination values
		total = resultArray.length;
		var limit = parseInt( searchProperties.limit );
		var pages = Math.ceil(total/limit);
		
		var firstInCurrentSet = ( (currentPage-1) * limit ) + 1;
		var lastInCurrentSet = Math.min( (currentPage * limit), total ); 

		// Display numbers in text
		$('ul.p-total-pages', paginationContainers).text( pages );
		$('span.p-total-results', paginationContainers).text( total );

		$('span.p-first-in-set', paginationContainers).text( firstInCurrentSet );
		$('span.p-last-in-set', paginationContainers).text( lastInCurrentSet );
			
		if (!paginationInited){

			$('a.p-sort').click( function(){

				// Sort results and go to first result in set
				sortMinPrice = 1;
				$(this).addClass('active');
				goTo( 'search-P' + 1 + createCurrentFilterHash());
				return false;
			});
			
			if ( searchProperties.searchType == 'hotel'){
				// For hotel, we're done now
				paginationInited = true;
				return true;
			}
			
			// Bind page link events once here, with 'live'			
			$('ul.p-pages li').live( 'click', function(){
				if ( currentPage == parseInt( $(this).text()) ){
					return false;
				}
				goTo( 'search-P' + $(this).text() + createCurrentFilterHash() );					
				scroll(0,0);
			});
			
			nextLink = $('a.p-next').click( function(){
				goTo( 'search-P' + Math.min( (currentPage+1), pages ) + createCurrentFilterHash() );
				scroll(0,0);
			});
			
			prevLink = $('a.p-previous').click( function(){
				goTo( 'search-P' + Math.max( (currentPage-1), 1 ) + createCurrentFilterHash());
				scroll(0,0);
			});
			

			paginationInited = true;
		}
		
		if ( searchProperties.searchType == 'hotel'){
			// For hotel, we're done now
			return true;
		}
		
		
		// Hide next and previous when needed
		if ( currentPage == pages ){
			nextLink.css('visibility', 'hidden');
		}else{
			nextLink.css('visibility', 'visible');
		}
		if ( currentPage == 1){
			prevLink.css('visibility', 'hidden');
		}else{
			prevLink.css('visibility', 'visible');
		}
		
		// Build a list with all page numbers
		var toShow = 5;
		
		var firstToShow = Math.max( (currentPage -  Math.floor(toShow/2)  ), 1);
		var lastToShow = Math.min( Math.max ( currentPage + Math.floor(toShow/2), toShow ), pages);

		var items = [];
		for ( var i = firstToShow; i <= lastToShow ; i++ ){
			if( currentPage != i ){
				items.push('<li>', i, '</li>');
			}else{
				items.push('<li class="current">', i, '</li>');
			}
		}

		$('ul.p-pages').html( items.join('') );

		return true;
	}
	
	function setView( type ){

		viewContainer.hide()
		searchContainer.hide();
		mapContainer.hide();
		favouritesContainer.hide();
		searchResultsEl.hide();
		sideView.hide();
		sideSearch.hide();
		sideBanner.hide();
		if (paginationContainers){	
			paginationContainers.hide();
		}
		$('#search-tabs li').removeClass('current');
		switch ( type ){
	
			case 'search':
				searchContainer.show().css('visibility', 'visible');
				searchResultsEl.show();
				mapContainer.hide();
				sideSearch.show();
				sideBanner.show();
				if (paginationContainers){	
					paginationContainers.show();
				}
				$('#show-results').parent('li').addClass('current');
				break;
 
			case 'view':
				//scroll(0,110)
				viewContainer.show();
				sideView.show();
				break;

			case 'map':
				searchContainer.show().css('visibility', 'visible');
				searchResultsEl.hide();
				mapContainer.show();
				SearchMap.show();
				sideSearch.show();
				sideBanner.show();
				if (paginationContainers){	
					paginationContainers.show();
				}
				$('#show-map').parent('li').addClass('current');
				break;
								
			case 'favourites':
				searchContainer.show().css('visibility', 'visible');
				searchResultsEl.hide();
				favouritesContainer.show();
				sideBanner.show();
				sideSearch.show();
				$('#show-favourites').parent('li').addClass('current');
				getFavourites();
				break;

			default:
				return;
		}
		currentView = type;
	}

	function setLoading(loading){
		// Use global loading routine
		util.setLoading(loading);
	}
	
	function addToFavourites( allotmentgroupId, anchor ){
		
		Ajax.doRequest( 'addtofav', {agId:allotmentgroupId}, function(){
			anchor.hide();
			setLoading(false);
		} );
	}
	
	function removeFromFavourites( allotmentgroupId, anchor ){
		Ajax.doRequest( 'removefromfav', {agId:allotmentgroupId}, function(){
			anchor.hide();
			getFavourites();
			setLoading(false);
		} );
	}
	
	function getFavourites(){
		
		Ajax.doRequest( 'getfavs', {}, function( resp ){
			favouritesContainer.html( resp.data.html );
			setLoading(false);	
		} );
	}
	
	function cleanUrl( url ){
		
		// IE will sometimes get this wrong by providing the absolute url, not the href attribute 
		if ( url.indexOf('http') > -1 ){
			// Remove http and the domain
			url = url.replace('http://' + window.location.hostname , '');
		}
		return url;
	}
	
	function goTo( hash ){
		setLoading(true);
		TBhistory.add( hash );
	}
	
	function doAction( hash ){

		var action = hash.substring( 0, hash.indexOf('-') );
		var param = hash.substring( hash.indexOf('-')+1 );

		switch ( action ){

			case 'view' :
				if (!buildCompleted){
					// If user came here via a direct link, we need to build the Slice & Dice first
					build();
				}
				viewDetails( param );
				break;

			case 'search':
			case 'map':
				// P2	A57786	B72361	C685 S1
				// Page - Filter1 - Filter2 - Filter3 - SortOnPrice
				
				var matchP = param.match('P[0-9]+');
				var page 	= matchP ? parseInt( matchP[0].substr(1) ) : 0;
				
				var matchA = param.match('A[0-9]+');
				var filter1 = matchA ? parseInt( matchA[0].substr(1) ) : 0;
				
				var matchB = param.match('B[0-9]+');
				var filter2 = matchB ? parseInt( matchB[0].substr(1) ) : 0;
				
				var matchC = param.match('C[0-9]+');
				var filter3 = matchC ? parseInt( matchC[0].substr(1) ) : 0;

				var matchMinPrice = param.match('S[0-1]+');
				sortMinPrice = matchMinPrice ? parseInt( matchMinPrice[0].substr(1) ) : 0;

				build( filter1, filter2, filter3, page );
				// set the view to map or search
				setView( action );
				break;
			
			default:
				build();
				setView('search');
				break;
		}

	}

	// Reveal public functions here.
	return {
		init: init,
		doAction: doAction,
		getDetailViewAction: function(){
			//	Tabs needs this to know where to gets its content from
			return detailViewAction; 
		},
		searchProperties:searchProperties,
		filters: filters,
		// For use in SearchMap
		buildOrgArray:buildOrgArray,
		cleanUrl: cleanUrl
	};
	
}();


var Tabs = function (){

	var currentTabLink = null;
	var tabsContainer = null; 
	var tabCache = {};
	
	function init(){
		
		// Let links load new tabs instead of following the link
		$('ul#ag-tab-list li a').bind( 'click', openTab );
		
		$('#book-link, #book-link-contact').live( 'click' , function(){
			$('#prices-link').click();
			return false;
		});
		tabsContainer = $('#ag-tab');
		tabCache = {};
	}
	
	function openTab(e){
		if (e.button == 2) return true; // Allow rightclick
		
		$('ul#ag-tab-list li').removeClass('current');
		
		currentTabLink =  $(this); // keep track of currently openened tab
		var li = currentTabLink.parent();

		li.addClass('current');

		var tabId = currentTabLink.attr('id');
		var tabName = tabId.substring ( 0, tabId.indexOf('-') );
		
		// Did we get this tab before?
		if ( tabCache[tabName] ){
			// Yes we did, show it
			tabsContainer.children().hide();
			tabCache[tabName].show();
		
		}else{

			// No we didn't, so get it
			setLoading( true );
			var url = SliceDice.cleanUrl( currentTabLink.attr('href') );
			TexelTracker.trackPageView(url);
			Ajax.doRequest( SliceDice.getDetailViewAction() + url, '', handleGetTab );
		}

		return false;
	}
	
	function handleGetTab( resp ){
		
		var tabName = resp.data.tab;
		tabsContainer.children().hide();
		
		// Add tab to cache
		tabCache[tabName] = $('<div></div>').html( resp.data.html ).appendTo( tabsContainer ); 
		
		switch( tabName ){
		
			//case 'map':	we now do this in the template	
				//	AllotmentGroupMap.init();
			case 'prices':	
					Calendar.init();
			case 'photos':	
				PhotoViewer.init();
			}
		
		setLoading(false);
	}

	function setLoading( loading ){
		// use default loading
		util.setLoading( loading );
	}
	
	function getCurrentTabUrl(){
		return currentTabLink ? currentTabLink.attr('href') : '';
	}
	
	return {
		init:init,
		getCurrentTabUrl: getCurrentTabUrl // Calendar needs this to know the url to fetch an updated calendar 
	};
	
}();

var SearchMap = function(){
	
	var map = null;
	var manager = null;
	var mapId = 'google-map';

	var tooltip = null;
	var infoWindow = null;

	var detailContainer = null;
	
	var activeMarker = null; // Were we store which marker was clicked
	
	// Will be filled with some properties we set in tpl (by calling getSettings )
	var settings = {};
	
	function init(){
		if ( map ) return true;
		getSettings();
		MapUtils.loadAPI( buildMap );
	}
	
	function buildMap(){
		if (map){
			return false;
		}

		// Create the map
		map = new GMap2(document.getElementById( mapId ));
		map.setCenter( new GLatLng( 53.092375,4.817505), 10 );
		map.setUIToDefault();

		// create the MapManager
		manager = new MarkerManager(map);
		MapUtils.restrictViewAreaToTexel( map );
		tooltip = $('<div id="tooltip"></div>').appendTo( $('#' + mapId ) );
		detailContainer = $('#search-map-detail');
		setLoading( false );
		refreshMarkers();
	}
	
	function show(){
		init();
	}
	
	// Fill the mapProperties obj based on values provided in the form
	function getSettings(){

		var inputs = $('#map-properties').find('input[type=hidden]');

		for ( var i = 0, j=inputs.length; i<j; i++ ){
			var input = inputs.eq(i);
			settings[input.attr('name')] = input.val(); 
		}
		
	}
	
	function refreshMarkers(){

		var limit = settings.limit;
		var data = {} ;

		var searchType = SliceDice.searchProperties.searchType;
		data.searchType = searchType;
		data.ids = '';

		if (  searchType == 'house'){
			
			// Build a string with array for performance reasons
			var ids = [];
			for ( var i=0, j=resultArray.length; i<j && i<=limit; i++){
				ids.push(
						'A',  resultArray[i][0], 
						'MP', resultArray[i][4], 
						'OP' , resultArray[i][7] 
				);
			}
			data.ids = ids.join('');
			
		}else if( searchType == 'hotel'){
			
			var orgArray = SliceDice.buildOrgArray();
			
			// Build the string that represents the orgs we want to fetch
			var ids = [];
			for ( var i = 0, j=orgArray.length; i<j ;i++ ){

				// Set the orgId
				ids.push('ORG', orgArray[i].id ); 
				
				// For each ag, provide min- and original price
				for ( var k = 0, l= orgArray[i].allotmentGroups.length; k<l; k++){
					ids.push( 
						'A', orgArray[i].allotmentGroups[k][0], // AllotmentgroupId
						'MP', orgArray[i].allotmentGroups[k][4],// MinPrice 
						'OP', orgArray[i].allotmentGroups[k][7] // OriginalPrice
						)
				}
			}
			
			data.ids = ids.join('');
			
			
		}

		setLoading(true);
		Ajax.doRequest('map/', data, handleRefreshMarkers );
	}
	
	// forMarker is the google GMarker obj. reqMarker is our obj we retrieved from backend
	function getSingleResult( forMarker, reqMarker ){

		util.setLoading(true);
		
		var searchType = SliceDice.searchProperties.searchType; 
		var data = {};
		
		data.ids = ''
		data.searchType = searchType;
		if (searchType == 'house'){
			data.ids = 'A' + reqMarker.id + 'MP' + reqMarker.minPrice + 'OP' + reqMarker.originalPrice;	
		}else if (searchType == 'hotel'){
						
			// Set the orgId
			data.ids += 'ORG'; 
			data.ids += reqMarker.id;

			// Get all orgs, so we can filter out the one we need to view
			var orgArray = SliceDice.buildOrgArray();
			
			for ( var i=0; i< orgArray.length; i++ ){
				
				if (orgArray[i].id == reqMarker.id ){					

					for ( var k = 0, l= orgArray[i].allotmentGroups.length; k<l; k++){
						data.ids += 'A' + orgArray[i].allotmentGroups[k][0]; 	// AllotmentgroupId
						data.ids += 'MP' + orgArray[i].allotmentGroups[k][4]; 	// MinPrice 
						data.ids += 'OP' + orgArray[i].allotmentGroups[k][7];	// OriginalPrice
					}
					break;
				}
			}
			
		}

		Ajax.doRequest('ags/', data, function(resp){

			if (!detailContainer){
				return false;
			}
			
			// Add some animation to clarify refreshing of result
			if ( !detailContainer.html().length ) {
				detailContainer.html(resp.data.html).show('slow');	
			}else{
				detailContainer.fadeOut();
				
				detailContainer.fadeOut(200, function(){
					detailContainer.html(resp.data.html);
					detailContainer.fadeIn(400);
				});
			}
			
			// If we already have an activemarker, revert the icon
			if (activeMarker){
				activeMarker.backToNormal();
			}
			
			// Set the picked icon to active
			forMarker.setImage(reqMarker.iconActiveUrl);
			
			// Assign new activeMarker an
			activeMarker = forMarker;
			
			activeMarker.backToNormal = function(){
				this.setImage(reqMarker.iconUrl);				
			};
			util.setLoading(false);
			
		} );
	}
	
	function handleRefreshMarkers( resp ){
		

		/*		
		var markers = [];
		for( var i= 0; i< resp.data.markers.length; i++ ){

			//var pos = new GLatLng( resp.data.markers[i].lat , resp.data.markers[i].lng );			
			// Create the marker 		
			//markers.push( new GMarker( pos) );
			markers.push( MapUtils.createMarker(resp.data.markers[i]) );
			
			//map.addOverlay(new GMarker( pos));
		}
		
		manager.clearMarkers();
		manager.addMarkers( markers, 5 );
		manager.refresh();
		setLoading( false );
			*/

		var batch = MapUtils.createMarkers( resp.data.markers, {
			tooltip: tooltip,
			infowindow:infoWindow,
			map:map
		});
		
		manager.clearMarkers();
		manager.addMarkers( batch, 5 );
		manager.refresh();
		setLoading( false );

	}

	function setLoading( load ){
		util.setLoading( load );
	}

	return {
		init:init,
		show:show,
		refresh: refreshMarkers,
		getSingleResult: getSingleResult,
		buildMap: buildMap
	};

}();

var AllotmentGroupMap = function(){
	
	var mapId = 'ag-map-container';
	var map = null;
	var manager = null; // MarkerManager instance
	
	var lastBoundingBox = null;  
	var lastParams = null;
	var tooltip = null;
	var infoWindow = null;

	var queryInput = null;  // Where user can type text to filter
	var queryDefaultValue = ''; // Where user can type text to filter

	function init(){
		util.setLoading(true);
		MapUtils.loadAPI( build );
	}

	function build(){
		
		var lat = parseFloat( $('#lat').val() );
		var lng = parseFloat( $('#lng').val() );
		var agLocation = new GLatLng( lat, lng);

		// Create the map
		map = new GMap2( document.getElementById( mapId  ) );
		map.setCenter(agLocation , 13 );
		map.setUIToDefault();
		map.enableContinuousZoom(); 
		
		MapUtils.restrictViewAreaToTexel( map );
		
		// Put allotmentgroup marker on the map
		map.addOverlay(new GMarker(agLocation));

		// Create the MapManager
		manager = new MarkerManager(map);
		
		// Create elements for overlay and tooltip
		tooltip = $('<div id="tooltip"></div>').appendTo( $('#' + mapId ) );
		infoWindow = $('<div id="info-window"></div>').appendTo( $('#' + mapId ) );
		
		GEvent.addListener(map, 'dragend', refresh );
		
		// Refresh when zooming out
		GEvent.addListener(map, 'zoomend', function( oldLevel, newLevel){
			if ( oldLevel > newLevel ){
				refresh();
			}
		});

		// Refresh map when an option has changed ( don't use 'change' event here cause it doesn't work properly in IE)
		$('ul#companies input').click( refresh );

		initKeywordFilter();
		refresh();
	}
	
	function initKeywordFilter(){
		
		queryInput = $('#map-query');
		// don't want the form to submit
		queryInput.parent('form').submit( function(){ return false; });
		
		queryDefaultValue = queryInput.val();
		
		queryInput
		.keyup( refresh )
		.focus( function(){
			var input = $(this);
			if ( input.val() == queryDefaultValue ){
				input.val('').addClass('active');
			}
		})
		.blur( function(){
			var input = $(this);
			if( input.val() == ''){
				input.val( queryDefaultValue ).removeClass('active');
			}
		});
	}
	
	function refresh(){

		// Which company types do we want ?
		var types = '';
		$('ul#companies input').each( function(){
			var input = $(this);
			if ( input.attr( 'checked' ) ){
				types += 'T' + input.attr('id').replace('type-', ''); 
			}
		});
		
		// Get the visible area of the map
		var bounds = map.getBounds();
		var NE = bounds.getNorthEast();
		var SW = bounds.getSouthWest();
		
		var searchQuery = queryInput.val() != queryDefaultValue ? queryInput.val() : '';
		var params = {
			lat1: NE.lat(),
			lng1: NE.lng(),
			lat2: SW.lat(),
			lng2: SW.lng(),
			types: types,
			q: searchQuery
		};

		if ( lastBoundingBox && params.types == lastParams.types && params.q == lastParams.q && lastBoundingBox.containsBounds(bounds) ){
			// We already got all markers for this area
			return;
		}
		
		lastBoundingBox = bounds;
		lastParams = params;
		util.setLoading(true);
		Ajax.doRequest( 'ag-map', params, handleRefresh );
		return false;
	}
	
	function handleRefresh( resp ){
		
		var markers = MapUtils.createMarkers( resp.data.markers, {
			tooltip: tooltip,
			infowindow:infoWindow,
			map:map
		} );

		manager.clearMarkers();
		manager.addMarkers( markers, 5);
		// Refresh MarkerManager 
		manager.refresh();
		util.setLoading(false);
	}
	
	return {
		init:init
	}
}();

// Here we put all functions that can be used in both maps
var MapUtils = function(){
	
	var loadCallback = null;
	var tooltip = null;
	var map = null;
	
	var markerClickHandler = null;
	
	// Loads the google maps api and Markermanager. 
	// If provided, the callback function will be called after completion 
	function loadAPI( callback ){
		
		if ( typeof(GMap2) != 'undefined' && typeof(MarkerManager) != 'undefined' ){
			if ( callback ){
				callback();
			}
		}
		
		// First get the Maps Api. After it has loaded, MapUtils.afterLoad() will be called.
		loadCallback = callback;
		
		if ( !$('#maps-api-url').length){ 
			return false;
		}
		util.setLoading( true );
		var url = $('#maps-api-url').val() + '&callback=MapUtils.afterLoad';
		$('body').append('<script type="text/javascript" src="' + url + '"></script>');

	}
	
	// Don't think the coordinates of Texel will change in the near future, so I figured I can place this here [len]
	function restrictViewAreaToTexel( gMap ){
		
		// Set texel bounding box
		var allowedArea = new GLatLngBounds(new GLatLng(52.95586368138139, 4.588852), new GLatLng(53.20745822897711, 4.972));

		GEvent.addListener( gMap , "move", function(){

			// Perform the check and return if OK
	        if ( allowedArea.containsLatLng(gMap.getCenter()) ) {
	        	return true;
	        }
	        // It`s not OK, so find the nearest allowed point and move there
	        var C = gMap.getCenter();
	        var X = C.lng();
	        var Y = C.lat();

	        var AmaxX = allowedArea.getNorthEast().lng();
	        var AmaxY = allowedArea.getNorthEast().lat();
	        var AminX = allowedArea.getSouthWest().lng();
	        var AminY = allowedArea.getSouthWest().lat();

	        if (X < AminX) {X = AminX;}
	        if (X > AmaxX) {X = AmaxX;}
	        if (Y < AminY) {Y = AminY;}
	        if (Y > AmaxY) {Y = AmaxY;}
	       
	        gMap.setCenter(new GLatLng(Y,X));

		});
		gMap.disableScrollWheelZoom();
		// Also we want to prevent zooming out too far
		
		var minMapScale = 9;
		// get array of map types
		var mapTypes = gMap.getMapTypes();

		// overwrite the getMinimumResolution() and getMaximumResolution() methods for each map type
		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return minMapScale;}
		}		
	}

	function afterLoad(){
		
		if ( !GBrowserIsCompatible ) { return false;} 
		
		if ( typeof( MarkerManager ) == 'undefined' ){
			// Only google api has been loaded, we need markermanager too
			$.getScript('/javascript/markermanager.js');
			return;
		}

		if ( loadCallback ){
			loadCallback();
		}
		loadCallback = null;
		util.setLoading( false );
	}
	
	function createMarkers( markers, mapOpts ){

		// Mapopts provided some elements we need - this way we can use all functions for both maps 
		tooltip = mapOpts.tooltip;
		map = mapOpts.map;

		// Create an array of markers
		var batch = [];
		for ( var i = 0, j = markers.length; i<j; i++ ){
			batch[i] = createMarker( markers[i] );
		}
		return batch;

	}
	
	function setMarkerClickHandler(func){
		markerClickHandler = func;
	}
	
	// Builds the icon to show on the map based on request marker attrs (could be a custom icon if defined )
	function buildIcon(reqMarker){
		
		var icon = new GIcon( G_DEFAULT_ICON );

		icon.iconSize = new GSize( reqMarker.iconWidth, reqMarker.iconHeight );
		if( !reqMarker.keepShadow ){
			icon.shadow =  '';	
		}
		
		// 0,0 means that top left corner of the iocon is the position. We have to move it somewhat to the top and left.
		var toLeft = parseInt(reqMarker.iconWidth)/2;
		var toTop = parseInt(reqMarker.iconHeight)/2;
		icon.iconAnchor = new GPoint(toLeft,toTop);
		
		if ( reqMarker.iconUrl != undefined && reqMarker.iconUrl != ''){
			icon.image = reqMarker.iconUrl;
		}
		
		return icon;
	}
	
	// reqMarker: the marker object returned by Ajax class
	function createMarker( reqMarker ){

		// Position of the marker 
		var pos = new GLatLng( parseFloat(reqMarker.lat), parseFloat( reqMarker.lng) );
		
		// Use our internal icon function
		var icon = buildIcon( reqMarker );
		
		// Create the marker 		
		var marker = new GMarker( pos, {
			title : reqMarker.name,
			icon : icon 
			}
		);

		// Show tooltip while hovering
		GEvent.addListener(marker, 'mouseover', function(){
			showToolTip( this.getTitle(),  map.fromLatLngToContainerPixel(this.getLatLng()) );
		});
		GEvent.addListener(marker, 'mouseout', hideToolTip );
		
		if ( typeof( markerClickHandler) == 'function'){
			
			// Open an infoWindow when clicked on a marker
			GEvent.addListener(marker, 'click', function( ){
				markerClickHandler( marker, reqMarker );
				return false;
			});
			
		}else{
			
			// Open an infoWindow when clicked on a marker
			GEvent.addListener(marker, 'click', function( ){
				SearchMap.getSingleResult( marker, reqMarker );
				return false;
			});
						
		}
		return marker;
	}
	
	
	function showToolTip( content, pos ){
		tooltip.html( content ).css({
			'top' : pos.y + 'px',
			'left' : pos.x + 'px'
		}).show();
	}
	
	function hideToolTip(){
		tooltip.hide().empty();	
	}
	
	function openInfoWindow( html ){
		infoWindow.html(html).show();
	}

	function closeInfoWindow(){
		if (infoWindow)infoWindow.hide().empty();
	}
	
	return {
		loadAPI : loadAPI,
		afterLoad: afterLoad,
		createMarkers:createMarkers,
		restrictViewAreaToTexel:restrictViewAreaToTexel,
		setMarkerClickHandler:setMarkerClickHandler,
		createMarker:createMarker
	}
}();

var Calendar = function(){
	
	var optionForm = null;
	var calendarContainer = null;
	
	function init(){
		optionForm = $('form#options');

		// When user changes an option, we update the calendar
		optionForm.find('select').change( update );
		calendarContainer = $('#date-picker').parent();

		// These make sure we can select the next month by changing the value in the arrival selectbox
		$('#next-month').click( function(){
			$('#arrival-select').val($('#arrival-select option:selected').next().val());
			update();
			return false;
		});
		$('#prev-month').click( function(){
			$('#arrival-select').val($('#arrival-select option:selected').prev().val());
			update();
			return false;
		});
		
		// Some visual enhancements
		$('table#calendar td.available').hover( function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
	}

	// Get a new calendar based on user input
	function update(){

		// Gather parameters from the form. Put them in an options obj which we'll pass to the request
		var formEls = optionForm.find('input, select');
		var options = {};
		formEls.each( function(){
			var el = $(this);
			options[el.attr('name')] = el.val() || 0;
		});
		
		util.setLoading(true);
		
		// Ask Tabs from which url he got this calendar
		var url = Tabs.getCurrentTabUrl();
		if(!url)url = document.location.pathname;
		url = SliceDice.cleanUrl(url);

		Ajax.doRequest( SliceDice.getDetailViewAction() + url, options, handleUpdate );

	}

	function handleUpdate( resp ){
	
		util.setLoading(false);
		calendarContainer.html( resp.data.html );
		Calendar.init();
	}
	
	
	var selectedTd = null;
	function showPriceSpecification( options, triggerTD  ){

		
		$('#no-price-selected').hide();
		if (selectedTd){
			selectedTd.removeClass('selected');
		}
		selectedTd = $(triggerTD);
		util.setLoading( true );

		// Fill specification with provided values
		$('#arrivalSpan').text( options.arrival );
		var duration = parseInt(options.duration);

		
		// Calc leave date
		// arrival is in dutch format
		var arr = options.arrival.split('-');		
		var day = parseInt( arr[0]);
		var month =  arr[1];
		var year = parseInt( arr[2]);
		
		// January is 0 in JS
		
		
		
		if ( month.indexOf('0') == 0 ){
			month = month.slice(1,2);
		}
		month = parseInt( month ) - 1;

		var leaveDate = new Date( year,month,day+duration );
		
		var leaveDay = leaveDate.getDate().toString();
		if (leaveDay.length == 1 ){
			leaveDay = '0' + leaveDay;
		}
		var leaveMonth = leaveDate.getMonth()+1;
		if (leaveMonth.toString().length == 1){
			leaveMonth = '0' + leaveMonth.toString();
		}
		var leaveString = leaveDay  + '-' + leaveMonth + '-' + leaveDate.getFullYear();
		
		$('#leaveSpan').text( leaveString );

		if (duration > 0) {
			$('#durationSpan').text( options.duration ).parents('li:first').show();
		} else {
			$('#durationSpan').parents('li:first').hide();
		}
		
		$('#allotment-count').text( options.allotment );
		$('#adultsSpan').text( options.adults );
		$('#babiesSpan').text( options.babies );
		$('#roomsSpan').text( options.rooms );

		$('#priceSpan').text( options.price.toString().replace('.', ',') );

		var reservationCosts = parseFloat( $('#reservationCosts').val() ).toFixed(2);
		if (reservationCosts == 0){
			$('#reservationCostsSpan').parents('li:first').hide();
		}else{
			$('#reservationCostsSpan').text( reservationCosts.toString().replace('.',',' ) ).parents('li:first').show();	
		}
		
		
		// Fill bookingform with right values
		var booking = document.forms.bookForm;
		
		booking.isVirtual.value 	= options.isVirtual;
		booking.accoPrice.value 	= options.price;
		booking.duration.value 		= options.duration;	

		var arrivalArr = options.arrival.split('-');
		booking.departure.value = parseInt(Number(arrivalArr[2]))+'-'+parseInt(Number(arrivalArr[1]))+'-'+parseInt(Number(arrivalArr[0]));
		booking.totalAdults.value 	= options.adults;
		booking.totalBabies.value	= options.babies;
		booking.totalAccommodations.value = options.rooms;
		booking.agesString.value	= options.agesString;
		
		// Baby Stuff
		var babyPriceSpan = $('#babyPriceSpan');
		var babyPrice = 0;
		if ( options.babies && babyPriceSpan.length ){
			
			var babyDiscountAmount = booking.babyDiscountAmount.value;
			var babyDiscount = booking.babyDiscount.value;

			// Wat doet dit?
			if ( parseFloat(babyDiscountAmount) > 0) {
				babyPrice = parseFloat( babyDiscountAmount ).toFixed(2);
			} else {
				babyPrice = parseFloat( parseFloat(options.price) * parseFloat(babyDiscount) / 100 ).toFixed(2);
			}

			babyPriceSpan.text( babyPrice );
			$('#babyPriceDiv').show();

		}else{
			$('#babyPriceDiv').hide();
		}
		
		// One Person extra price
		var onePersonExtraPrice = parseFloat(options.onePersonExtraPrice.replace(',', '.'));
		
		if ( onePersonExtraPrice ) {
			
			var totalOnePersonExtra = Math.max(0,(Math.max(options.rooms,1) * 2) - options.adults);
			var onePersonExtra = onePersonExtraPrice;
			var onePersonExtraPrice = parseFloat(totalOnePersonExtra * onePersonExtra).toFixed(2);
						
			$('#onePersonExtraSpan').text( onePersonExtraPrice );
			$('#onePersonExtraDiv').show();
		}else{
			$('#onePersonExtraDiv').hide();
		}
		
		var additionsPrice = 0;
		$('input.additional-cost').each( function(){
			additionsPrice += parseFloat( $(this).val() );
		});

		pricePerPerson = $('#pricePerPerson').val();
		var amountPrice = 1;
		if(pricePerPerson != '1'){
			onePersonExtraPrice = 0;
		}
		var amount = ( parseFloat( amountPrice ) * parseFloat(options.price.replace(',', '.')) ) + parseFloat( reservationCosts ) + parseFloat( onePersonExtraPrice ) + parseFloat( additionsPrice )	+ parseFloat( babyPrice * parseInt (options.babies)  );		
		var totalPrice = parseFloat( amount ).toFixed(2);
		
		$('#dateChosenDiv').show();
		$('#chooseDateDiv').hide();

		// Set parameters to pass to getAdditions script
		var additionParams = { totalPrice : totalPrice, allotmentgroupId: booking.allotmentgroupId.value, arrival: options.arrival, duration: options.duration, adults: options.adults, babies: options.babies, rooms: options.rooms };

		Ajax.doRequest('additions', additionParams, function( resp ){
			$('#price-specification li.addition').remove();
			$('#price-specification').append(resp.data.html ); 
			showReceipt( true );
			util.setLoading( false );
			selectedTd.addClass('selected');
		});
	}
	
	function showReceipt( show ){
		if (show){ 	$('#receipt').show();}
		else{		$('#receipt').hide();}
	}
	
	// public functions
	return {
		init:init,
		showPriceSpecification:showPriceSpecification
	};
	
}();


var PhotoViewer = function(){
	
	function init(){
		// Script below does the rest
		$.getScript('/javascript/thickbox.js');
	}
	
	return{
		init:init
	};
	
}();

// wrapper obj for GA tracking through js
var TexelTracker = function(){
	
	
	// Execute GA trackevent function in try-exept
	// 		_trackEvent(category, action, optional_label, optional_value)
	function trackEvent( category, action, label ){
		try {
			pageTracker._trackEvent(category, action, label);
		} catch (e) {}
		
		return false;
	}
	
	function trackPageView( pageUrl ){
		try {
			pageTracker._trackPageview( pageUrl );
		} catch (e) {}
		
		return false;
	}
	
	return{
		trackEvent:trackEvent,
		trackPageView:trackPageView
	};
}();
