﻿var InlineTextboxes = {};
var FastLinks = {
	PaddingTop:		0,
	ClosedHeight:	24,
	OpenHeight:		(278 - 24),
	List:			[],
	TimeOut:		null
};

$(document).ready(function()
{
	$('.inlinetext').each(function()
	{
		var tb = $(this);
		
		InlineTextboxes[tb.attr("id")] = tb.val();
		tb.focus(function()
		{
			var t = $(this);
			
			if (t.val() == InlineTextboxes[t.attr("id")])
				t.val("");
		});
		
		tb.blur(function()
		{
			var t = $(this);
			
			if (t.val() == "")
				t.val(InlineTextboxes[t.attr("id")]);
		})
	});
	
	$('.fastlinkbutton').each(function()
	{
		var flb = $(this);
		var isActive = false;
		
		if (flb.attr('active') == 'true')
		{
			flb.attr('active', '');
			
			isActive = true;
		}
		
		flb.click(FastLinkButton_Click);
		
		flb.hover(function()
		{
			FastLinks.TimeOut = setTimeout(function(){flb.click();}, 200);
		}, function()
		{
			clearTimeout(FastLinks.TimeOut);
		});
		
		FastLinks.List.push({
			IsActive: isActive,
			Btn: flb
		});
	});
	
	UpdateFastLinksByList(false);
});

function UpdateFastLinksByList(animate)
{
	var active = 0;
	if (animate == undefined)
		animate = true;
	
	for(var i = 0; i < FastLinks.List.length; i++)
	{
		var o = FastLinks.List[i];
		var top = FastLinks.PaddingTop + (i * FastLinks.ClosedHeight) + (active * FastLinks.OpenHeight);
		
		if (animate)
		{
			o.Btn.animate({'top': top}, 'normal');
		}
		else
		{
			o.Btn.css('top', top);
		}
		
		o.Btn.removeClass('fastlinkbuttonactive');
		if (o.IsActive)
		{
			o.Btn.addClass('fastlinkbuttonactive');
			active++;
		}
	}
}

function FastLinkButton_Click()
{
	var activeIndex = GetActiveFastLinkButtonIndex();
	var buttonIndex = GetIndexFromButton($(this));
	
	if (activeIndex == -1 || buttonIndex == -1)
	{
		alert('FastLinks: Unknown error. Please reload page');
	}
	else if (activeIndex == buttonIndex)
	{
		return;
	}
	
	FastLinks.List[buttonIndex].IsActive = true;
	FastLinks.List[activeIndex].IsActive = false;
	
	UpdateFastLinksByList();
}

function GetCssAsInt(o, p)
{
	return parseInt(o.css(p).replace('px', ''));
}

function GetIndexFromButton(btn)
{
	var t = GetCssAsInt(btn, 'top');
	for (var i = 0; i < FastLinks.List.length; i++)
	{
		if (GetCssAsInt(FastLinks.List[i].Btn, 'top') == t)
		{
			return i;	
		}
	}
	
	return -1;
}

function GetActiveFastLinkButtonIndex()
{
	for (var i = 0; i < FastLinks.List.length; i++)
	{
		if (FastLinks.List[i].IsActive)
		{
			return i;
		}
	}
	
	return -1;
}

function ResetContactform()
{
	for(var k in InlineTextboxes)
		$('#' + k).val(InlineTextboxes[k]);
	
	$('#chkNewsLetter').get(0).checked = false;
}

function btnSend_Click()
{
	var ok = true;
	for(var k in InlineTextboxes)
	{
		if ($('#' + k).val() == InlineTextboxes[k])
			ok = false;
	}
	
	if (!ok)
	{
		alert('Alla värden i formuläret måste fyllas i!');
		
		return;
	}
	
	$('#ContactInputs').animate({left: -202}, 'normal');
	$('#ContactSending').animate({left: 11}, 'normal', function()
	{
		$.post('index.php?action=mail',
		{
			'name':			$('#txtName').val(),
			'errand':		$('#txtErrand').val(),
			'email':		$('#txtEmail').val(),
			'description':	$('#txtDescription').val(),
			'newsletter':	$('#chkNewsLetter').get(0).checked
			
		}, function(r)
		{
			setTimeout(function()
			{
				$('#ContactInputs').animate({left: -404}, 'normal');
				$('#ContactSending').animate({left: -202}, 'normal');
				$('#ContactSent').animate({left: 11}, 'normal');
				
				setTimeout(function()
				{
					$('#ContactInputs').animate({left: 11}, 'fast');
					$('#ContactSending').animate({left: 202}, 'fast');
					$('#ContactSent').animate({left: 404}, 'fast');
					
					ResetContactform();
				}, 2000);
			}, 1500);
		});
	});
}