var currentPosition = 0;
var numberOfSlides = 0;
var slideWidth = 334;

function scrollNext() {
	// Determine new position
	currentPosition = currentPosition+1;

	// Move slideInner using margin-left
	if(currentPosition == (numberOfSlides - 2)){
		currentPosition = 0;
	}
	$('#slideInner').animate({
		'marginLeft' : slideWidth*(-currentPosition)
	});
};

// manageControls: Hides and Shows controls depending on currentPosition
function manageControls(position){
	$('#leftControl').show()
	$('#rightControl').show()
}
	
// stopControls: Hides controls ans stops animation
function stopControls(){
	$('#leftControl').hide()
	$('#rightControl').hide()
	clearInterval (timeout);
}

function scroller(perpage, total) {
	var slides = $('.slide');
	var pause = 8000;
	var timeout;
	numberOfSlides = slides.length;
	
	// Remove scrollbar in JS
	$('#slidesContainer').css('overflow', 'hidden');
	
	// Wrap all .slides with #slideInner div
	slides
		.wrapAll('<div id="slideInner"></div>')
	// Float left to display horizontally, readjust .slides width
		.css({
			'float' : 'left',
			'width' : slideWidth
		});
	
	// Set #slideInner width equal to total width of all slides
	$('#slideInner').css('width', slideWidth * numberOfSlides);

	if (total > perpage){
		// Insert controls in the DOM
		$('#slideshow')
			.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
			.append('<span class="control" id="rightControl">Clicking moves right</span>');
		
		// Hide left arrow control on first load
		manageControls(currentPosition);
		timeout = setInterval( scrollNext ,pause );

		// Create event listeners for .controls clicks
		$('.control')
			.bind('click', function(){
				clearInterval (timeout);
				// Determine new position
				currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

				// Move slideInner using margin-left
				if(currentPosition == (numberOfSlides - 2)){
					currentPosition = numberOfSlides-3;
				} else if(currentPosition == -1){
					currentPosition = 0;
				} else {
					$('#slideInner').animate({
						'marginLeft' : slideWidth*(-currentPosition)
					});
				}

				timeout = setInterval( scrollNext ,pause );
			});
	};
}

var current='a1';

function switchid(id){	
	hidediv(current);
	showdiv(id);
	current = id;
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function cnv (cr){
	bs = parseFloat(document.forms['fr_cnv'][cr].value);

	if (cr=='MDL') mdl = bs;
	else mdl = Math.round(10000 * bs * parseFloat(document.forms['fr_cnv']['hd_'+cr].value)) / 10000;
	
	rol = Math.round(10000 * mdl / parseFloat(document.forms['fr_cnv']['hd_ROL'].value)) / 10000;
	eur = Math.round(10000 * mdl / parseFloat(document.forms['fr_cnv']['hd_EUR'].value)) / 10000;
	usd = Math.round(10000 * mdl / parseFloat(document.forms['fr_cnv']['hd_USD'].value)) / 10000;
	uah = Math.round(10000 * mdl / parseFloat(document.forms['fr_cnv']['hd_UAH'].value)) / 10000;
	rub = Math.round(10000 * mdl / parseFloat(document.forms['fr_cnv']['hd_RUB'].value)) / 10000;

	if (!isNaN(rol) && cr !='ROL') document.forms['fr_cnv']['ROL'].value = rol;
	if (!isNaN(eur) && cr !='EUR') document.forms['fr_cnv']['EUR'].value = eur;
	if (!isNaN(usd) && cr !='USD') document.forms['fr_cnv']['USD'].value = usd;
	if (!isNaN(uah) && cr !='UAH') document.forms['fr_cnv']['UAH'].value = uah;
	if (!isNaN(rub) && cr !='RUB') document.forms['fr_cnv']['RUB'].value = rub;
	if (!isNaN(mdl) && cr !='MDL') document.forms['fr_cnv']['MDL'].value = mdl;
}
