// JavaScript Document
function togglePromotionBox() {
	pbox = $('promotionbox');
	plink = $('promotionlink');
	if (pbox.visible()) {	
		plink.update('Promotion-Code einlösen');
		pbox.blindUp();
	} else {
		plink.update('abbrechen');
		pbox.blindDown();
	}	
}

function submitCart(form){
	emptyFields = '';
	emptyFields += checkEmpty(form.last_name); 
	emptyFields += checkEmpty(form.first_name); 
	emptyFields += checkEmpty(form.address1);
	emptyFields += checkEmpty(form.city);
	emptyFields += checkEmpty(form.zip);
	emptyFields += checkEmpty(form.email);
	emptyFields += checkEmpty(form.night_phone_b);
	if(emptyFields.length != 0){
		$('validation-error-output').innerHTML = 'Alle Felder müssen ausgefüllt sein';
	}else if(!checkNumbers(form.zip)){
		$('validation-error-output').innerHTML = 'PLZ muss eine 4-stellige Zahl sein';
	}else{
		if(checkEmail(form.email)){
			if(form.email.value!=form.confirm.value){
				$('label_'+form.email.name).className = 'validation-error';
				$('label_'+form.confirm.name).className = 'validation-error';
				$('validation-error-output').innerHTML = 'E-Mail Adressen stimmen nicht überein';	
			}else{
				if(form.subscribe.checked){
					new Ajax.Request('?ctrl=newsletter&act=subscribe', {method: 'post', parameters: {name: form.first_name.value+' '+form.last_name.value, email: form.email.value}});
				}
				form.custom.value = form.anrede.value+';'+form.promocode.value;
				if(form.total.value==0){
					form.action = '?ctrl=createOrder';
				}
				form.submit();				
			}	
		}else{
			$('validation-error-output').innerHTML = 'Dies ist keine gültige E-Mail Adresse';	
		}
	}
	
}

function submitPromocode(form){
	new Ajax.Updater('validation-error-output-right','?ctrl=updatePromocode', {method: 'post', parameters: {code: form.promotioncode.value,name: form.last_name.value, firstname: form.first_name.value, address: form.address1.value, zip: form.zip.value, city: form.city.value, email: form.email.value, phone: form.night_phone_b.value},onComplete: function(transport){if(transport.responseText==" "){location.reload();}else{$('label_promotioncode').addClassName('validation-error');}}});
	
}

function checkEmpty(field){
	if(field.type=='text'||field.type=='textarea'){
		if(field.value.length == 0){
			$('label_'+field.name).className = 'validation-error';
			return field.name;
		}else{
			$('label_'+field.name).className = '';
			return '';
		}
	}
	else if(field.type=='select-one'){
		if(field.options[field.selectedIndex].value.length == 0){
			$('label_'+field.name).className = 'validation-error';
			return field.name;
		}else{
			$('label_'+field.name).className = '';
			return '';
		}
	}
}

function checkEmail(field) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(field.value)) {
		$('label_'+field.name).className = 'validation-error';
		return false;
	}else{
		$('label_'+field.name).className = '';
		return true;
	}	
}

function checkNumbers(field) {
	var filter = /^([0-9]){4}$/;
	if (!filter.test(field.value)) {
		field.className = 'f2 error';
		$('label_'+field.name).className = 'validation-error';
		return false;
	}else{
		field.className = 'f2';
		$('label_'+field.name).className = '';
		return true;
	}	
}

function showMoreArticles(offset, contentId, selAt, parent){
	parentarray = parent.ancestors();
	parentarray[1].hide();
	new Ajax.Updater('articles','?ctrl=showmore', {method: 'post', parameters: {offset: offset, contentId: contentId, selAt: selAt}, insertion: Insertion.Bottom});
	return false;
}

function addArticle(variantId, basePath){
	inID = 'cart';
	inClass = 'inactive';
	new Ajax.Updater('cart','?ctrl=addArticle', {method: 'post', parameters: {variantId: variantId}});
	if ($(inID).hasClassName(inClass)) {	
		$(inID).removeClassName(inClass);
	}
	$(inID).href = basePath+'/cart';
	new Effect.Highlight(inID, { startcolor: '#FF0C7F',
endcolor: '#000000' });
}

function toggleFilter(inID,inLink){
	inClass = 'short';
	if ($(inID).hasClassName(inClass)) {	
		$(inID).removeClassName(inClass);
		inLink.update('– weniger zeigen');
	} else {
		$(inID).addClassName(inClass);
		inLink.update('+ alle zeigen');
	}	
}


function jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function facebookShare(url, title, description, image) {
 title = encodeURIComponent(title);
 description = encodeURIComponent(description);
 window.open("http://www.facebook.com/sharer.php?s=100&p[title]="+title+
             "&p[summary]="+description+"&p[url]="+url+
             "&p[images][0]="+image);
}

function toggleContentSwitchClass(inContentID,inMethod,inSwitchID,inClass){
	Effect.toggle(inContentID, inMethod, { duration: 0.3 });
	toggleClass(inSwitchID,inClass) 
}

function toggleClass(inID,inClass) {
	if ($(inID).hasClassName(inClass)) {	
		$(inID).removeClassName(inClass);
	} else {
		$(inID).addClassName(inClass);
	}	
}

function updateImage(inIndex) {	
	fadeDuration = 0.2;
	appearDuration = 0.5;
	imageSrc = imageList[inIndex];
	containerDimensions = $('bigImageContainer').getDimensions();
	$('bigImageContainer').setStyle({ height:containerDimensions.height+'px'});
	   
	$('bigImage').fade({ duration: fadeDuration, afterFinish: function(){
		$('bigImage').observe("load", function(){
	   		setThumb(inIndex); 
	  		$('bigImage').appear({duration: appearDuration, afterFinish: function(){
		   		$('bigImageContainer').setStyle({ height:'auto'}); 
		   		}
		   	});
		   	$('bigImage').stopObserving('load');  
	   });	
	   $('bigImage').writeAttribute('src', imageSrc); 	   
     } 
	});
   	imageListIndex = inIndex;
   	return false;
}

function setThumb(inIndex) {
	thumbId = "t"+inIndex;
	var ts = $$('.thumbs a.selected');
	ts[0].removeClassName('selected');
	$(thumbId).addClassName('selected');
}


function nextImage() {
	if(imageListIndex == imageList.length-1) {
		setIndex = 0; }
	else {
		setIndex = imageListIndex+1; }
	updateImage(setIndex);
	return false;
}

function prevImage() {
	if(imageListIndex == 0) {
		setIndex = imageList.length-1; }
	else {
		setIndex = imageListIndex-1; }
	updateImage(setIndex);
	return false;
}















function checkNewsPositions(lastIDleft) {
	lal_dim = $(lastIDleft).getDimensions();
	c1_dim = $('column-1').getDimensions();
	c2_dim = $('column-2').getDimensions();
	if (c1_dim.height > c2_dim.height + lal_dim.height + 20) {
		$('column-2').insert($(lastIDleft), { position: 'bottom' });
	}
}

function saveNewsletter(email){
  new Ajax.Updater('newsletter-response', '?ctrl=saveNewsletter', {method: 'post', parameters: {email: email}});
}

function submitenter(searchfield,e)
{
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;
  
  if (keycode == 13)
     {
     searchfield.form.submit();
     return false;
     }
  else
     return true;
}

function resetText(feld)
{
    if(feld.value=="E-Mail")
    {
        feld.value="";
    }
} 

function resizeScreen() {
	imgResize('bg_image');
}

function imgResize(id) {
	windowHeigth = window.innerHeight;
	windowWidth = window.innerWidth;
	$(id).setStyle({ width: 'auto', height: 'auto'});
	imgDimensions = $(id).getDimensions();	
	imgRatio = imgDimensions.height / imgDimensions.width;
	
	imgWidth = windowWidth;	
	imgHeight = Math.round(imgWidth * imgRatio);
	if (imgHeight < windowHeigth) {
		imgHeight = windowHeigth;
		imgWidth = Math.round(imgHeight / imgRatio);
	}
	offsetHeight = (windowHeigth - imgHeight) / 2;
	offsetWidth = (windowWidth - imgWidth) / 2;

	$(id).setStyle({
		width: imgWidth+'px',
		height: imgHeight+'px',
		top: offsetHeight+'px',
		left: offsetWidth+'px'
	});
	
/*
	setImage = $(id);
	Element.setStyle(setImage, { 		
		width: imgWidth+'px',
		height: imgHeight+'px',
		top: offsetHeight+'px',
		left: offsetWidth+'px'
	});
*/
}


function resetText(feld)
{
    if(feld.value=="E-Mail")
    {
        feld.value="";
    }
} 

/*
function preloader(imageArray) {
	var i = 0;
	imageObj = new Image();
	var l = imageArray.length - 1;
	for(i=0; i<=l; i++) {
		imageObj.src=imageArray[i];
	}
}


function updateProductImage(inIndex) {	
	fadeDuration = 0.2;
	appearDuration = 0.7;
	imageName = imageList[inIndex];
	thumbId = "t"+inIndex;
	
	$('productImage').fade({ duration: fadeDuration, afterFinish: function(){
	   $('productImage').observe("load", function(){
	  		setThumb(thumbId); 
		   	$('productImage').appear({duration: appearDuration});  
	   });	
	   $('productImage').writeAttribute('src', ''); 
	   $('productImage').writeAttribute('src', imageName); 	   
	   $('productImage').setStyle({ width: "412px", height:"auto"});   
     } 
	}); 
   	imageListIndex = inIndex;
   	return false;
}


function setThumb(inID) {
	var ts = $$('#thumbs a.selected');
	ts[0].removeClassName('selected');
	$(inID).addClassName('selected');
}
*/

/*
function prevImage() {
	if(imageListIndex == 0) {
		imageListIndex = imageList.length-1; }
	else {
		imageListIndex--; }
	updateBigImage(imageListIndex);
	return false;
}

function nextImage() {
	if(imageListIndex == imageList.length-1) {
		imageListIndex = 0; }
	else {
		imageListIndex++; }
	updateBigImage(imageListIndex);
	return false;
}
*/

/*
function hideImage(inEntryID) {
	new Effect.Fade(inEntryID, {duration:.4, queue: { position: 'end', scope: inEntryID, limit:2 }});
}

function showImage(inEntryID) {
	setTimeout(function(){new Effect.Appear(inEntryID, {duration:.6, queue: { position: 'end', scope: inEntryID, limit:1 }})},700);
}
*/

