var pCarousel = '';
var test = 0;


document.observe("dom:loaded", function(){
									
	if(window.location.pathname == '/' || window.location.pathname == ''){
		
		var images = $$('#diashow img');
		
		var s = images.size();
		var i = 0;
		var n = 1;
		
		images[i].show();
	
		new PeriodicalExecuter(function(pe){

			if(i == (s-1)) n = 0;
			else n = i+1;
			//alert('n: '+n+' i: '+i);
			new Effect.Parallel(
				[new Effect.Fade(images[i], {sync: true}),
				new Effect.Appear(images[n], {sync: true})],
				{duration: 1.5}
			);
			i++;
			if(i > (s-1)) i = 0;

		}, 5);
	
	}

	var timages = $$('#titelzeile img');

	var ts = timages.size();
	var ti = 0;
	var tn = 1;
	
	timages[ti].show();

	new PeriodicalExecuter(function(pe){

		if(ti == (ts-1)) tn = 0;
		else tn = ti+1;
		//alert('n: '+n+' i: '+i);
		new Effect.Parallel(
			[new Effect.Fade(timages[ti], {sync: true}),
			new Effect.Appear(timages[tn], {sync: true})],
			{duration: 1.5}
		);
		ti++;
		if(ti > (ts-1)) ti = 0;

	}, 5);

	var mimages = $$('#metaFader img');

	var ms = mimages.size();
	var mi = 0;
	var mn = 1;
	
	mimages[ti].show();

	new PeriodicalExecuter(function(pe){

		if(mi == (ms-1)) mn = 0;
		else mn = mi+1;
		//alert('n: '+n+' i: '+i);
		new Effect.Parallel(
			[new Effect.Fade(mimages[mi], {sync: true}),
			new Effect.Appear(mimages[mn], {sync: true})],
			{duration: 1.2}
		);
		mi++;
		if(mi > (ms-1)) mi = 0;

	}, 2.5);


										
	if($$('#slideContainer li').size() > 4){
		window.setTimeout(function(){pCarousel = new UI.Carousel("slideContainer", {});}, 1000);
	}




	$('navi').down('.angebot').down(0).onmouseover = function(event){
		//$('debug').update(getEventTarg(event,'tg').className);
		$('subnavi_ueberuns').hide();
		Effect.Appear($('subnavi_angebot'),{duration:0.7, transition: Effect.Transitions.spring});
		$$('#navi li').each(function(n){
									 
			if($(n).className=='angebot')$(n).down(0).className='active';
			else $(n).down(0).className = '';	
								 
		});
		$(this).setStyle({cursor: 'default'});
		return false;
	
	};


	$('navi').down('.ueberuns').down(0).onmouseover = function(event){
		//$('debug').update(getEventTarg(event,'tg').className);
		$('subnavi_angebot').hide();
		Effect.Appear($('subnavi_ueberuns'),{duration:0.7, transition: Effect.Transitions.spring});
		$$('#navi li').each(function(n){
									 
			if($(n).className=='ueberuns')$(n).down(0).className='active';
			else $(n).down(0).className = '';	
								 
		});
		$(this).setStyle({cursor: 'default'});
		return false;
	
	};



	$('navi').down('.angebot').down(0).onclick = function(){return false;}
	$('navi').down('.ueberuns').down(0).onclick = function(){return false;}

	$('navi').down('.kundenmeinung').observe('mouseover',function(event){													
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='';
		$('navi').down('.laser-apfel').down(0).className='';
		$('navi').down('.bestellung').down(0).className='';
		return false;
	
	});

	$('navi').down('.bestellung').observe('mouseover',function(event){													
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='';
		$('navi').down('.laser-apfel').down(0).className='';
		$('navi').down('.kundenmeinung').down(0).className='';
		return false;
	
	});

	$('navi').down('.laser-apfel').observe('mouseover',function(event){													
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='';
		$('navi').down('.kundenmeinung').down(0).className='';
		$('navi').down('.bestellung').down(0).className='';
		return false;
	
	});


	$('navi').down('.kundenmeinung').observe('mouseout',function(event){
		if(window.location.pathname.indexOf('ueberuns')  >= 0){
		$('subnavi_ueberuns').show();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='active';

		}
		else if(window.location.pathname.indexOf('angebot')  >= 0){
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').show();
		$('navi').down('.angebot').down(0).className='active';
		$('navi').down('.ueberuns').down(0).className='';			
		}

		return false;
	
	});

	$('navi').down('.bestellung').observe('mouseout',function(event){													
		if(window.location.pathname.indexOf('ueberuns')  >= 0){
		$('subnavi_ueberuns').show();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='active';

		}
		else if(window.location.pathname.indexOf('angebot')  >= 0){
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').show();
		$('navi').down('.angebot').down(0).className='active';
		$('navi').down('.ueberuns').down(0).className='';			
		}

		return false;
	
	});

	$('navi').down('.laser-apfel').observe('mouseout',function(event){													
		if(window.location.pathname.indexOf('ueberuns')  >= 0){
		$('subnavi_ueberuns').show();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='active';

		}
		else if(window.location.pathname.indexOf('angebot')  >= 0){
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').show();
		$('navi').down('.angebot').down(0).className='active';
		$('navi').down('.ueberuns').down(0).className='';			
		}

		return false;
	
	});


	$('head').observe('mouseout', function(event){
		//alert($(Event.element(event)).up('.head'));
		var tg = getEventTarg(event, 'tg');
		var reltg = getEventTarg(event, 'reltg');

		if(reltg.id=='content' || reltg.id=='metaFader' || reltg.className=='logo' || reltg.id=='simplecontent'){
			if(window.location.pathname.indexOf('ueberuns')  >= 0){
			$('subnavi_ueberuns').show();
			$('subnavi_angebot').hide();
			$('navi').down('.angebot').down(0).className='';
			$('navi').down('.ueberuns').down(0).className='active';
	
			}
			else if(window.location.pathname.indexOf('angebot')  >= 0){
			$('subnavi_ueberuns').hide();
			$('subnavi_angebot').show();
			$('navi').down('.angebot').down(0).className='active';
			$('navi').down('.ueberuns').down(0).className='';			
			}
			else if(window.location.pathname.indexOf('bestellung')  >= 0){
			$('subnavi_ueberuns').hide();
			$('subnavi_angebot').hide();
			$('navi').down('.angebot').down(0).className='';
			$('navi').down('.ueberuns').down(0).className='';
			$('navi').down('.bestellung').down(0).className='active';
			}
			else if(window.location.pathname.indexOf('kundenmeinung')  >= 0){
			$('subnavi_ueberuns').hide();
			$('subnavi_angebot').hide();
			$('navi').down('.angebot').down(0).className='';
			$('navi').down('.ueberuns').down(0).className='';		
			$('navi').down('.kundenmeinung').down(0).className='active';
			}
			else if(window.location.pathname.indexOf('laser-apfel')  >= 0){
			$('subnavi_ueberuns').hide();
			$('subnavi_angebot').hide();
			$('navi').down('.angebot').down(0).className='';
			$('navi').down('.ueberuns').down(0).className='';	
			$('navi').down('.laser-apfel').down(0).className='active';
			}
		}
		return false;
	
	
	});

	$('titelzeile').observe('mouseover', function(event){
		//alert($(Event.element(event)).up('.head'));
		var tg = getEventTarg(event, 'tg');
		var reltg = getEventTarg(event, 'reltg');
		

		if(window.location.pathname.indexOf('ueberuns')  >= 0){
		$('subnavi_ueberuns').show();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='active';

		}
		else if(window.location.pathname.indexOf('angebot')  >= 0){
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').show();
		$('navi').down('.angebot').down(0).className='active';
		$('navi').down('.ueberuns').down(0).className='';			
		}
		else if(window.location.pathname.indexOf('bestellung')  >= 0){
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='';
		$('navi').down('.bestellung').down(0).className='active';
		}
		else if(window.location.pathname.indexOf('kundenmeinung')  >= 0){
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='';		
		$('navi').down('.kundenmeinung').down(0).className='active';
		}
		else if(window.location.pathname.indexOf('laser-apfel')  >= 0){
		$('subnavi_ueberuns').hide();
		$('subnavi_angebot').hide();
		$('navi').down('.angebot').down(0).className='';
		$('navi').down('.ueberuns').down(0).className='';	
		$('navi').down('.laser-apfel').down(0).className='active';
		}
		
		
		return false;
	
	
	});


	$('navi').down('.angebot').observe('mouseout',function(event){													
		if(window.location.pathname.indexOf('ueberuns')  <= 0 || window.location.pathname.indexOf('angebot')){
			if(window.location.pathname.indexOf('bestellung')  >= 0) $('navi').down('.bestellung').down(0).className='active';
			else if(window.location.pathname.indexOf('laser-apfel')  >= 0) $('navi').down('.laser-apfel').down(0).className='active';
			else if(window.location.pathname.indexOf('kundenmeinung')  >= 0) $('navi').down('.kundenmeinung').down(0).className='active';
		}

		return false;
	
	});
	
	$('navi').down('.ueberuns').observe('mouseout',function(event){													
		if(window.location.pathname.indexOf('ueberuns')  <= 0 || window.location.pathname.indexOf('angebot')){
			if(window.location.pathname.indexOf('bestellung')  >= 0) $('navi').down('.bestellung').down(0).className='active';
			else if(window.location.pathname.indexOf('laser-apfel')  >= 0) $('navi').down('.laser-apfel').down(0).className='active';
			else if(window.location.pathname.indexOf('kundenmeinung')  >= 0) $('navi').down('.kundenmeinung').down(0).className='active';
		}

		return false;
	
	});	
	
	


	try{
		
		accordion = new Accordion("accordion", 1);	
		
	}catch(e){};

	//try{calSum();}catch(e){};



//	$('subnavi').observe('mouseout',function(event){
//		alert(getEventTarg(event, 'reltg').id);												
//		Effect.Fade($('subnavi'),{duration:2, transition: Effect.Transitions.spring});
//		$(this).down(0).className='active';
//		return false;
//	
//	});

//			$onmouseover = ($page['title']=='angebot')?'onmouseover="Effect.Appear($(\'subnavi\'),{duration:2, transition: Effect.Transitions.spring});this.className=\'active\';return false;"':'';
//			$onmouseout = ($page['title']=='angebot')?'onmouseout="Effect.Fade($(\'subnavi\'),{duration:2, transition: Effect.Transitions.spring});this.className=\'\';return false;"':'';

										
});


showM = function(id){


	new Ajax.Request('templates/mform.php', {
		method: 'post',
		parameters: {a: 'show', id: (id+1)},
		onComplete: function(transport) {
			Element.insert(document.body, transport.responseText);
			new Effect.Appear($('mPopup'),{duration:.3});
			$('mPopup').setStyle({top: (document.viewport.getHeight()/2-$('mPopup').getHeight()/2)+'px', left: (document.viewport.getWidth()/2-$('mPopup').getWidth()/2)+'px'});
			
		},
		onFailure: function(){
			
		}
	});	

	
	
}

saveM = function(){
	
	var n = $('name')?$F('name'):'';
	var ue = $('ue')?$F('ue'):'';
	var t = $('text')?$F('text'):'';
	var id = $('id')?$F('id'):'';
	var tn = $('tel')?$F('tel'):'';
	var e = $('email')?$F('email'):'';
	
	var errorMsg = '';
			
	if (n.length == 0){
		errorMsg += "Bitte Name eingeben!<br />";
	}		
	if (ue.length == 0){
		errorMsg += "Bitte eine &Uuml;berschrift angeben!<br />";
	}
	if (t.length == 0){
		errorMsg += "Bitte einen Text angeben!<br />";			
	
	}
		
	
	if(errorMsg != ""){
		$('error').update(errorMsg);
	}
	else{
		$('error').update('');
		new Ajax.Request('templates/mform.php', {
			method: 'post',
			parameters: {a: 'save', n: n, ue: ue, t: t, id: id, tn: tn, e: e},
			onComplete: function(transport) {
				$('mPopup').update(transport.responseText);				
								
			},
			onFailure: function(){
				
			}
		});	

	}	
	
	
}


/////////////
//
// ShoppingCart Functions
//
////////////

add2Cart = function(id){

	new Ajax.Request('templates/cart.php', {
		method: 'post',
		parameters: {a: 'tocart', id: id},
		onComplete: function(transport) {
			//$('debug').update(transport.responseText);
			
		},
		onFailure: function(){
			
		}
	});	

}

productsPopup = function(type){
	
	new Ajax.Request('templates/productspopup.php', {
		method: 'post',
		parameters: {a: 'show', type: type},
		onComplete: function(transport) {
			Element.insert(document.body, transport.responseText);
			new Effect.Appear($('pPopup'),{duration:.3});
			$('pPopup').setStyle({top: (document.viewport.getHeight()/2-$('pPopup').getHeight()/2)+'px', left: (document.viewport.getWidth()/2-$('pPopup').getWidth()/2)+'px'});
			
		},
		onFailure: function(){
			
		}
	});	


}

changeGprice = function(e){
	
	if (!e) e = window.event;
	
	if(e.type == 'click'){
		
		if(Event.element(e).className == 'plus' || Event.element(e).className == 'minus'){
				
			var value = (Event.element(e).up(0).next(0).value)*(Event.element(e).up(1).next(3).down(0).getAttribute('value'))*(Event.element(e).up(1).next(1).down(0).getAttribute('value'));
			if (isNaN(value)){
				value = 0;
			}
			Event.element(e).up(1).next(4).down(0).innerHTML = (number_format(value,2,',','')) + ' EUR';
			Event.element(e).up(1).next(4).down(0).setAttribute('value', (Math.round(value*100)/100) );	
	
			
			calSum();
			
		}
	}
	else if(e.type == 'keyup'){
		
		var value = (Event.element(e).value)*(Event.element(e).up(0).next(3).down(0).getAttribute('value'))*(Event.element(e).up(0).next(1).down(0).getAttribute('value'));
		if (isNaN(value)){
			value = 0;
			Event.element(e).value = 0;
		}
		Event.element(e).up(0).next(4).down(0).innerHTML = (number_format(value,2,',','')) + ' EUR';
		Event.element(e).up(0).next(4).down(0).setAttribute('value', (Math.round(value*100)/100) );
		
		calSum();
	
	}
	//elem.up(1).next(4).down(0).innerHTML=elem.up(0).next(0).value)*(elem.up(1).next(3).down(0).getAttribute('value')) + ' &euro;';
}

deleteAll = function(e){
	
	if (!e) e = window.event;
	
	if(e.type == 'click'){
		Event.element(e).up(0).previous(0).down(0).innerHTML = '0,00 EUR';
		Event.element(e).up(0).previous(0).down(0).setAttribute('value', 0 );
		Event.element(e).up(0).previous(5).down(0).next(0).value = 0;
		Event.element(e).up(0).previous(5).down(0).next(0).setAttribute('value', 0 );
		//alert(Event.element(e).up(0).previous(5).down(0).next(0).value);
		
		calSum();
		
	}

	var iArr = new Array();
	
	$$('.amount').each(function(i, z){
			if(i.value != 0){
				iArr.push('"'+i.id+'": '+i.value);
			}
	});

	items = '{'+iArr.join(',')+'}';

	new Ajax.Request('templates/productspopup.php', {
		method: 'post',
		parameters: {a: 'del', items: items},
		onComplete: function(transport) {
		},
		onFailure: function(){
			
		}
	});	



}


keepProducts = function(){
	
	var iArr = new Array();
	
	$$('.amount').each(function(i, z){
			if(i.value != 0){
				iArr.push('"'+i.id+'": '+i.value);
			}
	});

	items = '{'+iArr.join(',')+'}';

	new Ajax.Request('templates/productspopup.php', {
		method: 'post',
		parameters: {a: 'keep', items: items},
		onComplete: function(transport) {
		},
		onFailure: function(){
			
		}
	});	

	try{$('pPopup').hide();}catch(e){};

}


checkCart = function(){
	
	new Ajax.Request('templates/productspopup.php', {
		method: 'post',
		parameters: {a: 'checkcart'},
		onComplete: function(transport) {
			Element.insert(document.body, transport.responseText);
			new Effect.Appear($('pPopup'),{duration:.3});
			$('pPopup').setStyle({top: (document.viewport.getHeight()/2-$('pPopup').getHeight()/2)+'px', left: (document.viewport.getWidth()/2-$('pPopup').getWidth()/2)+'px'});
			//calSum();
		},
		onFailure: function(){
			
		}
	});		
	
}


checkout = function(){
	
	var regExpEmail = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	var errorMsg = "";	
	
	var name = ($('name'))?$F('name'):'';
	var firma = ($('firma'))?$F('firma'):'';
	var str = ($('str'))?$F('str'):'';
	var plz = ($('plz'))?$F('plz'):'';	
	var ort = ($('ort'))?$F('ort'):'';
	var tel = ($('tel'))?$F('tel'):'';	
	var email = ($('email'))?$F('email'):'';

	if (name.length == 0 || str.length == 0 || plz.length == 0 || ort.length == 0 || tel.length == 0 || email.length == 0){
		errorMsg += "Bitte alle Felder ausf&uuml;llen!<br />";
	}

	if (!email.match(regExpEmail)) errorMsg += "Bitte eine g&uuml;ltige Emailadresse angeben!<br />";


	if(errorMsg != ''){
	
		$('error').update(errorMsg+'<br />');
		return false;
		
	}
	else $('error').update('');
	
	var formdata = $('bform').serialize(true);
	formdata = $H(formdata).toJSON();

	new Ajax.Request('templates/productspopup.php', {
		method: 'post',
		parameters: {a: 'checkout', formdata: formdata},
		onComplete: function(transport) {
			$('pPopup').update(transport.responseText);
		},
		onFailure: function(){
			
		}
	});		
	
}

checkoutForm = function(){
	
	new Ajax.Request('templates/productspopup.php', {
		method: 'post',
		parameters: {a: 'checkoutForm'},
		onComplete: function(transport) {
			$('pPopup').update(transport.responseText);

		},
		onFailure: function(){
			
		}
	});		
	
}


calSum = function(){

	var sum = gsum();

	$('gsum').innerHTML = sum[2] + ' EUR';
	$('psum').innerHTML = sum[0] + ' EUR';
	$('mwst').innerHTML = sum[1] + ' EUR';
	
}

gsum = function(){

	var copreis = 0;

	$$('.gpreis').each(function(p){
			
			copreis += parseFloat(p.getAttribute('value'));					
								
	});

	
	var mwst = copreis*0.07;
	var gmmwst = copreis + mwst;
	
	return [number_format(gmmwst,2,',',''), number_format(mwst,2,',',''), number_format(copreis,2,',','')];

}

function getEventTarg(e, type){
	if (!e) e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;	
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	
	if(type == 'tg') return tg;
	else if(type == 'reltg') return reltg;
};


function number_format( number, decimals, dec_point, thousands_sep ) {

    var n = number, prec = decimals;
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
    if (s.indexOf(dec) === -1 && prec > 1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}


//var updateScroller = function() {
//
//if(scrolldiv != null && scrolldiv.scrollUpdate) scrolldiv.scrollUpdate();	
//
//};
