/*function phnext() {
	
	cur++;
	if (cur==imgs.length-1) document.getElementById('phnext').style.display='none';
	if (cur>0) document.getElementById('phprev').style.display='block';
	
	$("img#phcur").attr("src",'/');		
	$("img#phcur").fadeOut("fast", function(){
		$("img#phcur").attr("src","/res/gallery/large/"+imgs[cur]+".jpg");
		$("img#phnextimg").attr("src","/res/gallery/small/"+imgs[cur+1]+".jpg");				
		$("img#phprevimg").attr("src","/res/gallery/small/"+imgs[cur-1]+".jpg");
		$("span#pointer").html(cur+1);
	});
	$("img#phcur").fadeIn("slow");

}
function phprev() {
	cur--;
	if (cur<imgs.length-1) document.getElementById('phnext').style.display='block';
	if (cur==0) document.getElementById('phprev').style.display='none';
	$("img#phcur").attr("src",'/');		
	$("img#phcur").fadeOut("fast", function(){
		$("img#phcur").attr("src","/res/gallery/large/"+imgs[cur]+".jpg");
		$("img#phnextimg").attr("src","/res/gallery/small/"+imgs[cur+1]+".jpg");				
		$("img#phprevimg").attr("src","/res/gallery/small/"+imgs[cur-1]+".jpg");
		$("span#pointer").html(cur+1);
	});
	$("img#phcur").fadeIn("slow");
	

}
*/
var gid;
var curnum=0;
function showGallery(id,cs,curi,idsaps) {
	
	gid=id;		
	curnum=0;
	cur=0;
	
	$('div#photos').css('top',parseInt(($(document).scrollTop())+110)+"px")
	$('div#shadow').css('top',parseInt(($(document).scrollTop())+100)+"px")
	$('span#css').html(cs);
	$('div#photos').fadeIn('slow');$('div#shadow').fadeIn('slow');
	$('img.ram').removeClass('ram');
	$('img#img'+curnum).addClass('ram');
	$('#phcur').attr("src","/res/gallery/large/"+curi+".jpg");
	//$('#phnextimg').attr("src","/res/gallery/small/"+next+".jpg");
	
	$("#phprev").hide();
	$("#phprev_dis").show();
	
	if(imgs[gid].length>4){
		$("#phnext").show();
		$("#phnext_dis").hide();
	}
	else{
		$("#phnext").hide();
		$("#phnext_dis").show();
	}
	
	
	var photos_width=(parseInt(imgs[gid].length)*(126+16)+4);
	var conteiner_left=photos_width-580;
		
	var conteiner_parent_width=photos_width+conteiner_left;
	$('#conteiner').empty();
	if(conteiner_left>0){
		$('#cont_parent').css('width',conteiner_parent_width+'px');
		$('#cont_parent').css('left','-'+conteiner_left+'px');
		$('#conteiner').css('left',conteiner_left+'px');
		$('#conteiner').css('width',photos_width+'px');
	}
	else{
		$('#conteiner').css('left','0px');
		$('#cont_parent').css('left','0px');
		$('#cont_parent').css('width',conteiner_parent_width+'px');
		$('#conteiner').css('width',photos_width+'px');
	}
	for(i in imgs[gid]){
		$('#conteiner').append('<a onclick="showImage(\'/res/gallery/large/'+imgs[gid][i]+'.jpg\',this);return false;" class="img"><img width="126" height="96" id="img0" src="/res/gallery/medium/'+imgs[gid][i]+'.jpg" /></a>');
	}
	var div=document.createElement('div');
	$(div).addClass('cl');
	$(div).html('&nbsp;');
	$('#conteiner').append(div);
	$('#conteiner :first-child').children('img').addClass('ram');
}

function showPlans(shadow_id,photo_id){	
	$('div#'+photo_id).css('top',parseInt(($(document).scrollTop())+130)+"px");
	$('div#'+shadow_id).css('top',parseInt(($(document).scrollTop())+90)+"px");
	$('div#'+photo_id).fadeIn();
	$('div#'+shadow_id).fadeIn();
}

function phnext() {
	cur++;
	curnum--;
	var conteiner_left=parseInt($('#conteiner').css('left'));
	var cont_parent=parseInt($('#cont_parent').css('left'));
	if(conteiner_left>=0){
		if(conteiner_left+cont_parent<=0){
			//$('#conteiner').css('left',(conteiner_left-(126+16))+'px');
			$("#conteiner").animate({"left":"-="+(126+16)+"px"},"slow");
		}else{
			$("#conteiner").animate({"left":"-="+(126+20)+"px"},"slow");
		}
		$("#phprev_dis").hide();
		$('#phprev').show();
	}else{
		$("#conteiner").animate({"left":"0px"},"slow");
		$("#phnext").hide();
		$("#phnext_dis").show();
	}
}

function phprev() {
	var conteiner_left=parseInt($('#conteiner').css('left'));
	var cont_parent=parseInt($('#cont_parent').css('left'));
	if(conteiner_left+cont_parent+142<=0){
		if(conteiner_left+cont_parent<=0){
			//$('#conteiner').css('left',(conteiner_left-(126+16))+'px');
			$("#conteiner").animate({"left":"+="+(126+16)+"px"},"slow");
		}else{
			$("#conteiner").animate({"left":"+="+(126+20)+"px"},"slow");
		}
		$("#phnext").show();
		$("#phnext_dis").hide();
	}else{
		$("#conteiner").animate({"left":-1*cont_parent+"px"},"slow");
		$("#phprev_dis").show();
		$('#phprev').hide();
	}
}

function showImage(link,a) {
	if(!disableSet){
		$("img#phcur").remove();
		var img=$("<img id=\"phcur\" src=\""+link+"\"/>");
		
		$('div.cur').append(img);
		$("img.ram").removeClass('ram');
		$(a).children('img').addClass('ram');
		img.css('opacity','0');
		img.load(function(){
			img.animate({opacity:1},300);
		});
	}
		
}

var disableSet;
$(document).ready(function (){

	var a=$('table tr:first').children('td');	
	$('table tr:first-child td,	table.table tr:first-child td, table tr:first-child th,table.table tr:first-child th ').css('border','none');
	$("div.container").draggable({ 
	    axis: "x",
	    containment: "parent",
	    start:function(e,ui){
			disableSet=true;			
		},
	    stop:function(){
	    	var conteiner_left=parseInt($('#conteiner').css('left'));
			var cont_parent=parseInt($('#cont_parent').css('left'));
			if(conteiner_left==0){
	    		$("#phnext_dis").show();
				$('#phnext').hide();
			}
			else{
	    		$("#phnext_dis").hide();
				$('#phnext').show();
			}
			
			if(conteiner_left+cont_parent>=0){
	    		$("#phprev_dis").show();
				$('#phprev').hide();
	    	}
	    	else{
	    		$("#phprev_dis").hide();
				$('#phprev').show();
	    	}
			setTimeout(function(){disableSet=false;},300);
	    },
	    cursor: "cursor:url('/res/gfx/closedhand.cur'), default"
						
	    
	    
	});
})
