/*
	
	Website: Fliesen-Zentrum
	BOB Design http://www.bob-design.de/
	Imagion AG http://www.imagion.de/
	Javascript Datei
	20.02.2008 Daan Meskers
	$last_change: 21.05.2008 Daan Meskers
*/

var mv ;
if ( !Browser ) { var Browser = new function() { this.Engine=[];} }
window.addEvent('domready', function() {
	mv=(MooTools.version=="1.11"||MooTools.version=="1.12") ; // "1.2.1" || "1.11"
	myMenu.init()
	setScrollContent()
	window.addEvent('resize', function() { setScrollContent(true) });
	openHiddenGallerie() 
	//myVideos.init()
}); 

var myVideos= {
   init: function() {
      $$('div.Video div.news-single-video').each( function( elm ) {
           var mychild = elm.getElement('span')
           //window.alert( "mychild  " + mychild + " " +mychild.get('html') )
      });      
   }
}

var MenuArray=[],counter=0,ULcounter=0;
var myMenu = {
	setUpMenu: function() {
		$$('DIV.menu UL').each( function( elm ) {
			ULcounter++;
			elm.ULcounter = ULcounter
		});
		$$('DIV.menu UL LI').each( function( elm ) {
			var mychild = elm.getElement('UL')
			if ( mychild ) 
			{
				var fx = ( mv ) ? new Fx.Styles( mychild, {duration:500, wait:false} ) : new Fx.Tween( mychild, {duration:500, wait:false} );
				counter++;
				elm.counter = counter;
				elm.addEvent('click', function(event){					
					var fx = ( mv ) ? new Fx.Styles( mychild, {duration:500, wait:false} ): new Fx.Tween( mychild, {duration:500, wait:false} );
					var event = new Event(event);
					event.preventDefault();	// stop default behaviour
					this.blur();	// remove click border
					var Hsmall 	= 0
					var Hbig 	= mychild.getElements('LI').length * 18
					if ( window.ie || Browser.Engine.trident5 ) { 
					   Hbig = (mv) ? Hbig * 1.08 : Hbig * 1.08  ; 
					   Hbig = Math.max( 22, Hbig )
					   Hsmall = (mv) ? 7 : 7 ;  
					}
					var sizeY = (mv) ? mychild.getSize()["size"]["y"]:mychild.getStyle('height').toInt()
					var oldH = ( sizeY < Hsmall+1 ) ? Hsmall : Hbig
					var newH = ( sizeY < Hsmall+1 ) ? Hbig : Hsmall
					if ( mv ) 
						fx.start({
 							'height': [oldH, newH]
						});
					else
						fx.start('height', newH);
					if ( oldH < newH ) {
						handlingMenurechts( elm )
						handleMenu( this )
					} else 
						deleteMenu( this )
					if ( mv )
						Cookie.set('Keks', add2Array(Cookie.get('Keks'), this.counter, (oldH>newH)), {path:'/',duration:0})
					else
						Cookie.write('Keks', add2Array(Cookie.read('Keks'), this.counter, (oldH>newH)), {path:'/',duration:0})
				});
				var cookiewert = ( mv ) ? Cookie.get('Keks') : Cookie.read('Keks'); 
                               // alert( "Cookie: " + cookiewert  + " : " counter  + " : " + inArray( cookiewert, counter )  )
				if ( inArray( cookiewert, counter )  ) {
					handleMenu( elm )
					if( mv )
						var fx = new Fx.Styles( mychild, {duration:1, wait:false} );
					else 
						var fx = new Fx.Tween( mychild, {duration:1, wait:false} );
					var Hbig 	= mychild.getElements('LI').length * 18
					if ( window.ie || Browser.Engine.trident5 ) Hbig = (mv) ? Hbig * 1.07 : Hbig * 1.07 ;
					if ( mv ) 
						fx.start({
 							'height': [0, Hbig]
						});
					else 
						fx.start('height', Hbig);
				}
			} else {
				elm.addEvent('click', function(event){
					var event = new Event(event);
					event.stopPropagation()
				});			
			}
		});
	},
	init: function(){
		if ( !Browser ) {
		   var Browser = new function() { this.Engine = [] ; }
		}
		this.setUpMenu();
		if ( msie && $('subMenu') && $('subMenu').getElements('LI').length < 1 ) {
			$('subMenu').setStyle('border', '0')
		}
	}
}

// 
function setScrollContent(resize) {
	setFooter(resize)
	var textElm = $('BigText')
	var elmtype = "bigtext" 
	if ( !textElm ) {
		textElm = $('Inhalt').getElement( 'UL.Produkte' )
		elmtype = "produkte" 
	}
	if ( !textElm ) {
		textElm = $('Inhalt').getElement( 'DIV.OneKolom' )
		elmtype = "ansprechpartner" 
	}
	if ( !textElm ) {
		textElm = $('Kolumne')
		elmtype = "Kolumne" 
	}
	if ( !textElm ) {
		textElm = $('searchBigText')
		elmtype = "Search" 
	}
	if ( textElm ) {
		textElm.setStyle('height', 'auto')
		var newH = getElmScrollHeight( textElm, resize )
		if ( msie == 6 &&  elmtype != "ansprechpartner" )  newH = newH - 5
		if ( msie && elmtype == "Kolumne" )  { newH = newH - 7; textElm.setStyle('top', '-9px') }
		elmH = textElm.getCoordinates()["height"]
		if ( elmH > newH ) {
			textElm.setStyle('height', newH)
			if (elmtype == "bigtext") textElm.setStyle('width', 455)
			if (elmtype == "Search") {
			    textElm.setStyle('width', 700)
			}
			if ((elmtype == "ansprechpartner" || elmtype == "Kolumne") && textElm.className.indexOf( "hasScrollbar" ) < 0 ) {
				textElm.className += " hasScrollbar"
				$('Inhalt').className += " hasScrollbar"
			}
			else textElm.setStyle('overflow', 'hidden')
			if ( msie && $('scrollbar')) $('scrollbar').remove();
			if ( !$('scrollbar') ) {
				var newleft = textElm.getCoordinates()["left"] + textElm.getStyle('width').toInt() + 10 ;
				if ( elmtype == "produkte" ) newleft += -30
				if ( elmtype == "ansprechpartner" ) newleft = 715
				if ( elmtype == "Kolumne") newleft = 708
				var scrollbar =  new Element('div',{'id':'scrollbar','styles':{'height':newH,'left': newleft}}).injectInside($('Inhalt'));
				if ( elmtype == "Search") {
				   //window.alert( "newtop " + textElm.getCoordinates()["top"]  )
				   $('scrollbar').setStyle('top', textElm.getCoordinates()["top"] - 10 )
				}
				var handle =  new Element('div', {'id': 'handle'}).injectInside($('scrollbar'));
			} else {
				$('scrollbar').setStyle('height', newH)
				if ( resize ) {
					var newleft = textElm.getCoordinates()["left"] + textElm.getStyle('width').toInt() + 10 ;
					$('scrollbar').setStyle('left', newleft)
				}
			}		
			makeScrollbar( textElm, $('scrollbar'), $('handle') );
			//setHeader();
		} else {
			// delete scrollbar
			if ($('scrollbar'))  $('scrollbar').remove();
			if (elmtype == "bigtext") textElm.setStyle('width', "475px")
			if (elmtype == "Search") textElm.setStyle('width', 710)
		}
	}
}
function setFooter(resize) {
	var windowsize = (!msie || resize) ? window.getScrollHeight(): document.body.offsetHeight;
	$('Footer').className = ( windowsize < 625 ) ? "Small" : "" ;
}

function getElmScrollHeight( elm, resize ) {
	var coords = elm.getCoordinates()
	var windowsize = (!msie || resize) ? window.getScrollHeight(): document.body.offsetHeight;
	var elmH = windowsize - coords["top"] - 1
	if ( $('Footer') ) elmH = elmH - $('Footer').getStyle('height').toInt();
	return elmH
}

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	if ( mv )
	   var steps = (horizontal?(content.getSize().scrollSize.x - content.getSize().size.x):(content.getSize().scrollSize.y - content.getSize().size.y))
	else 
	   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){	
			if (mv) e = new Event(e).stop();
			else 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()});
}

function setHeader() {
	/* $$('DIV#Inhalt DIV.csc-header-n1 H1').each( function( elm ) {
			var handle = elm.injectInside($('ContentImage'));
	}); */
}

//
function openHiddenGallerie() {
	$$('DIV.hiddenGallerie DIV.myGallery').each( function( el ) {
		var elmA = new Element('A', {'href': 'javascript: void('+el.id+'.lightbox.open(0));', 'class': 'LightBoxGallery'}); 
		elmA.innerHTML = $$('DIV.hiddenGallerie DIV.csc-header H1')[0].innerHTML.clean();

		$$('DIV.hiddenGallerie DIV.csc-header')[0].innerHTML = ""
		elmA.injectInside($$('DIV.hiddenGallerie DIV.csc-header')[0]);
		
		//setTimeout( "openHiddengallerieWithId('"+el.id+"')", 50) */
	});
}
function openHiddengallerieWithId( elmId ) {
	if ( window[ elmId ] )
		window[elmId].lightbox.open( 0 )
	else
		setTimeout( "openHiddengallerieWithId('"+elmId+"')", 150)
}
// CSS Setup
// ============ onload CSS Loader ============ 
var msie, msie_version
var dom, opera, mozilla, mozilla_version, khtml
if ( !window.opera && navigator.userAgent.indexOf( "MSIE" ) > 0 ) {
	msie = true
	writeCSS( "styles.msie" )
	if ( navigator.userAgent.indexOf( "MSIE 6" ) > 0 || navigator.userAgent.indexOf( "MSIE 5" ) > 0 ) {
		msie = 6 ; writeCSS( "styles.msie.6" )
	}
} else if ( window.opera ) {
	opera = true
} else if ( navigator.userAgent.indexOf( "Gecko" ) > 0 ) {
	mozilla = true
}
function writeCSS( cssname ) {
	document.write('<link href="/fileadmin/templates/fliesen-zentrum/lib/css/'+cssname+'.css" rel="Stylesheet" type="text/css" />')
}

// Lib
//  ============ Cookie functions ============ 
function add2Array( arraystring, value, remove ) {
	if ( !arraystring ) arraystring = "-1"
	if( arraystring.indexOf( "," ) < 0) arraystring = "-1,"+arraystring
	while (arraystring.indexOf( ",," ) > -1) {
		arraystring = arraystring.replace( /\,+/, ",")
	}
	if ( !remove ) 
		output = arraystring+","+value
	 else
		output = removeFromArray( arraystring, value )
	return output 
}
function inArray( arraystring, value ) {
	myArray = setUpMyArray( arraystring )
	var check
	for (var i=0; myArray[i] && !check;i++){
		check = ( parseInt( myArray[i], 10 ) == parseInt( value, 10 ) )
	}
	return check;
}
function removeFromArray(  arraystring, value  ) {
	myArray = setUpMyArray( arraystring )
	var arraystring = "", komma = ""
	for (var i=0; myArray[i];i++){
		if ( myArray[i]+"" !=  value+"" && myArray[i] != "-1" ) {
			arraystring += komma+myArray[i]
			komma = ","
		}
	}
	return arraystring;
}

function setUpMyArray( arraystring ) {
	if ( !arraystring ) 
		arraystring = "-2,-1"
	if( arraystring.indexOf( "," ) < 0) 
		arraystring = "-1,"+arraystring
	while (arraystring.indexOf( ",," ) > -1) {
		arraystring = arraystring.replace( /\,+/, ",")
	}
	return eval("new Array( "+arraystring+" )" )
}

// Lib
//  ============ Cookie functions ============ 
function handleMenu( elm ) {
	var parentUL = findParentUL( elm )
	if ( parentUL && MenuArray[ "ul"+parentUL.ULcounter ] ) {
		var oldElm = MenuArray[ "ul"+parentUL.ULcounter ]
		var mychild = oldElm.getElement('UL')
		var fx = (mv) ? new Fx.Styles( mychild, {duration:400, wait:false} ) : new Fx.Tween( mychild, {duration:400, wait:false} );
		var Hsmall 	= 0
		var Hbig 	= mychild.getElements('LI').length * 18
		if ( window.ie || Browser.Engine.trident5 ) { 
                   Hbig = ( mv ) ? Hbig * 1.07 : Hbig * 1.07; 
                   Hsmall = ( mv ) ?  7 : 7 ; 
                }
		if (mv) {
			fx.start({'height': [Hbig, Hsmall]});
			Cookie.set('Keks', add2Array(Cookie.get('Keks'), oldElm.counter, true), {path:'/',duration:0})
		}
		else {
			fx.start('height', Hsmall);
			Cookie.write('Keks', add2Array(Cookie.read('Keks'), oldElm.counter, true), {path:'/',duration:0})
		}
	}
	MenuArray[ "ul"+parentUL.ULcounter ] = elm
}
function deleteMenu( elm ) {
	var parentUL = findParentUL( elm )
	if ( parentUL && MenuArray[ "ul"+parentUL.ULcounter ] &&
		MenuArray[ "ul"+parentUL.ULcounter ].innerHTML == elm.innerHTML ) {
		MenuArray[ "ul"+parentUL.ULcounter ] = false
		Cookie.set('Keks', add2Array(Cookie.get('Keks'), elm.counter, true), {path:'/',duration:0})
	}
}
function handlingMenurechts( elm_orig ) {
   var parentUL = findParentUL( elm_orig )
   // closing other menus when menu-rechts
   if ( parentUL.parentNode.id == "MenuRechts" ) {
      $$('DIV.menu UL').each( function( elm ) {
         if ( elm.parentNode.id == "MenuRechts" ) {
            var mychild = elm.getElement('UL')
            var pUlCounter = parentUL.getElement('UL').ULcounter
            if ( mychild )
               var sizeY = ( mv ) ? mychild.getSize().y : mychild.getStyle('height').toInt()
            if ( mychild && mychild.ULcounter != pUlCounter && sizeY > 11 ) {
               var fx = (mv) ? new Fx.Styles( mychild, {duration:400, wait:false} ) : new Fx.Tween( mychild, {duration:400, wait:false} );
               var Hsmall 	= 0
               var Hbig 	= mychild.getElements('LI').length * 18
               if ( window.ie || Browser.Engine.trident5 ) { 
                  Hbig = ( mv ) ? Hbig * 1.07 : Hbig * 1.07  ; 
                  Hsmall = 7; 
               }
               var del_id  = mychild.parentNode.counter
               if ( mv ) {
                  fx.start({'height': [Hbig, Hsmall]});
                  Cookie.set('Keks', add2Array(Cookie.get('Keks'), del_id, true), {path:'/',duration:0})
               } else {
                  fx.start('height', Hsmall);
                  Cookie.write('Keks', add2Array(Cookie.read('Keks'), del_id, true), {path:'/',duration:0})
               }
            }
         }
      });
   } else {
      handlingMenulinks( elm_orig )
}
}
function handlingMenulinks( elm_orig ) {
	var parentUL = findParentUL( elm_orig )
	if ( parentUL.parentNode.id == "MenuLinks" ) {
		if( parentUL.ULcounter == 7 ) {
			var elm = MenuArray[ "ul14" ]
			if (elm) closeMenuElm( elm )
		} else if( parentUL.ULcounter == 14 ) {
			// close 14
			var elm = MenuArray[ "ul7" ]
			if (elm) closeMenuElm( elm )
		}
	}
}

function findParentUL( elm ) {
	if ( elm.tagName == "UL" && elm.parentNode.tagName == "DIV"  ){
		return elm
	} else {
		return findParentUL( elm.parentNode )
	}
} 
function closeMenuElm( elm ) {
	var mychild = elm.getElement('UL')
	if ( mychild && mychild.getSize()["size"]["y"] > 11 ) {
		var fx = (mv) ? new Fx.Styles( mychild, {duration:400, wait:false} ) : new Fx.Tween( mychild, {duration:400, wait:false} );
		var Hsmall 	= 0
		var Hbig 	= mychild.getElements('LI').length * 18
		if ( window.ie || Browser.Engine.trident5 ) { 
		   Hbig = ( mv ) ? Hbig * 1.07: Hbig * 1.07 ; 
		   Hsmall = 7; 
                }
		if ( mv ) {
			fx.start({'height': [Hbig, Hsmall]});
			Cookie.set('Keks', add2Array(Cookie.get('Keks'), elm.counter, true), {path:'/',duration:0})
		} else {
			fx.start( 'height', Hsmall );
			Cookie.write('Keks', add2Array(Cookie.read('Keks'), elm.counter, true), {path:'/',duration:0})		
		}
	}
}


// doHome
function doHome() {
	window.location.href = "http://www.fliesen-zentrum.de/"
}
var testClick
function clickMenu( n ) {
    n = n+4
    testClick = 4
    $$('DIV#MenuLinks UL LI').each( function( elm ) {
        var mychild = elm.getElement('UL')
        if ( mychild )  {
            if (1==1) {
               if ( mv ) 
                   Cookie.set('Keks', add2Array(Cookie.get('Keks'), testClick, (testClick!=n)), {path:'/',duration:0})
               else  
                   Cookie.write('Keks', add2Array(Cookie.get('Keks'), testClick, (testClick!=n)), {path:'/',duration:0})            
            }
            //alert( n + " : " + testClick + " : " + Cookie.get('Keks'))
            testClick++
        }
    });
    return true;
} 

// Einstieg
function showArea( zweigstelle ) {
	document.getElementById('FZMap').src = "/fileadmin/templates/fliesen-zentrum/img/Layout/Einstieg/"+zweigstelle+".png"
}
var zweigstellen = ["Trier","Muenchen","Magdeburg","Leipzig","Erfurt","Berlin"]
function preloader() {
	for (var i=0; zweigstellen[i]; i++) {
		var newimg = new Image(0,0)
		newimg.src = "/fileadmin/templates/fliesen-zentrum/img/Layout/Einstieg/"+zweigstellen[i]+".png"
	}
}
