var imageNr = 0;

var menuLen = 5;
var menuMinNr = 0;
var menuMaxNr = menuLen-1;
var pub_image_tab = new Object();

var active_hash  = -1;

function show(hash) {

	var cont = '#cont' + hash;	
	var pi = '#pi' + hash;

	if ($(cont).is(":hidden")) {
		$("a.publication_link").show();
		$("a#pl" + hash).hide();
		$("div.all_publications div.pub_content").slideUp();
		$(pi).prependTo($('.all_publications'));
		$(cont).slideDown("slow");
		$.scrollTo(pi);
		imageNr = 0;
	} else {
		$(cont).slideUp();
	}

	return false;
}

function shiftImagePub(hash, direction)
{
	var key = 'pit' + hash;
	if ((direction == 'l') || (direction == 'L'))
	{
		if (imageNr > 0)
		{
			imageNr--;
			replaceImage(hash, imageNr);
		}		
	}
	else
	{  	
		var len = pub_image_tab[key]['el'].length -1;
		if (imageNr < len)
		{
			imageNr++;
			replaceImage(hash, imageNr);
		}		
	}
}

function check_arrow(hash, imageNr)
{
	var len = $('#cont'+hash+' a.image_link').length -1;	
	
	if (active_hash < 0)
	{
		var prev = "div.pub_main_thumbnail a.pprev";
		var next = "div.pub_main_thumbnail a.pnext";
		var sleft = "a.arrayScrollLeft";
		var sright = "a.arrayScrollRight";
	}
	else
	{
		var prev = "div.full_gallery a.pprev";
		var next = "div.full_gallery a.pnext";
		var sleft = "div.full_gallery a.leftFullScroll";
		var sright = "div.full_gallery a.rightFullScroll";	
		hash = active_hash;
	}
		
	if (len > 0)
	{
		if (imageNr <= 0)
		{
			$(prev).removeClass('active_link');
			$(next).addClass('active_link');
			
		}
		else if (imageNr >= len)
		{
			$(next).removeClass('active_link');
			$(prev).addClass('active_link');
		}
		else	
		{
			$(next).addClass('active_link');
			$(prev).addClass('active_link');
		}
	}
	
	
	if (len >= menuLen)
	{
		if (menuMinNr <= 0)
		{
			$(sleft).removeClass('active_link');
			$(sright).addClass('active_link');
		}
		else if (menuMaxNr >= len)
		{
			$(sright).removeClass('active_link');
			$(sleft).addClass('active_link');
		}	
		else
		{
			$(sright).addClass('active_link');
			$(sleft).addClass('active_link');
		}
	}	
}

function shiftImageFull(direction)
{
	var hash = active_hash;
	var key = 'pit' + hash;
	if ((direction == 'l') || (direction == 'L'))
	{
		if (imageNr > 0)
		{
			imageNr--;
			replaceImage(hash, imageNr);			
		}
	}
	else
	{
		var len = pub_image_tab[key]['el'].length - 1;
		if (imageNr < len)
		{
			imageNr++;
			replaceImage(hash, imageNr);
		}		
	}
	
	//check_arrow(hash, imageNr);
}

function scrollImages(hash, direction)
{
	var key = 'div.pub_content a.ai' + hash +'-';
	if ((direction == 'l') || (direction == 'L'))
	{
		if (menuMinNr > 0)
		{
			$(key + menuMaxNr).hide();
			menuMaxNr--;
			menuMinNr--;
			$(key + menuMinNr).show();
			
			if (menuMinNr == 0)
			{
				//$("a.arrayScrollLeft").hide();				
			}
			else
			{
				//$("a.arrayScrollRight").show();
			}
		}
	}
	else
	{
		var len = $('#cont'+hash+' a.image_link').length - 1;
		if (menuMaxNr < len)
		{
			$(key + menuMinNr).hide();
			menuMinNr++;			
			menuMaxNr++;
			$(key + menuMaxNr).show();
			
			if (menuMaxNr == len)
			{
				//$("a.arrayScrollRight").hide();				
			}
			else
			{
				//$("a.arrayScrollLeft").show();
			}
			
		}		
	}
	check_arrow(hash, imageNr);
}

function scrollFullImages(direction)
{
	var hash = active_hash;
	var key = 'div.full_gallery a.ai' + hash +'-';
	if ((direction == 'l') || (direction == 'L'))
	{
		if (menuMinNr > 0)
		{
			$(key + menuMaxNr).hide();
			menuMaxNr--;
			menuMinNr--;
			
			$(key + menuMinNr).css('display', '').show();			
		}
	}
	else
	{
		var len = $('#cont'+hash+' a.image_link').length - 1;
		if (menuMaxNr < len)
		{
			$(key + menuMinNr).hide();
			menuMinNr++;			
			menuMaxNr++;
			$(key + menuMaxNr).css('display', '').show();	
		}		
	}
	check_arrow(hash, imageNr);
}

var dbg = new Array();

function makeFullGallery(hash)
{
	active_hash = hash;
	//alert("makeFullGallery");
	var key = 'pit' + hash;
	$("#containers > .inf_content").hide();
	$("#containers > .full_gallery").show();
	replaceImage(hash, imageNr);
	
	
	var thumb = 'div#cont' + hash + ' div.pub_scroll_window a.image_link';
	$(thumb).clone().click(function() {
		//replaceFullImageTh();
	}).appendTo('#containers > .full_gallery div.pub_scroll_window');
 	
	$("div.full_gallery img.thumbnail").each(function(index) {
	
		var w = parseInt($(this).css('width'));
		var h = parseInt($(this).css('height'));
		
		var nw = Math.round(94 * w / h);
		dbg.push({
			index: index,
			w: w,
			h: h,
			nw: nw			
		});
		
		$(this).height(94).width(nw);
	    
	  });

	
	
	var ai = 'div.full_gallery a.ai' + hash + '-';
	
	for (i=menuMinNr; i < menuMaxNr; i++)
	{
    	$(ai+i).show();
	}
}

function closeGallery()
{	
	$("#containers > .full_gallery").hide();
	$("#containers > .full_gallery div.pub_scroll_window").empty();
	$("#containers > .inf_content").show();
	
	var hash = active_hash;
	active_hash = -1;
	
	replaceImage(hash, imageNr);	
		
}


function insert_link(hash)
{
	
	var node = $("#pi" +hash+ " .publication_description");
	
}

function gup( name ) // get from url parameters
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


$(document).ready(function() // odwołania z konkretnym pid
{
	var pid = gup('pid');
	if (pid > 0)
	{
		var select = "a[name='cat" + pid + "']";
		var hash = $(select).parent().attr('id').substr(2);
		show(hash);
		
		var full = gup('full')
		if (full == 3)
		{
			makeFullGallery(hash);
		}
	}
}
);

function css_img_pmi_width()
{
	//return ($("#publication_main_image img.pmi").width() > 500 ? "500px": "auto");
	return ("auto");
}



