var vdwSlideShow = {
	timeout : false,
	init : function(options, elem) {
		this.options = $jq.extend({},this.options,options);
		this.elem = elem;
		this.$elem = $jq(elem);
		this.current = this.options.start;
		this.$slides = this.$elem.find(this.options.slideSel);
		this.total = this.$slides.length;
		this._goToSlide(this.current);
		var _this = this;
		if (this.options.prevSel) {
			this.$prev = $jq(this.options.prevSel).click(function(e) {
				e.preventDefault();
				_this.prev();
			});
		}
		if (this.options.nextSel) {
			this.$next = $jq(this.options.nextSel).click(function(e) {
				e.preventDefault();
				_this.next();
			});
		}
		if (this.options.playPauseSel) {
			this.$playPause = $jq(this.options.playPauseSel).click(function(e) {
				e.preventDefault();
				if (_this.options.autoPlay) {
					_this.pause();
				} else {
					_this.play();
				}
			});
		}
	},
	options : {
		start : 0,
		slideSel : 'li',
		prevSel : '',
		nextSel : '',
		playPauseSel : '',
		delay : 5000,
		autoPlay : true
	},
	pause : function() {
		this.options.autoPlay = false;
		clearTimeout(this.timeout);
	},
	play : function() {
		this.options.autoPlay = true;
		clearTimeout(this.timeout);
		var _this = this;
		this.timeout = setTimeout(function() { _this.next(); }, this.options.delay);
	},
	_goToSlide : function(index) {
		this.$slides.hide()
			.eq(index).show();
		this.current = index;
		if (this.options.autoPlay) {
			this.play();
		}
	},
	prev : function() {
		var index = (this.current == 0) ? this.total - 1 : this.current - 1;
		this._goToSlide(index);
	},
	next : function() {
		var index = (this.current == (this.total - 1)) ? 0 : this.current + 1;
		this._goToSlide(index);
	}
}

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    }
}

(function($) {
	$.fn.vdwSlideShow = function(options) {
		if (this.length) {
			return this.each(function() {
				var myvdwSlideShow = Object.create(vdwSlideShow);
				myvdwSlideShow.init(options, this);
				$.data(this, 'vdwslideshow', myvdwSlideShow);
			});
		}
	}
})(jQuery);

$jq(document).ready(function() {
	$jq('#new_construction_project_slideshow .content').append('<a id="btn_construction_slideshow_prev" href="#">Prev</a><a id="btn_construction_slideshow_playpause" href="#">Pause/Play</a><a id="btn_construction_slideshow_next" href="#">Next</a>')
		.vdwSlideShow({
			prevSel : '#btn_construction_slideshow_prev',
			nextSel : '#btn_construction_slideshow_next',
			playPauseSel : '#btn_construction_slideshow_playpause'
		});

	$jq('#new_construction_projects_module ul').after('<p class="slideshow_nav"><a id="btn_construction_slideshow_prev" href="#">Prev</a><a id="btn_construction_slideshow_playpause" href="#">Pause/Play</a><a id="btn_construction_slideshow_next" href="#">Next</a></p>')
		.vdwSlideShow({
			prevSel : '#btn_construction_slideshow_prev',
			nextSel : '#btn_construction_slideshow_next',
			playPauseSel : '#btn_construction_slideshow_playpause'
		});

});
