/**
* Javascript
* JS: Base
*
* @author i-fabrik GmbH
* @copyright 2008 i-fabrik GmbH
* @version $Id: core.js,v 1.1 2009-10-08 11:53:44 jan Exp $
*
*/


	var SITE = {
		start: function() {
			SITE.appendMenu();
			//SITE.appendQuickstart();
			//SITE.appendTooltips();
			SITE.appendHighslide();
			SITE.imageAutoMover($$('img.auto-mover'), '_on', '_off');
			//SITE.appendFormElements();
		},

		appendMenu: function() {
			// Main menu
			// ---------
			var nav_elements = $splat($$('#MainMenu ul>li'));

			nav_elements.each(function(el, i) {

				el.addEvents({
					'mouseover': function(ev) {
						this.addClass('iehover');
					},
					'mouseout': function(ev) {
						this.removeClass('iehover');
					},
					'focus': function(ev) {
						this.fireEvent('mouseover');
					},
					'blur': function(ev) {
						this.fireEvent('mouseout');
					}
				});
			});

		},

		appendQuickstart: function() {
			if($('sel_quicklink')) {
				$('sel_quicklink').addEvent('change', function() {
					var v = this.options[this.selectedIndex].value;
					if(['-1','0'].contains(v))
						this.selectedIndex = 0;
					else
						document.location.href = this.options[this.selectedIndex].value;
				});
			}
		},

		appendTooltips: function() {
			var tt_options = {
				'offsets': {
					'x': 14,
					'y': 6
				},
				'onShow': function(tip) {
					tip.setStyle('opacity', .9);
				},
				'onHide': function(tip) {
					tip.setStyle('opacity', .0);
				}
			};

			SITE.tooltipsEnabled = new Tips([], $merge(tt_options, {
				'className': 'tooltipsEnabled'
			}));

			SITE.tooltipsDisabled = new Tips([], $merge(tt_options, {
				'className': 'tooltipsDisabled'
			}));

			$$('.tooltip').filter(function(el) {
				return (el.get('title').clean() != '');
			}).each(function(el) {
				var ti = '', tx = '', tmp = el.get('title').split('::').clean();

				if(tmp.count()>1) {
					ti = tmp[0].clean();
					tx = tmp[1].clean();
				} else
					tx = tmp[0].clean();

				el.store('tip:title', ti);
				el.store('tip:text', tx);

				if(el.getProperty('disabled') || el.hasClass('disabled'))
					SITE.tooltipsDisabled.attach(el);
				else
					SITE.tooltipsEnabled.attach(el);
			});
		},

		appendFormElements: function() {
			$$('button, input, textarea, select').each(function(el) {
				var over_class, focus_class, el_tag = el.get('tag'), el_type = el.get('type');
				var flag_focus = false;

				if(['textarea', 'select'].contains(el_tag)) { // Check element tag
					if(el_tag == 'select')
						over_class = 'frm_hovered';
					else
						over_class = 'input_textarea_over';
				} else { // Check element type
					if(['text', 'password'].contains(el_type)) {
						flag_focus  = true;
						over_class  = 'frm_hovered';
						focus_class = 'frm_focused';
					} else if(['button', 'submit', 'reset'].contains(el_type)) {
						flag_focus  = true;
						over_class  = 'frm_hovered';
						focus_class = 'frm_focused';
					}
				}

				el.addEvents({
					'mouseenter': function(ev) {
						if(!this.focused)
							this.addClass(over_class);
					},
					'mouseleave': function(ev) {
						this.focused = false
						this.removeClass(over_class);
					}
				});

				if(flag_focus) {
					el.addEvents({
						'focus': function(ev) {
							if(!this.hasClass(over_class)) {
								this.addClass(focus_class);
								this.focused = true;
							}
						},
						'blur': function(ev) {
							this.removeClass(focus_class);
							this.focused = false;
						}
					});
				}
			});
		},

		appendHighslide: function() {
		    if($$('span.iBlock').length>0) {
				$$('span.iBlock').each(function(el) {
					el.addEvents({
						'mouseenter': function(ev) {
							if(!this.hasClass('iBlock_over'))
								this.addClass('iBlock_over');
						},
						'mouseleave': function(ev) {
							this.removeClass('iBlock_over');
						},
						'focus': function(ev) {
							if(!this.hasClass('iBlock_over'))
								this.addClass('iBlock_over');
						},
						'blur': function(ev) {
							this.removeClass('iBlock_over');
						}
					});
				});

			    hs.graphicsDir     = 'resources/themes/standard/highslide/';
			    hs.showCredits     = false;
			    hs.outlineType     = null;
				hs.fullExpandTitle = 'Erweitern auf volle Größe';
				//hs.restoreTitle    = 'Klick: Schließen, Drag/Drop: Verschieben, Pfeiltasten: Blättern';
				hs.restoreTitle    = '';
				hs.loadingText     = 'Lade Bild ...';
				hs.loadingTitle    = 'Laden abbrechen';
				hs.loadingOpacity  = 0.85;
				hs.focusTitle      = 'In den Vordergrund per Klick';
		    }
		},

		
		imageAutoMover: function(e, over, out) {
			if(e.length>0) {
				e.each(function(img) {
					var img = $(img);

					if($chk(img.src)) {
						if(img.src.indexOf(out)>0) {
							img.addEvent('mouseenter', function() {
								img.src = img.src.replace(out, over);
							}).addEvent('mouseleave', function() {
								img.src = img.src.replace(over, out);
							});
						}
					}
				});
			}
		},


		openWindow: function(url,wn,ft,ww,wh,wc){
			if(window.screen)
				if(wc){
					var wl = (screen.width-ww)/2;
					var wt = (screen.height-wh)/2;
					ft+=(ft!='')?',':'';
					ft+=',left='+wl+',top='+wt;
				}
			var w = window.open(url,wn,ft+((ft!='')?',':'')+'width='+ww+',height='+wh);
			w.focus();
		}
	};

	window.addEvent('domready', SITE.start);