// JavaScript Document

var backdrop;

var newsSliderWaitTime = 7000;
var newsSliderAnimSpeed = 750;

try {
	var foo = isInsideDir;
} catch (e) {
	var isInsideDir = false;
}

$(document).ready(function () {
	
	backdrop = $("#backdrop");
	
	$("[popup]").click(function () {
		var id = $(this).attr("popup");
		openPopup(id);
	});
	
	$("div.popup .closer").click(function () {
		$(this).parents("div.popup").hide();
		if (!$(this).attr("popup"))
			backdrop.fadeOut("fast");
	});
	
	$("ul.news-slider").each(function (i) {
		$(this).attr("ns-id",i);
		setTimeout('$("ul.news-slider[ns-id='+i+']").trigger("rotate")', newsSliderWaitTime);
		$(this).children().eq(0).addClass("selected");
	}).bind("rotate", function () {
		var current = $(this).children(".selected");
		var next = (current.nextAll().length == 0) ? $(this).children().eq(0) : current.next();
		current.animate({"top":"-100%"}, newsSliderAnimSpeed).removeClass("selected");
		next.css({"top":"100%"}).animate({"top":"0%"}, newsSliderAnimSpeed).addClass("selected");
		var eq = $(this).attr("ns-id");
		setTimeout('$("ul.news-slider[ns-id='+eq+']").trigger("rotate")', newsSliderWaitTime+newsSliderAnimSpeed);
	});
	
	
	$("ul.faq > li > a.question").click(function () {
		$(this).next().slideToggle().parent().toggleClass("selected").siblings().removeClass("selected").children("div.answer").slideUp();
	});
	
	
	$(window).resize(function () { positionPopups(); });
	
});

function openPopup (id)
{
	var popup = $("#" + id);
	if (popup.length == 0)
	{
		alert("Popup not found: " + id);
		return false;
	}
	
	popup.show();
	backdrop.fadeIn("fast");
	positionPopups();
}

function positionPopups ()
{
	var popup = $("div.popup:visible");
	
	popup.each(function () {
		var top = ((window.innerHeight - $(this).height()) / 2) * .7;
		$(this).css("margin-top",top + "px");
	});
}

function closePopup ()
{
	$("div.popup").hide();
	backdrop.fadeOut("fast");
}


function checkAndSend (form)
{	
	try
	{
		
		var isValid = checkForm(form);
		
		if (isValid)
		{
			var pieces = [];
			$(form).find("[name]:visible").each(function () {
				var name = $(this).attr("name");
				var val = $(this).val().trim().urlSafe();
				pieces.push(name+'='+val);
			});
			
			$(form).addClass("sending");
			
			var action = $(form).attr("action");
			
			$(form).find("input, textarea, button").attr("disabled","disabled").addClass("disabled");
			
			$.ajax({
				url: action + '?' + pieces.join('&'),
				cache: false,
				success: function (data) {
					if (data != "")
					{
						$(form).addClass("sent");
						/*data = data.substring(0,data.indexOf(' -->'));
						data = data.substring(('<!-- ').length);
						window.location = data;
						*/
					}
				},
				error: function (e) {
					alert("AJAX error");
					console.log(e);
				},
				complete: function () {
					$(form).removeClass("sending").addClass("sent");
					$(form).find("input, textarea, button").removeAttr("disabled").removeClass("disabled");
				}
			});
		}
	} catch (e) {
		alert("Could not send form");
		console.log(e);
	}
	
	return false;
}


function checkForm (form)
{
	try
	{
		$(form).find("li.error").removeClass("error");
		
		var isValid = true;
		
		$(form).find("[req]:visible").each(function () {
			var req = $(this).attr("req");
			var val = $(this).val();
			var li = $(this).parents("li:eq(0)");
			switch (req)
			{
				case "text":	if (val == "")
								{
									li.addClass("error");
									isValid = false;
								}
								break;
				case "email":	var at = val.indexOf('@');
								var dot = val.lastIndexOf('.')
								if (!(at > 0 && dot>at+1 && dot<val.length-1))
								{
									li.addClass("error");
									isValid = false;
								}
								break;
			}
		});
		
		return isValid;
	} catch (e) {
		alert("Could not validate form.");
		console.log(e);
		return false;
	}
}

String.prototype.cReplace = function (o,n) {
	var s = '';
	for (var i=0; i<this.length; i++)
		s += (this.charAt(i) == o) ? n : this.charAt(i);
	return s;
}

String.prototype.urlSafe = function ()
{
	var s = this;
	var c = [
		['%',"%25"],
		[' ',"%20"],
		['?',"%3F"],
		['/',"%2F"],
		['#',"%23"],
		['<',"%3C"],
		['>',"%3E"],
		['&',"%26"],
		['\n',"\\n"],
		['\t',"\\t"]
	];
	var len = c.length;
	for (var i=0; i<len; i++)
		s = s.cReplace(c[i][0],c[i][1]);
	return s;
}

String.prototype.trim = function ()
{
	var s = this;
	var fSp = -1;
	while (fSp++<s.length && (s.charAt(fSp) == ' ' || s.charAt(fSp) == '\t' || s.charAt(fSp) == '\n'));
	s = s.substring(fSp);
	var lSp = s.length;
	while (lSp-- > 0 && (s.charAt(lSp) == ' ' || s.charAt(lSp) == '\t' || s.charAt(lSp) == '\n'));
	s = s.substring(0,lSp+1);
	return s;
}
