//inicializacia stranky

$(document).ready(function(){
	//slideshow
	var slideShow = new Array()
	for(var i=0;i<7;i++) slideShow[i] = "/img/header/0"+(i+1)+".jpg";

	initSlideshow("#header", slideShow, 4000, 1000);
	
	//init fade buttons
	$('#mainmenu li a').css('opacity', 0.7).hover(function(){
		$(this).animate({opacity:'1'}, 200);
	}, function(){
		$(this).animate({opacity:'0.6'}, 200);
	});
	
	//date pickers
	$.datepicker.setDefaults($.datepicker.regional['sk']);
	$('.FDate').datepicker({ dateFormat: 'd. m. yy', showOn: 'both', buttonImage: '/img/calendar.png', buttonImageOnly: true, firstDay:1, minDate:+1, buttonText:'Zadajte dátum'});
	$('#frm\\[sp_zaplatenie_listka\\]').datepicker('option','minDate', '-1y');

	$('#frm\\[datum_odchodu\\]').change(function(){
		var dd = new Date($(this).datepicker('getDate'));
		dd.setDate(dd.getDate()+1);

		$('#frm\\[datum_prichodu\\]').val((dd.getDate())+'. '+(dd.getMonth()+1)+'. '+dd.getFullYear());
	});
	
	$('.peoplebirth select:not([name*="day"])').change(function(){
		var tr=$(this).closest('tr');
		
		var year=tr.find('select[name*="year"] option:selected').val();
		var month=tr.find('select[name*="month"] option:selected').val();
		if (year==0 || month==0) return;
		
		var numdays = 32 - new Date(year, month-1, 32).getDate();
		var dsel = tr.find('select[name*="day"]');
		
		var dselval = dsel.find('option:selected').val();
		
		var opts = '';
		for(var i=0;i<=numdays;i++) {
			opts += '<option value="'+i+'"'+(dselval==i ? ' selected="selected"':'')+'>'+(i==0 ? '--':i)+'</option>';
		}
		dsel.html(opts);
	});

	
	$('div.hint').appendTo(document.body);
	$('.hinticon').hover(function(){
		var dc=$(document);
		var fc=$('#formcontent');
		var fcpos=fc.position();
		var tp=325;
		if (dc.scrollTop()>325) tp=dc.scrollTop()+10;
		$('#'+$(this).attr('id')+'d').css({'left':((fcpos.left+fc.width())-280)+'px', 'top':tp+'px'}).fadeIn('medium');
	},function(){
		$('#'+$(this).attr('id')+'d').fadeOut('medium');
	});
	
	$('a.poisti').click(function(){
		$.modal('<div class="bwcontent" style="padding:10px;">Teraz budete presmerovaný na stránku poisťovne, kde doplníte údaje o poistníkovi / poistených osobách a zaplatíte poistné.<br /><br /><img src="/img/ajax-loader.gif" /><br /></div>');
		var nhref = $(this).attr('href');
		setTimeout(function(){
			window.location.href=nhref;
		}, 5000);
		return false;
	});
});

function initSlideshow(elem, imageList, imageDuration, fadeSpeed, current) {
	var listSize = imageList.length;
	if (!current || current >= listSize) current = 0;
	if (!imageDuration) imageDuration = 5000;
	if (!fadeSpeed) fadeSpeed = 1000;
	$(elem + " img").attr("src", imageList[current]);
	
	if (current == (listSize - 1)) {
		$(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
	} else {
		$(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
	}
	
	$(elem + " img").animate({ opacity: "1" }, imageDuration).animate({ opacity: "0.01" }, fadeSpeed, function(){ 
		$(this).css("opacity", "1");
		initSlideshow(elem, imageList, imageDuration, fadeSpeed, current + 1);
	});

}


