// JavaScript Document

    var urlToIcon = 'http://'+window.location.host+'/images/pubguide/';
    var WebUrl = 'http://'+window.location.host+'/cz/';
    var WebUrlAjax = 'http://'+window.location.host+'/';

    var AjaxLoad = 0;

//    var urlToIcon = 'http://localhost/arakis/pilsnerurquell/websvn/images/pubguide/';
//    var WebUrl = 'http://localhost/arakis/pilsnerurquell/websvn/cz/';

    var blogyLink = "Klub-Blogy";
    var PnpNextStep = "";

function SetUrlLocation( set , textContentIdTo , textContentIdFrom, linker)
{
  var tmp_link = top.document.location.href;
  var link = tmp_link.split("#");
  var new_link_temp = link[0] + "#" + set;
  var new_link = WebUrl + set + ".html";
  var actual_page_id = null;

  top.document.location.replace( new_link_temp );

  AjaxLoad = 1;

  if (linker && textContentIdTo != 'AjaxContentLB')
  {
		if (linker.hasAttribute("href")) loadingAnimation(1,linker,'klub',jQuery(linker).text());
		else loadingAnimation(1,linker,'klub');
  }

  if (textContentIdTo == 'AjaxContentLB') {
    linker.style.cursor = "wait";
    document.getElementById("AjaxContentLB").style.cursor = "wait";
    actual_page_id = document.getElementById("pnp_actual_step").value;

        if (PnpNextStep != textContentIdFrom) {
            PnpNextStep = textContentIdFrom;
        }
        else {
            PnpNextStep = '';
        }

        if (textContentIdFrom == "Step1") {
            // alert(actual_page_id + " | " + PnpNextStep + " | " + textContentIdFrom);
        }

        // podminky pri preklikavani horni listy
        if (actual_page_id == 'step1')  // jdeme z kroku 1
        {
            document.getElementById("pnp_actual_step").value = "";
            SetPubAndDates(linker,set);
            document.getElementById("AjaxContentLB").style.cursor = "default";
            return false;
        }
        if (actual_page_id == 'step2')  // jdeme z krok 2
        {
            document.getElementById("pnp_actual_step").value = "";
            SetMeetingInfo(linker,set);
            document.getElementById("AjaxContentLB").style.cursor = "default";
            return false;
        }
        if (actual_page_id == 'step3')  // jdeme z krok 3
        {
            SetMeetingUser(0);
            var u_count = parseInt(document.getElementById("pnp_user_count").value);
            document.getElementById("AjaxContentLB").style.cursor = "default";
            if (u_count <= 0)
            {
               return false;
            }
        }
  }


  $("#" + textContentIdTo).load(new_link + " #" + textContentIdFrom + " ", function()
    {
      //callback

      /// pro Pojd Na pivo
      if (textContentIdTo != 'AjaxContentLB') {
	    loadingAnimation(0);
      }

      if (textContentIdFrom == "Step1") {
        LoadCalendar();
        StartPubListener();

        var dts = document.getElementById("pnp_selected_dates").value;
        if (dts != null && NewCalendar != "")
        {
            var tmp_dts = dts.split("|");
            if (tmp_dts.length > 0)
            {
                for (var i = 0; i < tmp_dts.length;i++) {
                    var tmp = tmp_dts[i].split("_");
                    if (tmp.length == 2) {
                        NewCalendar.addToList(tmp[1],tmp[0]);
                    }
                }
            }
        }
      }

      if (textContentIdFrom == "Step2") {
        $('#timePickerWrap').timePicker({ input: 'pnp_action_time'});
      }

      if (textContentIdFrom == "Step3") {
        StartUserListener();
        ListMeetingUsers();
      }

      // zakladni veci pro vsechny
      Cufon.replace('#' + textContentIdTo + ' .textize');
      selectizeAllSelects();

      if (textContentIdTo == 'AjaxContentLB') {
        document.getElementById("AjaxContentLB").style.cursor = "default";
        linker.style.cursor = "default";
      }
    });

  return false;
}

// nacte stranku podle kotvy v URl
function klubLoadByAnchor()
{
    var tmp_link = top.document.location.href;
    var link = tmp_link.split("#");
    var params;
    var textContentIdFrom = "klubcommon";

    if (link[1] != null && AjaxLoad == 0) {
      // window.parent.location = WebUrl + link[1] + ".html";
      if (link[1] == blogyLink) {
        textContentIdFrom = "klubblogy";
      }

      SetUrlLocation( link[1] , "AjaxContent" , textContentIdFrom);
    }
}

function klubHideMessages(mess_id)
{
  var tmp = document.getElementById("nastenka-" + mess_id).style.display;

  if (tmp == "none") {
    document.getElementById("nastenka-" + mess_id).style.display = "block";
  }
  else {
    document.getElementById("nastenka-" + mess_id).style.display = "none";
  }

  return false;
}

// zjisti na co bylo reagovano a pokusi se nalezt formular s textareou na vkladani komentatu + nastavi formularove hidden pole (reakce na ktery prispevek) podle ID odkazu pred tim kterym div.vzkazN
function klubReact(e,idparent) {
	var puvE, newE;

	if (e) {
		e = jQuery(e);
		puvE = idparent; // e.parent().closest('.vzkazN').find(":nth-child(1)").attr('id').split('-')[1]; // rozdeli id prispevku podle "-" a pouzije druhou cast, muze byt upraveno dle libosti
		e = e.closest('.vzkazyNastenka').find('form').eq(0);

		if (e) {
			e = e.find('textarea');
			if (e && e.length > 0) {
				e = e.eq(0);
				e.trigger('focus');
				newE = document.getElementById('hiddenPrispevekNumber');
				if (!newE) {
					e.parent().append('<input id="hiddenPrispevekNumber" type="hidden" name="prispevek" />'); // nazev muze byt upraven dle libosti
				}
				newE = jQuery("#hiddenPrispevekNumber");
				newE.attr('value',puvE);
			}
		}
	}
}

// nalezne rodicovsky prvek vzkazTxt a pokusi se nalezt dalsi DIV, kteremu prepne class hide => skryti
function klubHide(e)
{
	if (e) {
		e = jQuery(e).closest('.vzkazTxt').next('div');
		if (e && e.hasClass('vzkazN')){
			e.toggleClass('hideIt');
		}
	}
}

$(document).ready(function() {

	/* functions for new klub 2010 */

	$("a[rel=showCommentReply]").click(function(e) {
		e.preventDefault();
		$(".vzkazN > .addComment").hide();
		$(this).closest(".vzkazTxt").next().show();
	});

	$("a[rel=showCommentReactions]").click(function(e) {
		e.preventDefault();
		var noteId = $(this).closest(".vzkazN").attr("id").split("note")[1];
		$("#reactionsForNote-" + noteId).toggle();
	});

	$("a[rel=showMoreText]").each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			$(this).hide();
			$(this).next().show();
		});
	});


	/* textarea counter */
	var textarea = $("textarea.counter");
	textarea.each(function() {
		var spanText = $(this).parent().parent().find("span.counterShowRemain");
		$(this).keyup(function() {
			var text = $(this).val();
			var charLeft = 1000 - text.length;
			if(charLeft == 1) {
				return spanText.html("Ještě můžete napsat <strong>" + charLeft + "</strong> znak");
			} else if(charLeft > 1 && charLeft < 5) {
				return spanText.html("Ještě můžete napsat <strong>" + charLeft + "</strong> znaky");
			} else if(charLeft >= 5) {
				return spanText.html("Ještě můžete napsat <strong>" + charLeft + "</strong> znaků");
			} else if (charLeft == 0) {
				return spanText.html("Ještě můžete napsat <strong>" + charLeft + "</strong> znaků");
			}
			if (text.length > 1000) {
				$(this).val(text.substring(0,999));
			}
		});
	});
	$("textarea.counter").focus(function(){
		if($(this).attr('value') == "Text příspěvku") {
			$(this).attr('value', '');
		}
	});
});

function setPalec(idMessage,stat, type)
{
	var statGet;
	var statVal;
	stat = parseInt(stat);

	//alert(WebUrl + " | " + idMessage + " " + stat);
	if (stat == 1)
	{
		statGet = "up";
		statVal = 1;
	}
	else
	{
		statGet = "down";
		statVal = 1;
	}

	$.ajax({
	   type: "GET",
	   url: WebUrlAjax + "ajax/clubSetPalec.php",
	   data: "idMessage=" + idMessage + "&" + statGet + "=" + statVal + "&type=" + type,
	   success: function(data){
	   	updatePalecStat(idMessage, data);
	   		if (data != "")
	   		{
	   			if (statGet == "up")
	   			{
	   				if ( $("#message-down-thumb-" + idMessage + "-best") )
	   				{
						$("#message-down-thumb-" + idMessage + "-best").show();
	   					$("#message-up-thumb-" + idMessage + "-best").hide();
	   				}
	   				$("#message-down-thumb-" + idMessage).show();
	   				$("#message-up-thumb-" + idMessage).hide();
				}
				else
				{
					if ( $("#message-down-thumb-" + idMessage + "-best") )
	   				{
						$("#message-down-thumb-" + idMessage + "-best").hide();
	   					$("#message-up-thumb-" + idMessage + "-best").show();
	   				}
					$("#message-down-thumb-" + idMessage).hide();
	   				$("#message-up-thumb-" + idMessage).show();
				}
			}
	   }
	 });

	 return false;
}

function updatePalecStat(idMessage, sum)
{
	if (sum != "")
	{
		sumInt = parseInt(sum);
		if (sumInt > 0)
		{
			$("#message-palce-up-" + idMessage).html("("+ sum +")");
		}
		else
		{
			$("#message-palce-up-" + idMessage).html(" ");
		}
		//$("#message-palce-down-" + idMessage).val("("+ sum +")");
		//alert( $("#message-palce-up-" + idMessage).val() );
	}
}
