/*
	Website: http:/www.herres-sekt.com/
	BOB Design http://www.bob-design.de/
	Imagion AG http://www.imagion.de/
	Javascript Datei
	20.06.2008 Daan Meskers
	$last_change: 05.11.2008 Daan Meskers
*/
var myEnglish, produktSeite 
var bilderArray = [];
var myMenu = {
	setUpMenu: function() {	
		$$('UL.subMenu LI A').each( function( elm ) {
			var mychild = new Element( elm.parentNode ).getElement('UL')
			if ( mychild && ( window.location.href.indexOf( "produkt" ) > 0 || produktSeite ) ) {
				elm.addEvent('click', function(event){
					var event = new Event(event);
					event.preventDefault();	// stop default behaviour
					this.blur();
					this.parentNode.className = (this.parentNode.className.indexOf("on")<0) ? "on" : "" ;
					if ( this.parentNode.parentNode.selectedElm &&
						 this.parentNode.parentNode.selectedElm.innerHTML !=  this.parentNode.innerHTML )
						this.parentNode.parentNode.selectedElm.className = ""
					this.parentNode.parentNode.selectedElm = this.parentNode
				});
			} else if ((window.location.href.indexOf( "produkt" ) > 0 || produktSeite ) && elm.href.indexOf( "menutype=category" ) > 0 ) {
				elm.addEvent('click', function(event){
					parItem = this.parentNode.parentNode.parentNode.firstChild.innerHTML 
					parParItem = this.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.innerHTML 
					var ItemClicked =  parParItem + ":" + parItem 
					var myCookie = createCookie('ItemClicked', ItemClicked);
				});
			}
		});
		$$('ul.language').each( function( elm ) {
			elm.inject ( $('Menu'), 'bottom' )
			elm.addClass('topMenu')
		});
		
	},
	setOn: function(){
		var myCookie = readCookie('ItemClicked');
		if( myCookie && window.location.href.indexOf( "menutype=category" ) > 0 && myCookie.indexOf(":")>0) {
			myCookie = myCookie.split(":")
			if ( MooTools.version == '1.11'  ) {
				var coll = $('subMenu').getChildren("LI").each(function(el){
    				if ( el.firstChild.innerHTML == myCookie[0]) {
						el.addClass("on");
						if ( el.getElement('UL') ) el.getElement('UL').getChildren("LI").each(function(elc){
							if ( elc.firstChild.innerHTML == myCookie[1]) elc.addClass("on");
						});
					}
				});
			} else {
				var coll = $$('UL.subMenu LI').each(function(el){
    				if ( el.firstChild.innerHTML == myCookie[0]) {
						el.addClass("on");
						$$('UL.subMenu LI UL LI').each(function(elc){
							if ( elc.firstChild.innerHTML == myCookie[1]) elc.addClass("on");
						});
					}
				});
			}
		}
		$$('UL.subMenu LI.on').each( function( elm ) {
			elm.parentNode.selectedElm = elm
		});
	},
	animationBilder: function(){
		var outputstring = "", mywidth = 0
		$$('DIV.Bild IMG').each( function( elm ) {
			myFile = elm.parentNode.href || elm.src
			if (myFile.indexOf("notfound_thumb.gif")>0)
				myFile = elm.getNext().href
			outputstring += "<img src='"+myFile
			outputstring += (bilderArray.length==0) ? "'>" : "' class='off'>"
			bilderArray[ bilderArray.length ] =  myFile
			var w = elm.getStyle('width').toInt()
			mywidth = ( w > mywidth ) ? w : mywidth ;
			if ( mywidth < 701 && elm.src.indexOf("notfound_thumb.gif")>0) {
			   mywidth  = 1024
			   elm.src = myFile
			}
		});
		if ( bilderArray.length > 1 && ( mywidth > 700 || mywidth < 1 ) ) {
			if ( MooTools.version == '1.11'  ) $('Bild').setHTML( outputstring )
			else $('Bild').set( 'html', outputstring )
			this.animationIndex = 0
			window.setInterval( "myMenu.rotate()", 5500)
		}	
	},
	rotate: function(){
		this.animationIndex++
		if ( this.animationIndex >= bilderArray.length)
			this.animationIndex = 0
		var myBild = $( 'Bild' ).getElements( "IMG" )
		if ( !this.previous ) 
			this.previous = myBild[0]
		if ( MooTools.version == '1.11'  ) {
			var fx = new Fx.Styles( myBild[this.animationIndex], {duration:1500} );
		   		fx.start({'opacity': [0.01, 1]})
		} else {
			var fx = new Fx.Tween( myBild[this.animationIndex], {duration:1500} );
		   		fx.start('opacity', 0.01, 1)
		}
		if ( this.previous ) {
			if ( MooTools.version == '1.11'  ) {
				var fx2 = new Fx.Styles( this.previous, {duration:1500} );
		   	    	fx2.start({'opacity': [1, 0.01]})
			} else {
				var fx2 = new Fx.Tween( this.previous, {duration:1500} );
		   	    	fx2.start('opacity', 1, 0.01)
			}
		}
		this.previous = myBild[this.animationIndex]
	},
	init: function(){
		this.setUpMenu();
		this.setOn();
		this.animationBilder()
	}
}
var bc = {
	mySetStyles: function() {
		if ( window.location.search.indexOf( "menutype=alfabet" ) > 0 ) {
			$$('UL.subMenu LI').each( function( elm ) {
				var mychild = new Element( elm ).getElement('UL LI A.menu-alfabet')
				if ( mychild ) 
					elm.addClass("on cur") ;
			});
		}
		if ( window.location.href.indexOf( "extra/suchen" ) > 0 ) {
			$$('DIV#Content FORM').each( function( elm ) {elm.target = "_self"});
		}
	},
	productMouseOver: function() {
		var i=0
		$$('DIV.Bilder IMG').each( function( elm ) {
			elm.title = ''
			elm.onmouseover = function() {
				var div = new Element( elm.parentNode ).getNext();
				if ( MooTools.version == '1.11'  ) $('myBilderText').setHTML( div.innerHTML )
				else $('myBilderText').set( 'html', div.innerHTML )
				$('myBilderText').setStyle( 'left', ( new Element( elm.parentNode ).offsetLeft -18 ) )
			} 
			if (i==0) {
				var div = new Element( elm.parentNode ).getNext();
				if ( MooTools.version == '1.11'  ) $('myBilderText').setHTML( div.innerHTML )
				else $('myBilderText').set( 'html', div.innerHTML )
				$('myBilderText').setStyle( 'left', ( new Element( elm.parentNode ).offsetLeft -18 ) )
			}
			i++
		});
	}
}

window.addEvent('domready', function() {
	bc.mySetStyles()
	bc.productMouseOver()
	myMenu.init()
	if (myEnglish) {
	    var legend = $$('FORM.srch legend')
            if ( MooTools.version == '1.11' ) 
            	legend.setHTML( 'Search' )
            else 
            	legend.set( 'html', 'Search' )
            document.forms[0].action = "/extra/suchen.html?L=1"
            var produkts = $$('DIV.ProdukteBlock TABLE TH')
            if ( produkts && produkts.length > 1 ) {
                produkts[0].innerHTML = "Category"
            	produkts[1].innerHTML = "Variants"
            	produkts[2].innerHTML = "Alcohol"
                $$('A.Bezugsquellen')[0].innerHTML = "Suppliers"
                $$('A.Bezugsquellen')[0].href += "?L=1"
            }
            $$('ul.Banner').setStyle( 'display' , 'none' );
	}
});

function popup(elm) {
 window.open('',elm.target,'status=0,menubar=0,resizable=0,location=0,directories=0,scrollbars=0,toolbar=0,width=400,height=665')
}
// =========== Quirksmode Cookie script
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
