// fisierul de functii javascript pentru un produs
$('document').ready(function() {
	$('#produse').click(function() {
		$(this).addClass('active');
		$('#detalii').removeClass('active');
		$('#prezentare').removeClass('active');
		$('#text-produse').show();
		$('#text-detalii').hide();
		$('#text-prezentare').hide();
	});
	$('#detalii').click(function() {
		$(this).addClass('active');
		$('#produse').removeClass('active');
		$('#prezentare').removeClass('active');
		$('#text-produse').hide();
		$('#text-detalii').show();
		$('#text-prezentare').hide();
	});
	$('#prezentare').click(function() {
		$(this).addClass('active');
		$('#produse').removeClass('active');
		$('#detalii').removeClass('active');
		$('#text-produse').hide();
		$('#text-detalii').hide();
		$('#text-prezentare').show();
	});
	
	// cand trimit in cos verific caracteristica
	$('#adaugaInCos').click(function() {
		var status = '';
		
		if ($('#stoc').val() == '') {
			status += 'Alege o combinatie de caracteristici!';
		}
		if ($('#stoc').val() == -1) {
			status += 'Produsul nu este in stoc si nu poate fi comandat!';
		}
		if (status.length) {
			alert(status);
			return false;
		} else {
			$('#formCos').submit();
		}
	});

	// launch thickbox from main picture
	$('a[id^="pozathumb"]').click(function() {
		var x = $(this).attr('id').substr(9);
		$('#poza-'+x).click();
		return false;
	});

	//change pictures from thumbs
	$('a[id^="thumb"]').click(function() {
		var source = $(this).children('img').attr('src');
		link = source.replace('thumb/', 'mare/');
		source = source.replace('thumb/', '');
		$('a[id^="thumb"]').removeClass('activ');
		$(this).addClass('activ');
		$('.productImage').attr('href', link);
		$('.productImage').attr('id', 'poza'+$(this).attr('id'));
		$('.productImage img').attr('src', source);
	});
	
	// set stoc
	$('div[id^="stocuri-"]').click(function() {
		var stuff = $(this).attr('id');
		stuff = stuff.split('-');
		
		switch (stuff[2]) {
			case 'stoc': var stocText = "Produs in stoc."; break;
			case 'comanda': var stocText = "<strong>Disponibil numai in baza comenzii, se livreaza in 30 de zile de la achitarea unui avans de 50% din valoarea produsului</strong>"; break;
			case 'lipsa': var stocText = 'Produsul nu este in stoc.'; break;
		}
		$('#stocText').html(stocText);
		if (stuff[2] == 'lipsa') {
			return false;
		}
		$('#stoc').attr('value', stuff[1]);
		$('div[id^="stocuri-"]').each (function() {
			var stuff = $(this).attr('id');
			stuff = stuff.split('-');
			if (stuff[2] != 'lipsa') {
				$(this).addClass('in_stoc');
			}
		});
		$('div[id^="stocuri-"]').removeClass('activ');
		$(this).removeClass('in_stoc');
		$(this).addClass('activ');
		
		
		//pun noul pret
		var pretCaracteristica = parseFloat($(this).attr('pret'));
		$('input[id^="acc"]').each(function() {
	 		if($(this).attr('checked')) {
	 			pretCaracteristica += parseFloat($(this).attr('pret'));
	 		}
		});
		pret = pretCaracteristica;
		document.getElementById('pretProdus').innerHTML = pretCaracteristica.toFixed(2)+' '+currency;		
	});
	
	
	
	$('#currency').change(function() {
		var currentLocation = window.location;
		currentLocation = String(currentLocation);
		currentLocation = currentLocation.split('|');
		currentLocation = currentLocation[0].split('%7C');		
		var newLocation = currentLocation[0]+'|'+$(this).val();
		window.location = newLocation;
	});
});

function schimbaPret(accesoriu, pretAccesoriu) {
	if(!accesoriu.checked) {
		pretAccesoriu = - pretAccesoriu;
	}
	pret += pretAccesoriu;
	
	//atentie ca nu pastreaza afisarea de float
	document.getElementById('pretProdus').innerHTML = pret.toFixed(2)+' '+currency;
}
