function right(str, n) {
	if (n < 0) return "";
	if (n > String(str).length) return str;
	var iLen = String(str).length;
	return String(str).substring(iLen, iLen - n);
}

function arbetsmarknaden() {
	$('div#arbetsmarknadsfolder').css({
		top: 0 - $('div#arbetsmarknadsfolder').outerHeight()
	});
	enlargeImg($($('div#arbetsmarknadsfolder img')[0]), function() {
		location.href="#";
	});
}

function handlingsprogrammet() {
	$('div#handlingsprogrammet').css({
		top: 0 - $('div#handlingsprogrammet').outerHeight()
	});
	enlargeImg($($('div#handlingsprogrammet img')[0]), function() {
		location.href="#";
	});
}

function fixForm(element) {
	$('input[data_default],textarea[data_default]', element).each (function () {
		if ($(this).attr('type') == 'password') $(this).attr('data_default_is_password', 'true');
		if (this.value == '') {
			this.value = $(this).attr ('data_default');
					if ($(this).attr('data_default_is_password') == 'true') {
						$textinput = $('<input class="default-password" type="text" value="' + $(this).attr('data_default') + '" />');
						$(this).hide().after($textinput).next('input.default-password').focus(function() {
							$(this).prev('input[type="password"]').show().focus();
							$(this).remove();
						});
					}
		}
		$(this)
			.focus (function () {
				if (this.value == $(this).attr ('data_default')) {
					this.value = '';
				}
			})
			.blur (function () {
				if (this.value == '') {
					this.value = $(this).attr ('data_default');
					if ($(this).attr('data_default_is_password') == 'true') {
						$textinput = $('<input class="default-password" type="text" value="' + $(this).attr('data_default') + '" />');
						$(this).hide().after($textinput).next('input.default-password').focus(function() {
							$(this).prev('input[type="password"]').show().focus();
							$(this).remove();
						});
					}
				}
			});
	});
}

function positionEnlargedBox() {
	$('div#enlargedFrame1')
		.css({
			'left': parseInt(($(window).width() / 2) - ($('div#enlargedFrame1').width() / 2)),
			'top': parseInt(($(window).height() / 2) - ($('div#enlargedFrame1').height() / 2) + $(window).scrollTop())
		});
		$contentBox = $($('div#enlargedFrame4').children()[0]);
		$('img#enlargedClose').css({
			'position': 'absolute',
			'top': $contentBox.offset().top + 5,
			'left': $contentBox.offset().left + $contentBox.innerWidth() - $('img#enlargedClose').width() - 5
		});
}

function enlargeHtml(htmlurl) {
	dimScreen(function() {
		$.ajax({
			type: "POST",
			url: htmlurl,
			async: true,
			data: data,
			dataType: 'html',
			success: function(data) {

				$("body").append('<div id="enlargedFrame1" style="position: absolute; left:-2000; top:-2000"><div id="enlargedFrame2"><div id="enlargedFrame3"><div id="enlargedFrame4"><div id="enlargedContent" style="width: 370px; background: #fff;"></div></div></div></div></div><img src="close.gif" id="enlargedClose" width="15" height="15" style="cursor: pointer" />');
				$('div#enlargedContent').html(data);
				fixForm($('div#enlargedContent'));
				positionEnlargedBox(true);
				$(window).scroll(function(){
					positionEnlargedBox(true);
				});
				$(window).resize(function(){
					positionEnlargedBox(true);
				});

				$('img#enlargedClose, div#dimming, input.cancel').click(function() {
					removeEnlargedImage(function() {
						location.href="#";
					});
				});
				$('input#betcheck').click(function() {
					if ($(this).is(':checked')) {
						$('input[name="kod"]').show();
					} else {
						$('input[name="kod"]').hide();
					}
				});
				$('div.medlem input.send').click(function() {
					$('div.medlem textarea[data_default], div.medlem input[data_default]').each(function() {
						if($(this).attr('data_default') == $(this)[0].value) {
							$(this).val('');
						}
					});
					var betcheck = 0;
					if ($('input#betcheck').is(':checked')) {
						betcheck = 1;
					}
					var centralen = '';
					if ($('input#centralen').is(':checked')) {
						centralen = 'Vill ha tillgång till Centralen.';
					}
					$.ajax({
						type: "POST",
						url: 'medlem.php',
						async: true,
						data: {
							'vill': $('input[name="vill"]:checked').val(),
							'betcheck': betcheck,
							'kod': $('input[name="kod"]').val(),
							'namn': $('input[name="namn"]').val(),
							'fodd': $('input[name="fodd"]').val(),
							'adr': $('input[name="adr"]').val(),
							'postadr': $('input[name="postadr"]').val(),
							'tele': $('input[name="tele"]').val(),
							'epost': $('input[name="epost"]').val(),
							'centralen': centralen,
							'txt': $('textarea[name="txt"]').val()
						},
						dataType: 'json',
						success: function(data) {
							if (data['error'] == '') {
								removeEnlargedImage(function() {
									alert('Tack, vi återkommer till dig med mer information inom några veckor!');
									location.href="#";
								});
							} else {
								$('div.medlem textarea[data_default], div.medlem input[data_default]').each(function() {
									if ($(this)[0].value == '') $(this).val($(this).attr('data_default'));
								});
								alert(data['error']);
							}
						}
					});
				});
			}
		});
	});
}

function medlem() {
	enlargeHtml('medlem.php');
}

$(document).ready(function() {
	addKlajaxHashChangeHook(function(hash) {
		if (hash == 'arbetsmarknaden') {
			arbetsmarknaden();
		}
		if (hash == 'handlingsprogram') {
			handlingsprogrammet();
		}
		if (hash == 'medlem') {
			medlem();
		}
	});
	$('select#specsokdropdown').change(function() {
		if ($(this).val() != '') {
			$('div.specsok').hide(400);
			$('div#' + $(this).val()).show(400);
		}
	});
});




