jQuery.noConflict();
// Put all your code in your document ready area
jQuery(document).ready(function($){
var MdevSlideShow  = {
	_init: function() {
		this._setupAryNavs();
		if (this._getData('btnNext')) {this._setupbtnNext();}
		if (this._getData('btnPrev')) {this._setupbtnPrev();}
		this.doAutoChange = this.options.autoRefreshTime ? true : false;
		if (this.doAutoChange && this._getData('idxSlideGroup') === 0) {this.startThisTimer();}
	},
	
	startThisTimer: function() {
		$(this).everyTime(
			this.options.autoRefreshTime,
			'timerLabel',
			function(){
				this._getData('btnNext').trigger('click', [1]);
			}
		);
	},
	stopThisTimer: function() {
		$(this).stopTime();
	},
	
	_setupAryNavs: function(){
		var that = this;
		this._getData('aryNavs').click(function(){
			that._switchNavAndSlide(that._getData('aryNavs').index(this));
		})
	},
	
	_setupbtnNext: function(){
		var that = this;
		this._getData('btnNext').click(function(event, onAuto){
			var nextIdx = that._getCurrentIndex() + 1;
			that._switchNavAndSlide(nextIdx == that._getData('aryNavs').length ? 0 : nextIdx, onAuto);
		})
	},
	_setupbtnPrev: function(){
		var that = this;
		this._getData('btnPrev').click(function(){
			var prevIdx = that._getCurrentIndex() - 1;
			that._switchNavAndSlide(prevIdx < 0 ? that._getData('aryNavs').length - 1 : prevIdx);
		})
	},
	_switchNavAndSlide: function(nextIdx, onAuto) {
		if (!onAuto && this.doAutoChange) {this.stopThisTimer();}
		var curIdx = this._getCurrentIndex();
		this._getData('aryNavs').removeClass(this._getData('strActiveNavBtnClassName'));
		this._getData('aryNavs').eq(nextIdx).addClass(this._getData('strActiveNavBtnClassName'));
		
		/** Jonny **/
		// Fade out
		var currentSlide = this._getData('arySlides').filter("div:visible");
		currentSlide.fadeOut(this._getData('fadeoutTime'));
		// In IE8 Positioned elements aren't affected by their parents opacity, so they must be faded out individually
		$('*', currentSlide).fadeOut(this._getData('fadeoutTime'));
		
		// Fade in
		var nextSlide = $(this._getData('arySlides')[nextIdx]);
		$(nextSlide).fadeIn(this._getData('fadeinTime'));
		$('*', nextSlide).fadeOut(0); // Required in IE8, otherwise element is shown immediately.
		$('*', nextSlide).fadeIn(this._getData('fadeinTime'));
		/** end Jonny **/
		
		if (!onAuto && this.doAutoChange) {this.startThisTimer();}
	},
	_getCurrentIndex: function() {
		// Use the navigation button, because as the slides fade in and out, it is unclear when the current slide status changes
		var thisNavAEl = this._getData('aryNavs').filter("[class='"+this._getData('strActiveNavBtnClassName')+"']");
		return this._getData('aryNavs').index(thisNavAEl);
	}
}; 
$.widget("ui.mdevslideshow", MdevSlideShow); 
$.ui.mdevslideshow.defaults = {
	fadeoutTime: 2500,
	fadeinTime: 2000,
	autoRefreshTime: 0, // number of milliseconds. false or 0 for no automatic changing
	strActiveNavBtnClassName: "activesticky",

	arySlides: $("#slides div"),
	aryNavs: '',
	btnNext: 0,
	btnPrev: ''
	//aryNavs: $("#slide_nav li a"),
	//btnNext: $("#slide_arrows li a.next"),
	//btnPrev: $("#slide_arrows li a.previous")
};
});