// Twitter and bit.ly characters length
var bitly_url_length = 20;

// If place shortcode prezent calculate the characters
if (!empty(twitter_limit))
{
	// + 1 is for '#' at the beginning of hash tag #kaywach
	twitter_limit = kayku_limit - (twitter_limit.length + 4);
}
// If no place shortcode present use kayku limit instead
else
{
	twitter_limit = kayku_limit;
}

var twitter_limit = twitter_limit - bitly_url_length;

var Places = new Object;

Places.res_per_page = 10;
Places.num_of_pages = 0;
Places.current_page = 0;

Places.elem = "";

Places.previous = function ()
{
	if (Places.current_page > 0)
	{
		$(Places.elem).html(Places.results[Places.current_page - 1]);
		Places.current_page--;
	}
}
Places.next = function ()
{
	if ((Places.current_page + 1) < Places.num_of_pages)
	{
		$(Places.elem).html(Places.results[Places.current_page + 1]);
		Places.current_page++;
	}
}

$(document).ready(function () {
	var logging_to_twitter = false;
	var logging_from_twitter = false;
	
	$(".placesAround select").bind("change", function (e){

		// Hide all other types 
		$(".placesAround > .typeName").each(function (){
			$(this).next().slideUp("fast");
		});

		// Folds all "+" to "-"
		$(".placesAround > .typeName").each(function () {
			$(this).children(":first").next().html("+");
		});
		
		$.ajax({
			type: "POST",
			url: base_url + "place/countplaces/",
			data:	"&placeid=" + place_id +
					"&radius=" + $(".placesAround select option:selected").val(),
			beforeSend: function ()
			{
				$(".placesAround > .typeName").each(function () {
					$(this).children(":last").css("visibility","visible");
				});
				//loading_places_in_progress = true;
			},
			complete: function ()
			{
				$(".placesAround > .typeName").each(function () {
					$(this).children(":last").css("visibility","hidden");
				});
				//loading_places_in_progress = false;
			},
			success: function (data)
			{
				if(!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					for (id in data.types)
					{
						$(".numberOfPlaces_" + data.types[id].id).html("(" + data.types[id].num_of_places + ")");
					}
				}
			}
		});
	});
	
	$(".placesAround > .typeName").click(function (){
		$(".placesAround > .types").html("");

		elem = $(this);
		
		// Hide all other types 
		$(".placesAround > .typeName").each(function (){
			$(this).next().slideUp("fast");
		});
		
		type_id = $(this).children("span").html();
		radius = $(".placesAround select option:selected").val();

		// Folds all "+" to "-"
		$(".placesAround > .typeName").each(function () {
			$(this).children(":first").next().html("+");
		});
		
		// If type have no places don't send ajax request
		if ($(".numberOfPlaces_" + type_id).html() == "(0)")
		{
			return false;
		}
		
		$.ajax({
			type: "POST",
			url: base_url + "place/getplaces/",
			data: 	"typeid=" + type_id + 
					"&placeid=" + place_id +
					"&radius=" + radius,
			beforeSend: function ()
			{
				elem.children(":first").next().html("-");
				elem.children(":last").css("visibility","visible");				
				//loading_places_in_progress = true;
			},
			complete: function ()
			{
				$(".placesAround > .typeName").each(function () {
					$(this).children(":last").css("visibility","hidden");
				});
				//loading_places_in_progress = false;
			},
			success: function (data)
			{
				if(!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					num_of_places = count(data.places);
					
					if (num_of_places > 0)
					{
						// count how many pages of places will be
						Places.num_of_pages = (Math.ceil(num_of_places / Places.res_per_page));
						
						// creates array with length of number of pages
						Places.results = new Array(Places.num_of_pages);

						// sets all place results array element to have navigation/empty string in it
						for (i = 0; i < Places.results.length; i++)
						{
							if (Places.num_of_pages > 1)
							{
								Places.results[i] = "<div class=\"previousNext\">&laquo; <a href=\"javascript:Places.previous();\">Previous</a> "
								+ (i + 1) + "/" + (Places.results.length) 
								+ " <a href=\"javascript:Places.next();\">Next</a> &raquo;</div>";
							}
							else
							{
								Places.results[i] = "";
							}
						}
						
						for (i = 0; i < num_of_places; i++)
						{
							// Math.ceil((i+1)/Places.res_per_page) calculates the current page
							Places.results[(Math.ceil((i+1)/Places.res_per_page)-1)] +=
							"<li><a href=\"" + base_url + qr_handler
							+ data.places[i].place_to_id + "\">" + data.places[i].place_to_name 
							+ "</a><br /> " + data.places[i].distance_rounded + "m (" + data.places[i].category + ")</li>";
						}

						// for the beginning show only first page of results
						$("#mainTypes_" + type_id).html(Places.results[0]);

						Places.elem = "#mainTypes_" + type_id;
						
						Places.current_page = 0;
						
						// Show list of places in type that is clicked only if places are available for it
						elem.next().slideDown("fast");
					}
				}
			}
		});
	});
});

$(document).ready(function () {

	// Calculates characters left for Kayku
	function calculate_chars()
	{
		if ($("#kaykus .kayku_content").val() != kaykus_content_text)
		{
			chars = kayku_limit - $("#kaykus .kayku_content").val().length;
		}
		else
		{
			chars = kayku_limit;
		}
		
		if (chars < 0)
		{
			chars = "<span style='color: red;'>" + chars + "</span>";
		}
		$("#kaykus .kaykuCharacters").html(chars);
	}
	
	$("#kaykus .kayku_content").bind("focus", function() {
		if ($("#kaykus .kayku_content").val() == kaykus_content_text)
		{
			$("#kaykus .kayku_content").val("");
		}
	});
	
	$("#kaykus .kayku_content").bind("blur", function() {
		if ($("#kaykus .kayku_content").val() == "")
		{
			$("#kaykus .kayku_content").val(kaykus_content_text);
		}
	});

	$("#kaykus .kayku_content").bind("keyup", function () {
		calculate_chars();
	});
	
	$("#kaykus .update_kayku").click(function (){
		kayku = $("#kaykus .kayku_content").val();
		if (loggedin == false)
		{
			return false;
		}
		
		// if kayku empty or not changed do not send ajax request
		if (empty(kayku) || kayku == kaykus_content_text)
		{
			return;
		}

		if (kayku.length > kayku_limit)
		{
			return false;
		}
		
		$("#kaykus .update_kayku").attr("disabled", "disabled");
		
		$.ajax({
			type: "POST",
			url: base_url + "place/newKayku/",
			data:	"kayku=" + urlencode(kayku) + 
					"&placeid=" + place_id + 
					"&twitter=" + $("#updateTwitter").attr("checked")+
					"&twitterUsername=" + $("#twitterUsername").attr("value")+
					"&twitterPassword=" + $("#twitterPassword").attr("value"),
			beforeSend: function ()
			{
				//elem.children(":first").next().html("-");
				//elem.children(":last").css("visibility","visible");				
				//loading_places_in_progress = true;
			},
			complete: function ()
			{
				//$(".placesAround > .typeName").each(function () {
					//$(this).children(":last").css("visibility","hidden");
				//});
				//loading_places_in_progress = false;
			},
			success: function (data)
			{
				if(!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					location = base_url + qr_handler + place_id; 
				}
			}
		});
	});
	
	$("#updateTwitter").bind("change", function(){
		updateTwitter();
	});
	
	function updateTwitter()
	{
		if ($("#updateTwitter").attr("checked") == true)
		{
			kayku_limit = twitter_limit;
			$("#twitterAccount").slideDown("fast");
		}
		else
		{
			kayku_limit = const_kayku_limit;
			$("#twitterAccount").slideUp("fast");
		}
		calculate_chars();
	}
	
	function twitterLogin()
	{
		// If user is not logged in can't login to twitter account
		if (!loggedin)
		{
			alert(twitter_first_login);
			return false;	
		}
		
		if (empty($("#twitterUsername").attr("value")) || empty($("#twitterPassword").attr("value")))
		{
			return false;
		}
		
		$.ajax({
			type: "POST",
			url: base_url + "place/updateTwitterAccount/",
			data:	"&twitterUsername=" + $("#twitterUsername").attr("value")+
					"&twitterPassword=" + $("#twitterPassword").attr("value"),
			beforeSend: function ()
			{
				logging_to_twitter = true;
			},
			complete: function ()
			{
				logging_to_twitter = false;
			},
			success: function (data)
			{
				if (!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					$("#twitterAccount .twitterLoginMessage").html(twitter_logout_text);
					$("#twitterAccount #twitterUsername").attr("disabled", "disabled");
					$("#twitterAccount #twitterPassword").hide();
					$("#twitterAccount .twitterLogin").hide();
					$("#twitterAccount .twitterLogout").show();
				}
			}
		});
	}
	
	// On click login user to twitter
	$("#twitterAccount .twitterLogin").click(function (){
		twitterLogin();
	});
	
	// On Enter key pressed login user to twitter
	$("#twitterPassword").bind("keypress", function (e){
		if (e.keyCode == 13)
		{
			twitterLogin();
		}
	});
	
	// On Enter key pressed login user to twitter
	$("#twitterUsername").bind("keypress", function (e){
		if (e.keyCode == 13)
		{
			twitterLogin();
		}
	});
	
	$("#twitterAccount .twitterLogout").click(function (){
		
		$.ajax({
			type: "POST",
			url: base_url + "place/removeTwitterAccount/",
			beforeSend: function ()
			{
				logging_from_twitter = true;
			},
			complete: function ()
			{
				logging_from_twitter = false;
			},
			success: function (data)
			{
				if (!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					$("#twitterAccount .twitterLoginMessage").html(twitter_login_text);
					$("#twitterAccount #twitterUsername").attr("disabled", "");
					$("#twitterAccount #twitterPassword").show();
					$("#twitterAccount .twitterLogin").show();
					$("#twitterAccount .twitterLogout").hide();
				}
			}
		});
	});
	
	// Activate republish if user has set it
	if (republish_twitter)
	{
		$("#updateTwitter").attr("checked", "checked");
		updateTwitter();
	}
});

$(document).ready(function () {

	$("#news .news_content").bind("focus", function() {
		if ($("#news .news_content").val() == news_content_text)
		{
			$("#news .news_content").val("");
		}
	});
	
	$("#news .news_content").bind("blur", function() {
		if ($("#news .news_content").val() == "")
		{
			$("#news .news_content").val(news_content_text);
		}
	});

	$("#news .news_content").bind("keyup", function () {
		chars = news_limit - $("#news .news_content").val().length;
		if (chars < 0)
		{
			chars = "<span style='color: red;'>" + chars + "</span>";
		}
		$("#news .newsCharacters").html(chars);
	});
	
	$("#news .update_news").click(function (){
		var news = $("#news .news_content").val();
		
		if (loggedin == false)
		{
			return false;
		}
		
		// if news empty or not changed do not send ajax request
		if (empty(news) || news == news_content_text)
		{
			return;
		}

		if (news.length > news_limit)
		{
			return false;
		}
		
		$("#news .update_news").attr("disabled", "disabled");
		
		$.ajax({
			type: "POST",
			url: base_url + "place/newNews/",
			data: 	"news=" + urlencode(news) + 
					"&placeid=" + place_id,
			beforeSend: function ()
			{
				//elem.children(":first").next().html("-");
				//elem.children(":last").css("visibility","visible");				
				//loading_places_in_progress = true;
			},
			complete: function ()
			{
				//$(".placesAround > .typeName").each(function () {
					//$(this).children(":last").css("visibility","hidden");
				//});
				//loading_places_in_progress = false;
			},
			success: function (data)
			{
				if(!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					location = base_url + qr_handler + place_id; 
				}
			}
		});
	});
	
	// Shows delete option for hovered News
	$("#news .item").bind("mouseover",function () {
		var elem = $(this).children(".deleteNews");
		elem.show();
	});
	
	// Hides delete option for hovered News
	$("#news .item").bind("mouseout",function () {
		var elem = $(this).children(".deleteNews");
		elem.hide();
	});
	
	$("#news .deleteNews").click(function () {
		
		if (!confirm(delete_news_confirmation))
		{
			return false;
		}
		
		var newsId = substr($(this).parent().attr("id"), 6);
		
		$.ajax({
			type: "POST",
			url: base_url + "place/deleteNews/",
			data: 	"newsId=" + newsId,
			beforeSend: function ()
			{
			},
			complete: function ()
			{
			},
			success: function (data)
			{
				if(!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					$("#newsId" + newsId).hide("slow", function () {
						$(this).remove();
					}); 
				}
			}
		});
	});
	
	// Shows delete option for hovered Kayku
	$("#kaykus .item").bind("mouseover",function () {
		var elem = $(this).children(".deleteKayku");
		elem.show();
	});
	
	// Hides delete option for hovered Kayku
	$("#kaykus .item").bind("mouseout",function () {
		var elem = $(this).children(".deleteKayku");
		elem.hide();
	});
	
	$("#kaykus .deleteKayku").click(function () {
		
		if (!confirm(delete_kayku_confirmation))
		{
			return false;
		}
		
		var kaykuId = substr($(this).parent().attr("id"), 7);
		
		$.ajax({
			type: "POST",
			url: base_url + "place/deleteKayku/",
			data: 	"kaykuId=" + kaykuId,
			beforeSend: function ()
			{
			},
			complete: function ()
			{
			},
			success: function (data)
			{
				if(!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					$("#kaykuId" + kaykuId).hide("slow", function () {
						$(this).remove();
					}); 
				}
			}
		});
	});
});


$(document).ready(function () {
	$("#placeDetails .mobileUrl").bind("mouseenter", function (e) {
		top = -165;
		left = 10;
		$("#mobileUrlQrCode").css("display", "block");
		$("#mobileUrlQrCode").css("top", e.pageY + top);
		$("#mobileUrlQrCode").css("left", e.pageX + left);
	});
	
	$("#placeDetails .mobileUrl").bind("mouseleave", function () {
		$("#mobileUrlQrCode").css("display", "none");
	});
});

$(document).ready(function () {
	$("#login .loginButton").click(function () {
		login();
	});
	
	$("#login .loginForm .password").bind("keypress", function (e) {
		if (e.keyCode == 13)
		{
			login();
		}
	});

	function login ()
	{
		var username = $("#login .username").val();
		var password = $("#login .password").val();

		if (!empty(username) && !empty(password))
		{
			$.ajax({
				type: "POST",
				url: base_url + "place/login",
				data: 	"username=" + username + 
						"&passwd=" + password,
				beforeSend: function ()
				{
					//elem.children(":first").next().html("-");
					//elem.children(":last").css("visibility","visible");				
					//loading_places_in_progress = true;
				},
				complete: function ()
				{
					//$(".placesAround > .typeName").each(function () {
						//$(this).children(":last").css("visibility","hidden");
					//});
					//loading_places_in_progress = false;
				},
				success: function (data)
				{
					if(!(data = json_parse(data)))return false;
					
					if (data.status == "yes")
					{
						// Whole page is refrashed when user logs in
						window.location = window.location;
					}
				}
			});
		}
	}
	
	$("#login .logoutButton").click(function () {
		
		$.ajax({
			type: "get",
			url: base_url + "place/logout",
			beforeSend: function ()
			{
				//elem.children(":first").next().html("-");
				//elem.children(":last").css("visibility","visible");				
				//loading_places_in_progress = true;
			},
			complete: function ()
			{
				//$(".placesAround > .typeName").each(function () {
					//$(this).children(":last").css("visibility","hidden");
				//});
				//loading_places_in_progress = false;
			},
			success: function (data)
			{
				if(!(data = json_parse(data)))return false;
				
				if (data.status == "yes")
				{
					// Whole page is refrashed when user logs out
					window.location = window.location;
				}
			}
		});
	});
});

$(document).ready(function () {
	$(".placeThumbImage").click(function (){
		tmp = $(this).attr("src");
		// Replaces mini image with bigger one ('album' size)
		$("#defaultPlaceImage").attr('src', tmp.replace("/mini/", '/dokodare/'));
	});
});