$(function() {
    
    
    
	$('#mx-gallery-bottom-tumbs img').click(function(){
		$('#mx-gallery-bottom-tumbs img').removeClass('mx-gallery-selected');
		$(this).addClass('mx-gallery-selected');

		$('#mx-gallery-main-frame img').remove();
 
		$('<img src="' + $(this).attr('src') + '" class="hidd_img" />').appendTo('#mx-gallery-main-frame');
        var img_h = $(this).attr('h');
        var img_w = $(this).attr('w');
        var img_resize_h = "";
        var img_resize_w = "";
        var img_resize_h_bot = "";
        var img_resize_w_bot = "";

        if(img_h>img_w){
            img_resize_h = "auto";
            img_resize_h_bot = "auto"; 
            img_resize_w = 450;
            img_resize_w_bot = 150;
        }else{
            img_resize_h = 375;
            img_resize_h_bot = 125;
            img_resize_w = "auto";
            img_resize_w_bot = "auto";
        }

        wrapper_h = 155+img_resize_h;
        
         $('#mx-gallery-main-frame img').css("height",img_resize_h).css("width",img_resize_w);
         $('#mx-gallery-main-frame ').css("height",img_resize_h);
	 $('#mx-gallery-wrapper ').css("height",wrapper_h);
	 $('#mx-gallery-overlay-slide-left ').css("height",img_resize_h);



        $('#mx-gallery-main-frame img').fadeIn(800);
         
	});

	$('#mx-gallery-overlay-slide-right').click(function(){
		next = $('.mx-gallery-selected').next();
		if (undefined == next.attr('src')) {
			$('img:first', $('#mx-gallery-bottom-tumbs')).click();
		} else {
			next.click();
		}
	});
	
	$('#mx-gallery-overlay-slide-left').click(function(){
		prev = $('.mx-gallery-selected').prev();
		if (undefined == prev.attr('src')) {
			$('img:last', $('#mx-gallery-bottom-tumbs')).click();
		} else {
			prev.click();
		}
	});
	
	var currentMargin = 0;
	var minMargin = -155 * ($('#mx-gallery-bottom-tumbs img').length - 2);
	var l_b_frame = $('#mx-gallery-bottom-tumbs-frame').width();
    var bot_img_leng = 155*$('#mx-gallery-bottom-tumbs img').size();
    
	$('#mx-gallery-bottom-slide-right').click(function(){

        if ((-1*currentMargin) > (-1*minMargin-155)) {
			return false;
		}

if(l_b_frame < bot_img_leng){
		currentMargin = currentMargin - 155;}
		$('#mx-gallery-bottom-tumbs').animate(
			{marginLeft: currentMargin + 'px'}
		);
	});

	$('#mx-gallery-bottom-slide-left').click(function(){
		if (currentMargin > -20) {
			return false;
		}
		
		currentMargin = currentMargin + 155;
		$('#mx-gallery-bottom-tumbs').animate(
			{marginLeft: currentMargin + 'px'}
		);
	});
	
	$('img:first', $('#mx-gallery-bottom-tumbs')).click();
   
	
});

