/**
* Javascript
* JS: Base
*
* @author i-fabrik GmbH
* @copyright 2008 i-fabrik GmbH
* @version $Id: core.js,v 1.2 2009/06/17 11:42:53 jan Exp $
*
*/

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

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

			nav_elements.each(function(el, i) {

				el.addEvents({
					'mouseenter': function(ev) {
						this.addClass('hover');
					},
					'mouseleave': function(ev) {
						this.removeClass('hover');
					},
					'focus': function(ev) {
						this.fireEvent('mouseenter');
					},
					'blur': function(ev) {
						this.fireEvent('mouseleave');
					}
				});
			});

		},

		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() {
				hs.graphicsDir      = 'resources/themes/standard/highslide/';
				hs.showCredits      = false;
				hs.outlineType      = null;
				hs.loadingOpacity   = 0.75;
				hs.align            = 'center';
				hs.transitions      = ['expand', 'crossfade'];
				hs.wrapperClassName = 'dark borderless floating-caption';
				hs.fadeInOut        = true;
				hs.dimmingOpacity   = .75;
			
				// Add the controlbar
				if (hs.addSlideshow) hs.addSlideshow({
					//slideshowGroup: 'group1',
					interval: 5000,
					repeat: false,
					useControls: true,
					fixedControls: 'fit',
					overlayOptions: {
						opacity: .6,
						position: 'bottom center',
						hideOnMouseOut: true
					}
				});

				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');
							}
						});
					});
		    }
		},
		
		appendFLV: function() {
			var s1 = new SWFObject('resources/mplayer.swf', 'ply', '400', '200', '9', '#ffffff');
			var mvars = 'file=video/animation.flv' +
						'&type=video' +
						'&bufferlength=3' +
						'&controlbar=over' +
						'&volume=50';
			s1.addParam('allowfullscreen', 'true');
			s1.addParam('allowscriptaccess', 'always');
			s1.addParam('flashvars', mvars);
			s1.write('video_container');
		},
		
		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);