var fragment_width = 290;
$(function() {
	function sync_galery_controls_rest(gallery) {
	    // Get calcs for next pass (in order  to sync controls)
	    var new_left = parseInt(gallery.find('.scroller-r').css('left').replace(/px$/, '')),
			min_left = -1 * ( (Math.ceil(gallery.find('.scroller-r a').length) / 3) - 1 ) * 100,
			max_left = 0;
		
		if (new_left <= min_left) {
	        gallery.find('.next-r').css({'visibility': 'hidden'});
	    } else {
	    	gallery.find('.next-r').css({'visibility': 'visible'});
		}
	    
	    if (new_left >= max_left) {
	        gallery.find('.prev-r').css({'visibility': 'hidden'});
	    } else {
	    	gallery.find('.prev-r').css({'visibility': 'visible'});
		}
	}
	$('.gallery-r .prev-r, .gallery-r .next-r').click(function(){
		
        var factor = $(this).hasClass('next-r') ? -1 : 1,
            scroller = $(this).parents('.gallery-r').find('.scroller-r')
            max_left = 0,
            num_per_page = 3,
            min_left = -1 * ( Math.ceil(scroller.find('a').length + 1) / num_per_page) * 100,
            new_left = parseInt(scroller.css('left').replace(/px$/, '')) + factor * 100;
        var $this = $(this);
        scroller.not(':animated').animate({'left': (isNaN(new_left) ? 0 : new_left) + 'px'}, 400, function () {
            sync_galery_controls_rest($this.parents('.gallery-r'));
        });
        return false;
    });
    $('.gallery-r').each(function () {
        sync_galery_controls_rest($(this));
    });
	/*
    $('.restaurant-switch img').click(function () {
        var bigimg = $(this).parents('.image').find('img:eq(0)');
        var new_big_img = $(this).attr('src').replace('/restaurants_thumb/', '/restaurants/');
        $(this).attr('src', bigimg.attr('src').replace('/restaurants/', '/restaurants_thumb/'));
        bigimg.attr('src', new_big_img);
        return false;
    });
    */
});
function sync_galery_controls(gallery) {
    // Get calcs for next pass (in order  to sync controls)
    var new_left = parseInt(gallery.find('.scroller').css('left').replace(/px$/, '')),
		min_left = -1 * ( (Math.ceil(gallery.find('.scroller a').length) / 5) - 1 ) * fragment_width,
		max_left = 0;
    if (new_left <= min_left) {
        gallery.find('.next').css({'visibility': 'hidden'});
    } else {
    	gallery.find('.next').css({'visibility': 'visible'});
	}
    
    if (new_left >= max_left) {
        gallery.find('.previous').css({'visibility': 'hidden'});
    } else {
    	gallery.find('.previous').css({'visibility': 'visible'})
	}
}
$(function(){
    $('.gallery .previous, .gallery .next').click(function(){
        var factor = $(this).hasClass('next') ? -1 : 1,
            scroller = $(this).parents('.gallery').find('.scroller')
            max_left = 0,
            num_per_page = 5,
            min_left = -1 * ( Math.ceil(scroller.find('a').length + 1) / num_per_page) * fragment_width,
            new_left = parseInt(scroller.css('left').replace(/px$/, '')) + factor * fragment_width;
        var $this = $(this);
        scroller.not(':animated').animate({'left': new_left + 'px'}, 400, function () {
            sync_galery_controls($this.parents('.gallery'));
        });
        
        return false;
    });
    $('.gallery').each(function () {
        sync_galery_controls($(this));
    });
});

var fragment_width = 290;
$(function() {
	function sync_galery_controls_rest(gallery) {
	    // Get calcs for next pass (in order  to sync controls)
	    var new_left = parseInt(gallery.find('.scroller-r').css('left').replace(/px$/, '')),
			min_left = -1 * ( (Math.ceil(gallery.find('.scroller-r a').length) / 3) - 1 ) * 100,
			max_left = 0;
		
		if (new_left <= min_left) {
	        gallery.find('.next-r').css({'visibility': 'hidden'});
	    } else {
	    	gallery.find('.next-r').css({'visibility': 'visible'});
		}
	    
	    if (new_left >= max_left) {
	        gallery.find('.prev-r').css({'visibility': 'hidden'});
	    } else {
	    	gallery.find('.prev-r').css({'visibility': 'visible'});
		}
	}
	$('.gallery-r .prev-r, .gallery-r .next-r').click(function(){
		
        var factor = $(this).hasClass('next-r') ? -1 : 1,
            scroller = $(this).parents('.gallery-r').find('.scroller-r')
            max_left = 0,
            num_per_page = 3,
            min_left = -1 * ( Math.ceil(scroller.find('a').length + 1) / num_per_page) * 100,
            new_left = parseInt(scroller.css('left').replace(/px$/, '')) + factor * 100;
        var $this = $(this);
        scroller.not(':animated').animate({'left': (isNaN(new_left) ? 0 : new_left) + 'px'}, 400, function () {
            sync_galery_controls_rest($this.parents('.gallery-r'));
        });
        return false;
    });
    $('.gallery-r').each(function () {
        sync_galery_controls_rest($(this));
    });
	/*
    $('.spec_events-switch img').click(function () {
        var bigimg = $(this).parents('.image').find('img:eq(0)');
        var new_big_img = $(this).attr('src').replace('/spec_events_thumb/', '/spec_events/');
        $(this).attr('src', bigimg.attr('src').replace('/spec_events/', '/spec_events_thumb/'));
        bigimg.attr('src', new_big_img);
        return false;
    });
    */
});
function sync_galery_controls(gallery) {
    // Get calcs for next pass (in order  to sync controls)
    var new_left = parseInt(gallery.find('.scroller').css('left').replace(/px$/, '')),
		min_left = -1 * ( (Math.ceil(gallery.find('.scroller a').length) / 5) - 1 ) * fragment_width,
		max_left = 0;
    if (new_left <= min_left) {
        gallery.find('.next').css({'visibility': 'hidden'});
    } else {
    	gallery.find('.next').css({'visibility': 'visible'});
	}
    
    if (new_left >= max_left) {
        gallery.find('.previous').css({'visibility': 'hidden'});
    } else {
    	gallery.find('.previous').css({'visibility': 'visible'})
	}
}
$(function(){
    $('.gallery .previous, .gallery .next').click(function(){
        var factor = $(this).hasClass('next') ? -1 : 1,
            scroller = $(this).parents('.gallery').find('.scroller')
            max_left = 0,
            num_per_page = 5,
            min_left = -1 * ( Math.ceil(scroller.find('a').length + 1) / num_per_page) * fragment_width,
            new_left = parseInt(scroller.css('left').replace(/px$/, '')) + factor * fragment_width;
        var $this = $(this);
        scroller.not(':animated').animate({'left': new_left + 'px'}, 400, function () {
            sync_galery_controls($this.parents('.gallery'));
        });
        
        return false;
    });
    $('.gallery').each(function () {
        sync_galery_controls($(this));
    });
});
