jQuery.fn.extend({
    everyTime: function(interval, label, fn, times, belay) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, times, belay);
        });
    },
    oneTime: function(interval, label, fn) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, 1);
        });
    },
    stopTime: function(label, fn) {
        return this.each(function() {
            jQuery.timer.remove(this, label, fn);
        });
    }
});
jQuery.extend({
    timer: {
        guid: 1,
        global: {},
        regex: /^([0-9]+)\s*(.*s)?$/,
        powers: {
            'ms': 1,
            'cs': 10,
            'ds': 100,
            's': 1000,
            'das': 10000,
            'hs': 100000,
            'ks': 1000000
        },
        timeParse: function(value) {
            if (value == undefined || value == null) return null;
            var result = this.regex.exec(jQuery.trim(value.toString()));
            if (result[2]) {
                var num = parseInt(result[1], 10);
                var mult = this.powers[result[2]] || 1;
                return num * mult;
            } else {
                return value;
            }
        },
        add: function(element, interval, label, fn, times, belay) {
            var counter = 0;
            if (jQuery.isFunction(label)) {
                if (!times) times = fn;
                fn = label;
                label = interval;
            }
            interval = jQuery.timer.timeParse(interval);
            if (typeof interval != 'number' || isNaN(interval) || interval <= 0) return;
            if (times && times.constructor != Number) {
                belay = !!times;
                times = 0;
            }
            times = times || 0;
            belay = belay || false;
            if (!element.$timers) element.$timers = {};
            if (!element.$timers[label]) element.$timers[label] = {};
            fn.$timerID = fn.$timerID || this.guid++;
            var handler = function() {
                if (belay && this.inProgress) return;
                this.inProgress = true;
                if ((++counter > times && times !== 0) || fn.call(element, counter) === false) jQuery.timer.remove(element, label, fn);
                this.inProgress = false;
            };
            handler.$timerID = fn.$timerID;
            if (!element.$timers[label][fn.$timerID]) element.$timers[label][fn.$timerID] = window.setInterval(handler, interval);
            if (!this.global[label]) this.global[label] = [];
            this.global[label].push(element);
        },
        remove: function(element, label, fn) {
            var timers = element.$timers,
            ret;
            if (timers) {
                if (!label) {
                    for (label in timers) this.remove(element, label, fn);
                } else if (timers[label]) {
                    if (fn) {
                        if (fn.$timerID) {
                            window.clearInterval(timers[label][fn.$timerID]);
                            delete timers[label][fn.$timerID];
                        }
                    } else {
                        for (var fn in timers[label]) {
                            window.clearInterval(timers[label][fn]);
                            delete timers[label][fn];
                        }
                    }
                    for (ret in timers[label]) break;
                    if (!ret) {
                        ret = null;
                        delete timers[label];
                    }
                }
                for (ret in timers) break;
                if (!ret) element.$timers = null;
            }
        }
    }
});
if (jQuery.browser.msie) jQuery(window).one("unload",
function() {
    var global = jQuery.timer.global;
    for (var label in global) {
        var els = global[label],
        i = els.length;
        while (--i) jQuery.timer.remove(els[i], label);
    }
});
(function($) {
    $.fn.jqGalScroll = function(options) {
        this.css('display', 'block');
        var CurrentIndex = 0;
        var timeOutTime = 6000;
        
        $('div.jq_container').everyTime(timeOutTime, 'controlled',
        function() {
            $('a#next_image').click();
        });

        $('div.jq_container').bind("mouseleave",
        function() {
          $('div.jq_container').everyTime(timeOutTime, 'controlled',
          function() {
            $('a#next_image').click();
          });
        });

        $('div.jq_container').bind("mouseenter",
        function() {
          $('div.jq_container').stopTime();
        });
        return this.each(function(i) {
            var el = this;
            el.curImage = 0;
            el.jqthis = $(this).css({
                position: 'relative'
            });
            el.jqchildren = el.jqthis.children();
            el.opts = $.extend({},
            jqGalScroll, options);
            el.index = i;
            el.totalChildren = el.jqchildren.size();
            var width, height;
            switch (el.opts.direction) {
            case 'horizontal':
                width = el.totalChildren * el.opts.width;
                height = el.opts.height;
                break;
            case 'vertical':
                width = el.opts.width;
                height = el.totalChildren * el.opts.height;
                break;
            default:
                width = el.totalChildren * el.opts.width;
                height = el.totalChildren * el.opts.height;
                break;
            };
            el.container = $('<div id="jqGS' + i + '" class="jqGSContainer">').css({
                position: 'relative'
            });
            el.ImgContainer = $('<div class="jqGSImgContainer" style="height:' + el.opts.height + 'px;position:relative;overflow:hidden">').css({
                height: el.opts.height,
                width: el.opts.width,
                position: 'relative',
                overflow: 'hidden'
            });
            el.jqthis.css({
                height: height,
                width: width
            });
            el.jqthis.wrap(el.container);
            el.jqthis.wrap(el.ImgContainer);
            el.pagination = $('<div class="jqGSPagination">');
            el.jqthis.parent().parent().append(el.pagination);
            var jqul = $('<ul>').appendTo(el.pagination);
            var pos = {
                x: 0,
                y: 0
            };
            $('a#next_image').click(function() {
                var next = CurrentIndex + 1;
                if ((CurrentIndex + 1) == el.totalChildren) {
                    el.pagination.find('[href$=#0]').click();
                } else {
                    el.pagination.find('[href$=#' + next + ']').click();
                }
            });
            $('a#prev_image').click(function() {
                var prev = CurrentIndex - 1;
                var last = el.totalChildren - 1;
                if ((CurrentIndex - 1) < 0) {
                    el.pagination.find('[href$=#' + last + ']').click();
                } else {
                    el.pagination.find('[href$=#' + prev + ']').click();
                }
            });
            el.jqchildren.each(function(j) {
                var selected = '';
                if (j == 0) selected = 'selected';
                
                var popup = ( $("#imageBanners li:nth-child("+(j+1)+") div.popup").html() != null ) ? '<div class="popup">' + $("#imageBanners li:nth-child("+(j+1)+") div.popup").html() + '</div>' : '';
                
                var $a = $('<a href="#' + (j) + '" class="' + selected + '">' + '&nbsp;'+popup+'</a>').hover(function() { $(this).children('.popup').fadeIn('fast'); }, function() { $(this).children('.popup').fadeOut('fast'); }).click(function() {
                    CurrentIndex = j;
                    var href = this.index;
                    el.pagination.find('.selected').removeClass('selected');
                    $(this).addClass('selected');
                    var params = {};
                    if (el.opts.direction == 'diagonal') {
                        params = {
                            right: (el.opts.width * href),
                            bottom: (el.opts.height * href)
                        };
                    } else if (el.opts.direction == 'vertical') {
                        params = {
                            bottom: (el.opts.height * href)
                        };
                    } else if (el.opts.direction == 'horizontal') {
                        params = {
                            right: (el.opts.width * href)
                        };
                    };
                    el.jqthis.stop().animate(params, el.opts.speed, el.opts.ease);
                    index = href;
                    return false;
                });
                var n = $a.get(0);
                n.index = j;
                $('<li>').appendTo(jqul).append($a);
                if (el.opts.direction == 'diagonal') {
                    pos.x = j * el.opts.width;
                    pos.y = j * el.opts.height;
                } else if (el.opts.direction == 'horizontal') {
                    pos.x = j * el.opts.width;
                } else if (el.opts.direction == 'vertical') {
                    pos.y = j * el.opts.height;
                };
                var jqchild = $(this).css({
                    height: el.opts.height,
                    width: el.opts.width,
                    position: 'absolute',
                    left: pos.x,
                    top: pos.y
                });
                var jqimg = jqchild.find('img.banner').hide();
                if (jqimg.parent().is('a')) {
                    var p = jqimg.parent();
                    jqimg.get(0).linkHref = p.attr('href');
                    p.remove();
                    jqimg.appendTo(jqchild);
                };
                jqimg.click(function() {
                    var next = n.index + 1;
                    if ((n.index + 1) == el.totalChildren) {
                        el.pagination.find('[href$=#0]').click();
                    } else {
                        el.pagination.find('[href$=#' + next + ']').click();
                    }
                });
                var $loader = $('<div class="jqGSLoader">').appendTo(jqchild);
								// var $titleHolder;
                // var $titleHolder = $('<div class="jqGSTitle">').appendTo(jqchild).css({
                //                     opacity: el.opts.titleOpacity
                //                 }).hide();
                var image = new Image();
                image.onload = function() {
                    image.onload = null;
                    $loader.fadeOut();
                    jqchild.find('.info').fadeIn('slow');
                    jqimg.css({
                        marginLeft: -image.width * .5,
                        marginTop: -image.height * .5,
                        position: 'absolute',
                        left: '50%',
                        top: '50%'
                    }).fadeIn();
                    var alt = jqimg.attr('alt');
                    // if (typeof alt != 'undefined') {
                    //                         $titleHolder.text(alt).fadeIn();
                    //                     }
                };
                image.src = jqimg.attr('src');
            });
        });
    };
    jqGalScroll = {
        ease: null,
        speed: 800,
        height: 500,
        width: 500,
        titleOpacity: .60,
        direction: 'horizontal'
    };
})(jQuery);

$(document).ready(function() {
    $("#imageBanners").jqGalScroll({
        width: 759,
        height: 391,
        speed: 200
    });	
});