$(document).ready(function() 
{
	Holla.init();
	
	$(".standard-listing tr:even").addClass("even");;
	$(".standard-listing tr:odd").addClass("odd");;
	$(".standard-listing tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
});

(function() 
{
	Holla = {
		the_time: {},
		currentSection: "",
		
		init: function()
		{
			$("a.selectedPlace").click(function() {
				$(".flyFromCity").toggle();
			});

			if (document.getElementById("newsletter_email") && document.getElementById("subscribe-newsletter-form"))
			{
				Holla.SetupNewsletter();
				Holla.SetupRss();
			}

			if (document.getElementById("home_calendar"))
			{
				var d = new Date();
				year = parseInt(d.getFullYear(), 10);
				month = parseInt(d.getMonth(), 10) + 1;
				Holla.ShowCalendar(year, month);
			}

			if (Holla.currentSection == "static_how_it_works")
			{
				$("#why-subscribe-link").click(function() {
					$("#newsletter-container").show();
					Holla.SetupNewsletterExpose();
					$("#newsletter_email").focus();
					$("#newsletter_email").val("");
				});
				$("#closeLink").click(function() {
					$("div.newsletterContainer").expose().close();
					$("#newsletter-container").hide();
				});	
			}
		},
		
		SetupRss: function()
		{
			$("#subscribe-rss-link").click(function() {
				Holla.SetupNewsletterExpose();
				$("#subscribe-cities-header-container").toggle();
				$("#subscribe-cities-container").hide();
				return false;
			});
			
			$("#subscribe-cities-header-container input").click(function() {
				var location_ids = "";
				$.each($("#subscribe-cities-header-container input:checked"), function() {
					location_ids += $(this).val() + ",";
				});
				location_ids = location_ids.substr(0, location_ids.length - 1);
				if (location_ids != "")
				{
					$("#subscribe-rss-go").attr("href", "/feed/" + location_ids);
					$("#subscribe-rss-go").unbind("click");
				}
				else
				{
					$("#subscribe-rss-go").attr("href", "javascript:;")
						.click(function() {
							return false;
					});
				}
			});
		},

		ShowCalendar: function(year, month)
		{
			$.ajax({
				url: "/ajax_calendar.php?year=" + year + "&month=" + month,
				cache: false,
				success: function(html){
					$("#results").append(html);
					$('#home_calendar').html(html);
				}
			});
		},
		
		ShowMore: function()
		{
			$('#description_more').show();
			$('#more_link').hide();
		},

		HideMore: function()
		{
			$('#description_more').hide();
			$('#more_link').show();
		},
		
		SetupNewsletter: function()
		{
			$("input#newsletter_email").click(function() {
				Holla.SetupNewsletterExpose();
				
				$("#subscribe-cities-header-container").hide();
				$("#subscribe-cities-container").show();
				
				if ($("input#newsletter_email").val() == "Adresa ta de email" || $("input#newsletter_email").val() == "Your email address")
				{
					$("input#newsletter_email").val("");
				}
			});
			
			$("input#subscribe").click(function() {
				if ($("input#newsletter_email").val() != "")
				{
					var location_ids = "";
					$.each($("#subscribe-cities-container input:checked"), function() {
						location_ids += $(this).val() + ",";
					});
					
					$.post(
						"/ajax_newsletter.php", 
						{ "email": $("input#newsletter_email").val(), "location_ids": location_ids },
						function (response)
						{
							$(".newsletter_subscribe_ok").remove();
							$(".newsletter_subscribe_error").remove();
							if (response.success == 1)
							{
								var msg = '<div class="newsletter_subscribe_ok">' + response.msg + '</div>';
								$(msg).insertBefore($("#subscribe-cities-container"));
								$("#subscribe-cities-container").hide();
							}
							else
							{
								var msg = '<div class="newsletter_subscribe_error">' + response.msg + '</div>';
								$(msg).insertBefore($("#subscribe-cities-container"));
							}
						},
						"json"
					);
				}
				
				return false;
			});
		},
		
		SetupNewsletterExpose: function()
		{
			var exp = $("div.newsletterContainer").expose({
				api: true,
				color: "#222",
				opacity: 0.7,
				onClose: function() {
					$("#subscribe-cities-container").hide();
					$("#subscribe-cities-header-container").hide();
				}
			});
			
			exp.load();
		}
	}
})();
