/**
*	@author vaneys@gmail.com (Ivan Maslennikov)
*	@fileoverview 
*/

(function ($) {
	$.fn.slideshow = function () {
        var h = {
            slides: 'div.home-slide',
            title: 'div.home-slide-title',
            animationSpeed: 400,
            autorotation: true,
            autorotationSpeed: 5,
            appendControlls: ''            
        };
		var j = $.extend(h);
		return this.each(function () {
            var f = $(this),
                slides = f.find(j.slides),
				title = f.find(j.title),
                slideCount = slides.length,
                blockNumber = 0,
                currentSlideNumber = 0,
                currentTransition = 0,
                current_class = 'active',
                controlls = '',
                skipSwitch = true,
				isFirstSlide = true,
                interval = '',
				loadedItem = [false, false, false, false];
				
            f.methods = {
                init: function () {
                    var a = 0;
                    slides.each(function () {
                        var name = $(this).find('img').attr('src');
                        var patt = /\"|\'|\)|\(|url/g;
                        $.data(this, "data", {
                            img: name.replace(patt, '')
                        })
                    });
					f.methods.preloadingDone();
                },
                preloadingDone: function () {
                    skipSwitch = false;
                    slides.filter(':first').hide().css({"left": "50px"}).animate({"opacity" : "show"}, j.animationSpeed, 'easeInCubic', function () {$(this).addClass('active');});
					f.methods.moveTitleIn();
                    if (j.autorotation) {
                        f.methods.autorotate();
                    }
                },
                autorotate: function () {
                    interval = setInterval(function () {
                        currentSlideNumber++;
                        if (currentSlideNumber == slideCount) currentSlideNumber = 0;
                        f.methods.prepareSlide()
                    }, (parseInt(j.autorotationSpeed) * 1000) + j.animationSpeed)
                },
                prepareSlide: function (c) {
                    var d = false;
                    clearInterval(interval);
                    if (!skipSwitch && d == false) {
                        skipSwitch = true;
                        var e = slides.filter('.active'),
                            nextSlide = slides.filter(':eq(' + currentSlideNumber + ')');
						f.methods.changeImage(e, nextSlide)
                    }
                    return false
                },
                changeImage: function (a, b) {
					if (!loadedItem[currentSlideNumber]) {
						var c = new Image();
						c.onload = function () {
							loadedItem[currentSlideNumber] = true;
							f.methods.switchSlides(a,b);
						};
						c.src = $(b).find('img:first').attr('src');
					}
					else {
						f.methods.switchSlides(a,b);
					}
                },
				switchSlides: function (a,b) {
					a.animate({"opacity" : "hide"}, j.animationSpeed, 'easeOutCubic', function () {
						a.removeClass('active');
						b.hide().css({"left": "50px"}).animate({"opacity" : "show"}, j.animationSpeed, 'easeInCubic', function () {b.addClass('active');});
					});
					f.methods.moveTitleOut();
					skipSwitch = false;
					f.methods.autorotate()
				},
				moveTitleIn: function () {
					title.hide().attr('rel', $(slides[currentSlideNumber]).attr('rel')).css({"left" : "1200px"}).animate({"left": "600px", "opacity" : "show"}, j.animationSpeed*1.2, 'easeOutCubic');
				},
				moveTitleOut: function () {
					title.animate({"left": "-300px", "opacity": "hide"}, j.animationSpeed/1.7, 'easeInCubic', function() {f.methods.moveTitleIn();});
				}
            };
            f.methods.init()
        })
	}
})(jQuery);
