
$(document).ready(function(){
	
	// zakladki
	$('#ybInfoBox ul.infoNav a').bind('click', function(){
		var tabPos = $(this).parent('li').prevAll('li').size();
		$(this).parent('li').addClass('current').siblings().removeClass('current');
		$('#ybInfoBox div.infoText').addClass('hide').eq(tabPos).removeClass('hide');
		$('#ybInfoBox').removeClass('pic0').removeClass('pic1').removeClass('pic2').addClass('pic'+tabPos);
		return false;		
	});
	
	// CUFON
	if(typeof Cufon != 'undefined') {
		Cufon.replace('h2.cufonReplace, h3.cufonReplace, .cufonLink, .article h2, h2.cufonReplace2, div.galleryPage h2');
	}
	
	// form kontaktowy 
	$('#contactSetSubject').bind('change', function(){
		var c = $(this).val();
		$('#fields-offer, #fields-archive, #fields-advert').addClass('hide');
		if(c != 'other') {
			$('#fields-'+c).removeClass('hide');
		}
	});
	
	// fancybox
	if($.fn.fancybox) {
		// galeria
		$('ul.galleryList li a').fancybox();
		// archiwum
		$('ul.archSampleList li a').fancybox();
	}
	
	// kalkulator - zaznaczanie modułów
	$('#formCalc input:radio').bind('change', function(){
		$('ul.calcModule label').removeClass('selected');
		$(this).prev('label').toggleClass('selected', $(this).is(':seletcted'));
		$('#rfModule').val($(this).val());
	});
	
	// kalkulator - oblicz
	$('#formCalc').bind('submit', function(){	
		calc.setValues();
		return false;
	});
	
});

var calc = {
	
	// results
	
	priceTotal:			0,
	pricePerStudent:	0,
	
	// variables
	studentsQty:		0,
	studentsPerPage:	1,
	studentsPagesQty:	0,
	additionalPagesQty:	0,
	totalPagesQty:		0,
	editionQty:			0,
	
	// constants
	
	priceDelivery:		0,
	pricePerPage:		0.24,		
	pricePreparation:	40.667,
	priceCover:			10.736,
	
	// logic
	
	setValues: function() {
		calc.studentsQty 		= parseInt($('#fMaxStudents').val(),10) || 0;
		calc.studentsPerPage	= parseInt($('input[name="module"]:checked').val(),10) || 1;
		calc.studentsPagesQty	= Math.ceil(calc.studentsQty / calc.studentsPerPage);
		calc.additionalPagesQty	= parseInt($('#fPagesAdditional').val(),10) || 0;
		calc.totalPagesQty		= calc.studentsPagesQty + calc.additionalPagesQty;
		calc.editionQty			= parseInt($('#fNaklad').val(),10) || calc.studentsQty;
				
		$('#fMaxStudents, #rfMaxStudents').val(calc.studentsQty);
		$('#fPagesAdditional, #rfPagesAdditional').val(calc.additionalPagesQty);
		$('#fNaklad, #rfNaklad').val(calc.editionQty);
		$('#fPagesIndividualQty, #rfPagesIndividualQty').val(calc.studentsPagesQty);
		$('#fPagesTotalQty, #rfPagesTotalQty').val(calc.totalPagesQty);
		
		$('#rfDelivery').val($('#fDelivery').val());
		
		$('#pagesIndividualQty').text(calc.studentsPagesQty);
		$('#pagesTotalQty').text(calc.totalPagesQty);
		
		var pt = (calc.totalPagesQty * calc.pricePerPage * calc.editionQty) + 
		(calc.pricePreparation * calc.totalPagesQty) + 
		(calc.priceCover * calc.editionQty) + calc.priceDelivery; 
		// pt = calc.round(pt);
		calc.priceTotal = Math.ceil(pt);
		
		$('#priceTotal').text(calc.priceTotal+'zł');
		$('#fPriceTotal, #rfPriceTotal').val(calc.priceTotal);
		
		if(calc.priceTotal > 0) {
			var ps = calc.priceTotal / calc.editionQty;
			// ps = calc.round(ps);
			calc.pricePerStudent = Math.ceil(ps);
		} 
			
		$('#priceItem').text(calc.pricePerStudent+'zł');
		$('#fPriceItem, #rfPriceItem').val(calc.pricePerStudent);
	},
	
	round: function(n) {
		return Math.round(n*100)/100;
	}
};
