//sound control
function playSound(xtra) {
	file = (xtra) ? 'sound-' + xtra + '.swf' : 'sound.swf';
	parent.playerFrame.location = _path + 'modules/flash.php?file=' + _path + 'images/' + file;
	new Ajax.Request(_path +'php/perform.php?perform=playSound',  {});
}

function stopSound() {
	parent.playerFrame.location = 'about:blank';
	new Ajax.Request(_path +'php/perform.php?perform=stopSound',  {});
}


//toggle subnavi
function toggleItemInfo(id_item, id_list) {
	var item = $(id_item);
	var current = item.style.display;
	var cat = $(id_list);
	
	
	if(current == 'none') {
		new Effect.BlindDown(id_item);		
		cat.addClassName('open');
	}	
	else {
		new Effect.BlindUp(id_item);
		cat.removeClassName('open')
	}
	return true;
}



//advanced slider for product images
function advancedSlide(mode, selector) {
	
	var arrSlides = $$(selector);
	
	var numSlides = arrSlides.length;
	var i = 0;

	arrSlides.each(function(n){

		i++;

		if(n.visible()) {
			active = n;
			currentNumber = i;
		}
	});

	if(mode == 'next') {

		if(currentNumber == numSlides) {
			nextSlide = arrSlides[0];
		} else {
			nextSlide = arrSlides[currentNumber];
		}

	} else {

		if(currentNumber == 1) {
			nextSlide = arrSlides[numSlides-1];
		} else {
			nextSlide = arrSlides[currentNumber-2];
		}

	}

	do_slide(active, nextSlide);


	return false;

}

function do_slide(active, next) {
	
	new Effect.Fade(active, {
		afterFinish: function() {
			new Effect.Appear(next);
		}
	});
	
}



//CHECKOUT

function updatePrice(qty, id, price) {
	$('price-' + id).update(number_format((price * qty), 2, true));
	new Ajax.Request(_path +'php/perform.php?perform=update&id='+id + '&qty=' + qty,  {});
	updateTotal();
}

function updateShipping(price) {
	newprice = number_format(price, 2, true);
	$('price-shipping').update(newprice);
	new Ajax.Request(_path +'php/perform.php?perform=updateShipping&shipping=' + price,  {});
	updateTotal();
}


function updateTotal() {
	
	new Ajax.Request(_path +'php/perform.php?perform=getTotal',  {
		
		onSuccess: function(transport) {
			if(transport.responseText) {
				$('price-total').update(transport.responseText);
			}
		}
		
	});
	
}


//format number ...
function number_format(zahl, k, fix) {

	if(!k) k = 0;
	var neu = '';
	
	var f = Math.pow(10, k);
	zahl = '' + parseInt( zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;
	
	var idx = zahl.indexOf('.');
	if(fix) {
		zahl += (idx == -1 ? '.' : '' )
		+ f.toString().substring(1);
	}
	
	idx = zahl.indexOf('.');
	if( idx == -1) idx = zahl.length;
	else neu = ',' + zahl.substr(idx + 1, k);
	
	while(idx > 0) {
	
		if(idx - 3 > 0)neu = '.' + zahl.substring( idx - 3, idx) + neu;
		else neu = zahl.substring(0, idx) + neu;
		idx -= 3;
	}
	
	return neu;
}


//check form
function checkForm(id_form) {
	

	var fElms = $(id_form).getElements();
	var errorMessage = '';
	var texterror = false;

	for(i=0; i < fElms.length; i++) {
		if(fElms[i].getAttribute('rel')) {
			var checktype	= fElms[i].getAttribute('rel');
			var checkvalue	= fElms[i].value;
			
			switch (checktype) {
				
				case 'check':
					if($('terms').checked == false) {
						errorMessage += 'Bitte lesen und akzeptieren Sie unsere AGB!'+"\n\n";
					}
					break;
					
				case 'integer':
				if(isNaN(checkvalue) || checkvalue == '') {
					if(fElms[i].getAttribute('type')=='hidden') {
						errorMessage += 'Da läft etwas falsch!'+"\n";
					}
					else {
						if(checkvalue) errorMessage += '"'+checkvalue+'" ist keine Zahl!'+"\n\n";
					}
				}
				break;
					
				case 'text':
					if(checkvalue == '' && !texterror) {
						var texterror = true;
						errorMessage += 'Ihre Angaben sind unvollständig. Einfach nochmal überprüfen, dann geht`s sofort weiter'+"\n\n";
					}
					break;
					
				case 'email':
					var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if(!filter.test(checkvalue)) {
						errorMessage += 'Die Emailadresse stimmt so nicht'+"\n\n";
					}
					break;
			}
		}
	}
	
	if(errorMessage!='') {
		alert(errorMessage);
		return false;
	}
	
	return true;
}


///gallery slider
var coverSlideIntervalIdentifier = -1;
var coverSlidePosition = 1;

function coverSlide(direction, numberOfImages) {
	if(direction == "right") {
		coverSlidePosition++;
	}
	if(direction == "left") {
		coverSlidePosition--;
	}
	if(coverSlideIntervalIdentifier == -1) coverSlideIntervalIdentifier = window.setInterval("coverSlideInterval("+numberOfImages+")", 40);
}

function coverSlideInterval(numberOfImages) {
	var element = document.getElementById("coverSliderContainer");
	var actualPosition = element.offsetLeft;
	var pictureWidth = 140;
	var stepSize = 10;
	var targetPosition = (coverSlidePosition - 1) * pictureWidth;
	
	if(Math.max(actualPosition, targetPosition) - Math.min(actualPosition, targetPosition) <= stepSize) {
		window.clearInterval(coverSlideIntervalIdentifier);
		coverSlideIntervalIdentifier = -1;
		element.style.left =  "0px";
	}
	if(actualPosition > targetPosition - targetPosition*2) {
		element.style.left = (actualPosition - stepSize) + "px";
	}
	else if(actualPosition < targetPosition - targetPosition*2) {
		element.style.left = (actualPosition + stepSize) + "px";
	}
	if(actualPosition < (numberOfImages * pictureWidth) - (numberOfImages * pictureWidth) * 2) {
		actualPosition = actualPosition - ((numberOfImages * pictureWidth) - (numberOfImages * pictureWidth) * 2);
		element.style.left = actualPosition + "px";
		coverSlidePosition -= numberOfImages;
	}
	if(actualPosition > 0) {
		tempActualPosition = actualPosition
		actualPosition = (numberOfImages * pictureWidth) - (numberOfImages * pictureWidth) * 2;
		element.style.left = (actualPosition + tempActualPosition) + "px";
		coverSlidePosition += numberOfImages;
	}
	
}
