/* =============================================================================
MN_SLIDESHOW JS
--------------------------------------------------------------------------------
Project: Slideshow library
Author: Filip Novak - mame.napilno.cz
E-mail: filip.novak@napilno.cz
WWW: http://mame.napilno.cz

Project notice:

--------------------------------------------------------------------------------
============================================================================= */ 

/* priklad spusteni akce - BEGIN */
/*
if (typeof jQuery != 'undefined') { 
    jQuery(document).ready( function () { 
	
	test1 = new mn_SlideShow('#sl1', {
	    images	: ['test_images/01.jpg', 'test_images/02.jpg', 'test_images/03.jpg', 'test_images/04.jpg', 'test_images/05.jpg'],
	    duration	: 2000,
	    speed	: 'slow'
	});
	
    });
};
*/
/* priklad spusteni akce - END */





// konstruktor - BEGIN
function mn_SlideShow (jq, opts) {

    // rodicovsky prvek slideshow
    this.parent_obj = jQuery(jq);
    
    // slouceni defaultnich a predanych parametru
    this.opts = jQuery.extend({}, this.defaults, opts);
    
    // index aktualniho prvku
    this.index = 0;
    
    // nastaveni nadrazeneho obkjektu na relativni pozicovani
    this.parent_obj.css('position', 'relative');
    
    // nastaveni prvniho obrazku na absolutni pozicovani
    this.parent_obj.children('img').css({
	'position'	: 'absolute',
	'left'		: '0px',
	'top'		: '0px'
    });
    
    // prevedeni slovnich casu na milisekundy
    this.opts.duration = this.strtime2milisec(this.opts.duration);
    this.opts.speed = this.strtime2milisec(this.opts.speed);
    
    // pole pro ukladani obrazku - BEGIN
    this.image_storage = new Array;
    for (var x = 0; x < this.opts.images.length; x++) {
	var image_obj = new Image();
	image_obj.path = this.opts.images[x];
	image_obj.is_load = -1;
	this.image_storage[x] = image_obj;
    }
    // pole pro ukladani obrazku - END
    
    // odpaleni prvniho obrazku - BEGIN
    var thisClass = this;
    jQuery(this.parent_obj.children('img[src*="' + this.opts.images[0] + '"]')[0]).load( 'thisClass', function () {
	thisClass.timer(thisClass.opts.duration);
    });
    // odpaleni prvniho obrazku - BEGIN
    
    
};
// konstruktor - END



// defaultni hodnoty pro mn_SlideShow - BEGIN
mn_SlideShow.prototype.defaults = {
    duration	: '2500',
    speed	: 'slow',
    alt		: '',
    title	: ''
};
// defaultni hodnoty pro mn_SlideShow - END




// prevod slovniho vyjadreni casu v jQuery na milisekundy - BEGIN
mn_SlideShow.prototype.strtime2milisec = function (strtime) {
    
    if (strtime == 'slow') { return 600; }
    if (strtime == 'normal') { return 400; }
    if (strtime == 'fast') { return 200; }
    
    if (isNaN(strtime)) { 
	return 400;
    } else return strtime;
    
};
// prevod slovniho vyjadreni casu v jQuery na milisekundy - END




// preload obrazku - BEGIN
mn_SlideShow.prototype.preloadImage = function (img_obj) {
    
    img_obj.src = img_obj.path;
    img_obj.onload = function () {
	this.is_load = 1;
    };
    
};
// preload obrazku - END




// timer - BEGIN
mn_SlideShow.prototype.timer = function (time) {
    
    // spusteni preloadu dalsiho obrazku
    this.preloadImage(this.image_storage[this.nextIndex()]);
    
    
    // predani spravneho scope pro setTimeout
    // podle: http://bytes.com/topic/javascript/answers/501450-oop-settimeout-scope
    var thisClass = this;
    setTimeout(function () {thisClass.nextImage();}, time);
    
}
// timer - END




// next image - BEGIN
mn_SlideShow.prototype.nextImage = function () {
    
    // pokud neni dalsi obrazek preloadovan spusti se cekaci timer - BEGIN
    if (this.image_storage[this.nextIndex()].is_load == -1) {
	var thisClass = this;
	setTimeout(function () {thisClass.nextImage();}, 100);
    }
    // pokud neni dalsi obrazek preloadovan spusti se cekaci timer - END
    
    else {
	
	// pridani noveho obrazku - BEGIN
	jQuery(this.image_storage[this.nextIndex()]).prependTo(this.parent_obj)
	.css({
	    'position'	: 'absolute',
	    'left'	: '0px',
	    'top'	: '0px'    
	}).attr('alt', this.opts.alt).attr('title', this.opts.title)
	.hide();
	// pridani noveho obrazku - END
	
	// prolnuti obrazku - BEGIN
	var old_img = jQuery(this.parent_obj.children('img[src*="' + this.opts.images[this.index] + '"]')[0]);
	var new_img = jQuery(this.parent_obj.children('img[src*="' + this.opts.images[this.nextIndex()] + '"]')[0]);
	old_img.fadeOut(this.opts.speed);
	// prolnuti obrazku - END
	
	// odstraneni obrazku a nastaveni noveho indexu - BEGIN
	var thisClass = this;
	new_img.fadeIn(this.opts.speed + 10);
	var thisClass = this;
	setTimeout(function () {
	    
	    jQuery(thisClass.parent_obj.children('img[src*="' + thisClass.opts.images[thisClass.index] + '"]')[0]).remove();
	    
	    // nastaveni noveho indexu - BEGIN
	    thisClass.index = thisClass.nextIndex();
	    // nastaveni noveho indexu - END
	    thisClass.timer(thisClass.opts.duration);
	    
	    
	}, this.opts.speed + 20);
	// odstraneni obrazku a nastaveni noveho indexu - END

	
    }
    
    
    
}
// next image - END


// next index - BEGIN
mn_SlideShow.prototype.nextIndex = function () {
    
    if (this.index == (this.opts.images.length - 1)) {
	return 0;
    }
    else {
	return this.index + 1;
    }
    
}
// next index - END
