SGVisibleNum = 3;
SGOpenSpeed = 0;
SGSpeed = 300;
ScrollReady = true;


$(document).ready(function(){
	$(".photos a").fancybox({
		hideOnContentClick: false
	});
	
	$('embed').attr('wmode','opaque').parent().css('display','block');
	firstItem = $('.SGallery .item:first');
	SGWidthItem = firstItem.width()+parseInt(firstItem.css('padding-left'))+parseInt(firstItem.css('padding-right'));
	SGHeightItem = firstItem.height()+parseInt(firstItem.css('padding-top'))+parseInt(firstItem.css('padding-bottom'));
	SGWidth = SGVisibleNum*SGWidthItem;
	SGHeight = SGHeightItem;
	ArrowH = $('.arrow-left img').height();
	ArrowW = $('.arrow-left img').width();
	$('.arrow-left img, .arrow-right img').css({ marginTop : SGHeight/2-ArrowH/2+"px" });
	
	$('.arrow-left, .arrow-right').height(SGHeight);
	$('.photos').width(SGWidth);
	$('.photos .inner').css({ marginLeft: "0px"}).width($(this).find('.item').length*SGWidthItem);
	
	SGWidthWhole = 2*ArrowW + SGWidth;
	$('.SGallery .wrap').width(SGWidthWhole);
	
	$('.SGallery').css('visibility', 'visible').animate({height : SGHeight}, SGOpenSpeed);
	$('.SGallery a').focus(function(){$(this).blur()});
	
	$(".arrow-right a").click(function() {
    		if(ScrollReady) {
	    		ScrollReady = false;
	    		obj = $(this).parents('.SGallery').find('.inner');
	    		tail = parseInt(obj.css('margin-left')) + obj.width() - SGWidth;
	    		//alert("12");
	    		offset = tail >= SGWidth ? SGWidth : tail;
	    		if(tail > 0) {
	    			obj.animate(
							{ marginLeft: "-="+offset+"px" },
							SGSpeed,
							function() { ScrollReady = true; }
						);
				} else { ScrollReady = true; };
			}
    		return false;
     });

    $(".arrow-left a").click(function() {
    	if(ScrollReady) {
	    	ScrollReady = false;
	    	obj = $(this).parents('.SGallery').find('.inner');
	    	tail = parseInt(obj.css('margin-left'));
	    	offset = tail + SGWidth <= 0 ? SGWidth : -tail;
	    	if(tail < 0) {
	    		obj.animate(
						{ marginLeft: "+="+offset+"px" },
						SGSpeed,
						function() { ScrollReady = true; }
					);
			} else { ScrollReady = true; };
		}
    	return false;
    });
});