var oldWidth=-200,
loading=false;

function lastPostFunc(id, rel, wh) 
{ 
	loading=true;
    //$('div#lastPostsLoader').html('<img src="bigLoader.gif">');
	jQuery("a.newimg").removeClass("newimg");
    $.post("getimg.php?action=getLastPosts&gId="+rel+"&lastID=" + $("#"+id+" .item:last").attr("subId")+"#images",     

    function(data){
        if (data != "") {
			$("#"+id+" .item:last").parent().parent().after(data);            
        }
		oldWidth = wh;
		loading=false;
		jQuery("a.newimg").fancybox();
    });
}; 


$(document).ready(function(){

    jQuery("a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox(/*{
		"zoomOpacity": 		true,
		"zoomSpeedIn":		300,
		"zoomSpeedOut":	300,
		"overlayShow":		true
	}*/);    
	
	$(".subGalList").each(function(){
			var sL=$("<a/>").appendTo($(this)),
			sR=$("<a/>").appendTo($(this));
			sR.attr({rel:$(this).attr("id"),"class":"sR",href:"#"});
			sL.attr({rel:$(this).attr("id"),"class":"sL",href:"#"});
		}
	);
	
	$("a.sL").click(function(){
		var id=$(this).attr("rel"),
		div=$("#"+id+" div")
		div.scrollLeft(div.scrollLeft()-200);
		return false;
	});
	
	$("a.sR").click(function(){
		var id=$(this).attr("rel"),
		div=$("#"+id+" div")
		div.scrollLeft(div.scrollLeft()+200);
		return false;
	});
	
	$(".subGalList div").scroll(function(){
		var wh = $(this).find("ul").width()
        if  ( !loading && wh > oldWidth && 
			$(this).scrollLeft() > wh - 800 
			&&
			$(this).scrollLeft() <= wh - 616
			){
           lastPostFunc($(this).parent().attr("id"),$(this).parent().attr("subId"),wh);
        }
	});
	
});
