(function ($) {
	$.fn.slideshow = function () {
        var h = {
            slides: 'div',
            animationSpeed: 400,
            autorotation: true,
            autorotationSpeed: 5,
            appendControlls: ''            
        };
		var j = $.extend(h);
		return this.each(function () {
            var f = $(this),
                slides = f.find(j.slides),
                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;
					//$("#loading").show();
                    slides.each(function () {
                        var name = $(this).find('img').attr('src');
                        var patt = /\"|\'|\)|\(|url/g;
                        $.data(this, "data", {
                            img: name.replace(patt, '')
                        })
                    });
					f.methods.preloadingDone();
                    //f.methods.appendControlls();
                },
                appendControlls: function () {
					var b = f;
					controlls = $('<ul></ul>').attr('id', 'slideshow-controls').insertAfter(b);
					slides.each(function (i) {
						var li = $('<li class="' + current_class +'"><a href="#">&nbsp;</a></li>').appendTo(controlls);
						li.find('a').bind('click', function (e) {
							e.preventDefault();
							if (currentSlideNumber != i) {
								currentSlideNumber = i;
								f.methods.switchSlide()
							}
						});
						current_class = ""
					});
					controlls.width(controlls.width()).css('float', 'none');
                    return this
                },/**/
                preloadingDone: function () {
                    skipSwitch = false;
                    slides.filter(':first').animate({"left": "693px"}, j.animationSpeed, 'easeOutCubic', function () {$(this).addClass('active');});//css('left','693px').addClass('active').fadeIn(j.animationSpeed);
					//$("#loading").hide();
					/*slides.filter(':first').css({
                        'z-index': '5',
                        display: 'block'
                    });*/
                    if (j.autorotation) {
                        f.methods.autorotate();
                    }
                },
                autorotate: function () {
					//j.autorotationSpeed = parseInt(slides.filter(':eq(' + currentSlideNumber + ')').attr('showtime'));
                    interval = setInterval(function () {
                        currentSlideNumber++;
                        if (currentSlideNumber == slideCount) currentSlideNumber = 0;
                        f.methods.switchSlide()
                    }, (parseInt(j.autorotationSpeed) * 1000) + j.animationSpeed)
                },
                switchSlide: function (c) {
                    var d = false;
                    clearInterval(interval);
                    if (!skipSwitch && d == false) {
                        skipSwitch = true;
                        var e = slides.filter('.active'),
                            nextSlide = slides.filter(':eq(' + currentSlideNumber + ')'),
                            nextURL = $.data(nextSlide[0], "data").img;
                        //controlls.find('li').removeClass('active');
                        //controlls.find('li:eq(' + currentSlideNumber + ')').addClass('active');/**/
						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({"left": "0px"}, j.animationSpeed, 'easeOutCubic', function () {a.removeClass('active').css('left', '1386px');});
					b.animate({"left": "693px"}, j.animationSpeed, 'easeOutCubic', function () {b.addClass('active');});
					
					if (isFirstSlide && currentSlideNumber > 0)
						f.methods.changeFirst();
					skipSwitch = false;
					f.methods.autorotate()
				},
				changeFirst: function () {
					$("#first-slide img:first").attr('src','http://x1.fring.com/static/images/homepage/slides/transparent/pict1_vacation.png');
					isFirstSlide = false;
				}
            };
            f.methods.init()
        })
	}
})(jQuery);
