function setSliderDimension()
{
    var padding = 41 + ($(document).width()-82)/2 - (Math.floor( ($(document).width()-82) / 283 ) * 283)/2;


    $('#slider').width( $(document).width()-padding*2 );
    $('#slider-arrow-right').css( 'padding-right',padding-30);
    $('#mask-gallery').width($('#slider').width() - 15 );
    $('#gallery').width( 570 * $('#gallery li').length );
    $('#mask-gallery, #gallery li').height($('#slider').height());
	
    $('#btn-next').unbind('click');
    $('#btn-next').click(function () {
	$('#btn-next').unbind('click');
	$('#btn-prev').unbind('click');
        newsslider(0);
        return false;
    });

    $('#btn-prev').unbind('click');
    $('#btn-prev').click(function () {
	$('#btn-prev').unbind('click');
	$('#btn-next').unbind('click');
        newsslider(1);
        return false;
    });

	//1683
	
    if( ($('#gallery li').length * 567 - 17) <= $('#slider').width() )
    {
	$( '#btn-next' ).hide();
	$( '#btn-prev' ).hide();
    }
    else
    {
	$( '#btn-next' ).show();
	$( '#btn-prev' ).show();
    }

    if( !$('#gallery li.selected').length )
	$('#gallery li:first').addClass('selected');
}

$(document).ready(function() {
    setSliderDimension();
    $('#gallery li:first').addClass('selected');
});  
   
   
function newsslider(prev) {   
    var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');  
   
    var next_image = '';
    if (prev) {  
        if(current_image.prevAll().length == 0) {
			if( $('#mask-gallery').width() > 1450 )
				next_image = $('#gallery li:last').prev().prev();
			else if( $('#mask-gallery').width() > 880 )
				next_image = $('#gallery li:last').prev();
			else
				next_image = $('#gallery li:last');
        } else {
            next_image = current_image.prev();  
        }        
    } else {
        if( ((current_image.nextAll().length+1) * 561 - 11) <= $('#mask-gallery').width() ) {
            next_image = $('#gallery li:first')
        } else {
         next_image = current_image.next();  
        }
    }  

     $('#gallery li').removeClass('selected');  

    next_image.addClass('selected');  

     $('#mask-gallery').scrollTo(next_image, 500, {onAfter : function(){
	    $('#btn-next').unbind('click');
	    $('#btn-next').click(function () {
		$('#btn-next').unbind('click');
		$('#btn-prev').unbind('click');
		newsslider(0);
		return false;
	    });

	    $('#btn-prev').unbind('click');
	    $('#btn-prev').click(function () {
		$('#btn-prev').unbind('click');
		$('#btn-next').unbind('click');
		newsslider(1);
		return false;
	    });
     }} );
}
