/*
  *  function scroll();
  *  content: Content id
  *	 position up/down: 1/0
  *	 action start/stop: 1/0
  */
var time = false;
var limit = 0;
var speed = 25;
function scrollDiv( content, position, action ) {
	if (action == 0) {
		if ( time ) {
			clearTimeout( time );
		}
	} else {
		var scroll_content = $( content );
		var top_margin = parseInt(scroll_content.style.marginTop);
		var step;
		if ( position == 0 ) {
			limit = bot_limit;
			step = -1;
		} else {
			limit = 0;
			step = 1;
		}
		if ( top_margin == limit ) {
			if ( time ) {
				clearTimeout( time );
			}
			return false;
		}

		scroll_content.style.marginTop = top_margin + step +'px';

		time = window.setTimeout("scrollDiv('"+ content +"', "+ position + ", " + action + ")", speed );
	}
}
function scrollHeight( outer, scroll ) {
	try {
		var bot_limit = $('outer').offsetHeight - $('scroll').offsetHeight;
		if ( bot_limit > 0) {
			bot_limit = 0;
		}
		return bot_limit;
	}
	catch ( e ) {

	}
}

/* showHideList */
var showHideListItem = false;
var showHideListTitle = false;
function showHideList ( title, content ) {
	var contentTitle = $( title );
	var contentBlock = $( content );

	/* from cookies */
	if ( contentBlock.style.display == 'block' ) {
		contentBlock.style.display = 'none';
		contentTitle.className = '';
		showHideListItem = false;
		showHideListTitle = false;

	} else {
		if ( ( showHideListTitle ) && ( showHideListItem ) ) {
			$( showHideListTitle ).className = '';
			$( showHideListItem ).style.display = 'none';
		}
		showHideListItem = content;
		showHideListTitle = title;
		/* cookies */
		setDataToCookie( 'showHideListItem', content );
		setDataToCookie( 'showHideListTitle', title );


		contentTitle.className = 'active';
		contentBlock.style.display = 'block';
	}
}
function updateList() {
	if (( !showHideListItem) && (!showHideListTitle)) {
		var activeItem = getDataFromCookie('showHideListItem');
		var activeTitle = getDataFromCookie('showHideListTitle');
		$( activeItem ).style.display = 'block';
		$( activeTitle ).className = 'active';
		showHideListItem = activeItem;
		showHideListTitle = activeTitle;
	}
}
/* showHideList */

function getDataFromCookie( cookieName ){
	labelName = cookieName;
	var labelLen = labelName.length;
	var cookieData = document.cookie;
	var cLen = cookieData.length;
	var i = 0;
	var cEnd;
	var selected_value = '';
	while (i < cLen) {
		var j = i + labelLen;
		if (cookieData.substring(i,j) == labelName) {
			cEnd = cookieData.indexOf(';',j);

		if (cEnd == -1) {
			cEnd = cookieData.length;
		}

		selected_value =  unescape(cookieData.substring(j+1, cEnd));
		}
		i++;
	}

	return selected_value;

}

function setDataToCookie( cookieName, value ){
	var exp = new Date();
	var oneYearFromNow = exp.getTime() + (365 * 24 * 60 * 60 * 1000);
	exp.setTime(oneYearFromNow);
	document.cookie = cookieName + "=" + value + "; expires=" + exp.toGMTString() + "; path=/;";
}

function fixScroll( ) {
	var oOuter = $('outer');
	var oScroll = $('scroll');

	if( oOuter.clientHeight > oScroll.clientHeight ) {
		//no scroll
		oScroll.className='scroll scroll_wf';
		oScroll.style.overflow='hidden';

	}
	oScroll.style.visibility='visible';
}


/**
 * Registration form dropdowns
 */
function changeBuildings( selected, id, empty_value ) {

	var element = $( id );
	var iterator = 1;

	element.options.length = 0;

	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}


	for ( var i in interiorBuildingsRel[selected] ) {

		if ( interiorBuildingsRel[selected][i] && typeof interiorBuildingsRel[selected][i] == 'string' ) {

			element.options[iterator] = new Option( interiorBuildingsRel[selected][i], i );
			iterator = iterator + 1;
		}
	}


	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}


	if ( !selected || selected == 0 ) {
		changeFloors( 0, 'floors_select', empty_value );
	}
}


function changeFloors( selected, id, empty_value ) {

	var element = $( id );
	var iterator = 1;

	element.options.length = 0;

	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}


	for ( var i in buildingsFloorsRel[selected] ) {

		if ( buildingsFloorsRel[selected][i] && typeof buildingsFloorsRel[selected][i] == 'string' ) {

			element.options[iterator] = new Option( buildingsFloorsRel[selected][i], i );
			iterator = iterator + 1;
		}
	}


	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}
}

function changeArea( selected, id, empty_value ) {

	var element = $( id );
	var iterator = 1;

	element.options.length = 0;

	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}


	for ( var i in roomQuantities[selected] ) {

		if ( roomQuantities[selected][i] && typeof roomQuantities[selected][i] == 'string' ) {

			element.options[iterator] = new Option( roomQuantities[selected][i], i );
			iterator = iterator + 1;
		}
	}


	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}
}

function changeInteriors( selected, id, empty_value ) {

	var element = $( id );
	var iterator = 1;

	element.options.length = 0;

	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}


	for ( var i in roomInteriorsRel[selected] ) {

		if ( roomInteriorsRel[selected][i] && typeof roomInteriorsRel[selected][i] == 'string' ) {

			element.options[iterator] = new Option( roomInteriorsRel[selected][i], i );
			iterator = iterator + 1;
		}
	}


	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}
}

function showHideTerrace() {

	var room_count = document.getElementById('reg_room_count').options[document.getElementById('reg_room_count').selectedIndex].value;
	var area = document.getElementById('reg_area').options[document.getElementById('reg_area').selectedIndex].value;
	var building = document.getElementById('buildings_select').options[document.getElementById('buildings_select').selectedIndex].value;
	var floor = document.getElementById('floors_select').options[document.getElementById('floors_select').selectedIndex].value;

	var params = '';

	if ( room_count != 0 ) {
		params += 'room_count=' + room_count;
	}

	if ( area != 0 ) {
		params += '&area=' + area;
	}

	if ( building != 0 ) {
		params += '&building=' + building;
	}

	if ( floor != 0 ) {
		params += '&floor=' + floor;
	}

	var ajaxRequest = new Ajax.Request(
		flat_search_url, {
			method: 'get',
			parameters: params,

			onSuccess: function (req) {
				if( req.responseText && req.responseText == 1 ) {
					$( 'terrace_block' ).style.display = 'block';
				} else {
					$( 'terrace_block' ).style.display = 'none';
					$( 'terrace_checkbox' ).checked = false;
				}
			}
		}
	);
}