function icons_fadeout() {
	$('a.icon_hide img').fadeTo(1000, .25);
}

function get_notifications(t) {
	if (!t) {
		t = 1;
	}
	
	$('#ph_notifications').animate({opacity: 1}, t, function(){		
		var url = g_path + "services/js/user/notifications" + g_path_c + "auth=" + g_auth;
		$.getJSON(url, function(json){
			if (json.stat == "ok") {
				if (typeof(json.total) && json.total) {
					$('#ph_notifications_total').html(' <span class="hide">(</span><span class="total">'+json.total+'</span><span class="hide">)</span>');
					$('#ph_notifications').addClass("notifications_exist");
				}
				else {
					$('#ph_notifications_total').html(' <span class="hide">(</span><span class="total">0</span><span class="hide">)</span>');
					$('#ph_notifications').removeClass("notifications_exist");
				}
			};
		});
		get_notifications(5000);
	});
}

function m(t) {
	if (!t) {
		var t = 2500;
	}
	
	$('#m').animate({opacity: 1}, t, function() {
	
		var url = g_site + "services/js/m" + g_path_c + "auth=" + g_auth;
		$.getJSON(url, function(json){});
		
		m(17500);
		
	});
}

function collapse_comment_forms() {
	$('[id^=cafc_]').each(function(){
		var id = ($(this).attr('id')).split('_');
		collapse_comment_form(id[1] + '_' + id[2]);
	});
}

function collapse_comment_form(id) {
	if (!st['cafc_' + id + '_coll']) {
		st['cafc_' + id] = $('#cafc_' + id).html();
		st['cafc_' + id + '_val'] = $('#cafc_' + id + ' .comment_input').val();
		st['cafc_' + id + '_coll'] = true;
		if (st['cafc_placeholder_' + id]) {
			var ph = st['cafc_placeholder_' + id];
		} else {
			var ph = 'Přidej komentář...';
		}
		$('#cafc_' + id).html('<div class="w"><p class="ph_form" id="phFormClickable_' + id + '">' + ph + '</p></div>');
	}
}

$(document).ready(function(){
	
	$('#body_msg').animate({width: 'auto'}, 10000).slideUp().hide();
	
	$('#body_popup_w1').live('mouseover', function(){
		$(this).stop().fadeOut('fast');
	});
	
	$('.unread').animate({opacity: 1}, 5000, function(){
		$(this).animate({backgroundColor: '#FFFFFF'}, 5000);
	});
	
	get_notifications();
	m();
	
	$("[class*='jq_remove']").remove();
	$("[class*='jq_show']").show();
	$("[class*='jq_enable']").attr('disabled', false);
	
	$('a.icon_hide img').fadeTo(0, .1, function(){
		$(this).fadeTo(1000, 1, function(){
			$(this).fadeTo(1000, .25);
		});
	});
	$('a.icon_hide img').live('mouseover', function(){
		$(this).fadeTo('fast', 1);
	});
	$('a.icon_hide img').live('mouseout', function(){
		$(this).fadeTo('fast', .25);
	});
	
	$('input, textarea').live('focus', function(){
		$(this).addClass('fel_active');
	});
	$('input, textarea').live('blur', function(){
		$(this).removeClass('fel_active');
	});
	
	$('.focus').focus(function(){
		var v = $(this).val();
		$(this).val('');
		$(this).val(v)
	});
	$('.focus').focus();
	
	$('select.goToUrl').change(function(){
		document.location = $("#"+$(this).attr("id")+" option:selected").val();
	});
	
	collapse_comment_forms();
	
	$('[id^=commentsAddFormCancel_]').live('click', function(event){
		event.preventDefault();
		var id = ($(this).attr('id')).split('_');
		collapse_comment_form(id[1] + '_' + id[2]);
	});
	
	$('[id^=cafc_] p.ph_form').live('click', function(event){
		var id = ($(this).attr("id")).split('_');
		$('#cafc_' + id[1] + '_' + id[2]).html(st['cafc_' + id[1] + '_' + id[2]]);
		$('#cafc_' + id[1] + '_' + id[2] + ' .comment_input').focus();
		$('#cafc_' + id[1] + '_' + id[2] + ' .comment_input').val(st['cafc_' + id[1] + '_' + id[2] + '_val']);
		st['cafc_' + id[1] + '_' + id[2] + '_coll'] = false;
	});
	
	$('.logout').click(function(event){
		event.preventDefault();
		FB.getLoginStatus(function(response) {
			if (response.session) {
				FB.logout(function(response){
					document.location = g_path + 'logout';
				});
			} else {
				document.location = g_path + 'logout';
			}
		});
	});
	
	$('form.update #f_text').live('keypress', function(event){
		rows = Math.ceil($(this).val().length / ($(this).width() / 10));
		if (!rows) rows = 1;
		if (rows > 5) rows = 5;
		$(this).height(rows * 25);
	});
	
	$('[id^=ratePersonHref_]').live('click', function(event){
		var unq = get_unq($(this).attr("id"));
		var id = $(this).attr("id").split('_');
		var skey = id[2];
		var action = id[3];
		var type = id[4];
		
		var cont_id = $('[id^=ratePersonCont_'+unq+']').attr("id").split('_');
		var cont_j = cont_id[2];
			
		var url = g_path + "services/js/user/rate/" + unq + "/" + action + g_path_c + "auth=" + g_auth + "&skey=" + skey + "&j=" + cont_j + "&type=" + type;
		
		$('#ratePersonCont_'+unq+'_'+cont_j).fadeOut(5000);
		$('#ratePersonCont_'+unq+'_'+cont_j+' ul.rating a').fadeOut();
		
		$.getJSON(url, function(json){
			if (json.stat == "ok") {
				if (json.user) {
					$('#ratePersonCont_'+unq+'_'+cont_j).clearQueue().fadeOut(function(){
						$('#ratePersonLi_'+cont_j).html(json.user);
						$('#ratePersonCont_'+json.user_unq+'_'+cont_j).hide().fadeIn();
					});
				}
			}
		});
		
		event.stopPropagation();
		return false;
	});
	
	$("[id^='dataSetChange_']").change(function(){
		var id = $(this).attr("id").split("_");
		var a = id[2].split('-');
		var val = $("#"+$(this).attr("id")+" option:selected").val();
		var unq = get_unq($(this).attr("id"));
		switch (id[2]) {
			case 'emails' :
				var url = g_path + "services/js/user/emails/" + id[3] + g_path_c + "auth=" + g_auth + "&v=" + ($(this).is(':checked') ? 1 : 0);
				$.getJSON(url, function(json){
					if (json.stat == "ok") { msg_popup(json.msg); }
				});
			break;
		};
	});
	
	$("[id^='dataSetChange_']").live('change', function(){
		var id = $(this).attr("id").split("_");
		var a = id[2].split('-');
		var val = $("#"+$(this).attr("id")+" option:selected").val();
		var unq = get_unq($(this).attr("id"));
		switch (id[2]) {
			
		}
	});
	
	$("[id^='dataSetBlur_']").blur(function(){
		var id = $(this).attr("id").split("_");
		var unq = get_unq($(this).attr("id"));
		var a = id[2].split('-');
		switch (id[2]) {
			
		}
	});
	
	$("[id^='dataSetClick_']").live('click', function(event){
		var id = $(this).attr("id").split("_");
		switch (id[2]) {
			
		}
		event.preventDefault();
	});
	
	$("[id^='dataSetSubmit_']").submit(function(event){
		var id = $(this).attr("id").split("_");
		var param = id[2].split("-");
		var unq = get_unq($(this).attr("id"));
		textarea_id = id[2];
		switch (id[2]) {
			
		}
		event.preventDefault();
	});
	
	/****************************************************************
		Comments
	*/
	
	$("a[id^='commentsAdd_']").live('click', function(event){
		var unq = get_unq($(this).attr("id"));
		if (st["commentsAddFormDisplayed_"+unq]) {
			$("#commentsAddFormShrinkable_"+unq).slideUp();
			st["commentsAddFormDisplayed_"+unq] = false;
		}
		else {
			$("form[id^='commentsAddFormShrinkable_']").each(function(){
				var fid = $(this).attr("id").split("_");
				var funq = fid[1];
				$(this).slideUp();
				st["commentsAddFormDisplayed_"+funq] = false;
			});
			
			var url = g_path + "services/js/comments/" + unq + "/add" + g_path_c + "auth=" + g_auth + "&base_doc=" + g_pg;
			$.getJSON(url, function(json){
				if (json.stat == "ok") {
					if (json.res == 'ok') {
						$("#ph_commentsAdd_"+unq).hide().html(json.form).slideDown(function(){
							$("#commentsAddTextarea_"+unq).focus();
						});
						st["commentsAddFormDisplayed_"+unq] = true;
					}
					else {
						msg_popup(json.msg);
					}
				};
			});
		};
		event.preventDefault();
	});
			
	$("a[id^='commentsDelete_']").click(function(event){
		var unq = get_unq($(this).attr("id"));
		var url = g_path + "services/js/comments/" + unq + "/delete" + g_path_c + "auth=" + g_auth;
		$.getJSON(url, function(json){
			if (json.stat == "ok") {
				if (json.res == "ok") {
					$("#comment_"+unq).slideUp(function(){
						msg_popup(json.msg);
						$(this).remove();
					});
				}
				else {
					msg_popup(json.msg);
				}
			}
		});
		event.preventDefault();
	});
	
	/* Other */
		
	$("a[id='topblockCancelHref']").live('click', function(event){
		topblock_close(event);
	});
	$("a[id='topblockCancelHref']").click(function(event){
		topblock_close(event);
	});
	
});
