window.addEvent('domready', function() {
  var cal = Calendar.setup({
          onSelect: function(cal) { cal.hide() }
      });
  cal.manageFields("formDateFromBtn", "formDateFrom", "%d.%m.%Y");
  cal.manageFields("formDateToBtn", "formDateTo", "%d.%m.%Y");
  
  var currency;
  if(currency = Cookie.read('currency')) {
	  Page.changeCurrency(currency);
  } else {
	  Cookie.write('currency', 'cz', {'path': '/', 'duration': 720});
  }
});

/* ******************************************************************** */
PageClass = function() {
	this.maxWalkDistance = 4000;
	this.maps = new Array();
}

PageClass.prototype.changeCurrency = function(code) {
	var priceFrom = 0;
	var priceTo = 0;
	$$('#tips select[id^=formPriceFrom_]').each(function(sel) {
		if(sel.style.display == 'block') {
			priceFrom = sel.selectedIndex;
		}
	});
	$$('#tips select[id^=formPriceFrom_]').each(function(sel) {
		if(sel.id == 'formPriceFrom_' + code) {
			sel.style.display = 'block';
			sel.disabled = '';
			if(priceFrom) {
				sel.selectedIndex = priceFrom;
			}
		} else {
			sel.style.display = 'none';
			sel.disabled = 'disabled';
		}
	});

	$$('#tips select[id^=formPriceTo_]').each(function(sel) {
		if(sel.style.display == 'block') {
			priceTo = sel.selectedIndex;
		}
	});
	$$('#tips select[id^=formPriceTo_]').each(function(sel) {
		if(sel.id == 'formPriceTo_' + code) {
			sel.style.display = 'block';
			sel.disabled = '';
			if(priceTo) {
				sel.selectedIndex = priceTo;
			}
		} else {
			sel.style.display = 'none';
			sel.disabled = 'disabled';
		}
	});
	Cookie.write('currency', code, {'path': '/', 'duration': 720});
}


PageClass.prototype.changeCurrencyBlock = function(code) {
	this.changeCurrency(code);
	document.location = document.location;
}

PageClass.prototype.showMap = function(holder, x, y, z) {
	if(!z) z = 13;
	if(!x && !y) {
		return false;
	}		
	if (GBrowserIsCompatible()) {
		$(holder).style.display = 'block';
		var map = new GMap2($(holder));
		map.setCenter(new GLatLng(x, y), z);
        var point = new GLatLng(x, y);
        map.addOverlay(new GMarker(point));
        var customUI = map.getDefaultUI();
        map.setUI(customUI);
	}
}

PageClass.prototype.showDistance = function(holder, header, x, y, coords, name, distance) {
	if(!x && !y) {
		return false;
	}		
	if (GBrowserIsCompatible()) {
		var hash = Math.round(Math.random()*1000000);
		var liHolder = document.createElement('li');
		var wordHolder = document.createElement('span');
		var mapHolder = document.createElement('div');
        mapHolder.className = 'bigMap';
		mapHolder.id = 'distanceMap' + hash;
		$(holder).appendChild(liHolder);
		liHolder.appendChild(wordHolder)
		liHolder.appendChild(mapHolder);

		mapHolder.style.display = 'block';
		var map = new GMap2(mapHolder);
		mapHolder.style.display = 'none';
		this.maps[hash] = map;
        var customUI = map.getDefaultUI();
        map.setUI(customUI);
        

        directionsPanel = document.createElement('div');
        var mode  = G_TRAVEL_MODE_WALKING;
        directions = new GDirections(map, directionsPanel);
        directions.load("from: N"+x+", E"+y+" to: "+coords, {'travelMode':mode, 'getSteps':true});
        GEvent.addListener(directions, 'load', function() {
        	var d = this.getDistance();
        	var t = this.getDuration();
        	if(d.meters < distance) {
        		//wordHolder.innerHTML = name + ' пешком ' + d.html + ' / ' + t.html + ' (<a href="javascript:void(0);" onclick="Page.showHideMap(' + hash + ');" class="alterlink">маршрут на карте</a>)';
        		wordHolder.innerHTML = '<b>' + name + '</b>: ' + d.html + ' / ' + t.html + ' пешком (<a href="javascript:void(0);" onclick="Page.showHide(\'' + mapHolder.id + '\');" class="alterlink">маршрут на карте</a>)';
        		$(header).style.display = 'block';
        	} else {
        		liHolder.style.display = 'none';
        	}
        });
        GEvent.addListener(directions, 'error', function() {
        	liHolder.style.display = 'none';
        });
	}
}

PageClass.prototype.showHideMap = function(hash) {
	if($('distanceMap' + hash).style.display != 'block') {
		$('distanceMap' + hash).style.display = 'block';
		setTimeout('Page.maps['+hash+'].checkResize();', 1000);
	} else {
		$('distanceMap' + hash).style.display = 'none';
	}
}

PageClass.prototype.switchOrderForm = function(what) {
	$('orderDatesTr').style.display = (what == 'question' ? 'none' : '');
	$('orderGuestsTr').style.display = (what == 'question' ? 'none' : '');
	$('orderCommentTr').style.display = (what == 'question' ? 'none' : '');
	$('orderQuestionTr').style.display = (what == 'question' ? '' : 'none');

	$('orderDates').disabled = (what == 'question' ? 'disabled' : '');
	$('orderGuests').disabled = (what == 'question' ? 'disabled' : '');
	$('orderComment').disabled = (what == 'question' ? 'disabled' : '');
	$('orderQuestion').disabled = (what == 'question' ? '' : 'disabled');
}

PageClass.prototype.showHide = function(holder) {
	$(holder).style.display = ($(holder).style.display != 'block' ? 'block' : 'none'); 
}

var Page = new PageClass();