
function MapCalendar(controller, zone, year, month, day, cat, display) {
	var parameters = {};
	parameters['display'] = display 
	var input = $('calendar-input');
	
	var update = function(key, value, ajax) {
		if (key == 'date')
			update.date(value);
		else
			parameters[key] = value;
		
		if (ajax !== false) {
			var replace = $('ajax-replace');
			while (replace.firstChild)
				replace.removeChild(replace.firstChild);
			var h3 = document.createElement('h3');
			var span = document.createElement('span');
      span.className = 'line';
			span.appendChild(document.createTextNode('Loading'));
			h3.appendChild(span);
			replace.appendChild(h3);
			var img = document.createElement('img');
			img.setAttribute('src', '/images/imgmap/loader_gray.gif');
			img.setAttribute('alt', 'loading');
      img.className = 'loading';
			replace.appendChild(img);
			
			new Ajax.Updater('ajax-replace', controller, { method: 'get', parameters: update.makeUrl(parameters) });
      
		}
	}
	
	update.date = function(value) {
		var date = value.split("/");
		parameters['date[year]']  = date[0];
		parameters['date[month]'] = date[1];
		parameters['date[day]']   = date[2];
	}
	
	update.makeUrl = function() {
		var url = '';
		for (var key in parameters)
			url += key+'='+parameters[key]+'&';
		return url
	}
	
	ImageMap(update);
	ImageMap.zones[ImageMap.zones[zone] ? zone : 'all'].activate(null, false);
	
  if($('calendar')){
  	var cal = new Epoch('epoch_basic','flat',$('calendar'));
  	cal.setSelectRange(14);
  	cal.selectDates([new Date(year, month-1, day)], true, true, true);
  	cal.goToMonth(year, month-1);
  	cal.setUpdateCall(update);
  	update.date(cal.dates[cal.dates.length-1].dateFormat());
	}
	var category = $('category-select');
	if (category){
    Event.observe(category, 'change', function() {
  		update('category_id', category.options[category.selectedIndex].value);
  	});
  }
 

  display_id = 'display-link-clubs';
  var display_link = $(display_id);
  if (display_link){
    Event.observe(display_link, 'click', function() {
  		update('display', 'clubs' );
    });
  }
  
  display_id = 'display-link-festivals';
  var display_link = $(display_id);
  if (display_link){
    Event.observe(display_link, 'click', function() {
  		update('display', 'festivals' );
    });
  }
  
  display_id = 'display-link-all';
  var display_link = $(display_id);
  if (display_link){
    Event.observe(display_link, 'click', function() {
  		update('display', 'all' );
    });
  }

}


function ImageMap(funct) {
	ImageMap.zones = {};
	ImageMap.simpleZones = ['zh-sh', 'be', 'bs-bl-so-ag', 'sc', 'se', 'vd-ge', 'ne-fr-ju', 'vs', 'gr', 'it'];
	ImageMap.multiZones = { all: ImageMap.simpleZones, chf: ['vd-ge', 'ne-fr-ju', 'vs'], chd: ['zh-sh', 'be', 'bs-bl-so-ag', 'sc', 'se'], abroad: [] };
	ImageMap.txtDefault = $('imgmap-txt-default');
	ImageMap.txtActive = ImageMap.txtDefault;
	ImageMap.txtDiv = $('imgmap-txt');
	ImageMap.imgDiv = $('imgmap-img');
	ImageMap.mapDiv = $('imgmap-map');
	ImageMap.funct = funct;
	
	// Zone
	function Zone(zone) {
		if (zone) {
			this.zone = zone;
			this.active = false;
			this.anchor = $('imgmap-map-'+zone);
			this.img = $(document.createElement('img'));
			this.img.id = 'imgmap-img-zone-'+this.zone;
      this.img.className = 'imgmap-img-zone';
			this.img.setAttribute('src', '/images/imgmap/'+this.zone+'.gif');
			this.img.setAttribute('alt', this.zone);
			this.img.hide();
			ImageMap.imgDiv.appendChild(this.img);
			this.createTxt(this.anchor.alt);
			this.createObservers();
		}
	}
	
	Zone.belongsToActiveZone = function(zone) {
		for (var z in ImageMap.zones)
			if (ImageMap.zones[z].active &&
				((ImageMap.zones[z] instanceof Zone && ImageMap.zones[z].zone == zone) ||
				(ImageMap.zones[z] instanceof MultiZone && ImageMap.zones[z].contains(zone))))
					return true;
		return false;
	};
	
	Zone.prototype.activate = function(event, ajax) {
		this.anchor.blur();
		if (event)
			Event.stop(event);
		for (var z in ImageMap.zones)
			if (ImageMap.zones[z].active)
				ImageMap.zones[z].disactivate();
		this.show();
		this.active = true;
		ImageMap.txtActive = this.txt;
		ImageMap.funct('states', this.key ? this.key : this.zone, ajax);
	};
	
	Zone.prototype.disactivate = function(event) {
		if (event)
			Event.stop(event);
		this.active = false;
		ImageMap.txtActive = ImageMap.txtDefault;
		this.hide();
	};
	
	Zone.prototype.show = function(event) {
		if (!this.active) {
			this.img.show();
			ImageMap.txtActive.hide();
			this.txt.show();
		}
	};
	
	Zone.prototype.hide = function(event) {
		if (!this.active) {
			if (!Zone.belongsToActiveZone(this.zone))
				this.img.hide();
			this.txt.hide();
			ImageMap.txtActive.show();
		}
	};
	
	Zone.prototype.createTxt = function(txt) {
		this.txt = $(document.createElement('p'));
		this.txt.appendChild(document.createTextNode(txt));
		this.txt.hide();
		ImageMap.txtDiv.appendChild(this.txt);
	};
	
	Zone.prototype.createObservers = function() {
		Event.observe(this.anchor, 'mouseover', this.show.bindAsEventListener(this));
		Event.observe(this.anchor, 'mouseout', this.hide.bindAsEventListener(this));
		Event.observe(this.anchor, 'click', this.activate.bindAsEventListener(this));
	};
  
  
	// MultiZone
	MultiZone.prototype = new Zone();
	function MultiZone(zone, key) {
		if (zone && key) {
			this.key = key;
			this.zone = zone;
			this.active = false;
			this.anchor = $('imgmap-link-'+this.key);
			if (this.anchor){
        this.createTxt(this.anchor.title);
  			this.createObservers();
      }
		}
	}
	
	MultiZone.prototype.contains = function(zone) {
		for (var z = 0; z < this.zone.length; ++z)
			if (this.zone[z] == zone)
				return true;
		return false;
	};
	
	MultiZone.prototype.parent_activate = MultiZone.prototype.activate;
	MultiZone.prototype.activate = function(event, ajax) {
    this.anchor.className = 'active';
		this.parent_activate(event, ajax);
	};
	
	MultiZone.prototype.parent_disactivate = MultiZone.prototype.disactivate;
	MultiZone.prototype.disactivate = function(event) {
		this.anchor.removeClassName('active');
		this.parent_disactivate(event);
	};
	
	MultiZone.prototype.show = function(event) {
		if (!this.active) {
			for (var z = 0; z < this.zone.length; ++z)
				ImageMap.zones[this.zone[z]].img.show();
			ImageMap.txtActive.hide();
			this.txt.show();
		}
	};
	
	MultiZone.prototype.hide = function(event) {
		if (!this.active) {
			for (var z = 0; z < this.zone.length; ++z)
				if (!Zone.belongsToActiveZone(this.zone[z]))
					ImageMap.zones[this.zone[z]].img.hide();
			this.txt.hide();
			ImageMap.txtActive.show();
		}
	};
	
	// Initialization
	for (var z = 0; z < ImageMap.simpleZones.length; ++z)
		ImageMap.zones[ImageMap.simpleZones[z]] = new Zone(ImageMap.simpleZones[z]);
	for (var z in ImageMap.multiZones)
		ImageMap.zones[z] = new MultiZone(ImageMap.multiZones[z], z);
	ImageMap.imgDiv.removeChild($('imgmap-img-loader'));
}

