// JavaScript Document

function loadPolls()
	{
	var i,l;
	l = '/getpolls.html?allpolls_search[id]=^0';
	for (i=0; i<poll.length;i++) 
		l += ('|' + poll[i]);
	l += '$&allpolls_search_word_method[id]=REGEXP';
	new Ajax.Request(l,
  		{
		method:'get',
    	onSuccess: function(response)
			{
			apolls = response.responseText;
			apolls = apolls.split('\n');
			for (i=0; i<apolls.length-1; i++)
				redraw_poll(apolls[i]);
			},
		onFailure: function(response)
			{
			alert('server access error.');
			}
  		});
	}

function submitPoll(id)
	{
	if (document.loading) return false;
	document.loading = true;
	$('poll_submit_' + id).disabled=true;
	$('poll_form_' + id).action = '/submitpoll.html';
	$('poll_form_' + id).request(
  		{
		method:'get',
    	onSuccess: function(response)
			{
			redraw_poll(response.responseText);
			document.loading = false;
			},
		onFailure: function(response)
			{
			alert('server access error.');
			$('poll_submit_' + id).disabled=false;
			document.loading = false;
			}
  		});
	}
	
function redraw_poll(rt)
	{
	var i,id;
	var ar = rt.split(' ');
	id = ar.shift();
	$('poll_total_' + id).innerHTML = ar.shift();
	en=ar.shift();
	dis=(en=='1')?false:true;
	$('poll_submit_' + id).disabled=dis;
	for (i=0; i<ar.length; i++)
		{
		var ans = ar[i].split(':');
		$('poll_control_' + id + '_' + i).disabled = dis;
		$('poll_count_' + id + '_' + i).innerHTML = ans[0];
		$('poll_percents_' + id + '_' + i).innerHTML = ans[1];
		$('poll_image_' + id + '_' + i).style.width = '' + ans[2]*100 + 'pt';
		}
	}
	
function loadGallery(index)
	{
	document.loading = false;
	if (index >= gallery.length) return;
	document.loading = true;
	$('gallery_saved_' + gallery[index]).innerHTML = $('gallery_container_' + gallery[index]).innerHTML;
	$('gallery_container_' + gallery[index]).innerHTML = $('gallery_wait_' + gallery[index]).innerHTML;
	new Ajax.Request('/getgallerypage.html?gallery_id='+ gallery[index] +'&item'+gallery[index]+'_page=1',
  		{
		method:'get',
    	onSuccess: function(response)
			{
			show_page(gallery[index],response);
			loadGallery(index+1);
			},
		onFailure: function(response)
			{
			failed_page(gallery[index]);
			loadGallery(index+1);
			}
  		});
	}

function gallery_page(id,page)
	{
	if (document.loading) return false;
	document.loading = true;
	$('gallery_saved_' + id).innerHTML = $('gallery_container_' + id).innerHTML;
	$('gallery_container_' + id).innerHTML = $('gallery_wait_' + id).innerHTML;

	new Ajax.Request('/getgallerypage.html?gallery_id='+ id +'&item'+id+'_page=' + page,
  		{
		method:'get',
    	onSuccess: function(response)
			{
			show_page(id,response);
			document.loading = false;
			},
		onFailure: function(response)
			{
			failed_page(id);
			document.loading = false;
			}
  		});
	}

function show_pic(ltitle,rtitle)
	{
	tmp=window.open('/_std/bigpic.html?ltitle='+ ltitle + '&rtitle=' + rtitle,'_blank','height=300,width=350,left=100,top=150,scrollbars=auto');
	tmp.focus(); 
	return false;
	}

function show_page(id,response)
	{
	$('gallery_container_' + id).innerHTML=response.responseText;
	}
	
function failed_page()
	{
	alert('server access error.');
	$('gallery_container_' + id).innerHTML = $('gallery_saved_' + id).innerHTML;
	}
	
