// Your slides must be named like this : 
// <div id='slide_slug'></div>
// Your buttons must be named like this : 
// id='homebtn_slug'

function Homeslides (titleTarget) {
                       
                                 
	// settings -------------------------------------------------------------- 
	//------------------------------------------------------------------------
	this._time = 7000;
	this._fadeInTime = 1900;
	this._fadeOutTime = 700;                                                         
	//------------------------------------------------------------------------
	//------------------------------------------------------------------------

 
	this.titleTarget = titleTarget;
	
	// public functions                                                        
	this.over = homeslides_over;	 // selects the slide plus the button given the slug
	this.out = homeslides_out;		 // unselects the slide plus the button given the slug
	
	this.getSlideBySlug = homeslides_getSlideBySlug;
	//------------------------------------------------------------------------
	
	
	// private functions
	this._next 		   		= homeslides_next;
	this._prev 		   		= homeslides_prev;
	                   
	this._startTimer   		= homeslides_startTimer;
	this._stopTimer    		= homeslides_stopTimer;
	this._tick 		   		= homeslides_tick;
                       
	this._loadSlideById	 	= homeslides_loadSlideById;
	this._swapSlides   		= homeslides_swapSlides;
	this._getId				= homeslides_getId;
	this._getSlug			= homeslides_getSlug;
	
	this._selectBtn			= homeslides_selectBtn;
	this._unselectBtn		= homeslides_unselectBtn;
	
	// private vars
	this._mode = 'running';
	this._interval = null;
	
	
	this._init = homeslides_init;
	this.slugs = new Array ();
	this.titles = new Array ();
	this.slidesNumber = 0;	
	this.slides = this._init ();
	
	

	this._startTimer ();




	this.oldNum = 0;
	this.crtNum = 0;
	
	

	
	
}

function homeslides_getSlideBySlug (slug) {
	for (var key in this.slugs) {
		if (this.slugs [key] == slug)
		return this.slides [key];
	}
	
	return null;
}

function homeslides_init () {
	var slides = new Array ();
	var count = this.slidesNumber;
	var slugs = new Array ();
	var titles = new Array ();
	
	$('div.homeslide').each (function () {
	 	
		
				
		id = this.id;
	 	
		if (id.indexOf ('slide_') != -1) {
			var slug = id.replace("slide_", "");
			slides [count] = $(this);
			slugs [count] = slug;
			
			// hide the title and store its string
			var title = $(this).find ('h1');
			title.hide ();
			var titleStr = title.html ();			
			titles [count] = titleStr;
			
			if (count > 0) {
				slides [count].hide ();
			}
			
			count ++;
			
		}		
	})
		
	this.slugs = slugs;
	this.slidesNumber = count;
	this.titles = titles
	return slides;
}

function homeslides_over (slug) {
	this._stopTimer ();
	this._mode = 'stopped';
	var id = this._getId (slug);
	this.oldNum = this.crtNum;
	this.crtNum = id;
	this._loadSlideById (id);
}
function homeslides_out (slug) {
	this._startTimer ();
	this._mode = 'running';
}

function homeslides_getId (slug) {
	for (var key in this.slugs) {
		if (this.slugs [key] == slug)
			return key;
		}
	return -1;
}

function homeslides_getSlug (id) {
	for (var key in this.slugs) {
		if (key == id) {
			return this.slugs [key];
		}
	}
	return null;
	
}

function homeslides_loadSlideById (id) {
	var oldSlide = this.slides [this.oldNum];
	var newSlide = this.slides [id];
	
	// deal with btn selection
	var oldSlug = this._getSlug (this.oldNum);
	var newSlug = this._getSlug (id);
	this._unselectBtn (oldSlug);
	this._selectBtn (newSlug);

	this.crtNum = id;
	this._swapSlides (oldSlide, newSlide);
	
	// update title
	var string = this.titles [id];
	this.titleTarget.html (string);
	
}

function homeslides_next () {
	
	this.oldNum = parseInt (this.crtNum);
 	this.crtNum = parseInt (this.crtNum +1);
	if (this.crtNum >= this.slidesNumber) {
		this.crtNum = 0;
	}
	
	
	this._loadSlideById (this.crtNum);
	
}

function homeslides_selectBtn (slug) {
	var btn = $('#homebtn_'+slug).parent().parent();
	if (btn != null) {
		
		btn.addClass ('homebtn_selected');
	}
		
}
function homeslides_unselectBtn (slug) {
	var btn = $('#homebtn_'+slug).parent().parent();
	if (btn != null) 
		btn.removeClass ('homebtn_selected');
}

function homeslides_prev () {
	
}

function homeslides_tick () {
	this._next ();
}

function homeslides_startTimer () {
	this._stopTimer ();
	// we use some special arguments for the function so that the receiving function
	// can access a this that is this Homeslides object
	this._interval = setInterval ((function(self) {
	  return function() {self._tick(); } } )(this), this._time);
}

function homeslides_stopTimer () {
	clearTimeout (this._interval);
}

function homeslides_updateTitle (string) {
}

function homeslides_swapSlides (oldSlide, newSlide) {
	if (oldSlide === newSlide) {
		return;
	}
	
	oldSlide.stop(true, true).fadeOut (this._fadeOutTime);
	newSlide.stop(true, true).fadeIn (this._fadeInTime);
	

	
}
