/// favourites stuff

	function saveToFavourites( carId ) {
		var target = baseHref + 'ajax.php';
	    var params = 'type=addToWishList&carId=' + carId;

	    var myAjax = new Ajax.Request(target,
	    {
	        method: 'post',
	        parameters: params,
	        onSuccess: function (request) { alert( request.responseText ); rebuildFavourites(); },
	        onFailure: function (request) { alert( request.responseText ) }
	    });
	}
	
	function removeFromFavourites( carId ) {
		var target = baseHref + 'ajax.php';
	    var params = 'type=removeFromWishList&carId=' + carId;

	    var myAjax = new Ajax.Request(target,
	    {
	        method: 'post',
	        parameters: params,
	        onSuccess: function (request) { rebuildFavourites();  },
	        onFailure: function (request) {  }
	    });
	    
	    // reload the used car compare page
	    location.reload(true);
	    
	}
	
	function rebuildFavourites(){
		var target = baseHref + 'ajax.php';
	    var params = 'type=rebuildWishList';
	    
	    var myAjax = new Ajax.Request(target,
	    {
	        method: 'post',
	        parameters: params,
	        onSuccess: function (request) { 
				returnedValue = request.responseText.split( '####' );
	        	$('favouritesHolder').innerHTML = returnedValue[0];
	        	currentIndex = 0;
	        	totalCars = returnedValue[1];  
	        	
	        	if ( totalCars >= 0 ) {
	        		$('jumpSaveCompare').style.display = 'block';
	        	} else {
	        		$('jumpSaveCompare').style.display = 'none';
	        	}
	        	
	       	},
	        onFailure: function (request) {  }
	    });
	    	    
	}
	
	
	/****************************************
	*	News pager
	****************************************/
	
	var newsPager = {
	
		currentIndex: 0,
	
		init: function ( element, options ) {
	
			//  Arguments
			this.element 		= $( element );
	
			this.options 		= options || {};
			this.options.delay 	= options.delay || 4;
	
			//  Elements to manipulate
			this.numbers	= $$( '#' + this.element.id + ' div.controlItem a' );
			this.pages 		= $$( '#' + this.element.id + ' div.item' );
	
			//  Timer
			this.timer = new PeriodicalExecuter( this.goNext.bind( this ), this.options.delay );
	
			//  Click events for the numbers
			this.numbers.invoke( 'observe', 'click', this.numberClick.bind( this ) );
			$('newsPreviousButton').observe( 'click', this.goPrevious.bind( this ) );
			$('newsNextButton').observe( 'click', this.goNext.bind( this ) );
	
		},
		
		goPrevious: function () {
	
			//  Hide current page
			this.hidePage( this.currentIndex );
	
			//  Go to previous page
			this.currentIndex--;
	
			//  Gone too far?
			if ( this.currentIndex < 0 ) {
				this.currentIndex = this.numbers.length - 1;
			}
	
			//  Show new page
			this.showPage( this.currentIndex );
	
		},
		
		goNext: function () {
	
			//  Hide current page
			this.hidePage( this.currentIndex );
	
			//  Go to next page
			this.currentIndex++;
	
			//  Gone too far?
			if ( this.currentIndex >= this.numbers.length ) {
				this.currentIndex = 0;
			}
	
			//  Show new page
			this.showPage( this.currentIndex );
	
		},
	
	
		goPage: function ( index ) {
	
			this.stopTimer();
	
			return false;
	
		},
	
	
		hidePage: function ( index ) {
	
			this.numbers[ index ].removeClassName( 'active' );
			this.pages[ index ].hide();
	
		},
	
	
		numberClick: function ( ev ) {
	
			Event.stop( ev );
	
			el = ev.element();
			
			if ( el.tagName.toLowerCase() != 'div' ) {
				el = el.up(0);
			}
	
			this.stopTimer();
	
			this.hidePage( this.currentIndex );
	
			this.currentIndex = el.previousSiblings().length - 1;
	
			this.showPage( this.currentIndex );
			
			this.startTimer();
	
		},
	
	
		showPage: function ( index ) {
			this.numbers[ index ].addClassName( 'active' );
			this.pages[ index ].show();
		},
	
	
		stopTimer: function () {
	
			if ( this.timer !== false ) {
				this.timer.stop();
				this.timer = false;
			}
	
		},
		
		startTimer: function () {
	
			if ( this.timer === false ) {
				this.timer = new PeriodicalExecuter( this.goNext.bind( this ), this.options.delay );
			}
	
		}
	
	}
	
	
	/****************************************
	*	Home page image pager
	****************************************/
	
	var homeImagePager = {
	
		currentIndex: 0,
	
		init: function ( element, options ) {
	
			//  Arguments
			this.element 		= $( element );
	
			this.options 		= options || {};
			this.options.delay 	= options.delay || 4;
	
			//  Elements to manipulate
			this.numbers	= $$( '#' + this.element.id + ' div.imageControlItem a' );
			this.pages 		= $$( '#' + this.element.id + ' div.imageItem' );
	
			//  Timer
			this.timer = new PeriodicalExecuter( this.goNext.bind( this ), this.options.delay );
	
			//  Click events for the numbers
			this.numbers.invoke( 'observe', 'click', this.numberClick.bind( this ) );
			$('imagePreviousButton').observe( 'click', this.goPrevious.bind( this ) );
			$('imageNextButton').observe( 'click', this.goNext.bind( this ) );
	
		},
		
		goPrevious: function () {
	
			//  Hide current page
			this.hidePage( this.currentIndex );
	
			//  Go to previous page
			this.currentIndex--;
	
			//  Gone too far?
			if ( this.currentIndex < 0 ) {
				this.currentIndex = this.numbers.length - 1;
			}
	
			//  Show new page
			this.showPage( this.currentIndex );
	
		},
		
		goNext: function () {
	
			//  Hide current page
			this.hidePage( this.currentIndex );
	
			//  Go to next page
			this.currentIndex++;
	
			//  Gone too far?
			if ( this.currentIndex >= this.numbers.length ) {
				this.currentIndex = 0;
			}
	
			//  Show new page
			this.showPage( this.currentIndex );
	
		},
	
	
		goPage: function ( index ) {
	
			this.stopTimer();
	
			return false;
	
		},
	
	
		hidePage: function ( index ) {
	
			this.numbers[ index ].removeClassName( 'active' );
			this.pages[ index ].hide();
	
		},
	
	
		numberClick: function ( ev ) {
	
			Event.stop( ev );
	
			el = ev.element();
			
			if ( el.tagName.toLowerCase() != 'div' ) {
				el = el.up(0);
			}
	
			this.stopTimer();
	
			this.hidePage( this.currentIndex );
	
			this.currentIndex = el.previousSiblings().length - 1;
	
			this.showPage( this.currentIndex );
			
			this.startTimer();
	
		},
	
	
		showPage: function ( index ) {
			this.numbers[ index ].addClassName( 'active' );
			this.pages[ index ].show();
		},
	
	
		stopTimer: function () {
	
			if ( this.timer !== false ) {
				this.timer.stop();
				this.timer = false;
			}
	
		},
		
		startTimer: function () {
	
			if ( this.timer === false ) {
				this.timer = new PeriodicalExecuter( this.goNext.bind( this ), this.options.delay );
			}
	
		}
	
	}	