(function ($) {
    $.fn.interiorSlider = function (options) {
        var defaults = {
            content: ".interior-slide-content", // container where sliding contents
            children: "li", // children items (default: li)
            transition: "fade", // transition effect (defaut: fade)
            animationSpeed: 300, // transition speed
            easing: '', // easing of animation (default. '' - no easing)
            autoplay: false, // autoplay easing
            autoplaySpeed: 3000, // autoplay interval speed
            pauseOnHover: false, // if autoplay on mouse over make pause (true|false - default false)
            bullets: true, // show bullets (default: true)
            arrows: true, // show arrows (default: true)
            arrowsHide: true, // hide arrow on mouse out (default: true)
            keyBrowse: true, // allow key browsing (default: true)
            mouseWheel: true, // allow mouse wheel browsing (default: true)
            preloadImages: true, // allow preload images browsing (default: true)
            itemSize: {
                width: 412, // item width in px
                height: 979 // item width in px
            },
            prev: "prev", // prev btn item
            next: "next", // next btn item
            running: false, // static usage
            animationStart: function () {},
            animationComplete: function () {}
        };
        var option = $.extend({}, defaults, options);
        return this.each(function () {
            var $t = $(this),
            item = $t.children(option.content).children(option.children),
            item_img = item.find('img'),
            sliderContent = $t.find(option.content),
            l = item.length - 1,
            w = item.width(),
            h = item.height(),
            allImage = [],
            step = 0,            
            play = '', front_item, front_item_img, next_item, next_item_img, bullets, arrows, z, on, bullet, evt, cc=0;
				
            var slider = {
                init: function () {

                    if(option.preloadImages){
                        $(".prealoadImages").remove();
                        $t.append('<img src="http://www.hotelvega.ro/wp-content/themes/hotelvega/images/ajax-loader.gif" class="prealoadImages" />');
                        item_img.hide();
                        item_img.each(function(i){
                            allImage[i] =  $(this).attr('src');
                        });
                        
                        slider.preLoadImages(allImage, function(){
                            item_img.show();
                            $("img.prealoadImages").hide();
                            slider.load_slider();

                        });
                    }else{
                        slider.load_slider();
                    }
                },

                load_slider: function(){
                    slider.data();

                    if (option.bullets === true) {
                        slider.bullets.create();
                    }
                    if (option.arrows === true) {
                        slider.arrows.create();
                    }
                    if (option.autoplay === true) {
                        slider.autoplay();
                    }
                    slider.triggers();
                },
				
                data: function () {
                    item.each(function (i) {
                        var $this = $(this);
						
                        $this.css("z-index", -(i - l));
                        $this.find('img').css("opacity", 0);
                    });
					
                    if (option.transition === "fade") {
                        item.eq(0)
                        .addClass('on')
                        .find('img')
                        .css("opacity", 1)
                        ;
                    }
                },
                
                preLoadImages: function(imageList, callback) {    
                    var pic = [], i, total, loaded = 0;
                    if (typeof imageList !== 'undefined') {
                        if ($.isArray(imageList)) {
                            total = imageList.length; // used later
                                for (i=0; i < total; i++) {
                                    pic[i] = new Image();
                                    pic[i].onload = function() {
                                        loaded++; // should never hit a race condition due to JS's non-threaded nature
                                        if (loaded === total) {
                                            if ($.isFunction(callback)) {
                                                callback();
                                            }
                                        }
                                    }
                                    pic[i].src = imageList[i];
                                }
                        }
                        else {
                            pic[0] = new Image();
                            pic[0].onload = function() {
                                if ($.isFunction(callback)) {
                                    callback();
                                }
                            };
                            pic[0].src = imageList;
                        }
                    }
                    pic = undefined;
                },
				
                zindex: {
                    prev: function () {
                        if(step === l){
                            item.eq(0).css("z-index", l + 3);
                        }else{
                            item.css("z-index", l + 1);
                        }
                        item.eq(step).css("z-index", l + 4).next(item).css("z-index", l + 2);
                    },
                    next: function () {
                        item.css("z-index", l + 1).eq(step).css("z-index", l + 3).prev(item).css("z-index", l + 2);
                    },
                    bullets: function () {
                        item.css("z-index", l + 1).eq(on).css("z-index", l + 2);
                        item.eq(step).css("z-index", l + 3);
                    },
                    trigger: function () {
                        if (z === 1) {
                            slider.zindex.next();
                        } else {
                            if (z === -1) {
                                slider.zindex.prev();
                            } else {
                                if (z === 0) {
                                    slider.zindex.bullets();
                                }
                            }
                        }
                    }
                },

                slide: {
                    fade: function () {
                        
                        option.animationStart.call(this);

                        front_item = sliderContent.find('.on');
                        front_item_img = front_item.find('img');
                        next_item  = item.eq(step);
                        next_item_img = next_item.find('img');
						
						slider.zindex.trigger();

						next_item_img.animate({
							opacity: 1
						}, option.animationSpeed, option.easing, function () {
							
							front_item_img.animate({
								opacity: 0
							}, option.animationSpeed, option.easing, function () {
							});

							// find element with .on class and remove class
							sliderContent.find('.on').removeClass('on');
							
							// add .on class to current element
							next_item.addClass('on');
				
							option.animationComplete.call(this);
							
							// release running
							option.running = false;
						});
                    } 

                },

                animation: {
                    previous: function () {
                        if(step === 0){
                            step = l;
                        } else {
                            step--;
                        }
                        z = -1;
                        switch (option.transition) {
                            case "fade":
                                slider.slide.fade();
                                break;
                        }
                    },
                    next_item: function () {
                        if(step === l){
                            step = 0;
                        }else{
                            step++;
                        }
                        z = 1;
                        switch (option.transition) {
                            case "fade":
                                slider.slide.fade();
                                break;
                        }
                    }
                },
				
                keyBrowse: function () {	
                    $(document).keyup(function(e) {
                        // exit if animations still running
                        if(option.running){return false;}
                        
                        // set up true running script
                        option.running = true;
                        
                        if (e.keyCode === 37){
                            slider.animation.previous();
                            if (option.bullets === true) {
                                slider.bullets.update();
                            }
                        }
                        if (e.keyCode === 39){
                            slider.animation.next_item();
                            if (option.bullets === true) {
                                slider.bullets.update();
                            }
                        }
                        return false;
                    });
                },

                mouseWheel: function () {
                     $t.mousewheel(function(event, delta) {
                         // exit if animations still running
                        if(option.running){return false;}

                        // set up true running script
                        option.running = true;

                        var dir = delta > 0 ? 'up' : 'down',
                            vel = Math.abs(delta);
                            if(dir === 'down'){
                                 slider.animation.next_item();
                                if (option.bullets === true) {
                                    slider.bullets.update();
                                }
                            }else{
                                 slider.animation.previous();
                                if (option.bullets === true) {
                                    slider.bullets.update();
                                }
                            }
                        return false;
                    });
                },
				
                autoplay: function () {
                    if (option.autoplay === true  && play == '') {
                        play = setInterval(function () {
                            slider.animation.next_item();
                            if (option.bullets === true) {
                                setTimeout(function () {
                                    slider.bullets.update();
                                }, option.animationSpeed + 200);
                            }
                        }, option.autoplaySpeed);
						if (play!='') {
							cc++;
						}
                    }
                },
                pause: function () {
                	if (play!='') {
                    	clearInterval(play);
                    	play = '';
                    	cc--;
                	}
                },
                bullets: {
                    create: function () {
                        $(".sliderBullets").remove();
                        $t.append($("<div />").addClass("sliderBullets"));
                        bullets = $t.find(".sliderBullets");
                        for (i = 0; i <= l; i++) {
                            bullets.append($("<a />").attr({
                                href: "#",
                                rel: i
                            }).text(i));
                        }
						
						// center bullets for center
						bullets.css('margin-left', "-" + parseInt(bullets.width() / 2) + "px");
                    },
                    trigger: function () {
                        bullet = bullets.find("a");
                        bullet.eq(0).addClass("on");
                        bullet.click(function () {
                            var b = $(this),
                            rel = b.attr("rel");
                            on = bullet.filter(".on").attr("rel");
                            step = rel;
                            sign = rel > on ? "+" : "-";
                            z = 0;
                            if (!b.hasClass("on")) {
                                switch (option.transition) {
                                    case "fade":
                                        slider.slide.fade();
                                        break;
                                }
                            }
                            
                            bullet.removeClass("on");
                            b.addClass("on");
                            return false;
                        });
                    },
                    update: function () {
                        bullet.removeClass("on").eq(step).addClass("on");
                    }
                },
				
                arrows: {
                    create: function () {
                        $(".nextBackControllers").remove();
                        $t.append($("<div />").addClass("nextBackControllers"));
                        arrows = $t.find(".nextBackControllers");
                        arrows.append($("<a />").attr("href", "#").addClass(option.prev).text("Previous"));
                        arrows.append($("<a />").attr("href", "#").addClass(option.next).text("Next"));
                    },
                    trigger: function () {
                        arrows.find("." + option.prev).click(function () {
                            // exit if animations still running
                            if(option.running){return false;}

                            // set up true running script
                            option.running = true;
                        
                            slider.animation.previous();
                            if (option.bullets === true) {
                                slider.bullets.update();
                            }
                            return false;
                        });
                        arrows.find("." + option.next).click(function () {
                            // exit if animations still running
                            if(option.running){return false;}

                            // set up true running script
                            option.running = true;
                            
                            slider.animation.next_item();
                            if (option.bullets === true) {
                                slider.bullets.update();
                            }
                            return false;
                        });
                        if (option.arrowsHide === true) {
                            arrows.find('a').hide();
                            $t.hover(function () {
                                $t.mousemove(function(e) {
                                    var offset = $(this).offset();
                                    var x = e.pageX - (offset.left);
									
                                    if(x < w / 2){
                                        // left side (prev)
                                        arrows.find("." + option.next).hide();
                                        arrows.find("." + option.prev).fadeIn(150);
                                    }else{
                                        // right side (next)
                                        arrows.find("." + option.prev).hide();
                                        arrows.find("." + option.next).fadeIn(150);
                                    }
                                });
                            }, function () {
                                arrows.find('a').hide();
                            });
                        }
                    }
                },
				
                triggers: function () {
                    if (option.arrows === true) {
                        slider.arrows.trigger();
                    }
                    if (option.bullets === true) {
                        slider.bullets.trigger();
                    }
                    if (option.mouseWheel === true) {
                        slider.mouseWheel();
                    }
                    if (option.keyBrowse === true) {
                        slider.keyBrowse();
                    }
                    if (option.pauseOnHover === true) {
						
                        $t.hover(function () {
                            slider.pause();
                            //console.log('pause: ' + cc);
                        }, function () {
                            slider.autoplay();
                            //console.log('autoplay: ' + cc);
                        });
                       
                    }
                }
            };
            slider.init();
        });
    };
})(jQuery);


/*! Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 *
 * Requires: 1.2.2+
 */
(function($) {

var types = ['DOMMouseScroll', 'mousewheel'];

$.event.special.mousewheel = {
	setup: function() {
        var i;
		if ( this.addEventListener ){
			for ( i=types.length; i; ){
				this.addEventListener( types[--i], handler, false );
            }
        }else{
			this.onmousewheel = handler;
        }
	},

	teardown: function() {
        var i;
		if ( this.removeEventListener ){
			for (i=types.length; i; ) {
				this.removeEventListener( types[--i], handler, false );
            }
        }else{
			this.onmousewheel = null;
        }
	}
};

$.fn.extend({
	mousewheel: function(fn) {
		return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
	},

	unmousewheel: function(fn) {
		return this.unbind("mousewheel", fn);
	}
});


function handler(event) {
	var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;

	event = $.event.fix(event || window.event);
	event.type = "mousewheel";

	if ( event.wheelDelta ) {delta = event.wheelDelta/120;}
	if ( event.detail     ) {delta = -event.detail/3;}

	// Add events and delta to the front of the arguments
	args.unshift(event, delta);

	return $.event.handle.apply(this, args);
}

}(jQuery));
