function formatNumbers(number){
	

	var ret = number.toString()
	if(ret.length > 3){
		ret = ret.substring(0,ret.length-3) + "." + ret.substring(ret.length-3)					
	}
	
	return ret

}

var destinations = {}

trips.sort(function(a,b){
	destinations[a.Country] = true
	destinations[b.Country] = true
	
	if(typeof(a.Date) == "string"){
		var dData = a.Date.split("T")[0].split("-")
		a.Date = new Date(dData[0],dData[1]-1,dData[2])
	}
	if(typeof(b.Date) == "string"){
		var dData = b.Date.split("T")[0].split("-")
		b.Date = new Date(dData[0],dData[1]-1,dData[2])
	}
	return a.Date.getTime() == b.Date.getTime() ? parseInt(a.Price) > parseInt(b.Price) ? 1 : -1 : 0
//	return parseInt(a.Price) > parseInt(b.Price) ? 1 : -1 
//	return  parseFloat(a.Stars) === parseFloat(b.Stars) ? (parseFloat(a.Price) > parseFloat(b.Price) ? 1 : -1 ) : ( parseFloat(a.Stars) < parseFloat(b.Stars) ? 1  : -1 )
})



var destinationsArr = []
for( var x in destinations){
	destinationsArr.push(x)
}


var toProcess = []
var months = ["Januar","Febuar", "Marts", "April", "Maj","Juni","Juli","August","September","Oktober","November","December"]



function renderSet(set){
	var html = "";
	var added = 0;
	var currentDate = new Date(1971,0,1)

	var container = $('#tripContainerPre')
	
	if(set.length == 0){
		container.append('<h3>Der er desvære ingen rejser der matcher din søgning</h3>')
	}

	var html = [];
	for( var a=0;a<set.length;a++){
		var trip = set[a];
		if(trip.Date.getTime() != currentDate.getTime()){
			html.push(
					'<h1>' +
					["Søndag", "Mandag", "Tirsdag", "Onsdag","Torsdag","Fredag","Lørdag"][trip.Date.getDay()] + 
					" den " +
					trip.Date.getDate() + " " +   
					months[trip.Date.getMonth()] + " " + 
					trip.Date.getFullYear() + 
					'</h1>'
			)
			currentDate = trip.Date
		}

		if(trip.Date.getTime() == currentDate.getTime()){
			var starsHTML = ""
			for( var b=0;b<parseInt(trip.Stars);b++){
				starsHTML += '<img src="/i/g.gif"/>'
			}
			if(trip.Stars*1 != parseInt(trip.Stars)){
				starsHTML += '<img src="/i/h.gif"/>'
			}
			html.push('<div class="entry">')
			html.push('<h2><a target="_new" href="'+trip.BookPath+'"><b>'+trip.Name+'</b>, '+trip.Area+', '+trip.Country+'</a> '+starsHTML+'</h2><div class="airport">Fra '+{CPH:'København', BLL:'Billund',AAL:'Ålborg'}[trip.Airport]+'</div><div style="clear:both"></div>')
			html.push('<div class="el"><img width="133" height="82" src="/i/temp.jpg"><a target="_new" href="'+trip.BookPath+'"><img width="133" height="24" src="/i/o.gif"></a></div>')
			html.push('<div class="er"><div class="days'+(trip.Period != 7 ? " long" : "")+'">'+(trip.Period != 7 ? " 2 Uger" : "1 Uge")+'</div><h3>'+formatNumbers(trip.Price)+',-</h3><h4>Per person, ved køb af to billetter.</h4></div>')

			var morehtml = '<div class="more"><p>'+unescape(trip.Description)+'</p>'

			if(trip.Pool) morehtml += '<div><b>Pool</b>: '+trip.Pool+'</div>'
			if(trip.Beach) morehtml += '<div><b>Strand</b>: '+trip.Beach+'</div>'
			if(trip.Kidspool) morehtml += '<div><b>Børnepool</b>: '+trip.Kidspool+'</div>'
			if(trip.Centrum) morehtml += '<div><b>Lokalt centrum</b>: '+trip.Centrum+'</div>'
			if(trip.Restaurant) morehtml += '<div><b>Restaurant</b>: '+trip.Restaurant+'</div>'
			if(trip.Bar) morehtml += '<div><b>Bar</b>: '+trip.Bar+'</div>'

			html.push(morehtml + '</div>')
			html.push('<div style="clear:both"></div></div>')
			
			if(++added==2){
				container.html(html.join(''))
				html = []
				container = $('#tripContainerPost')
			}
			
		}
		

		
		
		
	}
	if(html.length > 0)
		container.html(html.join(''))
}


function showSubset(e){
	var path = "/"

	var airport = $('#airportSelector').val()
	if(airport!=0)path+=airport+"/"
	
	path+=$('#daySelector').val()+"/"
	path+=months[$('#monthSelector').val()-1]+"/"

	var days = $('#periodSelector').val()
	if(days!=0)path+=days+"d/"

	var location = $('#destinationSelector').val()
	if(location!=0)path+=location+"/"

	top.location = path
//	changeSubset()
}

function changeSubset(e){
	$('#tripContainer').html('')
	var airport = $("#airportSelector").val()

	var set = [];
	for( var a=0;a<trips.length;a++){
		var trip = trips[a]
		if(trip.Airport ==  airport || airport == 0){
			var date = new Date(2009, $('#monthSelector').val()-1, $('#daySelector').val())
			if(date.getTime() <= trip.Date.getTime()){
				var period = parseInt($('#periodSelector').val())
				if(period == 0 || period == trip.Period){
					var destination = $('#destinationSelector').val()
					if(destination == "0" || destination == trip.Country){
						set.push(trip)
					}
				}
			}
		}
	}
	
		
	renderSet(set.splice(0,50))
}