function RM_Scheme( regionPrefix, schemeId )
{
	this.loader = 'loader';
	
	this.regionPrefix = regionPrefix;
	this.schemeId = schemeId;
	this.disabled = false;

	this.departureStation = {};
	this.arrivalStation = {};
	
	this.installedClasses = {};
	
	this.previewDepartureStation = null;
	
	var obSelf = this;
	$(document).ready(function(){
		obSelf.getSurvey();
	});
};


RM_Scheme.prototype.init = function( options )
{
	for ( var i in options )
		this[ i ] = options[ i ];
	
	return this;
}


RM_Scheme.prototype.onMouseOver = function( ob )
{
	if ( this.disabled )
		return;
	
	if ( this.installedClasses[ $(ob).attr('id') ] == undefined )
	{
		this.installedClasses[ $(ob).attr('id') ] = {};
		this.installedClasses[ $(ob).attr('id') ].common = $(ob).attr( 'class' );
	}
	
	$(ob).find('p').addClass( 'hover' );
};


RM_Scheme.prototype.onMouseOut = function( ob )
{
	if ( this.disabled )
		return;
	
	$(ob).find('p').removeClass( 'hover' );
};


RM_Scheme.prototype.onClick = function( ob, stationId, stationName )
{
	if ( this.disabled )
		return;
	
	if ( this.departureStation.id != undefined && this.departureStation.id == stationId )
	{
		this.restoreClasses( this.departureStation.ob );
		
		$('#searchDepartureStationName').val( this.previewDepartureStation.name );
		$('#searchDepartureStationId').val( this.previewDepartureStation.id );
		RM_Form.fieldHint( document.getElementById( 'searchDepartureStationName' ), 'focus' );
		RM_Form.fieldStateIcon( $('#searchDepartureStationId') );
		
		if ( $('#searchDepartureStationName').val() == '' || $('#searchDepartureStationName').val() == $('#searchDepartureStationName').attr( 'title' ) )
		{
			$('#searchDepartureStationName').val( $('#searchDepartureStationName').attr('title') );
			$('#searchDepartureStationName').css( 'color', '#999' );
		}
		
		this.departureStation = {};
		
		return;
	}
	
	
	if ( this.departureStation.id == undefined )
	{
		this.departureStation = {
			'ob'	: ob,
			'id'	: stationId
		};
		
		this.previewDepartureStation = {
			'id'	: $('#searchDepartureStationId').val(),
			'name'	: $('#searchDepartureStationName').val()
		};
		
		$('#searchDepartureStationName').val( stationName );
		$('#searchDepartureStationId').val( stationId );
		RM_Form.fieldHint( document.getElementById( 'searchDepartureStationName' ), 'focus' );
		RM_Form.fieldStateIcon( $('#searchDepartureStationId') );
	}
	else
	{
		this.arrivalStation = {
			'ob'	: ob,
			'id'	: stationId
		};
		
		$('#searchArrivalStationName').val( stationName );
		$('#searchArrivalStationId').val( stationId );
		RM_Form.fieldHint( document.getElementById( 'searchArrivalStationName' ), 'focus' );
		RM_Form.fieldStateIcon( $('#searchArrivalStationId') );
	}
	
	
	$(ob).addClass( 'flag' );
	
	
	if ( this.departureStation.id != undefined && this.arrivalStation.id != undefined )
		this.refreshSchemeTable();
};


RM_Scheme.prototype.restoreClasses = function( ob )
{
	$(ob).attr( 'class', this.installedClasses[ $(ob).attr('id') ].common );
};


RM_Scheme.prototype.refreshSchemeTable = function()
{
	regionPrefix = this.regionPrefix.substr( 0, 1 ).toUpperCase() + this.regionPrefix.substr( 1 );
	
	var getData = '&schemeId=' + this.schemeId;
	getData += '&departureStationId=' + this.departureStation.id;
	getData += '&arrivalStationId=' + this.arrivalStation.id;
	
	obPopup.show( this.loader );
	
	var obSelf = this;
	$.ajax({
		type: 'post',
		url: url.get('ajax') + '?Action=getEtrain' + regionPrefix + 'SchemeTableTemplate&rand=' + Math.random() + getData,
		dataType: 'json',
		success: function( data, textStatus )
		{
			obPopup.hide( obSelf.loader );
		
			$('#scheme_table').html( data.tableHtml );
		
			obSelf.departureStation = {};
			obSelf.arrivalStation = {};			
			obSelf.installedClasses = {};
		},
		error: function( data, textError )
		{
			obMessage.add( 'error', lang.get('error.critical') ).show();
		}
	});
};


RM_Scheme.prototype.disable = function()
{
	this.disabled = true;
};


/**
 * Заполнить поисковую форму значениями из куков
 */
RM_Scheme.prototype.setCookies = function ()
{
	var typeList = ['Departure', 'Arrival'];
	for (i in typeList)
	{
		if (!$('#search'+typeList[i]+'StationId').val())
		{
			if (cookieData = this.getLastCookie(typeList[i]))
			{
				$('#search'+typeList[i]+'StationName').rmeasysuggest().pasteValue(cookieData.stationName, cookieData.stationNumber);
				$('#search'+typeList[i]+'StationSelectBox').val(cookieData.stationNumber);
			}
		}
	}
	if (cookieDate = getCookieValue('etrainSearchDate'))
		$('#searchDate').val(cookieDate);
};


/**
 * Получить самую свежую куку
 */
RM_Scheme.prototype.getLastCookie = function (stationType)
{
	if (getCookieValue('etrainSearch'+stationType+'StationName'))
	{
		var stationNameList = jQuery.parseJSON(getCookieValue('etrainSearch'+stationType+'StationName'));
		var stationNumberList = jQuery.parseJSON(getCookieValue('etrainSearch'+stationType+'StationNumber'));
		if (stationNameList && stationNumberList)
		{
			if (stationNameList[this.regionPrefix] && stationNumberList[this.regionPrefix])
			{
				return {
					stationName		: stationNameList[this.regionPrefix].value,
					stationNumber	: stationNumberList[this.regionPrefix].value
				};
			}
		}
	}
};


/**
 * Вставить опросник
 */
RM_Scheme.prototype.getSurvey = function ()
{
	var obSelf = this;
	var data = {Action : 'getActiveSurvey'};
	data.rand = Math.random();
	var obSelf = this;
	$.ajax({
		url: url.get('ajax', null, data),
		success: function (data, textStatus)
		{
			$('.etrainSurvey').html(data);
		},
		error: function (data, textError)
		{
			new Message().add('error', lang.get('error.critical')).show();
		}
	});
};
