// Load the appropriate functions when the document is ready.
$(document).ready(function() {
	
	/*
	
	// DynamicWeb killed my beautiful javascript :|
	
	$("#rightbox li a.highlight").bind("click", function() {
		$("#rightbox").slideUp("normal", function() {
			$("#rightbox2").slideDown("normal");
		});
		
		// Don't use the original link if javascript is enabled, show our fancy box instead
		return false;
	});
	
	$("#rightbox2 li.closeself a").bind("click", function() {
		$("#rightbox2").slideUp("normal", function() {
			$("#rightbox").slideDown("normal");
		});
		
		return false;
	});
	*/
	
	$("a.submitgo").each(function(e) {
		// I have to seperate them into 2 seperate click functions, as otherwise it will both submit AND go to the link
		$(this).bind("click", function() {
			return false;
		});

		$(this).bind("click", function() {
			$(this).parents("form").submit();
		});	
	});
	
	// Do pagination on DealerSearch
	if ($("table.dealerlist").size())
	{
		paginateDealerSearch();
	}

	// Create archive in News Archive
	if ($("#newslistsort").size())
	{
		sortNewsArchive();
	}
});

function sortNewsArchive()
{
	var currentyear = 0;
	var current = null;
	var zebra = null;
	var temp = null;
	var target = null;
	var even = true;
	
	if ($("#newslistsort > div").size()) {
		$("#newslistsort > div").each(function() {
			if (!$(this).is("."+currentyear))
			{
				// Check if a current element (if this is not the first) and append it to the document
				if (current != null) {
					zebra = (even) ? "stripe" : "";
					temp = $("<div class='monthcategory'><a href='#' class='newsselector "+zebra+"' rel='"+currentyear+"'>"+currentyear+"</a></div>");			
					$("#newslistsort").append(temp).append(current);
					current = null;
					even = !even;
				}
				
				// If this is empty, it means we're starting a new archive section with a new year
				if (!current) {
					currentyear = $(this).attr("class");
					current = $("<div id='archive"+currentyear+"' class='newsarchive'></div>");
				}
			}
			
			current.append(this);
		});
	
		zebra = (even) ? "stripe" : "";
		temp = $("<div class='monthcategory'><a href='#' class='newsselector "+zebra+"' rel='"+currentyear+"'>"+currentyear+"</a></div>");			
		$("#newslistsort").append(temp).append(current);
		current = null;
		
		// Hide the archives 
		$("div.newsarchive").show();
		$("a.newsselector").each(function() {
			$(this).bind("click", function() {
				target = $(this).attr("rel");			
				$("#archive"+target).slideToggle("fast");
				return false;
			});
		});
	}
}

var curpage = 1;

function paginateDealerSearch()
{
	var base = $("table.dealerlist");
	var include = base.parent();
	var header = base.find("tr:first");
	var entries = base.find("tr:gt(0)");
	var curElm = null;
	var first = true;
	var i = 0;
	var t = 0;
	
	// First we fix the e-mail/website icons for empty directions
	entries.find("td a").each(function() {
		if ($(this).attr('href') == null || $(this).attr('href') == "mailto:") {
			$(this).remove();
		}
		else
		{
			$(this).parent().css('text-align','center');
		}
	});

	// Check if there are any results at all
	if (entries.size())
	{
		// Add them to the document (but only if necessary)
		if (entries.size() > 1000)
		{	
			// Create the Next/Previous links and place them above the list
			var pagectrls = $("<div style='height: 10px;'></div>");
	
			// Previous link - only show when you are further than page 1
			var prev = $("<a href='#' id='prevpage' style='display: none; float: left;'>&lsaquo; Forrige side</a>");
			prev.bind("click", function() {
				$("#dlist"+curpage).css('display','none');
				curpage--;
				$("#dlist"+curpage).css('display','block');
		
				if (curpage == 1)
				{
					$(this).css('display','none');
				}
				
				$("#nextpage").css('display','inline');
				
				return false;
			});
			
			// Only show when we are not on the last page - check if "next page" exists
			var next = $("<a href='#' id='nextpage' style='float: right'>N&aelig;ste side &rsaquo;</a>");
			next.bind("click", function() {
				$("#dlist"+curpage).css('display','none');
				curpage++;
				$("#dlist"+curpage).css('display','block');
				
				if ($("#dlist"+(curpage+1)).size() == 0)
				{
					$(this).css('display','none');
				}
				
				$("#prevpage").css('display','inline');
				
				return false;
			});

			pagectrls.append(prev).append(next);
			include.append(pagectrls);
		}

		// Arrange them up nice and neatly in 10-row tables and hide all of them but the first
		entries.each(function(intIndex) {
			if (!curElm)
			{
				t++;
				curElm = $("<table class='dealerlist' id='dlist"+t+"'></table>");
				if (!first)
				{
					curElm.css('display', 'none');
				}
				curElm.append(header.clone());
				first = false;
			}
			
			curElm.append($(this));
			i++;
			
			if (i >= 1000)
			{
				include.append(curElm);
				curElm = null;
				i = 0;
			}
		});
		
		// Include the last element if it hasn't been already
		if (curElm)
		{
			include.append(curElm);
		}
	}
	else
	{
		include.append($("<div id='pagecontrol'>Der blev ikke fundet en bager, der matcher dine s&oslash;gekriterier.</div>"));
	}
	
	base.remove();
}