var aRentTariffs = new Array();

var RentTariff = function( options ) {
	if( !options ) return false;
	this.officeName = options.officeName;
	this.officeId = options.officeId;
	this.sizeName = options.sizeName;
	this.sizeId = options.sizeId;
	this.perday = options.perday;
	this.tariffs = options.tariffs;
	aRentTariffs.push( this );
}
RentTariff.prototype.getEl = function( id ) {											/* Get HTML Element */
	if( !id ) return false;
	if( typeof( id ) == 'string' ) {
		var obj = document.getElementById( id );
		if( obj && typeof( obj ) == 'object' ) return obj;
	} else if( id.nodeType ) {
		return id;
	}
	return false;
}
RentTariff.prototype.cNode = function( options ) { 									/* Node Create */
	if( !options || !options.node ) return false;
	var node = document.createElement( options.node );
	if( options.html ) node.innerHTML = options.html;
	if( options.id ) node.id = options.id;
	if( options.disabled ) node.disabled = options.disabled;
	if( options.name ) node.name = options.name;
	if( options.type ) node.type = options.type;
	if( options.src ) node.src = options.src;
	if( options.alt ) node.alt = options.alt;
	if( options.title ) node.title = options.title;
	if( options.value ) node.value = options.value;
	if( options.cl ) node.className = options.cl;
	if( options.st ) node.setAttribute( 'style', options.st );
	if( options.beacon && options.beacon.appendChild ) {
		options.beacon.appendChild( node );
	}
	return node;
}

RentTariff.prototype.calculate = function( params ) {
	var result = new Object();
	if( params.period >= 30 ) {
		var period = parseInt( params.period / 30 ) * 30;
		var addsum = ( params.period - period ) * this.perday;
		var sum = this.tariffs[ 'd' + period ];
		result.sum = sum + addsum;
	} else {
		result.sum = params.period * this.perday;
	}
	return result;
}

RentTariff.prototype.initHTML = function( params ) {
	var result = this.calculate( params );	
	if( !result || !result.sum || isNaN( result.sum ) ) return false;
	var div = this.cNode({ node: 'div', cl: 'contribute', beacon: this.getEl('output') });

	var p = this.cNode({ node: 'p', cl: 'name', html: 'Офис <a href="#">"' + this.officeName + '"</a>', beacon: div });
	var table = this.cNode({ node: 'table', cl: 'cont_params', beacon: div });
	var tbody = this.cNode({ node: 'tbody', beacon: table });

	function createRow( t1, t2 ) {
		var tr = this.cNode({ node: 'tr', cl: 'cont_params', beacon: tbody });
		this.cNode({ node: 'td', cl: 'first', html: t1, beacon: tr });
		this.cNode({ node: 'td', html: t2, beacon: tr });
	}

	var daystr = params.period.toString();

	if( daystr.substring( daystr.length - 2, daystr.length - 1 ) != "1" ) {
		switch( parseInt( daystr.substring( daystr.length - 1, daystr.length ) ) ) {
			case 1 : { daystr = ' день'; break; }
			case 2 :
			case 3 :
			case 4 : { daystr = ' дня'; break; }
			default : daystr = ' дней'
		}
	} else {
		daystr = ' дней';
	}

	createRow.call( this, 'Размер ячейки: ', this.sizeName );
	createRow.call( this, 'Срок аренды: ', params.period + daystr );
	createRow.call( this, 'Стоимость (руб.): ', result.sum );
}


$(function() {
	$('#office').change(
		function() {
			var cO = $(this).val();
			var sizes = $('#safe_size');
			sizes.html('');
			for( var c = 0; c < aRentTariffs.length; c++ ) {
				if( aRentTariffs[c].officeId == cO ) {
					sizes.append('<option value="' + aRentTariffs[c].sizeId + '">' + aRentTariffs[c].sizeName + '</option>');
				}
			}
		}
	);

	$('#calculator').submit(
		function( ev ) {
			ev.preventDefault ? ev.preventDefault() : window.event.ReturnValue = false;
			$('#output').html('');
			
			var period = parseInt( $('#calculator #rent_period').val() );
			if( !period || isNaN( period ) ) return false;
			var office = $('#calculator #office').val();
			var size = $('#calculator #safe_size').val();
			
			var params = new Object();
			params.office = office;
			params.size = size;
			params.period = period;

			var notfound = true;

			if( period > 0 ) {
				for( var c = 0; c < aRentTariffs.length; c++ ) {
					var cI = aRentTariffs[c];
					if( cI.officeId == office && cI.sizeId == size  ) {
						notfound = false;
						cI.initHTML( params ); 
					}
				}
			}
		
			if( notfound ) {
				$('#output').html('<h2>Ничего не найдено.</h2>');
			}
		}
	);
});
