/* Czech initialisation for the jQuery UI date picker plugin. */
/* Written by Tomas Muller (tomas@tomas-muller.net). */
jQuery(function($) {
    $.datepicker.regional['cs'] = {
        closeText: 'Zavřít',
        prevText: '&#x3c;Dříve',
        nextText: 'Později&#x3e;',
        currentText: 'Nyní',
        monthNames: ['leden', 'únor', 'březen', 'duben', 'květen', 'červen', 'červenec', 'srpen',
            'září', 'říjen', 'listopad', 'prosinec'],
        monthNamesShort: ['led', 'úno', 'bře', 'dub', 'kvě', 'čer', 'čvc', 'srp', 'zář', 'říj', 'lis', 'pro'],
        dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
        dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
        dayNamesMin: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
        weekHeader: 'Týd',
        dateFormat: 'dd. mm. yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''
    };
    $.datepicker.setDefaults($.datepicker.regional['cs']);
    $.datepicker.setDefaults();
});

$(document).ready(function () {
    $("input.date").each(function () { // input[type=date] does not work in IE
        var el = $(this);
        var value = el.val();
        var date = (value ? $.datepicker.parseDate($.datepicker.W3C, value) : null);

        var minDate = el.attr("min") || null;
        if (minDate) minDate = $.datepicker.parseDate($.datepicker.W3C, minDate);
        var maxDate = el.attr("max") || null;
        if (maxDate) maxDate = $.datepicker.parseDate($.datepicker.W3C, maxDate);

        // input.attr("type", "text") throws exception
		
        if (el.attr("type") == "date") {
            var tmp = $("<input/>");
            $.each("class,disabled,id,maxlength,name,readonly,required,size,style,tabindex,title,value".split(","), function(i, attr)  {
                tmp.attr(attr, el.attr(attr));
            });
            el.replaceWith(tmp);
            el = tmp;
        }
        el.datepicker({
            minDate: minDate,
            maxDate: maxDate
        });
        el.val($.datepicker.formatDate(el.datepicker("option", "dateFormat"), date));
    });
});

$(document).ready(function() {    
	var fieldsWidth = 0;
	var fieldWidth = 0;
	var count = 0;
    
	$(".close-yellow").click(function () {
        $("#message-yellow").fadeOut("slow");
    });
    $(".close-red").click(function () {
        $("#message-red").fadeOut("slow");
    });
    $(".close-blue").click(function () {
        $("#message-blue").fadeOut("slow");
    });
    $(".close-green").click(function () {
        $("#message-green").fadeOut("slow");
    });
	
	$('.menu-block').hide();
	$('.js-add-comment').hide();
	$('.inside-filter').hide();
	$('.js-friend-hover-block').hide();
	$('.block-s').hide();
	$('.form-edit').hide();
	$('.form-change-btn').hide();
	$('.js-wall-nothing-else').hide();
	$('a.js-wall-more').find('img.spinner').hide();
	$('.js-changer').hide();
	
	fieldWidth = $('.m-block').width();
	$('.js-sections-menu').children().each(function(){
		if($(this).hasClass('js-block')) {	
			fieldsWidth += $(this).width();
			count += 1;
		}
	});
	
	$("[rel^='prettyPhoto']").prettyPhoto({
		keyboard_shortcuts: false
	});
	
	var move = 0;
	if( fieldsWidth > fieldWidth ) {
		move = ( fieldsWidth - fieldWidth + ( count * 15 ) ) / ( fieldWidth );
	} else {
		var center = ( fieldWidth - fieldsWidth - ( count * 15 ) ) / 2;
		$('.js-sections-menu').animate({
			'left': center+'px'
		}, 0);	
	}
	
	$('.js-show-add-comment').live('click',function(){
		$(this).hide(0);
		$('.js-add-comment').show('slow');
	});
	
	$('.more-less').jTruncate({  
        length: 390,  
        minTrail: 0,  
        //moreText: '<span class="section-color-1 right">[celý komentář]</span>',  
        moreText: '[celý komentář]',  
        //lessText: "<span class='section-color-1 right'>[skrýt]</span>",    
        lessText: "[skrýt]",    
        moreAni: "fast",  
        lessAni: "fast"  
    }); 
	
	$('.m-block').mousemove(function(e){
		var x = e.pageX - this.offsetLeft;
		if( move ) {
			var pos = -1 * x * move;

			$('.js-sections-menu').animate({
				'left': pos+'px'
			}, 0);		
		}
	});
	
	$('body').click(function() {
		$('.inside-filter').each(function(){
			if( $(this).is(':visible')) {
				$(this).stop(true,true).slideToggle(250).prev('h4').css('background-position','0 0px');
			}
		});
	});

	$('.filter').click(function(event){
		event.stopPropagation();
	});
	
	$('.filter').find('h4').click(function(event){
	   event.stopPropagation();
	   if( $(this).next('.inside-filter').is(':visible') ) {
		   $(this).css('background-position','0 0px');
	   } else {
		   $(this).css('background-position','0 -21px');		   
	   }
	   $(this).next('.inside-filter').stop(true,true).animate({
		   'height': 'toggle',
		   'opacity': 1
	   },200,function(){
		   if( !$(this).is(':visible') ) {
			   $(this).prev('h4').css('background-position','0 0px');
		   } else {
			   $(this).prev('h4').css('background-position','0 -21px');		   
		   }
	   });
	   
	});
	
	/*$('.filter').hover(
		function(){
			$(this).find('.inside-filter').stop(true,true).slideToggle('slow');
		},function(){
			$(this).find('.inside-filter').stop(true,true).hide();
	});*/
	
	$('li.menu').hover(
		function(){
			$('.menu-block').stop(true, true).hide();
			var name = '#menu-block-'+(this).id;
			$(name).stop(true,true).show(0);
		},function(){
			var name = '#menu-block-'+(this).id;
			$(name).hide();
	});
	
	//CHATER
	var chaterMsgs = 0;
	var chaterPos = 0;
	var chaterMove = 1;
	
	var timeout;
	
	$(document).mouseup(function(){
		clearInterval(timeout);
	});
	
	$('.js-chater-msgs').find('.js-msg').each(function(){
		chaterMsgs += $(this).height();
	});
	
	$('.js-chater-down').live('scrolldown',function(event){
		if( ( chaterMsgs + chaterPos - chaterMove ) > 0 )
		{
			chaterPos -= chaterMove;
		}
		$('.js-chater-msgs').css('margin-top',chaterPos);
	});
	
	$('.js-chater-down').live('mousedown',function(event){
		event.preventDefault();
		timeout = setInterval(function(){
			if( ( chaterMsgs + chaterPos - chaterMove ) > 0 )
			{
				chaterPos -= chaterMove;
			}
			$('.js-chater-msgs').css('margin-top',chaterPos);
			
		}, 15);
	});
	
	$('.js-chater-up').live('mousedown',function(event){
		event.preventDefault();
		timeout = setInterval(function(){
			if( chaterPos + chaterMove <= 0 )
			{
				chaterPos += chaterMove;
			}
			$('.js-chater-msgs').css('margin-top',chaterPos);
		}, 15);
	});
	
	/*
	 *
	setInterval(function(){
		$('#js-refresh').each(function(){
			var link = $(this).attr('href');
			$.getJSON(link);			
		});
	},1000 * 30);
	 */

	// settings
	
	$('.clicker').live('click',function(event){
		event.preventDefault();
		var name = '#block-'+this.id;
		var id = this;
		
		$(name).stop(true,true).slideToggle('slow', function(){
			if( $(this).is(':visible') ) {
				$(id).css('background-position','right -43px');
			} else {				
				$(id).css('background-position','right 2px');
			}
		});
	});
	
	
	$('a.form-change').live('click',function(event){
		event.preventDefault();
		$(this).prev().prev().hide();
		$(this).prev('.form-edit').show();
		$(this).hide();
		if( $('.form-change-btn').is(':hidden') ) {
			$('.form-change-btn').show();
		}
	});

	//wall
	var msg = 'Vyhledat uživatele';
	var ofs = 10;
	var sender = true;
	
	$('input.wall-input').val(msg);
	$('input.wall-input').css('color','#8c8c8c');
	
	$('input.wall-input').focus( function() {
		if( $(this).val() == msg) {
			$(this).val('');
			$(this).css('color','#000');
		}
	}).blur( function() {
		if( $(this).val() == '') {
			$(this).val(msg);
			$(this).css('color','#8c8c8c');
		}
	});
	
	$('a.js-wall-more').live('click',function(event){
		event.preventDefault();
		if( sender ) {
			sender = false;
			block = this;
			var hr = this.href + '&ofs=' + ofs;
			$(this).find('img.spinner').show();

			/*$.getJSON(hr,function(data){
				alert('hi');
			});
			*/
			$.ajax({
				type: "GET",
				url: hr,
				dataType: "html",
				success: function(html) {
					if(!html) {
						$('a.js-wall-more').hide();
						$('.js-wall-nothing-else').show(100);
					} else {	
						$('table.js-wall-stats').hide();
						$('table.js-wall-stats tr:last').after(html);
						$('table.js-wall-stats').fadeIn(1000);
						$(block).find('img.spinner').hide();
						ofs += 10;
						sender = true;
					}
				},
				error: function(x,y,z) {
					alert('Ups!! Něco se porouchalo.');
					sender = false;
				}
			});
		}
	});
	
	//friends miniblock
	
	
	
	$('.js-friend-hover').hover(function(){
	    $(this).find('.js-friend-hover-block').stop(true,true).animate({
		
		opacity: 'toggle'
	    },{
		duration: 250
	    });
	},function(){
	    $(this).find('.js-friend-hover-block').stop(true,true).animate({
		
		opacity: 'toggle'
	    },{
		duration: 250
	    });
	});
	
	$('.js-change').live('click',function(elem){
	    elem.preventDefault();
	    var field = '#js-changer-'+this.rel;
	    var desc = '#js-title-change-'+this.rel;
	    
	    $(field).show();
	    $(desc).hide();
	});
	
	$('.js-ip-show').live('click',function(elem){
		elem.preventDefault();
		jAlert($(this).text(),'IP adresa');
	});
	
	$('a.js-show-next').live('click',function(elem){
		elem.preventDefault();
		var anch = this;
		if($(this).text() == '[zobrazit]') {
			$(this).parent().next('div').stop(true,true).show('fast',function(){
				$(anch).text('[skrýt]');
			});
		} else {
			$(this).parent().next('div').stop(true,true).hide('fast',function(){
				$(anch).text('[zobrazit]');
			});
		}
	});
	
	$('h1.invite-btn').live('click',function(){
		$(this).hide();
		$('div.invite-block').show(100);
	});
	
	$('a.js-icon').live('click',function(elem){
		elem.preventDefault();
		jAlert($(this).find('span.text').text(), $(this).find('span.title').text());
	});
	
	
	// comments for status
	var default_visible = 5;
	var cm_more = 'více';
	var cm_less = 'méně';
	
	$('div.small-comments-container').each(function(){
		var count = $(this).find('div.small-comment').length;
		$(this).find('div.small-comment').slice( default_visible ).hide();
		if( count > default_visible ) {
			$(this).append('<a href="#" class="js-more-cm cl-white more-block">' + cm_more + '(' + count + ')</a>');
		}
	});
	
	$('a.js-more-cm').live('click',function(elem){
		elem.preventDefault();
		var href = this;
		var count = $(this).parent().find('div.small-comment').length;
		if($(this).text() != cm_less) {	
			$(this).parent().find('div.small-comment').filter(':hidden').show(100,function(){
				$(href).text(cm_less);
			});
		} else {
			$(this).parent().find('div.small-comment').slice( default_visible ).hide(100,function(){
				$(href).text(cm_more + '(' + count + ')');
			});
			
		}
	});
	
	$('a.show-list').live('click', function(elem){
		elem.preventDefault();
		$(this).hide();
		$('.player-list').show(100);
	});
	
	$('.tooltip').tooltip({
		track: true,
		delay: 0,
		fixPNG: true, 
		showURL: false,
		showBody: " - ",
		top: -35,
		left: 5
	});
	
	$('.clshower').live('click', function(elem){
		elem.preventDefault();
		var id = '.' + this.id;
		if ( $(id).is(':hidden') ) {
			$(id).stop(true, true).show();
		} else {
			$(id).stop(true, true).hide();
		}
	});
	
	$('.js-hover-fadmin a.prevent').live('click', function(elem){
		elem.preventDefault();
	});
	
	$('.js-hover-fadmin').hover(function(){
		$(this).find('.fadmin-box').show(50);
	}, function(){
		$(this).find('.fadmin-box').hide(50);
	});
	
	var showText = 'Označit jako přečtené';
	var hideText = 'Zrušit označení';
	
	$('a.show-readed').live('click', function(elem){
	    elem.preventDefault();
		$(this).removeClass('show-readed');
		$(this).text(hideText);
		$(this).addClass('hide-readed');
	    $('.readed-' + this.id).show();
	});
	
	$('a.hide-readed').live('click', function(elem){
	    elem.preventDefault();
		$(this).removeClass('hide-readed');
		$(this).text(showText);
		$(this).addClass('show-readed')
	    $('.readed-' + this.id).hide();
	});
	
	$('.js-checkme').hover(function(){
		$(this).stop(true,true).animate({
			backgroundColor: "#cdcdcd"
		}, 50);
	}, function(){
		if($(this).hasClass('server-hovered')) {
			$(this).stop(true,true).animate({
				backgroundColor: "#efefef"
			}, 2000);
		} else {
			$(this).stop(true,true).animate({
				backgroundColor: "#fff"
			}, 2000);
		}
	});
	
	$('.imgtooltip').tooltip({
		delay: 0,
		showURL: false,
		extraClass: "imgtooltip",
		top: -55,
		left: -55,
		bodyHandler: function() { 
			return $("<img/>").attr("src", $(this).attr('src')); 
		} 
	});
	
	var editClicked = false;
	var lid = 0;
	var href = '';
	var pattern = '0edit00Pattern00Replacement00';
	
	$('textarea:input.area-vip').live('keypress', function(event){
		if( event.keyCode == 13 ) {
			event.stopPropagation();
			event.preventDefault();
			
			href = lid.replace(pattern, $(this).val());
			var parent = $(this).parent();
			
			if($(this).val() != '') {
				$.ajax({
				  url: href,
				  dataType: 'json',
				  success: function(data) {
					  $(parent).find('span.vip-editor').text(data.text).show();
					  $(parent).find('textarea').hide();
					  lid = 0;
					  editClicked = false;
				  }
				});
			} else {
				jAlert('Váš příspěvek nesmí být prázdný!')
			}
			
		}
	});

	$('span.vip-edit').live('click', function(event) {
		event.stopPropagation();
		
		if (!editClicked) {
			editClicked = true;
			lid = $(this).attr('id');
			$(this).find('span.vip-editor').hide();
			var oldText = $(this).find('span.vip-editor').text();
			$(this).append('<textarea class="area-vip" rows="6" cols="45">' + oldText + '</textarea>');
		}
	});
	
	$('html').live('click', function(event) {
		if(editClicked) {
			$('span.vip-editor').show();
			$('.area-vip').remove();
			editClicked = false;
			
			lid = 0;
		}
 	});
	
});


