var WIB = new Class_WIB()

function Class_WIB()
{
	this.WoningUrl = ''
	this.StatsUrl = ''
	this.stap = 0
	this.steps = 15
	this.delay = 15
	this.Pagina = 1
	this.bMoving = false
	this.Interval = false
	this.arrObjects = new Array()
	this.BeweegBreedte = 590
	this.nAantalObjecten = 0
	if ( navigator.userAgent.indexOf('Firefox') > -1 ) this.steps = Math.ceil(this.steps/2)
	this.steps *= 2
	this.nFactor = new Array()
	var x = 0
	var v = 0
	var a = 1
	for(var i=0; i<=this.steps; i++)
	{
		if (i == this.steps/2) a = 0
		if (i > this.steps/2) a = -1
		v+=a
		x+=v
		this.nFactor[i] = x
	}
	for(i=0; i<=this.steps; i++)
	{
		this.nFactor[i] /= x
	}
	
	this.SetWoningUrl = function(sUrl)
	{
		if (sUrl.indexOf('?') == -1) sUrl += '?id=[id]'
		if (sUrl != '') this.WoningUrl = sUrl
	}

	this.SetStatsUrl = function(sUrl)
	{
		if (sUrl.indexOf('?') == -1) sUrl += '?stats=[stats]'
		if (sUrl != '') this.StatsUrl = sUrl
	}

	this.Class_WibObject = function(nummer, Guid, Foto, Adres, Postcode, Plaats, Prijs, ObjectType)
	{
		this.Nummer = nummer
		this.Guid = Guid
		this.Foto = Foto
		this.Adres = Adres
		this.Postcode = Postcode
		this.Plaats = Plaats
		this.Prijs = Prijs
		this.ObjectType = ObjectType
		this.ImageLoaded = false
		this.URL = WIB.WoningUrl
		this.URL = this.URL.replace(/\[id\]/g, this.Guid)
		this.URL = this.URL.replace('[objecttype]', this.ObjectType)
		
		// Bij de eerste 5 is de bStatsRegisteren true. Bij de rest false
		this.bStatsRegistered = (nummer < 5)
		
		this.LoadImage = function()
		{
			if ( this.ImageLoaded == false )
			{
				document.getElementById('Image' + this.Nummer).src = this.Foto
				this.ImageLoaded = true
			}
		}
		
		this.GetStatRegStr = function()
		{
			if ( this.bStatsRegistered == false )
			{
				this.bStatsRegistered = true
				return this.Guid
			}
			else
			{
				return ''
			}
		}
		
		this.getHTML = function()
		{
			var sPlaatsTemp = ( this.Plaats.length > 15 ? this.Plaats.substring(0,13) + '...' : this.Plaats )
			var s = ''
			sEnter = '\n'
			if ( navigator.userAgent.indexOf('Firefox') > -1 ) sEnter = ''

			s += '<div class="wib-object" style="left:' + (WIB.BeweegBreedte/5)*this.Nummer + 'px;" align="center"><a href="' + this.URL + '" class="sbox_black"><img id="Image' + this.Nummer + '" '
			if ( this.Nummer < 10 )
			{
				s += 'src="' + this.Foto + '"'
				this.ImageLoaded = true
			}
			else
			{
				s += 'src="images/spacer.gif"'
			}
			s += ' width="90" height="68" alt="'+this.Adres + ' ' +this.Plaats+'" class="thumb" title="' + this.Adres + sEnter
			s += this.Plaats + '" />' + sPlaatsTemp + '<br />' + this.Prijs + '</a></div>'

			return s
		}
	}	
	
	this.AddObject = function(Guid, Foto, Adres, Postcode, Plaats, Prijs, ObjectType)
	{
		var nummer = this.arrObjects.length
		this.arrObjects[nummer] = new this.Class_WibObject(nummer, Guid, Foto, Adres, Postcode, Plaats, Prijs, ObjectType)
	}

	this.Start = function()
	{
		this.nAantalObjecten = this.arrObjects.length
		this.xMax = 0
		this.AantalPaginas = Math.ceil(this.nAantalObjecten / 5)
		this.xMin = - (this.AantalPaginas-1) * (this.BeweegBreedte)
		var s = ''
		for ( var i=0; i<this.nAantalObjecten; i++ )
		{
			 s += this.arrObjects[i].getHTML(i)
		}
		document.getElementById('wib-inner').innerHTML = s
		this.SetButtons()
	}
	
	this.SetButtons = function()
	{
		if ( this.nAantalObjecten > 5 )
		{
			document.getElementById('Wib-arrow-r').style.display = ( this.Pagina < this.AantalPaginas ? 'block' : 'none' )
			document.getElementById('Wib-arrow-l').style.display = ( this.Pagina > 1 ? 'block' : 'none' )
		}
		else
		{
			document.getElementById('Wib-arrow-r').style.display = 'none'
			document.getElementById('Wib-arrow-l').style.display = 'none'
		}
	}

	this.Mouseover = function(oImg)
	{
		oImg.src = oImg.src.replace('0.gif', '1.gif')
	}

	this.Mouseout = function(oImg)
	{
		oImg.src = oImg.src.replace('1.gif', '0.gif')
	}
	
	this.Click = function(ax)
	{
		if ( this.bMoving == false )
		{
			var paginaVorige = this.Pagina
			this.Pagina += ax
			if ( this.Pagina > this.AantalPaginas ) this.Pagina = this.AantalPaginas
			if ( this.Pagina < 1 ) this.Pagina = 1
			if ( paginaVorige == this.Pagina ) return
			this.bMoving = true
			
			sStats = ''
			for ( var i=0; i<5; i++ )
			{
				xTemp2 = (this.Pagina * 5 ) + i - 5
				if ( xTemp2 < this.nAantalObjecten )
				{
					this.arrObjects[xTemp2].LoadImage()
					s = this.arrObjects[xTemp2].GetStatRegStr()
					sStats += s + ( s == '' ? '' : ',' )
				}
			}
			if ( sStats != '' ) 
			{
				//document.getElementById('WIB_Stats').src = this.StatsUrl.replace('[stats]', sStats) + '&nocache=' + new Date()
			}
			if ( !this.Moving ) this.Move(0, ax)
			this.bMoving = true
		}
	}
	
	this.Move = function(stap, direction)
	{
		this.x = this.nFactor[stap] * direction * this.BeweegBreedte + (this.Pagina - direction - 1) * this.BeweegBreedte

		this.xLocation = Math.round(this.x)
		if ( this.x > this.xMax ) this.x = this.xMax
		if ( this.x < this.xMin ) this.x = this.xMin
		
		document.getElementById('wib-inner').style.left = -this.xLocation + 'px'
		if ( stap == this.steps )
		{
			this.bTurned = false
			this.a = 0
			clearInterval(this.Interval)
			this.bMoving = false
			this.Interval = false
			this.SetButtons()
			this.PaginaStap = 1
		}
		else
		{
			stap ++
			setTimeout('WIB.Move(' + stap + ', ' + direction + ')',15)
		}
	}


}