jQuery.extend(jQuery.easing, {
    onet: function(x, t, b, c, d) {
        var dc = 0.9, dt = 0.35; // 0..dt: v = dc/dt, dt..1: v = (1-dc)/(1-dt)
        if(t/d < dt) {
            return b + (t/d / dt)*c*dc;
        } else {
            return b + dc*c + ((t/d - dt)/(1 - dt))*c*(1 - dc);
        }
    }
});
$.prototype.allWidth = function() {
  var ret = this.outerWidth() + parseInt(this.css('margin-left')) + parseInt(this.css('margin-right'));
  return ret || 0;
};
var AjaxGalleryAds = {
    curOffset: 0,
    lock: true,
    loadContainer: function(suffix, offset, buttons) {
        var path = AjaxGalleryAds.url_prefix;
        path += offset + '/';
        if(AjaxGalleryAds.search_query)
            path += '?query=' + encodeURIComponent(AjaxGalleryAds.search_query);
        $('#'+AjaxGalleryAds.prefix+suffix).load(path, function() {
            $('#'+AjaxGalleryAds.prefix+suffix).find('.gallery_ads_box').removeClass('gallery_ads_box');
            AjaxGalleryAds.lock = false;
            if($('.'+AjaxGalleryAds.prefix+' .gallery_ad').length) {
                $(AjaxGalleryAds.show_elem).show();
            }
            AjaxGalleryAds.resetContainers();
            if(buttons) {
                if(AjaxGalleryAds.curOffset < AjaxGalleryAds.scrollby) {
                    $('#aga_prev').css("display","none");
                } else {
                    $('#aga_prev').css("display","block");
                }
                if($('#'+AjaxGalleryAds.prefix+'2 .gallery_ad').length <= AjaxGalleryAds.count-AjaxGalleryAds.scrollby) {
                    $('#aga_next').css("display","none");
                } else {
                    $('#aga_next').css("display","block");
                }
            }
			$(".gallery_ad").mouseover(function () {$(this).addClass("hover");})
    		$(".gallery_ad").mouseout(function () {$(this).removeClass("hover");})
        });
    },
    resetContainers: function() {
        $('#'+AjaxGalleryAds.prefix).css({
            'left': '0'
        });
        $('#'+AjaxGalleryAds.prefix+'0').css({
            'display': 'block',
            'top': '-'+$('#'+AjaxGalleryAds.prefix).height()+'px',
            'left': '-'+$('#'+AjaxGalleryAds.prefix+' .gallery_ad').allWidth()*AjaxGalleryAds.scrollby_cols+'px',
            'height': $('#'+AjaxGalleryAds.prefix).height()
        });
        $('#'+AjaxGalleryAds.prefix+'2').css({
            'display': 'block',
            'top': '-'+$('#'+AjaxGalleryAds.prefix).height()*2+'px',
            'left': $('#'+AjaxGalleryAds.prefix+' .gallery_ad').allWidth()*AjaxGalleryAds.scrollby_cols,
            'height': $('#'+AjaxGalleryAds.prefix).height()
        });
        $('#ajax_gac_cut').css({
            'height': $('#'+AjaxGalleryAds.prefix).height()
        });
    },
    scrollRight: function() {
        if(AjaxGalleryAds.lock || $('#'+AjaxGalleryAds.prefix+'2 .gallery_ad').length <= AjaxGalleryAds.count-AjaxGalleryAds.scrollby) {
            return;
        }
        AjaxGalleryAds.curOffset += AjaxGalleryAds.scrollby;
        AjaxGalleryAds.lock = true;
        $('#'+AjaxGalleryAds.prefix).animate({
            'left': '-'+$('#'+AjaxGalleryAds.prefix+' .gallery_ad').allWidth()*AjaxGalleryAds.scrollby_cols
        }, 'slow', 'onet');
        $('#'+AjaxGalleryAds.prefix+'2').animate(
            {'left': '0'},
            {easing: 'onet',
             duration: 'slow',
             complete: function() {
                $('#'+AjaxGalleryAds.prefix+'0').html($('#'+AjaxGalleryAds.prefix).html());
                $('#'+AjaxGalleryAds.prefix).html($('#'+AjaxGalleryAds.prefix+'2').html());
                AjaxGalleryAds.resetContainers();
                AjaxGalleryAds.loadContainer('2', AjaxGalleryAds.curOffset+AjaxGalleryAds.scrollby, true);
            }}
        );
    },
    scrollLeft: function() {
        if(AjaxGalleryAds.lock || AjaxGalleryAds.curOffset < AjaxGalleryAds.scrollby) {
            return;
        }
        AjaxGalleryAds.curOffset -= AjaxGalleryAds.scrollby;
        AjaxGalleryAds.lock = true;
        $('#'+AjaxGalleryAds.prefix).animate({
            'left': $('#'+AjaxGalleryAds.prefix+' .gallery_ad').allWidth()*AjaxGalleryAds.scrollby_cols
        }, 'slow', 'onet');
        $('#'+AjaxGalleryAds.prefix+'0').animate(
            {'left': '0'},
            {easing:'onet',
             duration: 'slow',
             complete: function() {
                $('#'+AjaxGalleryAds.prefix+'2').html($('#'+AjaxGalleryAds.prefix).html());
                $('#'+AjaxGalleryAds.prefix).html($('#'+AjaxGalleryAds.prefix+'0').html());
                AjaxGalleryAds.resetContainers();
                AjaxGalleryAds.loadContainer('0', AjaxGalleryAds.curOffset-AjaxGalleryAds.scrollby, true);
            }}
        );
    },
    init: function(url_prefix, prefix, previd, nextid, show_elem, count, scrollby, scrollby_cols, search_query) {
        AjaxGalleryAds.url_prefix = url_prefix;
        AjaxGalleryAds.prefix = prefix;
        AjaxGalleryAds.previd = previd;
        AjaxGalleryAds.nextid = nextid
        AjaxGalleryAds.show_elem = show_elem;
        AjaxGalleryAds.count = count;
        AjaxGalleryAds.scrollby = scrollby;
        AjaxGalleryAds.scrollby_cols = scrollby_cols;
        AjaxGalleryAds.search_query = search_query;
        $(document).ready(function() {
            AjaxGalleryAds.loadContainer('', 0);
            AjaxGalleryAds.loadContainer('2', AjaxGalleryAds.scrollby, true);
            $('#'+previd).click(function() {
                AjaxGalleryAds.scrollLeft();
                return false;
            });
            $('#'+nextid).click(function() {
                AjaxGalleryAds.scrollRight()
                return false;
            });
        });
    }
};

