var noticeboard_page;

var noticeboard_waiting = '<span class="ajax-waiting"></span>';

var noticeboard_msgA = '<p class="noteinfo"><span class="author"><a href="/skaut-chat/seznamka/';
var noticeboard_msgB = '" class="mpTrigger" rel="';
var noticeboard_msgB2 = '">';
var noticeboard_msgC = '</a></span> <span class="date">';
var noticeboard_msgD = '</span>';
var noticeboard_msgE = '</p><p class="note">';
var noticeboard_msgF = '</p>';
var noticeboard_msgD2 = '<a class="delete" rel="';
var noticeboard_msgD3 = '" title="smazat příspěvek"><span>smazat</span></a>';



	function updateNotices(data){
		//console.log(data);
		$('#notices-hp .inbox').html('');
		for( var key in data){
			if((key >= 0) && (key <= 7)){
				var x = key *1 +1;
				var msgText = noticeboard_msgA + data[key].menuname +
					noticeboard_msgB + data[key].uid +
					noticeboard_msgB2 + data[key].nick +

					noticeboard_msgC + data[key].time + noticeboard_msgD;
				if($('#noticeboard_admin').val() == '1'){
					msgText = msgText + noticeboard_msgD2 + data[key].id + noticeboard_msgD3;
				}
				msgText = msgText + noticeboard_msgE
					 + data[key].text +
					noticeboard_msgF;

				$('#note' + x).html(msgText);
			}

		}
		$('#notices-hp .nth').html(data.pages.actual_page);
		$('#notices-hp .tot').html(data.pages.max_page);

		$('#notices-hp .delete').click(function(){
			event.preventDefault();
			$('#notices-hp .inbox').html(noticeboard_waiting);
			deleteNotice($(this).attr("rel"));
		});
	}

	function deleteNotice(nid){
		if(confirm('Opravdu chcete tento vzkaz smazat?')){
		$.ajax({
			type: 'POST',
			url: '/skaut-chat/vzkazovnik/?ajax=1',
			data: 'wrajaxfunction=delete_notice&notice_id=' + nid,
			dataType: 'json',
			cache: 'false',
			success: function(data){
				if(data.status == 'err'){
					$('#hp-smallnote-field .ajax-message').html(data.message);
					$('#hp-smallnote-field .ajax-message').show();
					setTimeout(function(){
						$('#hp-smallnote-field .ajax-message').fadeOut(2000);
					}, 4000);
				}
				loadNotices();
			}
		});
		}else{loadNotices();}
	}

	function loadNotices(){
		$.ajax({
			  type: 'POST',
			  url: '/skaut-chat/vzkazovnik/?ajax=1',
			  data: 'wrajaxfunction=show_noticeboard&wrajaxsmarty=../custom/component/noticeboard&wrajaxargs[0]=' + noticeboard_page,
			  dataType: 'json',
			  cache: 'false',
			  success: function(data){
				  updateNotices(data);
			  }
		  });
	}

	function addNotice(){
		noticeboard_page=1;
		var params = 'wrajaxfunction=add_notice&notice_text=' + $('#smallnote-input').val();

		$.ajax({
			type: 'POST',
			url: '/skaut-chat/vzkazovnik/?ajax=1',
			data: params,
			dataType: 'json',
			cache: 'false',
			success: function(data){
				if(data.status == 'err'){
					$('#hp-smallnote-field .ajax-message').html(data.message);
					$('#hp-smallnote-field .ajax-message').show();
					setTimeout(function(){
						$('#hp-smallnote-field .ajax-message').fadeOut(2000);
					}, 4000);
				}else{
					$('#smallnote-input').val('');
					$('#notice_count').html($(this).val().length);
				}
				loadNotices();
			}
		});


	}

	function showSlider(data){
		var sliderHtml = '<ul>';
		for(var key in data){
			sliderHtml += '<li><a href="/noRW_/pg/'+data[key].normalfile+'" rel="gal" class="lightbox">'
				+'<img src="/noRW_/pg/'+data[key].smallfile+'" style="padding: '+data[key].pt+'px 0;" height="'+data[key].h+'"/></a>'
				+'<span class="auth"><a class="mpTrigger" rel="'+data[key].author.id+'" href="'+data[key].author.user_url+'">'+data[key].author.nick+'</a></span><span class="date">'+data[key].nahrano+'</span></li>';
		}
		sliderHtml += '</ul><span class="prev"></span><span class="next"></span>';
		$('#hp-photos-slider').html(sliderHtml);
		$('#hp-photos-slider').jCarouselLite({
			visible: 1,
			btnNext: '#hp-photos-slider .next',
			btnPrev: '#hp-photos-slider .prev'
		});
		$('#nahrajfotky a.lightbox').fancybox();
	}

	function getSliderData(album_id){
		$.ajax({
			type: 'POST',
			url: '/skaut-chat/fotoalbum/?ajax=1',
			data: 'wrajaxfunction=album_slider&album_id=' + album_id,
			dataType: 'json',
			cache: 'false',
			success: function(data){
				showSlider(data);
			}

		});
	}

	var hiddenPhoto = '';

	function switchPhotos(tohide){
		$('#' + tohide).slideUp('slow', function(){
			if(hiddenPhoto != ''){
				$('#' + hiddenPhoto).slideDown('slow');
			}
			hiddenPhoto = tohide;
			getSliderData($('#' + tohide).attr('rel'));
		});
	}

	function sendPrivateMsg(){
		var params = 'wrajaxfunction=meet_send_message&msg_to='+$('#notice_autocomplete_uid').val()+'&msg_subj='+$('#priv-subj').val()+'&msg_text=' + $('#priv-text').val();
		//console.log(params);
		$.ajax({
			type: 'POST',
			url: '/skaut-chat/seznamka/napsat-zpravu/?ajax=1',
			data: params,
			dataType: 'json',
			cache: 'false',
			success: function(data){
				if(data.status == 'err'){
					//alert(data.message + "ERR");
					$('#private-msg .infobox .in').html(data.message);
					$('#private-msg .infobox').show();
					setTimeout(function(){
						$('#private-msg .infobox').fadeOut(2000);
					}, 4000);
				}else{
					//alert(data.message + "OK");
					/*$('#smallnote-input').val('');
					$('#notice_count').html($(this).val().length);*/
					$('#private-msg .infobox2 .in').html(data.message);
					$('#private-msg .infobox2').show();
					setTimeout(function(){
						$('#private-msg .infobox2').fadeOut(2000);
					}, 4000);
				}
			}
		});
	}

	function messageFor(uid, displayName, subject){
		$('#notice_autocomplete_uid').val(uid);
		$('#notice_autocomplete').val(displayName);
		if(subject != null){
			$('#priv-subj').val(subject);
		}
		if($('#private-msg').is('visible')){
			$('#priv-subj').focus();
		}else{
			switchMessages('private-msg',true);
			//$('#priv-subj').delay(1500).focus();
		}
		
	}

	function switchMessages(what,focus){
		if(what == 'notices-hp'){
			if($('#private-msg').is(':visible')){
				$('#private-msg').slideUp(500, function(){$('#notices-hp').slideDown(500)});
			}
			if($('#teepek-msg').is(':visible')){
				$('#teepek-msg').slideUp(500, function(){$('#notices-hp').slideDown(500)});
			}
		}

		if(what == 'private-msg'){

			if($('#notices-hp').is(':visible')){
				if(focus){
					$('#notices-hp').slideUp(500, function(){$('#private-msg').slideDown(500,function(){$('#priv-subj').focus();})});
				}else{
					$('#notices-hp').slideUp(500, function(){$('#private-msg').slideDown(500)});
				}
			}
			if($('#teepek-msg').is(':visible')){
				if(focus){
					$('#teepek-msg').slideUp(500, function(){$('#private-msg').slideDown(500,function(){$('#priv-subj').focus();})});
				}else{
					$('#teepek-msg').slideUp(500, function(){$('#private-msg').slideDown(500)});
				}
			}
		}

		if(what == 'teepek-msg'){
			if($('#private-msg').is(':visible')){
				$('#private-msg').slideUp(500, function(){$('#teepek-msg').slideDown(500)});
			}
			if($('#notices-hp').is(':visible')){
				$('#notices-hp').slideUp(500, function(){$('#teepek-msg').slideDown(500)});
			}
		}

		/*$('#notices-hp').slideUp(500, function(){
			$('#' + what).slideDown(500);
		});
		, function(){
			$('#' + what).slideDown(500);
		});
		$('#teepek-msg').slideUp(500, function(){
			$('#' + what).slideDown(500);
		});*/
		
		
		
	}


$(document).ready(function(){
	noticeboard_page = 1;
	loadNotices();
	$('#smallnote-input').keyup(function(){
		$('#notice_count').html($(this).val().length);
	});

	$('#noticeboard-submit').click(function(event){
		event.preventDefault();
		$('#notices-hp .inbox').html(noticeboard_waiting);
		addNotice();
	});

	$('#priv-msg-submit').click(function(event){
		event.preventDefault();

		sendPrivateMsg();
	});

	$('#notices-hp .prev').click(function(){
		if(parseInt($('#notices-hp .nth').html()) > 1){
			$('#notices-hp .inbox').html(noticeboard_waiting);
			noticeboard_page--;
			loadNotices();
		}
	});
	$('#notices-hp .next').click(function(){
		if(parseInt($('#notices-hp .nth').html()) < parseInt($('#notices-hp .tot').html())){
			$('#notices-hp .inbox').html(noticeboard_waiting);
			noticeboard_page++;
			loadNotices();
		}
	});
	$('#nahrajfotky .newestphotos a').click(function(event){
		event.preventDefault();
		$('#hp-photos-slider').html(noticeboard_waiting);
		switchPhotos($(this).attr("id"));
	});
	var rnd = Math.floor(Math.random()*5)+2;
	switchPhotos('hpfoto-' + $('#nahrajfotky .select:nth-child('+rnd+')').attr('rel'));
});




