$(function(){

	// admin: control status of displayed
	$('#adminnav span select').change(function(){
																					
		location.href = 'http://'+window.location.host+'/blog/admstat/'+$(this).val();	
		
	});

	// clear day post 
	$('#calender').bind("mouseleave", function(e){
		$('.dayPost').remove();
	});
																					
	// calender posts load
	$('#calender td a').mouseover(function(){
		var url 		= $(this).attr('href');
		var day 		= url.substr(url.lastIndexOf('/')+1);
		var month 	= url.substring(url.indexOf('datum/')+11,url.lastIndexOf('/'));
		var year		= url.substr(url.indexOf('datum/')+6,4);
		var pos			= $('#wrapper').position();
		$('#calender div.dayPost').remove();
		$('<div class="dayPost" id="dayPost'+day+'">').load(
			'/blog/dayPosts/'+year+'/'+month+'/'+day
		).insertAfter(this).bind("mouseleave",function(){
			$(this).remove();
    }).css('left', pos.left+21);
	});
	
	// calender & upcomming redirect
	$('.calenderpost').livequery('click',function(){
		var url = $(this).children('h5').children('a.dpurl').attr('href');
		location.href = url;
	});
	$('#upcommingPosts').children('div').each(function(){
		$(this).click(function(){
			var url = $(this).children('a').attr('href');
			location.href = url;			
		});
	});	

	/* message */
	$('#main div.message, #main div.info').click(function(){
		$(this).hide();
	}).attr({title: 'zatvoriť'});
	$('#main div.message, #main div.info').each(function(){
		if 	(
					($(this).html().indexOf('field')==-1) &&
					($(this).html().indexOf('Ďakujeme')==-1)
				) $(this).fadeOut(5000);
	});

	/* color edit link */
	$('#main div.edit .right').each(function() {
		var editCont = $(this).html();
		if(editCont) { 
			editStat = editCont.substr(editCont.lastIndexOf('Stav: ')+6,2);
			var editColor;
			switch (editStat) {
				case 'on':
					editColor = '#006600';
					break;
				case 'ča':
					editColor = '#CC6600';
					break;
				case 'of':
					editColor = '#CC0000';
					break;
			}
			$(this).css('color',editColor);
			$(this).children().css('color',editColor);
		}
	});
	
	/* color form status */
	$('form select[name=status]').change(function(){
		switch ($(this).val()) {
			case 'on':
				$(this).css('color','#006600');
				break;
			case 'pe':
				$(this).css('color','#CC6600');
				break;
			case 'off':
				$(this).css('color','#CC0000');
				break;
		}
	}).change();

	/* color labels for optional fields */
	$('form label').each(function(){
		var text = $(this).text();
		if (text.indexOf('*')>-1) {
			$(this).css('color','gray');
		}
	});

	/* readony help */
	$('form input.readonly').click(function(){
		alert('Tento údaj sa nedá zmeniť, je read-only!');
	});

	$('#upcommingPosts div').each(function(){
		html = $(this).html();
		if (html.indexOf('• dnes')>0) {
			$(this).find("a").css('color','#4552cc').css('font-weight','bold');
			$(this).css('color','#4552cc');
		}						 
	});
	
	$('#left #calender td > a > strong').each(function(){
		$(this).parent().css('background-color','#4552cc');
	});
	
	/* hide other then first inactive schedules */
	$('.post .head').each(function(){
		var hidden = false;
		$(this).children('.inactive').each(function(i){
			if(i>0) {
				$(this).hide();
				hidden = true;
			}
		});
		if (hidden) {
			$(this).append('<a href="javascript:;" class="showHiddenSchedules" title="zobraz všetky termíny">viac...</a>');
		}
	});	
	$('.showHiddenSchedules').livequery('click',function(event){
		$(this).hide().siblings('.inactive:hidden').show();
	});
	
});


// include ligtbox and set options
document.write('<script type="text\/javascript" src="\/js\/jquery\/jquery_lightbox\/js\/jquery.lightbox.packed.js"></script>');
$(function(){
	$.Lightbox.construct({
		download_link: false,
		show_linkback: false
	});
});
