/**
* Javascript
* JS: Base
*
* @author i-fabrik GmbH
* @copyright 2008 i-fabrik GmbH
* @version $Id: core.js,v 1.8 2009-12-16 13:20:25 heiko Exp $
*
*/



	var DyTeRe = new Class({
		Implements: [Options, Events],

		options: {
			fSize                : 20,
			fColor               : null,
			useWordwrap          : true,
			onReplaced           : $empty(),
			onReplacementComplete: $empty()
		},

		initialize: function(elements, requestFile, options) {
			this.setOptions(options);

			if(!$chk(requestFile) || !$chk(elements))
				return;

			this.requestFile      = requestFile;
			this.elements         = ($type(elements) == 'string') ? $$(elements) : elements;
			this.elementToReplace = null;

			if(this.elements.length > 0) {

				this.elements.each(function(el) {
					var text  = this.getText(el);

					if(text) {

						var color = this.getColor(el);

						this.elementToReplace = el;
						this.elementToReplace.empty();
						el.empty();

						text.each(function(t, i) {

							if(i > 0)
								t = ' ' + t;

							this.buildImage(escape(t), color);

						}.bind(this));
					}

				}.bind(this));

			}

		},

		buildImage: function(text, color) {
			var self  = this;
			var query = ((this.requestFile.contains('?')) ? '&' : '?') +
						'text=' + text +
						'&fsize=' + this.options.fSize +
						'&fcolor=' + color;

			var dyn_image = new Asset.image(this.requestFile + query , {
				id    : 'myImage',
				alt   : unescape(text).clean(),
				onload: function() {
					self.fireEvent('onReplaced', this);
				}
			}).inject(this.elementToReplace);

		},

		getText: function(el) {

			var text = ($chk(el.get('text'))) ? el.get('text') : null;
	//		var text = el.get('html');

			if(text) {

				if(this.options.useWordwrap) {

					text = text.split(' ');

				} else {

					text = $splat(text);

				}

			}

			return text;

		},

		getColor: function(el) {

			return (!$defined(this.options.fColor)) ? el.getStyle('color').replace(/#/, '') : this.options.fColor.replace(/#/, '');

		}

	});

	var SITE = {
		start: function() {

			if(!(Browser.Engine.trident && Browser.Engine.version <= 4)) { // IE 6 erstmal weglassen

				var header_h1	   	= new DyTeRe('#siteHeadline h1', SiteUI_Options.dyterePath + 'dytere.php', {fSize: 42});
				var header_h2		= new DyTeRe('#siteHeadline h2', SiteUI_Options.dyterePath + 'dytere.php', {fSize: 30});
				var header_number   = new DyTeRe('#number', SiteUI_Options.dyterePath + 'dytere.php', {fSize: 15});
				var mainmenu_h3 	= new DyTeRe('#MainMenu h3, #login h3, #search label, #warenkorb .content h3, #love_sushi .content h3, #restaurant .content h3, .box h3', SiteUI_Options.dyterePath + 'dytere.php', {fSize: 13});
				var main_h1   = new DyTeRe('#main h1', SiteUI_Options.dyterePath + 'dytere.php', {fSize: 18});
				var mainmenu_li 	= new DyTeRe('#MainMenu ul li a', SiteUI_Options.dyterePath + 'dytere_menu.php', {fSize: 12});

			}
			SITE.initGalleries('.init-gallery');

			SITE.attentionOpeningHours();

			//SITE.appendMenu();
			//SITE.appendQuickstart();
			//SITE.appendTooltips();
			//SITE.appendHighslide();
			//SITE.appendFormElements();
			//SITE.imageAutoMover($$('img.img_change'), '_on', '_off')
		},

		initGalleries: function(selector) {

			if($chk(selector) && $$(selector)) {

				$$(selector).each(function(el) {
					new gallery(el);
				});

			}
		},

		attentionOpeningHours: function() {

			if($defined($('attentionOpeningHours')) && $defined($('mb_attentionOpeningHours'))) {
				Mediabox.open('#mb_attentionOpeningHours', '', '[inline 300 160]');
			}

		},

		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() {
				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();
		},
		appendCheckoutSuccess:function(checkurl, oID){
			var req = new Request({

				url:checkurl,

				onSuccess: function(response) {

					if (response.trim() == "1") {
						$clear(timer);
						$('order_true').setStyle('display', 'block');
						$('order_progress').setStyle('display', 'none');
						$('order_error').setStyle('display', 'none');
					}
					else if (response.trim() == "0") {
						$('order_true').setStyle('display', 'none');
						$('order_error').setStyle('display', 'none');
						$('order_progress').setStyle('display', 'block');
					} else {
						$clear(timer);
						$('order_true').setStyle('display', 'none');
						$('order_progress').setStyle('display', 'none');
						$('order_error').setStyle('display', 'block');
					}
				},
				onFailure: function() {
					alert('request failed');
				}
			});

			req.get({'oID':oID});
			req.send();

			var doPeriode = function() {
				req.get({'oID':oID});
				req.send();
			};

			var timer = doPeriode.periodical(2500);
		}
	};

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