


// Bound to "page navigation" links in lists 
function list_load_page(el_link)
{
	if(typeof el_link == "string") {
		var page = el_link.replace(/^sida-(\d+)$/, "$1");
		var uri = page + ".html";
	}
	else {
		var uri = el_link.href;
		var page = uri.replace(/.+(\d+)\.html$/, "$1");
	}

	$.get(uri, { ajax: 'full' }, function(data, status) {
		$(".list_content").replaceWith(data);
		$('.page_nav a').bind('click', function(){ return list_load_page(this);});
	});
	
	// Set location to internal link
	window.location.hash = "#sida-" + page;

	return false;
}


// Bound to "See all" links in the index/main page
function index_adlist_expander(el_link)
{
	if(typeof el_link == "string") {
		el_link = $("#" + el_link + " a.all-link")[0];
		if(!el_link) return true;
	}
	if(window.index_adlist_cache == undefined) {
		window.index_adlist_cache = {
			compact: {},
			full: {},
			current: null
		};
	}
	
	var el_container = el_link.parentNode.parentNode;
	var compactlist = $(".ad-list-compact", el_container);
	var id = el_container.id;
	var uri = el_link.href;

	if(!window.index_adlist_cache.compact.id) {
		window.index_adlist_cache.compact[id] = compactlist;
	}

	$.get(uri, { ajax: 'full' }, function(data, status) {
		data = data.replace(/href=["']([^"']+)["']/g, "href=\"" + id + "/$1\"");
		var full_list = $("<div class='ad-list-full'>"+data+"</div>");
		compactlist.replaceWith(full_list);
		$('.page_nav a').bind('click', function(){ return index_adlist_load_page(this, id); });

		var id_old = window.index_adlist_cache.current;
		if(id_old != id) {
			$("#" + id_old + " .ad-list-full").replaceWith(window.index_adlist_cache.compact[id_old]);
		}
		window.index_adlist_cache.current = id;

	});



	// Set location to internal link
	window.location.hash = "#" + id;

	return false;
}

function index_adlist_load_page(el_link, id)
{
	$.get(el_link.href, { ajax: 'full' }, function(data, status) {
		data = data.replace(/href=["']([^"']+)["']/g, "href=\"" + id + "/$1\"");
		$(".list_content").replaceWith(data);
		$('.page_nav a').bind('click', function(){ return index_adlist_load_page(this, id);});
	});

	return false;
}






function adform_init()
{

	$("#adform input[name='preview']").bind('click', function() {
		$("#adform").validate().form();
		$("#adform").ajaxSubmit({
			target: "#forhandsgranskning",
			url: "?ajax=preview&preview=1",
			type: "POST",
			success: function() { 
				// Hopefully we'll only get here when previewing
				window.location.hash = "#forhandsgranskning";
			}
		});
		return false;
	});
	$("#adform").validate({
		errorPlacement: function(error, element) {
			error.appendTo(element.parent("li"));
		},
		messages: {
			Namn: "Måste fyllas i",
			Epost: "Måste fyllas i",
			Slutdatum: "ÅÅÅÅ-MM-DD. Max 3 månader",
			Rubrik: "Måste fyllas i",
			Text: "Måste fyllas i"
		}
	});
}

function form_array_to_object(data)
{
	var o = {};
	for(var i = 0; i < data.length; ++i) {
		o[data[i].name] = data[i].value;
	}
	return o;
}


function ad_comment_init()
{
	$("#commentform").ajaxForm({
		url: "kommentera.html?ajax=comment",
		resetForm: true,
		success: function(responseText, statusText) {
			var comment = $(responseText);
			var id = comment[0].id;
			var row_class = $("#kommentarer div.comment").length % 2 ? "odd" : "even";
			comment.addClass(row_class);
			$("#kommentarer").append(comment);
			window.location.hash = id
		}
	});
	$("#commentform").validate({
		errorPlacement: function(error, element) {
			error.appendTo(element.parent("li"));
		},
		messages: {
			Namn: "Måste fyllas i",
			Epost: "Måste fyllas i",
			Text: "Måste fyllas i"
		}
	});
}
