var timerID; // timer for popup block showing
var products_list_max_len = 255; // maximal length of used products list text
var ip = null; // image preloader

Event.observe(window, 'load', function() {
	if ($('upload_form'))
	{
		var text_value = $('products_used').value || '';
		var text_length = Math.max((text_value ? (products_list_max_len - text_value.length) : products_list_max_len), 0);
		
		$('products_list_chars_ctr').innerHTML = text_length.toString();
		
		Event.observe($('products_used'), 'keyup', function(){
			var text_value = $('products_used').value || '';
			var text_length = Math.max((products_list_max_len - text_value.length), 0);
			
			$('products_list_chars_ctr').innerHTML = text_length.toString();
		});
	}
	else if ($('email_form'))
	{
		var text_value = $('comment_text').value || '';
		var text_length = Math.max((text_value ? (products_list_max_len - text_value.length) : products_list_max_len), 0);
		
		$('comment_text_chars_ctr').innerHTML = text_length.toString();
		
		Event.observe($('comment_text'), 'keyup', function(){
			var text_value = $('comment_text').value || '';
			var text_length = Math.max((products_list_max_len - text_value.length), 0);
			
			$('comment_text_chars_ctr').innerHTML = text_length.toString();
		});
	}
	else
	{
		form_grid(start_page_no);
	}
});

/**
* AJAX request to update table contents
* @param string init_page - first page for displaying
*/
function form_grid(init_page)
{
	if (init_page == '')
	{
		init_page = '1';
	}
	
	var page_no = parseInt(init_page);
	
	if ($("page_no"))
	{
		page_no = $("page_no").value;
	}
	
	new Ajax.Request('ajax/gallery_page.php',{method:"post", parameters : $H({'page_no' : page_no}).toQueryString(), onRequest: displayBox(), onSuccess:render_grid});
}

/**
* actions to render grid
*/
function render_grid(transport)
{
	//alert(transport.responseText);

	var data = eval('(' + transport.responseText + ')');

	$('gallery_grid').update(data.layout);
	$('pagination').update(data.pagination);
	$('upload_link').update(data.upload_link);
	
	var grid_pos = $("pagination").cumulativeOffset();
	//var grid_pos = $("pagination").realOffset();
	var prog_dim = $("preload_progress").getDimensions();
	
	grid_pos[0] = grid_pos[0] + Math.ceil( ( parseInt($("pagination").getWidth()) - parseInt(prog_dim.width) ) / 2 );
	grid_pos[1] = grid_pos[1] + Math.ceil( ( parseInt($("pagination").getHeight()) - parseInt(prog_dim.height) ) / 2 );
	
	//alert("pagination left = "+grid_pos[0]+", pagination width = "+$("pagination").getWidth()+", progress width = "+prog_dim.width);
	
	$("preload_progress").style.left = grid_pos[0].toString() + "px";
	$("preload_progress").style.top  = grid_pos[1].toString() + "px";
	
	ip = new ImagePreloader(data.used_images, onPreload);

//	hideBox();
}

/**
* initiating new select with pagination value given
* @param string page_no - page to be open
*/
function set_page(page_no)
{
	if ($("page_no"))
	{
		$('page_no').value = page_no;
		form_grid();
	}
}

/*
* clear and hide popup panel
*/
function hide_pan()
{
	$("popup_panel").style.left = '0px';
	$("popup_panel").style.top = '0px';
	$("popup_panel").style.visibility = "hidden";
} 

/*
* set up popup panel parameters and timer
* @param integer product - product identifier
* @param string image - path to gallery fullsize image
* @param string project - project name
* @param string architect - architect name
* @param string products_list - used products list
*/
function show_with_delay(product, image, project, architect, products_list)
{
	timerID = setTimeout("show_pan("+product+", '"+image+"', '"+project+"', '"+architect+"', '"+products_list+"');", 1000);
}

/*
* show popup panel itself
* @param integer product - product identifier
* @param string image - path to gallery fullsize image
* @param string project - project name
* @param string architect - architect name
* @param string products_list - used products list
*/
function show_pan(product, image, project, architect, products_list)
{
	var posReal = $('top_left_point').positionedOffset();
	
	$("email_link").href = $("email_link").href.slice(0,($("email_link").href.lastIndexOf('=') + 1)) + product.toString();
	$("pp_project_name").innerHTML = project;
	$("pp_architect_name").innerHTML = architect;
	$("pp_products_list").innerHTML = decodeURIComponent(products_list).replace(/\+/g, ' ');
	$("pp_image_name").src = image;

	$("pp_product_list_block").style.width = $("pp_image_name").width.toString()+'px';
	
	if (parseInt(document.body.clientHeight) < parseInt($("popup_panel").clientHeight))
	{
		$("popup_panel").style.top = "0px";
	}
	else
	{
		$("popup_panel").style.top = ((parseInt(document.body.clientHeight)-parseInt($("popup_panel").clientHeight))/2).toString()+"px";//posReal[1].toString()+'px';
	}
	
	$("popup_panel").style.left = ((parseInt(document.body.clientWidth)-parseInt($("popup_panel").clientWidth))/2).toString()+"px";//(posReal[0] - 1).toString()+'px';
	
	$("popup_panel").style.visibility = "visible";
}

/*
* suppressing popup panel appearance
*/
function supress_popup()
{
	clearTimeout(timerID);
}

/*
* check for maximal allowed number of characters entered in the field
*/
function check_max_count(e){
  e = e || window.event;
  var target = e.target || e.srcElement;
  var code = e.keyCode ? e.keyCode : (e.which ? e.which : e.charCode)

  switch (code){
    case 13:
    case 8:
    case 9:
    case 46:
    case 37:
    case 38:
    case 39:
    case 40:
    return true;
  }

  return target.value.length < products_list_max_len;
}

/*
* images preloader call-back function
*/
function onPreload(aImages, nImages)
{
	$('preload_progress').style.visibility = "hidden";
	$('pagination_panel').style.visibility = "visible";
	
	hideBox();
	
	return;
}

