(function ($) {
    $.fn.simpleSlider = function (options) {
        var defaults = {
            content: ".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)
            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)
            progressivePreload: true, // allow preload images browsing (default: true)
            itemAttr: {
                phpImageProcessor: 'assets/thumb.php?src=',
                width: 412, // item width in px
                height: 979, // item width in px
                crop: 1 // 1 true, 0 false
            },
            thumbs: {
				width: 71,
				height: 71
            },
            navigation: true,
            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'),
            navigation = $t.find(".thumbs ul"),
            sliderContent = $t.find(option.content),
            l = item.length - 1,
            w = item.width(),
            h = item.height(),
            allImage = [],
            allThumbs = [],
            step = 0,
			howManyLoaded = 0,
            play = '', front_item, front_item_img, next_item, next_item_img, arrows, z, on, bullet, cc=0, sign;
				
            var slider = {
                init: function () {
                    // preProcessor php all image
                    item_img.each(function(i){
                        var src =  $(this).attr('src');
                        $(this).attr('src', option.itemAttr.phpImageProcessor + src + '&w=' + option.itemAttr.width + '&h=' + option.itemAttr.height + '&zc=' + option.itemAttr.crop);
                        allThumbs[i] = option.itemAttr.phpImageProcessor + src + '&w=' + option.thumbs.width + '&h=' + option.thumbs.height;
						allImage[i] =  $(this).attr('src');
                    });

					if(option.preloadImages){
						if(option.progressivePreload === true) {
							slider.preload_next_img(0);
							slider.load_slider();
							
						}else{
							$(".prealoadImages").remove();
							$t.append('<img src="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();
					}
                },
				
				preload_next_img: function(key){
					
					if(key <= l && howManyLoaded <= l){
						if(howManyLoaded == 0) {
							$(".prealoadImages").remove();
							$t.append('<img src="images/ajax-loader.gif" class="prealoadImages" />');
						}
						
						slider.preLoadImages(allImage[key], function(){
							item_img.eq(key).show();
							navigation.find('a').eq(key).animate({opacity: 1}, 250);
							
							if(howManyLoaded == 0) {
								$("img.prealoadImages").hide();
							}
							// go and preload next img
							key = key + 1;
							howManyLoaded++;
							slider.preload_next_img(key);
						});
					}
				},

                load_slider: function(){
                    slider.data();
                    
                    if (option.navigation === true) {
                        slider.navigation.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();
                            }
                        }
                    }
                },
				
                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');
							
							// select thumbnail in list
							navigation.find('a.on').removeClass('on');
							navigation.find('a').eq(step).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 (e.keyCode === 39){
                            slider.animation.next_item();
                        }
                        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();
                        }else{
                            slider.animation.previous();
                        }
                        return false;
                    });
                },
				
                autoplay: function () {
                    if (option.autoplay === true  && play == '') {
                        play = setInterval(function () {
                            slider.animation.next_item();
                        }, option.autoplaySpeed);
                        if (play!='') {
                            cc++;
                        }
                    }
                },
                pause: function () {
                    if (play!='') {
                        clearInterval(play);
                        play = '';
                        cc--;
                    }
                },
				
                navigation: {
                    create: function () {
                        for (var i = 0; i <= l; i++) {
                            navigation.append(
                                $("<li />").append($("<a />").attr({
                                    href: "#",
                                    rel: (i)
                                }).append($("<img />").attr({
                                    src: allThumbs[i]
                                }))
                            ));
                        }
                    },
                    trigger: function () {
                        bullet = navigation.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 ? "+" : "-";
							
                            option.auto_running = false;
                            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();
                            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();
                            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.navigation === true) {
                        slider.navigation.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));

(function( $ ){
	$.fn.glint = function(options) {
		var conf = {
			//how many pixels per interval to move
			speed: 3,
			//how often to move glint
			interval: 10,
			//pause after glint before another one
			pause: 1000,
			//size of glint
			size: 100,
			//red channel
			r: 255,
			//green channel
			g: 255,
			//blue channel
			b: 255,
			//angle of glint
			angle: 45,
			//density/maximal alpha channel
			density: 255,
			//horizontal or vertical
			horizontal: true,
			//from left to right or backward
			forward: true,
			onerror: null
		};
		//copy settings
		if ( options ) { 
			$.extend( conf, options );
		}
		//check orientation
		if(!conf.horizontal)
		{
			var orient = {
				offsize: "outerHeight",
				offspace: "outerWidth",
				move: "y",
				pos: "x"
			};
		}
		else
		{
			var orient = {
				offsize: "outerWidth",
				offspace: "outerHeight",
				move: "x",
				pos: "y"
			};
		}
		//fix types
		conf.size = parseInt(conf.size);
		conf.angle = parseInt(conf.angle);
		conf.speed = parseInt(conf.speed);
		conf.pause = parseInt(conf.pause);
		conf.interval = parseInt(conf.interval);
		//fix angle
		conf.angle = (conf.angle > 0 && conf.angle < 180) ? conf.angle : 90;
		//fix density
		conf.density = (conf.density > 0 && conf.density < 255) ? conf.density : 255;
		//apply to each element
		return this.each(function() {        
			var c = document.createElement('canvas');
			//check canvas support
			if(c.getContext)
			{
				var $this = $(this);
				var position = new Object();
				position.x = 0;
				position.y = 0;
				if($this.css("position") == "static")
				{
					$this.css("position", "relative");
				}
				var glint_size = Math.round(conf.size + Math.abs($this[orient.offspace]()/Math.tan((conf.angle/180)*Math.PI)));
				var canvas = $(c);
				canvas.css({
					'position':'absolute',
					'top':0,
					'left':0
				});
				c.setAttribute("width", $this.outerWidth() + "px");
				//set canvas height
				c.setAttribute("height", $this.outerHeight() + "px");
				var p = document.createElement('canvas');
				if(conf.horizontal)
				{
					p.setAttribute("width", glint_size + "px");
					p.setAttribute("height", $this.outerHeight() + "px");
				}
				else
				{
					p.setAttribute("width", $this.outerWidth() + "px");
					p.setAttribute("height", glint_size + "px");
				}
				$this.append(c);
				var main = c.getContext("2d");
				var ctx = p.getContext("2d");
				position[orient.move] = -glint_size;
				position[orient.top] = 0;
				var size = (!conf.horizontal) ? $this.outerHeight() : $this.outerWidth();
				var draw_pixels = function(){
					//calculate offset
					var offset = glint_size-conf.size;
					//create image
					if(conf.horizontal)
					{
						var data = ctx.createImageData(glint_size, $this.outerHeight());
					}
					else
					{
						var data = ctx.createImageData($this.outerWidth(), glint_size);
					}
					//calculate density coeficient
					var half = Math.round(conf.size/2);
					var coef = conf.density/half;
					//rotate through pixels
					for(var i = 0; i < data.width; i++)
					{
						for(var j = 0; j < data.height; j++)
						{
							//check pixel type
							if(conf.horizontal)
							{
								if(conf.angle > 90)
								{
									var check = Math.round(j*(offset/$this.outerHeight()));
								}
								else
								{
									var check = offset - Math.round(j*(offset/$this.outerHeight()));
								}
								var e = i-check;
							}
							else
							{
								if(conf.angle > 90)
								{
									var check = Math.round(i*(offset/$this.outerWidth()));
								}
								else
								{
									var check = offset - Math.round(i*(offset/$this.outerWidth()));
								}
								var e = j-check;
							}
							//if pixel should be colored
							if(e >= 0 && e <= conf.size)
							{
								// Index of the pixel in the array
								var idx = (i + j * data.width) * 4;
								// The RGB values
								data.data[idx + 0] = conf.r;
								data.data[idx + 1] = conf.g;
								data.data[idx + 2] = conf.b;
								//calculate alpha
								data.data[idx + 3] = (e <= half) ? parseInt(coef*e) : Math.abs(conf.density - (parseInt(coef*e)-conf.density));
							}
						}
					}
					//save image
					ctx.putImageData(data, 0, 0);
				};
				var do_glint = function(){
					//clear previous image
					if(conf.horizontal)
					{
						main.clearRect(position.x, position.y, Math.abs(glint_size), $this.outerHeight());
					}
					else
					{
						main.clearRect(position.x, position.y, $this.outerWidth(), Math.abs(glint_size));
					}
					//draw new image
					main.drawImage(p, position.x, position.y);
					//calculate positions
					if(conf.forward)
					{
						position[orient.move] += conf.speed;
						if(size < position[orient.move])
						{
							position[orient.move] = -glint_size;
							setTimeout(do_glint, conf.pause);
						}
						else
						{
							setTimeout(do_glint, conf.interval);
						}
					}
					else
					{
						position[orient.move] -= conf.speed;
						if(-glint_size > position[orient.move])
						{
							position[orient.move] = size+glint_size;
							setTimeout(do_glint, conf.pause);
						}
						else
						{
							setTimeout(do_glint, conf.interval);
						}
					}
				};
				draw_pixels();
				do_glint();
			}
			else if(conf.onerror)
			{
				conf.onerror();
			}
		});
	};
})( jQuery );
