function nc2ImageGalleryObj() {
    var _this = this
    this._currentIndex = 0;
	this._imageCount = 0;
	this.timer = null;
	this._xml = null;
	
    this.init = function() {
    	_this.play();
    };
	
	this.setImage = function(itm) {
		var img = '<img src="' + $('image', itm).text() + '" alt="' + $('caption', itm).text() + '" />';
		$('.imageGallery div').empty().append(img);
		_this._currentIndex++;
		timer = setTimeout(_this.rotate, 5000);
	};
	
	this.rotate = function() {
		if ( _this._currentIndex == _this._imageCount ) {
			_this._currentIndex = 0;
		}
		_this.setImage($('gallery item', _this._xml)[_this._currentIndex]);
	};

    this.play = function() {
		
		function successFunc(xml, successFlag) {
			if ( successFlag == 'success' ) {
				if ( $('gallery item', xml).length > 0 ) {
					_this._xml = xml;
					_this._imageCount = $('gallery item', xml).length;
					_this.rotate();
				}
			}
		}
		
		$.ajax({
			type: 'GET',
			url: '/assets/content/xml/imagegallery.xml',
			data: { },
			success: function(xml, successFlag) { successFunc(xml, successFlag); }
		});
	};

}

var nc2ImageGallery = new nc2ImageGalleryObj();

$(nc2ImageGallery.init);
