/*
 *	JavaScript for prdfwc public forms
 *	Copyright(C) 2007-2008 Pantacle, All Rights Reserved.
 *	Copyright(C) 2007-2008 Kawanagare workshop, All Rights Reserved.
 */

var webroot = "/";
var categories_menu_acc = null;

Event.observe( document, 'dom:loaded', function(){
	$A( document.getElementsByTagName('form') ).each( function(elem){
		Event.observe( elem, 'submit', prdfwc_judge_form_submited, false );
	});

	var focus_elem = null;
	var edit_form = $('PrdfwcEditForm');
	if ( edit_form )
	{
		focus_elem = Form.getElements(edit_form).find( function(elem){
										return ( ( elem.name != '' ) &&
												 ( elem.type != 'hidden' ) &&
												 ( elem.type != 'button' ) &&
												 ( elem.type != 'submit' ) &&
												 ( Element.visible(elem) ) &&
												 ( elem.type != 'reset'  ) );
									});
	}

	if ( focus_elem )
	{
		Field.focus( focus_elem );
	}

	categories_menu_acc = new accordion( 'categories_menu', { resizeSpeed: 10 } );

});


var g_prdfwc_is_form_submit = false;

function prdfwc_judge_form_submited()
{
	if ( g_prdfwc_is_form_submit )
	{
		return( false );
	}
	g_prdfwc_is_form_submit = true;
	return( true );
}

function prdfwc_image_adjust( aElem, aMaxWidth, aMaxHeight )
{
	var img = new Image();
	img.src = aElem.src;

	if ( ( aMaxWidth ) && ( img.width != aMaxWidth ) )
	{
		var rate   = img.width / aMaxWidth;
		img.width  = aMaxWidth;
		img.height = Math.floor( img.height / rate );
	}

	if ( ( aMaxHeight ) && ( img.height > aMaxHeight ) )
	{
		var rate = img.height / aMaxHeight;
		img.height = aMaxHeight;
		img.width = Math.floor( img.width / rate );
	}

	if ( aMaxWidth )
	{
		aElem.width = img.width;
	}

	if ( aMaxHeight )
	{
		aElem.height = img.height;
	}
}