var authButtonDisplayed = false;
var refreshTableCount = 0;
var typePrefix = '';
var authSuccess = false;

function serviceSelected( value, typePrefix )
{
	if( !typePrefix ) typePrefix = 'i-';

	resetForm( typePrefix );
	$( typePrefix + 'service-name' ).value = value;
	if ( value )
	{
		if ( 'Rss' == value )
		{
			$( 'mode1-dl' ).style.display = 'none';
			$( 'mode3-dl' ).style.display = 'none';
			$( 'mode2' ).checked = true;
			modeSelected( 2 );
		}
		$( 'import_modes' ).style.display = '';
		setAuthButton( 'link' );
	}
}

function resetForm( typePrefix )
{
	if( !typePrefix ) typePrefix = 'i-';

	$( typePrefix + 'task-form' ).reset();
	$( typePrefix + 'auth-block' ).style.display = 'none';
	$( typePrefix + 'settings' ).style.display = 'none';
	$( typePrefix + 'username' ).disabled = false;
	$( typePrefix + 'password' ).disabled = false;
	$( typePrefix + 'username' ).readonly = false;
	$( typePrefix + 'password' ).readonly = false;
	$( typePrefix + 'login-password-div' ).style.display = '';
	$( typePrefix + 'path-to-xmlrpc-div' ).style.display = 'none';
	if ( typePrefix == 'i-' )
	{
		$( 'rss-url-div' ).style.display = 'none';
		$( 'mode1-dl' ).style.display = '';
		$( 'mode3-dl' ).style.display = '';
		$( 'import_modes' ).style.display = 'none';
	}
	setAuthButton( typePrefix );
}

function modeSelected( value, typePrefix )
{
	if( !typePrefix ) typePrefix = 'i-';
	
	$( typePrefix + 'auth-block' ).style.display = value ? '' : 'none';

	if ( !authButtonDisplayed )
	{
		$( typePrefix + 'auth-btn' ).innerHTML = $( typePrefix + 'auth-btn-tmpl').innerHTML;
		authButtonDisplayed = true;
	}
	
	if ( 'WordPress' == $F( typePrefix + 'service-name') )
	{
		$( typePrefix + 'path-to-xmlrpc-div').style.display = '';
	}
	else if ( 'Rss' == $F( typePrefix + 'service-name') )
	{
		$( 'rss-url-div' ).style.display = '';
		$( typePrefix + 'login-password-div' ).style.display = 'none';
	}
	else if ( 'LiveInternet' == $F( typePrefix + 'service-name' ) )
	{
		$( typePrefix + 'with-tags-div' ).style.display = 'none';
	}
	else if ( 'WordPress' == $F( typePrefix + 'service-name' ) )
	{
		$( 'avatar-div' ).style.display = 'none';
		$( typePrefix + 'path-to-xmlrpc-div' ).style.display = '';
	}
	else
	{
		$( typePrefix + 'path-to-xmlrpc-div' ).style.display = 'none';
	}
}


function setAuthButton( mode, typePrefix )
{
	if( !typePrefix ) typePrefix = 'i-';
	
	if ( mode == 'indicator' )
	{
		$( typePrefix + 'auth-btn' ).innerHTML = '<img src="/img/loader.gif">';
		$( typePrefix + 'username' ).readonly = true;
		$( typePrefix + 'password' ).readonly = true;
		if ( $( 'rss_url' ) )
			$( 'rss_url' ).readonly = true;

	}
	else if ( mode == 'link' )
	{
		$( typePrefix + 'auth-btn' ).innerHTML = $( typePrefix + 'auth-btn-tmpl' ).innerHTML;
		$( typePrefix + 'username' ).readonly = false;
		$( typePrefix + 'password' ).readonly = false;
		if ( $( 'rss_url' ) )
			$( 'rss_url' ).readonly = false;
	}
	else if ( mode == 'failture' )
	{
		$( typePrefix + 'auth-btn' ).innerHTML = $( typePrefix + 'auth-btn-fail-tmpl' ).innerHTML;
		$( typePrefix + 'username' ).readonly = false;
		$( typePrefix + 'password' ).readonly = false;
		if ( $( 'rss_url' ) )
			$( 'rss_url' ).readonly = false;
	}
	else 
	{
		$( typePrefix + 'auth-btn' ).innerHTML = '';
	}
}


function toggleCheckboxInput( elm, disabled )
{
	elm.disabled = disabled;	
	if ( !disabled )
		elm.focus();
}

function isEnter( evt )
{
	var keyCode = evt == null ? event.keyCode : evt.which;
	return keyCode == 13;
}


function getFormParams( elm )
{
	var tags = new Array('input','select','textarea');
	var result = {};
	for( var i = 0; i<tags.length; i++ )
	{
		var elements = elm.getElementsByTagName( tags[i] );
		for( var j = 0; j < elements.length; j++ )
		{
			var elm = elements[j];
			if ( elm.name )
					result[elm.name] = 'checkbox' == elm.type ? elm.checked & 1 : elm.value;
		}
	}
	return result;
}


function checkAuth( mode, typePrefix )
{
	if( !typePrefix ) typePrefix = 'i-';
	
	var args = {};
	if ( 'Rss' == $F( typePrefix + 'service-name' ) )
	{
		args['rss_url'] = $F( 'rss_url' );
	}
	else
	{
		args['username'] = $F( typePrefix + 'username' );
		args['password'] = $F( typePrefix + 'password' );
	}

	if ( 'WordPress' == $F( typePrefix + 'service-name' ) )
		args['path_to_xmlrpc'] = $F( typePrefix + 'path_to_xmlrpc' );
	
	args['serviceName'] = $F( typePrefix + 'service-name' );
	args['mode'] = mode;
	args = _makeServiceAuth( args );
	setAuthButton( 'indicator', typePrefix );
	new Ajax.Request('/ajax/importexport/checkauth', {
			method:'post',
			parameters:args,
			onFailure: function(req){
					setAuthButton( 'failture' );
			},
			onSuccess: function(req) {

				var result = eval( '(' + req.responseText + ')' );
				if ( 'success' == result['status'] )
				{
					if ( 'function' == typeof( authOnSuccess ) )
					{
						authOnSuccess();
					}
					else
					{
						setAuthButton( '', typePrefix );
						$( typePrefix + 'settings' ).style.display = '';
						$( typePrefix + 'hid-username' ).value = $F(typePrefix + 'username');
						$( typePrefix + 'hid-password' ).value = $F(typePrefix + 'password');
					}
					$(typePrefix + 'username').disabled = true;
					$(typePrefix + 'password').disabled = true;
					authSuccess = true;
				}
				else if ( 'failure' == result['status'] )
				{
					if ( result['error_msg'] )
						alert( result['error_msg'] );
					setAuthButton( 'failture', typePrefix );
				}
			}
	});
}

function togglePlayPause( id, btnElm )
{
	new Ajax.Request('/ajax/importexport/toggleplaypause', {
			method:'post',
			parameters:{taskId:id},
			onSuccess: function(req) {
				var result = eval( '(' + req.responseText + ')' );
				if ( 'success' == result['status'] )
					btnElm.className = result['content'];
			}
	});
}


function clearFailtureMsg( typePrefix )
{
	if ( authSuccess )
		return;
	setAuthButton( 'link', typePrefix );
}


//** helper functions
function _makeServiceAuth( args )
{
//	if ( args['serviceName'] == 'LiveJournal' )
//	{
//		args['md5_password'] = MD5( args['password'] );
//		delete args['password'];
//	}
	return args;
}

var stns = {};
var itms = {};
function toggleSettings ( elmId, doDisable ) {
	var itmId = 'itm-' + elmId;
	if ( 'undefined' == typeof( stns[elmId] )  )
		if ( !(stns[elmId] = $(elmId) ) )
			return false;

	if ( 'undefined' == typeof( itms[itmId] )  )
		if ( !(itms[itmId] = $(itmId) ) )
			return false;

	for( var i in stns )
	{
		if ( stns[i].nodeType != 1 )
			continue;
			
		if ( stns[i].style.display != 'none' && stns[i].id != elmId )
			stns[i].style.display = 'none';
	}

	for( var i in itms )
	{
		if ( itms[i].nodeType != 1 )
			continue;
			
		if ( itms[i].className == 'box-container blog-item settings-active' && itms[i].id != itmId )
			itms[i].className = 'box-container blog-item';
	}

	if ( 'undefined' == typeof( doDisable ) || !doDisable )
	{
		stns[elmId].style.display = stns[elmId].style.display == 'none' ? 'block' : 'none';
		itms[itmId].className = 'box-container blog-item settings-active';
	}
	else
	{
		stns[elmId].style.display = 'none';
		itms[itmId].className = 'box-container blog-item';
	}
}

var tabs = {};
var btns = {};
function toggleTab( tabUid, tabDisable ) 
{
	if ( 'undefined' == typeof( tabDisable ) )
		var tabDisable = false;

	if ( 'undefined' == typeof( tabs[tabUid] )  )
		if ( !(tabs[tabUid] = $(tabUid + '-tab') ) )
			return false;

	if ( 'undefined' == typeof( btns[tabUid] )  )
		if ( !(btns[tabUid] = $(tabUid + '-btn')) )
			return false;

	if (/active/.test(btns[tabUid].className)) 
	{
		btns[tabUid].className = 'box-container tab-button';
		tabs[tabUid].style.display = 'none';
	} 
	else 
	{
		btns[tabUid].className = 'box-container tab-button active';
		for ( var t in tabs ) 
		{
			if ( tabs[t].nodeType != 1 ) // ELEMENT_NODE
				continue;

			if ( tabUid + '-tab' == tabs[t].id ) 
			{
				tabs[t].style.display = tabDisable ? 'none' : 'block';
			} else {
				btns[t].className = 'box-container tab-button';
				tabs[t].style.display = 'none';
			}
		}
	}
}
