var myurl = location.href;

window.addEvent('domready', function() {
	if(myurl.search('kuechenwelten.php') > 0 || myurl.search('materialundtechnik.php') > 0) {
    ReMooz.assign();
    $$('.popup_frame a').each(function(element) {
        new ReMooz(element, {
            centered: true,
            opacityResize: 0,
            shadow: 'onOpen',
            origin: element.getElement('img'),
            moreleft: 100
        });
    });
  }
	
	if(myurl.search('materialundtechnik.php') > 0) {
    var myMenu = new ImageMenu($$('#imageMenu a'),{openWidth:245, border:2, onOpen:function(e,i){alert(e);}});
  }	 
});

window.onload = function() {
	var size = $('body').getSize();
	var preLoadImg = new Array();
	var get = $get();
	
	if(size.x < 1280 || size.y < 900) {
		if(size.x < 1030 || size.y < 750) {
			$('content_bg').setStyle('background', '#fff');
			$('content_bg').setStyle('width', '980px');
			$('content_bg').setStyle('height', '560px');
			$('content_bg').setStyle('margin-top', '5px');
			$('footer').setStyle('display', 'none');
			$('content').setStyle('left', '6px');
			$('content').setStyle('top', '6px');
		}
	} else {
		$('body').setStyle('background-position', '0 0');
		$('content_bg').setStyle('margin-top', '135px');
		$('content_bg').setStyle('width', '1311px');
		$('content_bg').setStyle('background', 'url(hpimg/content_bg.png)');
		$('footer').setStyle('left', '172px');
		$('content').setStyle('left', '171px');	
	}
	
	$$('.main_nav').each(function(element) {
		for(var i=1; i < 3; i++) {
			preLoadImg.src = 'hpimg/nav_btn_' + element.id + '_' + (i == 1 ? 'in' : '') + 'active.' + (element.id.search('_') > 0 ? 'gif' : 'jpg');
		}
		element.addEvent('mouseover', function(){
			element.setStyle('background', 'url(hpimg/nav_btn_' + element.id + '_active.' + (element.id.search('_') > 0 ? 'gif' : 'jpg') + ') 0 0 no-repeat');
		});
		element.addEvent('mouseout', function(){
			element.setStyle('background', 'url(hpimg/nav_btn_' + element.id + '_inactive.' + (element.id.search('_') > 0 ? 'gif' : 'jpg') + ') 0 0 no-repeat');
		});
	});
	
	if(myurl.search('kuechenwelten.php') > 0 || myurl.search('impressum.php') > 0) {
		var sbcount = 0;
		$$('.popup_frame').each(function(element) {
			sbcount++;
			makeScrollbar(element, $('scrollbar' + sbcount), $('handle' + sbcount));
		});
		
		$$('.scrollbar-vert').each(function(element) { element.setStyle('display', 'none'); });
	}
	
	IMGpreloaderDFL();
	
	if(myurl.search('index.php') > 0 || myurl.search('.php') == -1)
	{
		preLoadImg[0] = 'index/mdl_bg.gif';
		preLoadImg[1] = 'index/mdl_bg_mdl_kw_active.gif';
		preLoadImg[2] = 'index/mdl_bg_mdl_kw_inactive.gif';
		preLoadImg[3] = 'index/mdl_bg_mdl_mt_active.gif';
		preLoadImg[4] = 'index/mdl_bg_mdl_mt_inactive.gif';
		preLoadImg[5] = 'index/mdl_bg_mdl_nk_active.gif';
		preLoadImg[6] = 'index/mdl_bg_mdl_nk_inactive.gif';
		
		$$('#mdl_kw', '#mdl_nk', '#mdl_mt').each(function(element) {
			element.addEvent('mouseover', function(){
				element.setStyle('background', 'url(hpimg/index/mdl_bg_' + element.id + '_active.gif) 0 0 no-repeat');
			});
			element.addEvent('mouseout', function(){
				element.setStyle('background', 'url(hpimg/index/mdl_bg_' + element.id + '_inactive.gif) 0 0 no-repeat');
			});
		});
	}
	else if(myurl.search('kuechenwelten.php') > 0)
	{
		preLoadImg[0] = 'kuechenwelten/kw_btn_klassisch_active.jpg';
		preLoadImg[1] = 'kuechenwelten/kw_btn_landhaus_active.jpg';
		preLoadImg[2] = 'kuechenwelten/kw_btn_modern_active.jpg';
		
		initCarussel();
		
		$$('.popup_close').each(function(element) {
			element.addEvent('click', function(){
				var parent = element.getParent('div');
				popupMorph(parent.id, false, 510, false);
			});
		});
				
		$$('#kw_btn_landhaus', '#kw_btn_klassisch', '#kw_btn_modern').each(function(element) {
		$(element.id+'_popup').setStyle('display', 'none');
		$(element.id+'_popup').setStyle('visibility', 'visible');
			element.addEvent('mouseover', function(){
				element.setStyle('background', 'url(hpimg/kuechenwelten/' + element.id + '_active.jpg) 0 0 no-repeat');
			});
			element.addEvent('mouseout', function(){
				element.setStyle('background', 'url(hpimg/kuechenwelten/' + element.id + '_inactive.jpg) 0 0 no-repeat');
			});
			element.addEvent('click', function(){
				$$('#kw_btn_landhaus', '#kw_btn_klassisch', '#kw_btn_modern').each(function(tmp_element) {
					if(tmp_element.id != element.id && $(tmp_element.id + '_popup').getStyle('width').toInt() != 0) { popupMorph(tmp_element.id + '_popup', false, 510, false); }
				});
				if($(element.id + '_popup').getStyle('width').toInt() == 0) {
					popupMorph(element.id + '_popup', true, 510, false);
				} else {
					popupMorph(element.id + '_popup', false, 510, false);
				}
			});
		});
		
		if(get.page == 'landhaus') {
			popupMorph('kw_btn_landhaus_popup', true, 510, false);
		}
		else if(get.page == 'klassisch') {
			popupMorph('kw_btn_klassisch_popup', true, 510, false);
		}
		else if(get.page == 'modern') {
			popupMorph('kw_btn_modern_popup', true, 510, false);
		}
	}
	else if(myurl.search('ihreneuekueche.php') > 0)
	{
		preLoadImg[0] = 'ihreneuekueche/mdl_beratung_no_active.jpg';
		preLoadImg[1] = 'ihreneuekueche/mdl_entstehung_no_active.jpg';
		preLoadImg[2] = 'ihreneuekueche/mdl_verwirklichung_no_active.jpg';
		
		$$('.popup_close').each(function(element) {
			element.addEvent('click', function(){
				var parent = element.getParent('div');
				popupMorph(parent.id, false, false, 360);
			});
		});
		
		$$('#ink_beratung', '#ink_entstehung', '#ink_verwirklichung').each(function(element) {
		$(element.id+'_popup').setStyle('display', 'none');
		$(element.id+'_popup').setStyle('visibility', 'visible');
			element.addEvent('click', function(){
				$$('#ink_beratung', '#ink_entstehung', '#ink_verwirklichung').each(function(tmp_element) {
					if(tmp_element.id != element.id && $(tmp_element.id + '_popup').getStyle('height').toInt() != 0) { popupMorph(tmp_element.id + '_popup', false, false, 360); }
				});
				if($(element.id + '_popup').getStyle('height').toInt() == 0) {
					popupMorph(element.id + '_popup', true, false, 360);
				} else {
					popupMorph(element.id + '_popup', false, false, 360);
				}
			});
		});
		
		if(get.page == 'beratungplanung') {
			popupMorph('ink_beratung_popup', true, false, 360);
		}
		else if(get.page == 'entstehung') {
			popupMorph('ink_entstehung_popup', true, false, 360);
		}
		else if(get.page == 'verwirklichung') {
			popupMorph('ink_verwirklichung_popup', true, false, 360);
		}
	}
	else if(myurl.search('materialundtechnik.php') > 0)
	{
		$$('.popup_close').each(function(element) {
			element.addEvent('click', function(){
				var parent = element.getParent('div');
				popupMorph(parent.id, false, false, 456, true);
			});
		});
		
		/*$$('#mt_arbeitsflaechen_popup', '#mt_moebelfronten_popup', '#mt_griffe_popup', '#mt_spuelenarmaturen_popup', '#mt_egeraete_popup').each(function(element) {
		$(element.id).setStyle('display', 'none');
		$(element.id).setStyle('visibility', 'visible');
			element.addEvent('click', function(){
				if($(element.id).getStyle('width').toInt() == 0) {
					popupMorph(element.id, true, false, 456);
				}
			});
		});*/
		
		if(get.page == 'arbeitsflaechen') {
			popupMorph('mt_arbeitsflaechen_popup', true, false, 456, true);
			ImageFlowMT(true, 24, 'mt_arbeitsflaechen_popup_cnt');
		}
		else if(get.page == 'moebelfronten') {
			popupMorph('mt_moebelfronten_popup', true, false, 456);
			ImageFlowMT(true, 45, 'mt_moebelfronten_popup_cnt');
		}
		else if(get.page == 'griffe') {
			popupMorph('mt_griffe_popup', true, false, 456);
			ImageFlowMT(false, 49, 'mt_griffe_popup_cnt');
		}
		else if(get.page == 'spuelenarmaturen') {
			popupMorph('mt_spuelenarmaturen_popup', true, false, 456);
			ImageFlowMT(false, 9, 'mt_spuelenarmaturen_popup_cnt');
		}
		else if(get.page == 'egeraete') {
			popupMorph('mt_egeraete_popup', true, false, 456, true);
			ImageFlowMT(true, 10, 'mt_egeraete_popup_cnt');
		}
	}
	else if(myurl.search('wirueberuns.php') > 0)
	{
		$$('.popup_close').each(function(element) {
			element.addEvent('click', function(){
				var parent = element.getParent('div');
				popupMorph(parent.id, false, false, false);
				$('wuu_team_layer').setStyle('display', 'none');
			});
		});
		
		$$('#wuu_team_norbert', '#wuu_team_svend', '#wuu_team_kim', '#wuu_team_cornelia', '#wuu_team_berthold').each(function(element) {
		$(element.id+'_popup').setStyle('display', 'none');
		$(element.id+'_popup').setStyle('visibility', 'visible');
		$(element.id+'_popup').setStyle('width', '380px');
			element.addEvent('click', function(){
				if($(element.id+'_popup').getStyle('display') == 'none' || $(element.id+'_popup').getStyle('opacity').toInt() == 0) {
					popupMorph(element.id+'_popup', true, false, false);
					$('wuu_team_layer').setStyle('display', 'block');
				} else {
					popupMorph(element.id+'_popup', false, false, false);
				}
			});
			element.addEvent('mouseover', function(){
				element.setStyle('cursor', 'pointer');
			});
		});
	}
	else if(myurl.search('kontakt.php') > 0)
	{
		$$('.popup_close').each(function(element) {
			element.addEvent('click', function(){
				var parent = element.getParent('div');
				popupMorph(parent.id, false, false, 480);
			});
		});
		
		$('kontakt_btn_formular').addEvent('mouseover', function(){
			$('kontakt_btn_formular').setProperty('src', 'hpimg/kontakt/kontakt_btn_formular_active.jpg');
		});
		$('kontakt_btn_formular').addEvent('mouseout', function(){
			$('kontakt_btn_formular').setProperty('src', 'hpimg/kontakt/kontakt_btn_formular.jpg');
		});
		
		$('kontakt_form_popup').setStyle('display', 'none');
		$('kontakt_form_popup').setStyle('visibility', 'visible');
		$('kontakt_btn_formular').addEvent('click', function(){
			if($('kontakt_form_popup').getStyle('display') == 'none' || $('kontakt_form_popup').getStyle('opacity').toInt() == 0) {
				popupMorph('kontakt_form_popup', true, false, 480);
			} else {
				popupMorph('kontakt_form_popup', false, false, 480);
			}
		});
		
		if($('kf_form')) {
     $('kf_form').addEvent('submit', function(e) {
       var error = false;
       var email_error = false;
       this.getElements('.validate').each(function(input) {
         if(input.value == '' || (input.id == 'kf_email' && validate_email(input.value) != true)) { input.addClass('error').morph({ 'border-color': '#F00', 'background-color': '#FFEBE8' }); if(input.id == 'kf_email') { email_error = true; } else { error = true; } }
         else if(input.hasClass('error') && input.value != '' || (input.id == 'kf_email' && validate_email(input.value) == true)) { input.removeClass('error').morph({ 'border-color': '#A0A0A0', 'background-color': '#FFF' }); }
       });
       if(error || email_error) {
         var error_msg = this.getElement('.kf_error');
         if(error_msg && error) {
          error_msg.setStyles({ 'opacity': '0', 'display': 'block' });
          error_msg.morph({ 'opacity': '1' });
         } else if(!error && error_msg){
          error_msg.setStyles({ 'opacity': '0', 'display': 'none' });
         	error_msg.morph({ 'opacity': '0' });
         }
         var error_email_msg = this.getElement('.kf_error_email');
         if(error_email_msg && email_error) {
          error_email_msg.setStyles({ 'opacity': '0', 'display': 'block' });
          error_email_msg.morph({ 'opacity': '1' });
				 } else if(!email_error && error_email_msg){
				  error_email_msg.setStyles({ 'opacity': '0', 'display': 'none' });
         	error_email_msg.morph({ 'opacity': '0' });
         }
       } else {
       		/*
       		e.stop();
       	  var log = $('kf_result').empty().addClass('ajax-loading');
					this.set('send', {onComplete: function(response) { 
						log.removeClass('ajax-loading');
						log.set('html', response);
					}});
				  this.send();
				  */
       }
       return (error || email_error ? false : true);
     });
    }
    
		if(get.page == 'shoud') {
			popupMorph('kontakt_form_popup', true, false, 450);
		}
	}
	
	if(preLoadImg.length > 0) {
		IMGpreloader(preLoadImg);
	}
}

function popupMorph(myelement, forward, width, height, elementx) {
	var duration = 1000;
	var popup = new Fx.Elements('#'+myelement, {duration: duration, transition: Fx.Transitions.Sine.easeOut});
	
	if(!forward) {
		$$('.popup_close').each(function(element) { element.setStyle('display', 'none'); });
		if($$('.scrollbar-vert')) { $$('.scrollbar-vert').each(function(element) { element.setStyle('display', 'none'); }); }
		if($$('.scrollbar-up')) { $$('.scrollbar-up').each(function(element) { element.setStyle('display', 'none'); }); }
		if($$('.scrollbar-down')) { $$('.scrollbar-down').each(function(element) { element.setStyle('display', 'none'); }); }
    $$('.popup_frame img').each(function(element) {
			element.setStyle('display', 'none');
    });
    if(elementx) {
    	$(myelement + '_cnt').setStyle('display', 'none');
    }
	} else {
		$(myelement).setStyle('display', 'block');
		$(myelement).setStyle('position', 'absolute');
    if(elementx) {
    	$(myelement + '_cnt').setStyle('display', 'block');
    }
	}
	
	popup.start({
			'0': {
				'opacity': [(forward ? 0 : 1), (forward ? 1 : 0)],
				'width': [(forward ? (width ? 0 : $(myelement).getStyle('width').toInt()) : (width ? width : $(myelement).getStyle('width').toInt())), (forward ? (width ? width : $(myelement).getStyle('width').toInt()) : (width ? 0 : $(myelement).getStyle('width').toInt()))],
				'height': [(forward ? (height ? 0 : $(myelement).getStyle('height').toInt()) : (height ? height : $(myelement).getStyle('height').toInt())), (forward ? (height ? height : $(myelement).getStyle('height').toInt()) : (height ? 0 : $(myelement).getStyle('height').toInt()))]
			}
	});
	
	(function(){
		if(forward) {
			$$('.popup_close').each(function(element) { element.setStyle('display', 'block'); });
			if($$('.scrollbar-vert')) { $$('.scrollbar-vert').each(function(element) { element.setStyle('display', 'block'); }); }
			if($$('.scrollbar-up')) { $$('.scrollbar-up').each(function(element) { element.setStyle('display', 'block'); }); }
			if($$('.scrollbar-down')) { $$('.scrollbar-down').each(function(element) { element.setStyle('display', 'block'); }); }
	    $$('.popup_frame img').each(function(element) {
				element.setStyle('display', 'block');
	    });
		} else {
			$(myelement).setStyle('position', 'relative');
		}
	}).delay(duration);
}

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse) {
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
	
	$$('.scrollbar-up').each(function(element) {
		element.addEvent('click', function(){
			slider.set(slider.step - 5);
		});
	});
	
	$$('.scrollbar-down').each(function(element) {
		element.addEvent('click', function(){
			slider.set(slider.step + 5);
		});
	});
}

function validate_email(email) {
  var regex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	
  if (!email.match(regex)) {
      return false;
  } else {
      return true;
  }
}

function IMGpreloader(myimages) {
	var imageObj = new Array();
	for(i=0; i<=myimages.length; i++) {
		imageObj[i] = new Image();
		imageObj[i].src = 'hpimg/' + myimages[i];
	}
}

function ImageFlowMT(reflection, startid, flowid) {
	var instanceOne = new ImageFlow();
	instanceOne.init({ ImageFlowID: flowid, reflections: reflection, reflectionGET: '&bgc=ffffff', startID: startid });
}

function IMGpreloaderDFL() {
	var preDFLLoadImg = new Array();
	
	preDFLLoadImg[0] = 'nav_btn_impressum_active.jpg';
	preDFLLoadImg[1] = 'nav_btn_kontakt_active.jpg';
	preDFLLoadImg[2] = 'nav_btn_kw_active.jpg';
	preDFLLoadImg[3] = 'nav_btn_mt_inactive.jpg';
	preDFLLoadImg[4] = 'nav_btn_nk_inactive.jpg';
	preDFLLoadImg[5] = 'nav_btn_onlineshop_inactive.jpg';
	preDFLLoadImg[6] = 'nav_btn_wirueberuns_inactive.jpg';
	preDFLLoadImg[7] = 'nav_btn_impressum_inactive.jpg';
	preDFLLoadImg[8] = 'nav_btn_kontakt_inactive.jpg';
	preDFLLoadImg[9] = 'nav_btn_kw_klassisch_active.gif';
	preDFLLoadImg[10] = 'nav_btn_kw_klassisch_inactive.gif';
	preDFLLoadImg[11] = 'nav_btn_kw_landhaus_active.gif';
	preDFLLoadImg[12] = 'nav_btn_kw_landhaus_inactive.gif';
	preDFLLoadImg[13] = 'nav_btn_kw_modern_active.gif';
	preDFLLoadImg[14] = 'nav_btn_kw_modern_inactive.gif';
	preDFLLoadImg[15] = 'nav_btn_mt_arbeitsflaechen_active.gif';
	preDFLLoadImg[16] = 'nav_btn_mt_arbeitsflaechen_inactive.gif';
	preDFLLoadImg[17] = 'nav_btn_mt_egeraete_active.gif';
	preDFLLoadImg[18] = 'nav_btn_mt_egeraete_inactive.gif';
	preDFLLoadImg[19] = 'nav_btn_mt_griffe_active.gif';
	preDFLLoadImg[20] = 'nav_btn_mt_griffe_inactive.gif';
	preDFLLoadImg[21] = 'nav_btn_mt_moebelfronten_active.gif';
	preDFLLoadImg[22] = 'nav_btn_mt_moebelfronten_inactive.gif';
	preDFLLoadImg[23] = 'nav_btn_mt_spuelenarmaturen_active.gif';
	preDFLLoadImg[24] = 'nav_btn_mt_spuelenarmaturen_inactive.gif';
	preDFLLoadImg[25] = 'nav_btn_nk_beratung_active.gif';
	preDFLLoadImg[26] = 'nav_btn_nk_beratung_inactive.gif';
	preDFLLoadImg[27] = 'nav_btn_nk_entstehung_active.gif';
	preDFLLoadImg[28] = 'nav_btn_nk_entstehung_inactive.gif';
	preDFLLoadImg[29] = 'nav_btn_nk_verwirklichung_active.gif';
	preDFLLoadImg[30] = 'nav_btn_nk_verwirklichung_inactive.gif';
	
	IMGpreloader(preDFLLoadImg);
}

function $get(key,url) {
 if(arguments.length < 2) url =location.href;  
 if(arguments.length > 0 && key != ""){  
   if(key == "#"){  
       var regex = new RegExp("[#]([^$]*)");  
   } else if(key == "?"){  
       var regex = new RegExp("[?]([^#$]*)");  
   } else {  
       var regex = new RegExp("[?&]"+key+"=([^&#]*)");  
   }  
   var results = regex.exec(url);  
   return (results == null )? "" : results[1];  
 } else {  
   url = url.split("?");  
   var results = {};  
       if(url.length > 1){  
           url = url[1].split("#");  
           if(url.length > 1) results["hash"] = url[1];  
           url[0].split("&").each(function(item,index){  
               item = item.split("=");  
               results[item[0]] = item[1];  
           });  
       }  
   return results;  
 }
}
