var currentPos = 0;

function scrollArtistsUp(element) { scrollElementUp(element, 19, function() { } ); }
function scrollArtistsDown(element) { scrollElementDown(element, 19, function() { } ); }

function scrollTextUp(element) { scrollElementUp(element, 64, function() { } ); }
function scrollTextDown(element) { scrollElementDown(element, 64, function() { } ); }

function scrollTracksUp(element) { scrollElementUp(element, 19, function() { currentPos -= 1; } ); }
function scrollTracksDown(element) { scrollElementDown(element, 19, function() { currentPos += 1; hideListenButtons(); } ); }


function scrollElementUp(element, step, callback) {		 
	offset = Element.getStyle(element, 'top');
	offset = offset == null ? 0 : parseInt(offset) * (-1);
	if (offset > 0)
		new Effect.Move(element, { x: 0, y: step, transition: Effect.Transitions.sinoidal, queue: { position: 'end', scope: 'global', limit: 1 }, afterFinish: function() { callback(); } });
}

function scrollElementDown(element, step, callback) {	
	offset = Element.getStyle(element, 'top');
	offset = offset == null ? 0 : parseInt(offset) * (-1);	
	parentHeight = $($(element).parentNode).getHeight();
	maxHeight = parseInt(Element.getDimensions(element).height);
	
	if (offset < maxHeight - parentHeight)
		new Effect.Move(element, { x: 0, y: (-1) * step, transition: Effect.Transitions.sinoidal, queue: { position: 'end', scope: 'global', limit: 1 } , afterFinish: function() { callback(); } });
	else {
		new Effect.Move(element, { x: 0, y: offset, transition: Effect.Transitions.sinoidal, queue: { position: 'end', scope: 'global', limit: 1 }, afterFinish: function() { callback(); } });		
	}
}

/* fix bug on Safari & Firefox */

Event.onDOMReady(function() { 
	$$("div#wrapper-tracks div.listen").each(function(listen) {
		// Element.show(listen);
	});
});

function hideListenButtons() {
	$$("div#wrapper-tracks li").each(function(tag) {
		id = parseInt(tag.id.split("_")[1]);		
		
		if (id < currentPos || id > currentPos + 4)
			$$("#track_" + id + " div.listen").each(function(listen) { Element.hide(listen); });
		else
			$$("#track_" + id + " div.listen").each(function(listen) { Element.show(listen); });			
	});
}

/* === CATALOG DETAIL === */

function validateCatalogDetailForm() {
	var message = "";

	if ($('product-type').value.length == 0) message += "\nYou must choose a product type.";
	if ($('product-qty').value.length == 0) message += "\nYou must choose a quantity."
		
	if (message.length != 0)
		alert("Sorry, we can not handle your request:" + message);
	
	return message.length == 0
}

function refreshQtyList(product_type) {
	switch(product_type) {
		case "":
		case "lp":
		case "cd":
			$('product-qty').show();
			$('product-qty-for-mp3').hide();			
			break
		default:
			// mp3
			$('product-qty').hide();
			$('product-qty-for-mp3').show();	
	}	
}
