var Calculator = {

	'_init_ok' : false,
	'use' : false,
	'submit' : false,
	'required' : [],
	'def' : {},
	'message' : {
		'alert' : 'Required fields empty!'
		},

	'Garage' : {},

	'idForm' : 'calc-form',
	'idPrefix' : 'calc-',
	'idPrefixRow' : 'row-',
	'idPrefixTxt' : 'txt-',
	'idPrefixSel' : 'sel-',

	'PriceTemplate' : '{price} $',

	'advanceCost' : {
		'20' : 8.9,
		'30' : 7.5,
		'40' : 6.3
		},

	'PriceMin' : 1000,
	'PriceMax' : 100000000,

	'PriceSliderMin' : 5000,
	'PriceSliderMax' : 1000000,
	'PriceSliderStep' : 5000,
	'PriceSlider' :  null

	};

	
Calculator.calc_contract = function ()
{
	var rc = true;

	var advance = Calculator.o('advance').getValue();
	var period = Calculator.o('period').getValue();
	var price = Calculator.o('price').getValue();

	var contract_sum = null;
	var advance_payment = null;
	var month_payment = null;

	if (typeof(Calculator.advanceCost[advance]) == 'undefined')
		advance= null;
	period = Calculator.check_num(period);
	price = Calculator.check_price(price);


	if (advance && period && price)
	{
		contract_sum = price * ( ( ( Calculator.advanceCost[advance] * period ) / ( 12 * 100 ) ) + 1 );
		month_payment = ( contract_sum - ( price * advance / 100 ) ) / period;
		advance_payment = price * advance / 100;

		contract_sum = Calculator.format_number(contract_sum, true);
		month_payment = Calculator.format_number(month_payment, true);
		advance_payment = Calculator.format_number(advance_payment, true);
	}

	Calculator.show_price('contract-sum', contract_sum);
	Calculator.show_price('month-sum', month_payment);
	Calculator.show_price('advance-sum', advance_payment);
}

Calculator.show_price = function (id, price)
{
	var o = Calculator.o(id);
	if (o) o.update(
		price ? Calculator.PriceTemplate.replace('{price}', price) : '--'
		);
}

Calculator.find_values = function (path, cond)
{
	var path_arr = path.split(':');

	var values, value, result;
	var i = 0, p;

	var garage = Calculator.Garage;

	while( p = path_arr.shift() )
	{

		if (garage && garage[p])
		{
			values = garage[p];
		}
		else
		{
			values = null;
			break;
		}

		value = Calculator.o(p) ? Calculator.o(p).getValue() : null;
		garage = (value && typeof(values['i'+value]) == 'object') ? values['i'+value] : null;

		i++;
	}

	if (values)
	{
		if (typeof(values) == 'object')
		{
			result = [];
			for( p in values )
			{
				if (/^g/.test(p)) i = -1;
				else i = p.replace(/^i/, '');
				if (typeof(values[p]) == 'object')
				{
					if (Calculator.check_cond(values[p]['_cond'], cond))
					{
						result.push( [ i, values[p]['_title'], values[p]['_level'],
							values[p]['_descr']
							] );
					}
				}
				else
				{
					result.push([ i, values[p] ]);
				}
			}
		}
		else
		{
			result = values;
		}
	}

	return result;
}

Calculator.check_cond = function (cond, values)
{
	if (cond)
	{
		for ( var c in cond )
		{
			var r = new RegExp('^(' + cond[c] + ')$');
			if ( ! (values[c] && r.test(values[c])))
				return false;
		}
	}
	return true;
}

Calculator.load_options = function (oid, opts_arr, safe_old)
{
	osel = Calculator.o(oid) || Calculator.s(oid);

	var old_value = '';
	for( var i = osel.length - 1; i >= 0; i-- )
	{
//		if ( ! ( i == 0 && osel.options[i].value == '' ))
//		{
			if (safe_old && osel.options[i].selected) old_value = osel.options[i].value;
			$(osel.options[i]).remove();
//		}
	}

	osel.descendants().each(function(q){ if (q.tagName == 'OPTGROUP') q.remove() });

	if ( ! Calculator._init_ok && Calculator.def[oid] && Calculator.def[oid] != '') old_value = Calculator.def[oid];

	if (opts_arr && opts_arr.length)
	{
		var sel_i = 0;
		if (old_value)
		{
			for( var i = 0, l = opts_arr.length; i < l ; i++ )
			{
				if (old_value == opts_arr[i][0])
				{
					sel_i = i;
					break;
				}
			}
		}

		var optg = null;
		var opt = null;
		for( var i = 0, l = opts_arr.length, tab; i < l ; i++ )
		{
			tab = '';
			if (opts_arr[i][2] && opts_arr[i][2] > 1)
			{
				for(var k = 1; k < opts_arr[i][2]; k++ )
				{
					tab += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				}
			}
			
			if (opts_arr[i][0] < 0)
			{
				optg = new Element('optgroup', { 'label' : opts_arr[i][1] });
				osel.insert(optg);
			}
			else
			{
				opt = new Element('option', { 'value' : opts_arr[i][0] != '0' ? opts_arr[i][0] : '',  'title' : (opts_arr[i][3] || '') , 'selected' : sel_i == i ? true : false  } ).update(tab + opts_arr[i][1]);
	
				if (optg && ! ( opts_arr[i][0] == '0' || opts_arr[i][0] == '')) optg.insert(opt);
				else osel.insert(opt);
			}
		}
		
		osel.disabled = false;
		return true;
	}

	osel.disabled = true;
	return false;
}

Calculator.change_txt = function (id)
{
	var osel = Calculator.o(id);
	var otxt = Calculator.t(id);
	if ( ! (otxt && osel)) return;

	if (otxt.value == '' || Calculator._init_ok)
	{
		var t = osel.selectedIndex < 0 ? '' : osel.options[osel.selectedIndex].innerHTML;
		otxt.value = (osel.getValue() == '') ? '' :
			t.replace(/^(\s|&nbsp;)+/, '').replace(/(&nbsp;)+/, ' ');
	}
}

Calculator.format_number = function (num, fix)
{
	if (fix) num = num.toFixed(2);
	num = num.toString();
	var x = num.split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

Calculator.showhide_txt = function ()
{
	var sel, txt, flag;
	$H({'category':'category', 'model':'model', 'variant':'model'}).each(function(p) {
		sel = Calculator.o(p.key);
		flag = Calculator.o(p.value).disabled ? true : false;
		txt = Calculator.t(p.key);
		if (txt)
		{
			if (flag) { sel.hide(); txt.show(); }
			else { txt.hide(); sel.show(); }
		}
		else sel.show();
		});
}

Calculator.change_category = function ()
{
	Calculator.load_options('category', Calculator.find_values('category'));
}

Calculator.change_model = function ()
{
	Calculator.load_options('model', Calculator.find_values('model', {
		'category':Calculator.o('category').getValue()
		}));
}

Calculator.change_variant = function ()
{
	Calculator.load_options('variant', Calculator.find_values('model:variant'));
}

Calculator.change_period = function ()
{
	var values = Calculator.find_values('model:variant:period');
	if ( ! (values && values.length))
		values = Calculator.find_values('model:period');
	if ( ! (values && values.length))
		values = [ ['24','24'], ['36','36'], ['48','48']  ];
	Calculator.load_options('period', values, true);
}

Calculator.change_price = function ()
{
	var price = Calculator.find_values('model:variant:price');

	var el = Calculator.o('price');

	if (price)
	{
		el.value = Calculator.format_number(price);
		el.readOnly = true;
		Calculator.PriceSlider.setDisabled();
		Calculator.o('priceslider').setOpacity(0.5);
	}
	else
	{
		el.readOnly = false;
		Calculator.PriceSlider.setEnabled();
		Calculator.o('priceslider').setOpacity(1);
	}
}

Calculator.check_price = function (price)
{
	price = Calculator.check_num(price.replace(/,/g, ''));
	if (price < Calculator.PriceMin || price > Calculator.PriceMax)
	{
		price = null;
	}
	return price;
}

Calculator.check_email = function (email)
{
	if (email == '' || /^\s*([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}\s*$/i.test(email))
	{
		return true;
	}
	return false;
}

Calculator.check_num = function (num)
{
	num = parseInt(num);
	if (isNaN(num) || num <= 0) return null;
	return num;
}

Calculator.change_centre = function ()
{
	var centre = Calculator.s('centre');

	if ( ! centre) return;

	var list = Calculator.find_values('centre', {
		'category':Calculator.o('category').getValue()
		});

	Calculator.load_options('centre', list);
	Calculator.on_change(centre);
}


Calculator.on_change = function (el)
{
	if (el.hasClassName('extra-select'))
	{
		var id = el.id.replace(/^(?:calc|txt|sel)\-/, '');
		var html = el.selectedIndex >= 0 ? el.options[el.selectedIndex].innerHTML : '';
		var tit = el.selectedIndex >= 0 ? el.options[el.selectedIndex].title : '';
		var val = el.selectedIndex >= 0 ? el.options[el.selectedIndex].value : '';
		//var other = $(el.options[el.selectedIndex]).hasClassName('other');
		var other = val == '';

		var q1 = $('extra-'+id);
		var q2 = $('txt-'+id);

		if (other)
		{
			if (q1) q1.hide();
			if (q2)
			{
				if ( ! q2.visible()) q2.value = '';
				q2.show();
			}
		}
		else
		{
			if (q2)
			{
				q2.hide();
				q2.value = q1 ? html : '';
			}

			if (q1)
			{
				if (tit)
					q1.update( el.options[el.selectedIndex].title ).show();
				else
					q1.hide();
			}
		}
	}
}

Calculator.Onchange = function (ev)
{
	var el, id;

	if (ev)
	{
		el = ev.element()
		id = el.id;
	}

	if (el)
	{
		Calculator.on_change(el);
	}


	if (Calculator.use)
	{
		if ( ! el) id = 'all';
		else id = el.id.replace(/^calc\-/, '');

		switch ( id )
		{
			case 'price':
				var price = Calculator.check_price(el.value);
				if (price)
				{
					if (ev.type == 'change') el.value = Calculator.format_number(price);
					Calculator.PriceSlider.setValue(price);
				}
				break;
			case 'all':
				Calculator.change_category();
			case 'category':
				Calculator.change_centre();
				Calculator.change_model();
				Calculator.change_txt('category');
			case 'model':
				Calculator.change_variant();
				Calculator.change_txt('model');
			case 'variant':
				Calculator.change_period();
				Calculator.change_txt('variant');
			case '_default_':
				Calculator.change_price();
				break;
		}

		Calculator.showhide_txt();
		Calculator.calc_contract();
	}

	Calculator.check_form();
}

Calculator.check_form = function ()
{
	var rc = true;
	var bad = {};
	var res, i, opts;

	$A(Calculator.required).each(function(req){
		if (typeof(req) != 'object') { opts = {}; opts[req] = ''; }
		else opts = req;
		$H(opts).keys().each(function(k){ if (typeof(bad[k]) == 'undefined') bad[k] = 0; });
		res = Calculator._check_opt(opts);
		if ( ! res[0])
		{
			rc = false;
			for( i = 1; i < res.length; i++ ) bad[res[i]] = 1;
		}
		});

	$H(bad).each(function(pair){
		if (i = Calculator.r(pair.key))
		{
			if (pair.value)
				i.addClassName('alert');
			else
				i.removeClassName('alert');
		}
		});

	return rc;
}

Calculator._check_opt = function (opts)
{
	var result = [ false ];

	var o, v, rc;

	$H(opts).each(function(pair){

		rc = false;
		if (
			((o = Calculator.o(pair.key)) && o.visible()) ||
			((o = Calculator.t(pair.key)) && o.visible()) ||
			((o = Calculator.s(pair.key)) && o.visible()) ||
			(o = null)
			)
		{
			if (o.disabled) rc = true;
			else
			{
				v = o.getValue();
				if (v) v = v.replace(/^\s+/, '').replace(/\s+$/, '');
	
				if (pair.value)
				{
					if (Calculator[pair.value])
						rc = Calculator[pair.value](v);
				}
				else
				{
					rc = !! (v);
				}
			}
		}
		if (rc)
		{
			result[0] = true;
		}
		else
		{
			result.push(pair.key);
		}
		});

	return result;
}



/*
Calculator.check_form = function ()
{
	var r1 = new RegExp('^('+Calculator.idPrefix + '|' + Calculator.idPrefixTxt + ')' );
	var r2 = new RegExp('\-price$');
	$(Calculator.idForm).getElements().each(function(el){
		if ((el.type == 'text' || el.options) && el.visible() && el.hasClassName('require'))
		{
			var v = el.getValue();
			var id = el.id.replace(r1, Calculator.idPrefixRow);
			if (el.disabled !== true && (! v || (r2.test(id) && ! Calculator.check_price(v))))
			{
				$(id).addClassName('alert');
			}
			else
				$(id).removeClassName('alert');
		}
		});
}
*/


Calculator.o = function (id)
{
	return $(Calculator.idPrefix + id);
}

Calculator.t = function (id)
{
	return $(Calculator.idPrefixTxt + id);
}

Calculator.r = function (id)
{
	return $(Calculator.idPrefixRow + id);
}

Calculator.s = function (id)
{
	return $(Calculator.idPrefixSel + id);
}

Calculator.init = function ()
{
	var oprice = Calculator.o('price');

	oprice.up().insert('<div id="' + Calculator.idPrefix + 'priceslider"><div id="' + Calculator.idPrefix + 'priceslider-handle"></div></div>');

	var sliderRange = [];
	for( var i = Calculator.PriceSliderMin; i <= Calculator.PriceSliderMax ; i += Calculator.PriceSliderStep )
	{
		sliderRange.push(i);
	}

	Calculator.PriceSlider = new Control.Slider(Calculator.o('priceslider-handle'), Calculator.o('priceslider'), {
		range: $R(Calculator.PriceSliderMin, Calculator.PriceSliderMax),
		values: sliderRange,
		onSlide : function(value) {
			Calculator.o('price').value = Calculator.format_number(parseInt(value));
			Calculator.calc_contract();
			},
		onChange : Calculator.check_form
		});

	var price = Calculator.check_price(oprice.value);
	if (price)
	{
		Calculator.PriceSlider.setValue(price);
	}

	$(Calculator.idForm).getElements().each(function(el){
		if (1)
		{
			Calculator.on_change(el);

			if (el.type == 'radio' || el.type == 'checkbox')
				el.observe('click', Calculator.Onchange );
			else if (el.options || el.type == 'text')
				el
					.observe('change', Calculator.Onchange )
					.observe('keyup', Calculator.Onchange )
					.observe('keydown', Calculator.Onchange );
		}
	});

	Calculator.Onchange();

	Calculator._init_ok = true;
}

Calculator.init_submit = function ()
{
	if ( ! Calculator.use)
	{
		$(Calculator.idForm).getElements().each(function(el){
			if (1)
			{
				Calculator.on_change(el);

				if (el.type == 'radio' || el.type == 'checkbox')
					el.observe('click', Calculator.Onchange );
				else if (el.options || el.type == 'text' || el.type == 'textarea')
					el
						.observe('change', Calculator.Onchange )
						.observe('keyup', Calculator.Onchange )
						.observe('keydown', Calculator.Onchange );
			}
		});
		Calculator.Onchange();
	}

	$(Calculator.idForm).observe('submit', function(ev){
		if ( ! Calculator.check_form())
		{
			ev.stop();
			alert(Calculator.message.alert);
		}
	});
}

function onloaded ()
{
	if (Calculator.use) Calculator.init();
	if (Calculator.submit) Calculator.init_submit();

	if (Mapper.use)
		Mapper.init();

	if ($('txt-captcha')) $('txt-captcha').setAttribute( "autocomplete", "off" );
}

function go_order (c, m, v, a)
{
	var f = $('order-form');
	if (f)
	{
		if (a) f.action = a;
		f['_category'].value = c;
		f['_model'].value = m;
		f['_variant'].value = v;
		f.submit();
		return false;
	}
	return true;
}

function open_win (name, url, w, h)
{
	var t=0, l=0;
	var mw = screen.availWidth;
	var mh = screen.availHeight;
	if ( ! w) w = 500;
	if ( ! h) h = 400;
	if (mw > 0 && mh > 0)
	{
		l = (mw - w) * .5;
		t = (mh - h) * .5;
	}
	var win = window.open(url ,name, "width="+w+",height="+h+"," +"left="+l+",top="+t+"," + "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,titlebar=no");
	if (win)
	{
		win.focus();
		if (win.opener==null) win.opener=window;
		return false;
	}
	return true;
}

var NewCaptcha = {

id: 'captcha-div',
t: null,

'refresh': function()
{
	if (NewCaptcha.t) return;

	$(NewCaptcha.id).setOpacity(0.5);
	NewCaptcha.t = setTimeout( 'NewCaptcha.stop()', 5000);

	new Ajax.Request('/new_captcha.php', {
		requestHeaders : {
			'If-Modified-Since' : 'Sat, 1 Jan 2000 00:00:00 GMT'
		},
		method: 'get',
		onSuccess: function(transport) {
			var t, e;
			if (t = transport.responseText.match(/^oke:(.+)$/))
			{
				if (e = $(NewCaptcha.id).down('img')) e.src = t[1];
			}
		},
		onComplete: NewCaptcha.stop
	});
},

'stop': function()
{
	if (NewCaptcha.t) clearTimeout(NewCaptcha.t);
	NewCaptcha.t = null;
	$(NewCaptcha.id).setOpacity(1.0);
}

};



function ScrollTo (id)
{
	Effect.ScrollTo(id, {
		duration:	1.0,
		offset:	0
		});
}


/* Mapper
---------------------- */

var Mapper = {};

Mapper.width = 400;
Mapper.height = 350;

Mapper.use = false;
Mapper.ready = false;

// only for leasingbc.ru !
Mapper.google_api_key = 'ABQIAAAAJAvBAjXsRnSNNriF4hZI6hSxCyMhprWD_uhPfV-kZ0djux66TxRFNMyalgOKDQCWT1jUKvJ7z-p7fw';

// run after DOM ready
Mapper.init = function ()
{
	// load google api
	var script = document.createElement('script');
	script.src = 'http://www.google.com/jsapi?key='+Mapper.google_api_key+'&callback=Mapper.loadedGoogleMaps';
	script.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(script);
}

Mapper.loadedGoogleMaps = function ()
{
	google.load("maps", "2", {
		"callback" : Mapper._loaded,
		"language" : "ru"});
}

Mapper._loaded = function ()
{
	Mapper.map_id = 'contact-map';
	Mapper.canvas_id = Mapper.map_id+'-canvas';
	Mapper.markers = {};
	
	var map = new Element('div', { 'id': Mapper.map_id, 'class':'contact-map', 'style': 'position:absolute; visibility:hidden; z-index:1000;' });
	Element.insert(document.body, {top:map});

	var canvas = new Element('div', { 'id': Mapper.canvas_id, 'class':'contact-map-canvas', 'style':'width: '+Mapper.width+'px; height: '+Mapper.height+'px;'}).update('<img src="/images/loading.gif" width="16" height="16" alt="загружаю" style="vertical-align: middle;" /> &nbsp; загрузка карты');

	map.insert('<div id="contact-map-header" style="position: relative;"></div>').insert(canvas);

	new Draggable(map, { handle: 'contact-map-header' });


	if ( ! google.maps.BrowserIsCompatible()) return;

	Mapper.gmap = new google.maps.Map2(canvas);

	//Создание значка для маркеров
	Mapper.icon = new GIcon();
	Mapper.icon.image = "/images/mm_bc_red.png";
	Mapper.icon.shadow = "/images/mm_bc_shadow.png";
	Mapper.icon.iconSize = new GSize(40, 50);
	Mapper.icon.shadowSize = new GSize(71, 50);
	Mapper.icon.iconAnchor = new GPoint(20, 50);
	Mapper.icon.infoWindowAnchor = new GPoint(20, 2);
//	Mapper.icon.infoShadowAnchor = new GPoint(20, 25);


	map.setStyle({
		'visibility':'visible',
		'display':'none'
		});

	Event.observe(document, 'click', Mapper.hideMap);
	Event.observe(window, 'unload', google.maps.Unload);

	Mapper.ready = true;

}


Mapper.showMap = function (link, id, points)
{
	if (Mapper.timeout)
	{
		clearTimeout(Mapper.timeout);
		Mapper.timeout = null;
		link = Mapper._link;
		id = Mapper._id;
		points = Mapper._points;
	}

	if ( ! Mapper.ready)
	{
		Mapper._link = link;
		Mapper._id = id;
		Mapper._points = points;
		Mapper.timeout = setTimeout(
			Mapper.showMap,
			100
			);
		return;
	}

	$(link).addClassName('contact-map-button');

	var contact = $('contact-'+id);
	var title = '', html = '';

	if (contact)
	{
		if (contact.down('.fn'))
		{
			title = contact.down('.fn').innerHTML;
			html += '<b>' + title + '</b><br />';
		}
		if (contact.down('.adr'))
		{
			html += '<br />' + contact.down('.adr').innerHTML;
		}
		if (contact.down('.tel'))
		{
			html += '<br />Тел.: ' + contact.down('.tel').innerHTML;
		}
	}

	var m = /([\d\.]+)\s*,\s*([\d\.]+)/.exec(points);

	if ( ! m) return;

	Mapper._show_map(link, points, title, html);
}

Mapper._show_map = function (link, points, title, html)
{
	var map = $(Mapper.map_id);

	$('contact-map-header').update(title);


	var lt = 0, tp = 0;
	var wh = Mapper.width, ht = Mapper.height;

	var pscroll = getPageScroll();
	var psize = getPageSize();

	var top_point = pscroll[1] + 10;
	var bottom_point = pscroll[1] + psize[3] - ht - 35;

	lt = parseInt(map.getStyle('left')); if (isNaN(lt)) lt = 0;
	tp = parseInt(map.getStyle('top')); if (isNaN(tp)) tp = 0;

	if ( ! Mapper._show)
	{
		lt = (psize[2] - wh) * 0.5;
		tp = (psize[3] - ht) * 0.5 + pscroll[1];
	}

	var move = Mapper._show ? false : true;

	if (tp > bottom_point) { tp = bottom_point; move = true; }
	if (tp < top_point) { tp = top_point; move = true; }

	if (move)
	{

//	var link_offset = $(link).cumulativeOffset();

//	lt = link_offset[0] + $(link).getWidth() + 20;
//	tp = link_offset[1] - (ht * 2 / 3);

		if (Mapper._show)
		{
			Effect.Fade(map, { duration: 0.15, to: 0.25, afterFinish: function(){
				Mapper._Appear(map, lt, tp);
				}});
		}
		else Mapper._Appear(map, lt, tp);
	}
	else Mapper._Appear(map);

	var m = /([\d\.]+)\s*,\s*([\d\.]+)/.exec(points);

	if ( ! m) return;

	var marker_point = new google.maps.LatLng(m[2], m[1]);

	Mapper.gmap.setCenter(marker_point, 15);
	Mapper.gmap.addControl(new google.maps.SmallZoomControl());
//	Mapper.gmap.addControl(new google.maps.MapTypeControl());
//	Mapper.gmap.enableScrollWheelZoom();

//	if (RGooogle.map_marker)
//		RGooogle.SetCenterMarker(RGooogle.map_marker);

	Mapper.createMarker(marker_point, title, html);

	Mapper._show = true;
}

Mapper._Appear = function (el, x, y)
{
	if (x && y) el.setStyle({ left: x+'px', top: y+'px' });
	Effect.Appear(el, {duration: 0.3 });
}

Mapper.createMarker = function (point, title, html)
{
	var hashkey = point.toUrlValue();

	if (Mapper.markers[hashkey])
	{
		Mapper.markers[hashkey].closeInfoWindow();
		return;
	}

	var marker = new GMarker(point, {'title':title, icon:Mapper.icon});
	
	if (html != '')
	{
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
	}

	Mapper.gmap.addOverlay(marker);
	
	Mapper.markers[hashkey] = marker;
}


Mapper.hideMap = function (ev)
{
	if ( ! Mapper.ready) return;

	var el = ev.element();

	var m, id;

	while(1)
	{
		if (el == document) break;
		if (el.hasClassName('contact-map') || el.hasClassName('contact-map-button')) return;
		el = el.up();
	}

	$(Mapper.map_id).fade({duration: 0.15});
	Mapper._show = false;
}




// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

document.observe('dom:loaded', onloaded);
