//

function gallery_init(div,ids,galery_id) 
{
	window['jq_pictures'] = ids;
	window['jq_galery_id'] = galery_id;
	window['jq_div'] = div;
	window['jq_pages'] = Math.ceil(ids.length/8);
	window['jq_current_page'] =  1;
	window['jq_current_image'] =  -1;
	window['jq_check'] = 0;
	
	gallery_create();	
	gallery_get(0);
}

//

function gallery_create()
{
	var h = 0;
	
	$(jq_div).append('<table cellpadding="0" cellspacing="0" border="0"><tr><td id="prevPage"></td><td><div id="pageContainer" style="background-color: #EEE; overflow: hidden; position: relative; height: 83px; width: 615px;"><div id="thumbnails" style="width: 10000px;"></div></div></td><td id="nextPage"></td></tr><tr><td>&nbsp;</td><td style="background-color: #EEE; border-top: 2px solid #FFF; padding: 20px; text-align: center; height: 400px;"><div id="bigImageContainer" style="position: relative; margin: auto;"></div></td><td>&nbsp;</td></tr></table>');
	
	for (i=0; i<=jq_pages; i++ ) 
	{
		//startdiv
		$('#thumbnails').append("<div id='page_"+i+"' style='margin: 3px; float: left;'>");
		//thumbnails
		for (j=h+7;h<=j&&h<jq_pictures.length;h++) 
		{
			$('#page_'+i+'').append("<img id='pic_"+h+"' onclick='gallery_get("+h+")' style='cursor: pointer; border: 0px solid black; vertical-align: top; margin: 3px;' src='/image/article/galery/"+jq_galery_id+"/"+jq_pictures[h]+"_70x70.jpg' alt='' />");
			$('#pic_'+h+'').css("opacity",0.6);
		}
		$('#page_'+i+'').css("width",($('#pageContainer').width()-7));
	}
	
	gallery_pager(0);
}

//

function gallery_pager(jump)
{
	if (jump == 0) 
	{
		if (jq_pages > 1 && jq_current_page < jq_pages)
		{
			$('#nextPage').html("<img src='/pic/galery/next.jpg' alt='' style='vertical-align: bottom;' />");
			$('#nextPage').css("cursor","pointer");
		}
		else
		{
			$('#nextPage').html("<img src='/pic/galery/next_dis.jpg' alt='' style='vertical-align: bottom;' />");
			$('#nextPage').css("cursor","default");
		}
		if (jq_pages > 1 && jq_current_page != 1)
		{
			$('#prevPage').html("<img src='/pic/galery/prev.jpg' alt='' style='vertical-align: bottom;' />");
			$('#prevPage').css("cursor","pointer");
		}
		else
		{
			$('#prevPage').html("<img src='/pic/galery/prev_dis.jpg' alt='' style='vertical-align: bottom;' />");
			$('#prevPage').css("cursor","default");
		}
	}
	if (jq_check == 1) {
		return;
	}
	if (jq_pages > 1 && jump == +1 && jq_current_page < jq_pages)
	{
		jq_check = 1;
		gallery_get(((jq_current_page)*8));
		$('#pageContainer').animate({scrollLeft: '+=614px'},500,function()
		{
			jq_current_page++;	
			gallery_pager(0);
			jq_check = 0;
		});
	}
	//

	if (jq_pages > 1 && jump == -1 && jq_current_page != 1)
	{
		jq_check = 1;
		gallery_get(((jq_current_page-2)*8));
		$('#pageContainer').animate({scrollLeft: '-=614px'},500,function()
		{
			jq_current_page--;	
			gallery_pager(0);
			jq_check = 0;
		});
	}
	
	//
	
	if (jq_pages > 1 && jump == 'last')
	{
		jq_check = 1;
		$('#pageContainer').animate({scrollLeft: '+='+((jq_pages-1)*614)+'px'},500,function()
		{
			jq_current_page = jq_pages;	
			gallery_pager(0);
			jq_check = 0;
		});		
	}
	
	//
	
	if (jq_pages > 1 && jump == 'first')
	{
		jq_check = 1;
		$('#pageContainer').animate({scrollLeft: '-='+((jq_pages-1)*614)+'px'},500,function()
		{
			jq_current_page = 1;	
			gallery_pager(0);
			jq_check = 0;
		});		
	}
	
	//
	
	if (jq_pages > 1) 
	{
		//next arrow
		$('#nextPage').click(function () 
		{
			gallery_pager(+1);
		});
		//prev arrow
		$('#prevPage').click(function () 
		{
			gallery_pager(-1);
		});
	}
}

//

function gallery_get(image)
{
	//alert(jq_current_image);
	if (image == jq_current_image) { return; }
	$('#bigImageContainer').html("<div id='loader' style='font-size: 11px; font-family: Arial;'><img src='/pic/galery/loader.gif' alt='' /><br />Kérem várjon...</div>");			
		//
	$('#bigImageContainer').append("<img id='bigImage' style='cursor: pointer; vertical-align: bottom; display: none;' src='/image/article/galery/"+jq_galery_id+"/"+jq_pictures[image]+"_400x400.jpg' alt='' />",function () {

	});
			for (i=0; i<jq_pictures.length; i++) {
				if (i != image) {
					$('#pic_'+i+'').animate({opacity: 0.6},300);
				}
			}
			$('#pic_'+image+'').animate({opacity: 1},300);
			$('#bigImage').load(function () {
				jq_current_image = image;
				$('#loader').hide();
				$('#bigImage').fadeIn(500);
				$('#bigImageContainer').append("<div id='position' style='position: absolute; left: 0px; margin: auto; font-family: Arial; font-size: 11px; padding: 5px 0 5px 0; background-color: white; bottom: 0px; cursor: pointer; width: 100%;'><div style='float: right; margin-right: 5px;' id='nextImage'>Következő</div><div style='float: left; margin-left: 5px;' id='prevImage'>Előző</div></div>");
				$('#bigImageContainer').css("width",$('#bigImage').width());
				$('#position').css("opacity",0.5);
				$('#position').append((image+1)+"/"+jq_pictures.length);
			   $('#bigImage').click(function () { window.open("/image/article/galery/"+jq_galery_id+"/"+jq_pictures[image]+".jpg",'Kép','width=800,height=600'); });
			   $("#bigImageContainer").mousemove(function(e){

					$('#position').slideDown(300);
			   }); 
			   ///
			   $("#bigImageContainer").mouseleave(function()
			   {
			   		$('#position').slideUp(300);
			   	});
				//nextImage
				
				$('#nextImage').click(function () {
					if ((image+1)%8 == 0) {
						gallery_pager(+1);
					}
					if (image+1 == jq_pictures.length)
					{
						gallery_get(0);
						gallery_pager('first');
					}
					else
					{
						gallery_get(image+1);
					}	
				});
				
				//prevImage
				
				$('#prevImage').click(function () {
					if ((image)%8 == 0) {
						gallery_pager(-1);
					}
					if (image == 0)
					{
						gallery_get(jq_pictures.length-1);
						gallery_pager('last');
					}
					else
					{
						gallery_get(image-1);
					}
					
				});
			});		
}

