
/* Wunschzettel Scripts */
/* ==================== */

function scrollToSheet() {
  var param = getRequestParameter("wish");
  
  if (param != "") { 
    element = "sheet_" + param
    document.getElementById(element).scrollTo();
  }
};

function getRequestParameter(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

Object.extend(Prototype.Browser, {
	IE6: Prototype.Browser.IE && !window.XMLHttpRequest,
	IE7: (navigator.userAgent.indexOf("MSIE 7") != -1),
	IE8: (navigator.userAgent.indexOf("MSIE 8") != -1),
	/** Konqueror & old Safari versions */
	KHTML: (navigator.userAgent.indexOf("KHTML") != -1)
});


Object.extend(Object, {
	isEvent: function(object) {
		return !!object.target;
	},
	isClass: function(object){
		return !!(
			object && object.prototype && object.prototype.constructor && 
			Object.isFunction(object.prototype.initialize)
		);
	}
});

var onDomLoaded = function(callback) {
	if (document.loaded) {
		callback.bind(document)();
	} else {
		document.observe("dom:loaded", callback);
	}
};

/* === The Snow === */



Event.observe(window, 'load', function() {

	var snow_container = $('snow');
	var snow_div = snow_container.firstDescendant();
	
  Event.observe(document, "scroll", function(event){
  	// how far gescrollt
  	//alert("soviel: "+window.pageXOffset );
  	//alert("soviel: "+document.body.scrollLeft  );
  	//var off_x = window.pageXOffset;
  	// um die hälte in die andere richtung
  	//snow_div.style.backgroundPosition = "-"+off_x + "px 0";
  	
  });
});







/* === AJAX Stuff === */

FormUpdater = function() {
	var div1 = 'form_container_1';
	var div2 = 'form_container_2';
	return Class.create({
		initialize: function(form_id, url) {
			this._form = $(form_id);
			this_url = url;
			this._old_div = $(div1);
			this._update_div = $(div2);
			if(this._update_div.visible()) {
				var tmp = this._old_div;
				this._old_div = this._update_div;
				this._update_div = tmp;
			}
			this._call();
		},
		_call: function() {
			new Ajax.Updater(this._update_div, this_url , {
			  parameters: Form.serialize(this._form, true),
			  evalScripts: true,
			  method: 'post',
			  onSuccess: this._morph.bind(this)
			});
		},
		_morph: function() {
			Effect.Fade(this._old_div, { duration: 1.0 });
			Effect.Appear(this._update_div, { duration: 1.0 });
		}
	});
}();







/* ================================= */


Form.Suggestions = function() {
	var REG_EXP = /[\r\n]/g;
	var HINT_COLOR = "#a6a6a6";
	
	return Class.create({
		initialize: function(form, suggestions) {
			this._form = $(form);
			this._suggestions = suggestions || {};		
			this._events = {
				focus: this._onFocus.bind(this),
				blur: this._onBlur.bind(this),
				set: this.setInputs.bind(this),
				reset: this.resetInputs.bind(this)
			};		
			this._observeForm();
			this._observeInputs();		
			this.setInputs();
		},
	
		_observeForm: function() {
			this._form.observe("reset", this._events.set);
			this._form.observe("submit", this._events.reset);
		},
		
		_observeInputs: function() {
			for (var name in this._suggestions) {
				var input = $(this._form[name]);
				input.observe("focus", this._events.focus);
				input.observe("blur", this._events.blur);
			}
		},
		
		setInputs: function() {
			for (var name in this._suggestions) {
				var input = $(this._form[name]);
				var suggestion = this._suggestions[input.name].replace(REG_EXP, "");
				var value = input.value.replace(REG_EXP, "");
				if (!value || value == suggestion) {
					input.setStyle({ color: HINT_COLOR });
					input.value = this._suggestions[name];
					this._suggestions[name] = input.value;
				} else {
					input.setStyle({ color: "" });
				}
			}
		},
		
		resetInputs: function() {
			for (var name in this._suggestions) {
				var input = $(this._form[name]);
				var suggestion = this._suggestions[input.name].replace(REG_EXP, "");
				var value = input.value.replace(REG_EXP, "");
				if (value == suggestion) {
					input.setStyle({ color: "" }).clear();
				}
			}
		},
		
		restoreHints: function(input) {
			if (!input.present()) {
				input.setStyle({ color: HINT_COLOR });
				input.value = this._suggestions[input.name];
				this._suggestions[input.name] = input.value;
			}
		},
		
		_onBlur: function(event) {
			this.restoreHints(Event.element(event));
		},
		
		_onFocus: function(event) {
			var input = Event.element(event);
			if (this._suggestions[input.name]) {
				var suggestion = this._suggestions[input.name].replace(REG_EXP, '');
				var value = input.value.replace(REG_EXP, '');
				if (value == suggestion) {
					input.setStyle({ color: "" }).clear();
				}
			}
		}
	});
}();

Form.Suggestion = Class.create({
	initialize: function(input, suggestion){
		suggestion = suggestion || input.title;
		if (!suggestion){ return; }
		var suggestions = {};
		suggestions[input.name] = suggestion;
		new Form.Suggestions(input.form || input.up("form"), suggestions);
	}
});


ElementRules = {
	
	_rules: $H(),	
	PSEUDOS: Object.keys(Selector.pseudos),	
	_selectorCache: $H(),
	
	initialize: function(){
		onDomLoaded(function() {
			this.update();
		}.bind(this));
	},
	

	update: function(container){
		this._rules.each(function(rule){
			this._assign(rule.key, rule.value, container);
		}.bind(this));
	},
	
	_splitSelector: function(selector){
		
		var parts, part, key, cache = this._selectorCache.get(selector);		
		if (cache){ 
			return cache;
		} else {
			parts = selector.split(":");
			key = parts.shift();
			
			while ((part = parts.first()) && this.PSEUDOS.include(part)) {
				key += ":" + part;
				parts.shift();
			}
			
			return this._selectorCache.set(selector, {
				key: key,
				event: parts.join(":") || null
			});
		}
	},
	
	_assign: function(selector, action, container){
		
		selector = this._splitSelector(selector);		
		var allElements; 
		if (container) {
		  allElements = $(container).select(selector.key);
		} else {
			allElements = $$(selector.key);
		}
		
		allElements.each(function(element){
			var actions = element.retrieve("behavior_actions", $A());
			if (!actions.include(action)){ 
				actions.push(action);
				if (Object.isClass(action)){
					new action(element);
				} else {
					if (!selector.event){
						action(element);
					} else {
						element.observe(selector.event, function(event){
							action(element, event);
						});
					}
				}
			}
		});
	},
	
	_addSingleRule: function(selector, action){
		this._rules.set(selector, action);
		if (document.loaded){
			this._assign(selector, action);
		}
	},
	
	add: function(hashOrSelector, action){
		if (Object.isString(hashOrSelector)){
			this._addSingleRule(hashOrSelector, action);
		} else {
			$H(hashOrSelector).each(function(pair){
				this._addSingleRule(pair.key, pair.value);
			}.bind(this));
		}
	}
};
ElementRules.initialize();

ElementRules.add({
	"input[type=text][title], textarea[title]": Form.Suggestion,	
	"textarea.focus-on-load, select.focus-on-load, input.focus-on-load": function(input){
		try { input.focus(); } catch(e) {}
	}
});


if (Prototype.Browser.IE6) {
	ElementRules.add({
		".ie6-hover": function(element) {
			new IE6Hover(element, "hover");
		}
	});
}


Effect.ScrollTo = Class.create();
Object.extend(Object.extend(Effect.ScrollTo.prototype, Effect.Base.prototype), {

  initialize: function(element) {
    this.element = $(element);
    this.start(arguments[1] || {});
  },
  
  setup: function() {
    Position.prepare();
    var offsets = Position.cumulativeOffset(this.element);
    if(this.options.offsetX) offsets[0] += this.options.offsetX;
    if(this.options.offsetY) offsets[1] += this.options.offsetY;

    var maxWidth = window.innerWidth ? 
      window.width - window.innerWidth :
      document.body.scrollWidth - 
        (document.documentElement.clientWidth ? 
          document.documentElement.clientWidth : document.body.clientWidth);
    this.scrollStartX = Position.deltaX;
    this.deltaX = (offsets[0] > maxWidth ? maxWidth : offsets[0]) - this.scrollStartX;

    var maxHeight = window.innerHeight ? 
      window.height - window.innerHeight :
      document.body.scrollHeight - 
        (document.documentElement.clientHeight ? 
          document.documentElement.clientHeight : document.body.clientHeight);
    this.scrollStartY = Position.deltaY;
    this.deltaY = (offsets[1] > maxHeight ? maxHeight : offsets[1]) - this.scrollStartY;
  },
  
  update: function(position) {
    Position.prepare();
    window.scrollTo(
      this.scrollStartX + (position * this.deltaX),
      this.scrollStartY + (position * this.deltaY)
    );
  }
});

