
	// Written by East Devon IT
	// http://www.eastdevon.it
	// All rights reserved; (c) 2010

	var img;

	var scroller = {
		
		endpoint: 'business/bannermanager/c_items.php',
		limit: 3,
		website_block: '',
		items: {},
		timer_id: {},
		wrapper_el: '#business_carousel #inner',
		first_id: -1,
		update_time: 5000,
		img_prefix: '',
		
		init: function() {
			this.load_data();
		},
		load_data: function() {
			$.ajax({
				type: "GET",
				url: this.endpoint + '?website_block=' + this.website_block + "&ts=" + Math.random(),
				dataType: 'json',
				success: function(data){
					 if ( scroller.items = data)
					 	scroller.start_show();
				}
			});
		},
		format_item: function(item) {
			
			return '<div class="image"><a href="' + this.img_prefix + 'business/click.php?id=' + item.id + '"><img width="233" src="' + this.img_prefix + item.image_url + '" border="0" /></a></div>' + 
 					'<div class="title"><a href="business/' + this.img_prefix + item.business_url + '">' + item.business_name + '</a></div>' + 
 					'<div class="type">' + item.business_type + '</div>';

		
		},
		start_show: function() {
			$(this.wrapper_el).html('');
			$('<div class="item" /><div class="item" /><div class="item" />').appendTo(this.wrapper_el);
			this.timer_id = window.setInterval('scroller.move_items()', this.update_time);
			this.move_items();
		},
		move_items: function() {
			
			this.first_id++;
			
			if ( this.first_id < this.items.length ) {
				first_item = this.items[this.first_id];
				$(this.wrapper_el + " .item:last").html($(this.wrapper_el + " .item:eq(1)").html());
				$(this.wrapper_el + " .item:eq(1)").html($(this.wrapper_el + " .item:first").html());
				$(this.wrapper_el + " .item:first").html(this.format_item(first_item));
			}
			else {
				this.first_id = -1; this.move_items();	
			}

			
		}
		
	};