// TO change the number of items in the carousel, the following must change:
//1.carousel_items_per_page
//2.DisplayWindowWidth = number of col per page * 142
//3.In carousel css, change:
// CarouselContainer width = DisplayWindowWidth + 22
// CarouselInner width = DisplayWindowWidth + 20
// Change size of Backgroung image = CarouselContainer width
// CarouselButtonRight left = CarouselContainer + 12

var carousel_items_per_page = 6

function CarouselItem(Number, Guid, Photo, TextLine1, TextLine2)
{
	this.Nummer = Number
	this.Guid = Guid
	this.Photo = Photo
	this.TextLine1 = TextLine1
	this.TextLine2 = TextLine2
	this.ImageLoaded = false
	this.URL = "javascript: mobileforms = window.open('ShowProperty.aspx?Propid="+this.Guid+"','SAOH','toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=850,height=1050,left=0,top=0'); return false;";
}


	
CarouselItem.prototype.LoadImage = function()
{
	if (!this.ImageLoaded)
	{
		document.getElementById('Image' + this.Nummer).src = this.GetPhoto('240')
		this.ImageLoaded = true
	}
}
	
CarouselItem.prototype.getHTML = function()
{
	var sTextLine1Temp = ( this.TextLine1.length > 22 ? this.TextLine1.substring(0,19) + '...' : this.TextLine1 )	
	var s = ''
	sEnter = '\n'
	if ( navigator.userAgent.indexOf('Firefox') > -1 ) sEnter = ''
	s += '	<a href="#" onclick="' + this.URL + '">'
	s += '		<img id="Image' + this.Nummer + '" border="0" '
	if ( this.Nummer < carousel_items_per_page )  // if the item is on the first page, then load the source, else don't load the source
	{
		s += 'src="' + this.GetPhoto('240') + '"'
		this.ImageLoaded = true
	}
	else
	{
		s += 'src="images/empty.gif"'
	}
	s += ' width="142" height="80" alt="" /><br>'
	s += '		<nobr><b>' + sTextLine1Temp + '</b></nobr><br />'
	s += '		R&nbsp;' + this.TextLine2 + ''
	s += '	</a>'
    return s
}

CarouselItem.prototype.GetPhoto = function()
{
	s = this.Photo
	if ( s.indexOf('http') == -1 ) {
	   var lastDivider = document.location.href.lastIndexOf('/');
	   var baseUrl =  document.location.href.substr(0,lastDivider+1);	   
	   s = baseUrl+s;
	}
	return s
}

function Carousel()
{
	this.delay = 25
	this.Page = 1
	this.bTurned = false
	this.bMoving = false
	this.Interval = false
	this.acceleration = 3
	this.arrObjects = new Array()
	this.DisplayWindowWidth = 426
	this.v = 0
	this.vmax = 0
	this.a = 0
	this.x = 0
	this.nNumberOfObjects = 0
    this.calculateVMax();
}    

Carousel.prototype.calculateVMax = function()
{
   var x = 0;
   var vmax_calc = 0;
   for(var i=0;i<240;i++)  {
     vmax_calc += this.acceleration
     x += vmax_calc
     if (x*2 > this.DisplayWindowWidth)  {
	   this.vmax = vmax_calc
	   this.Factor = this.DisplayWindowWidth / (x*2)
       break;
     } 
   }
}

Carousel.prototype.AddObject = function(Guid, Photo, TextLine1, TextLine2)
{
	var number = this.arrObjects.length
	this.arrObjects[number] = new CarouselItem(number, Guid, Photo, TextLine1, TextLine2)
}

Carousel.prototype.Start = function()
{
	this.nNumberOfObjects = this.arrObjects.length
	this.xMax = 0
	this.NumberOfPages = Math.ceil(this.nNumberOfObjects / carousel_items_per_page)
	this.xMin = - (this.NumberOfPages-1) * (142 * 5)
	var s = ''
   	
	for ( var i=0; i<this.nNumberOfObjects; i++ )
	{
         if (i%2==0) {
           s += '<span class="CarouselItem" style="left:' + (-10+Math.round(i/2)*142) + 'px;">';
         }         
		 s += this.arrObjects[i].getHTML(i)+((i%2==0)?'<br><br>':'');
         if (i%2!=0) {
           s += '</span>';
         }         
	}
    if (this.nNumberOfObjects%2==0) {
       s += '</span>';
    }
	document.getElementById('CarouselContent').innerHTML = s
	this.SetButtons()
}
	
Carousel.prototype.SetButtons = function()
{
	if ( this.nNumberOfObjects > 5 )
	{
		document.getElementById('CarouselButtonRight').style.display = ( this.Page < this.NumberOfPages ? 'block' : 'none' )
		document.getElementById('CarouselButtonLeft').style.display = ( this.Page > 1 ? 'block' : 'none' )
		var s = 'Pages: '
		for ( var i=1; i<=this.NumberOfPages; i++ ) s += '<a href="javascript:Carousel.GotoPage(' + i + ')"' + (this.Page == i ? ' class="CarouselCurrentPage"' : '') + '>&nbsp;' + i + '&nbsp;</a>'
		document.getElementById('CarouselPagenumber').innerHTML = s
	}
}

Carousel.prototype.Mouseover = function(sDirection)
{
	if (document.getElementById('Carousel_Img_' + sDirection))
	{
		document.getElementById('Carousel_Img_' + sDirection).src = 'images/' + sDirection + '_hover.gif'
	}
}

Carousel.prototype.Mouseout = function(sDirection)
{
	if ( document.getElementById('Carousel_Img_' + sDirection) )
	{
		document.getElementById('Carousel_Img_' + sDirection).src = 'images/' + sDirection + '.gif'
	}
}
	
Carousel.prototype.MouseDown = function(_ax)
{
	this.GotoPage(this.Page+_ax)
}

Carousel.prototype.GotoPage = function(n)
{
	if (!this.bMoving)
	{
		this.bMoving = true
		this.a = ( this.Page>n ? 1 : -1 ) * this.acceleration
		this.PageMove = Math.abs(this.Page - n)
		this.Page = n
		if (this.Page > this.NumberOfPages) this.Page = this.NumberOfPages
		if (this.Page < 1) this.Page = 1
		for ( var i=0; i<carousel_items_per_page; i++ )
		{		    
			xTemp2 = (this.Page * carousel_items_per_page ) + i - carousel_items_per_page
			if ( xTemp2 < this.nNumberOfObjects )
			{
				this.arrObjects[xTemp2].LoadImage()
			}
		}
		if ( !this.Interval ) this.Interval = setInterval('Carousel.Move()',this.delay)
	}
}
	
Carousel.prototype.Move = function()
{
	this.x += this.v * this.Factor * this.PageMove		
	var xRounded = Math.round(this.x)
	if (this.x > this.xMax) this.x = this.xMax
	if (this.x < this.xMin) this.x = this.xMin
	if (this.bTurned == false && (Math.abs(this.v) == this.vmax))
	{
		this.a = -this.a
		this.bTurned = true
	}
	else
	{
		this.v += this.a
	}
	if ( this.v == 0 )
	{
		this.bTurned = false
		this.a = 0
		clearInterval(this.Interval)
		this.bMoving = false
		this.Interval = false
		this.SetButtons()
	}
	document.getElementById('CarouselContent').style.left = xRounded + 'px'
}
