$(function(){
	// add classes for first-child, last-child and empty
	$('body :first-child').addClass('first-child');
	$('body :last-child').addClass('last-child');
	$('body :empty').addClass('empty');
	
	// Who's on first?
	var initialID = 1;
	var listAlwaysOpen = false;
	// Record inital selection
	oldSelection = $("div.text#" + initialID);
	oldTogglePoint = $("span.clickToExpand[rel=" + initialID + "]");
	// Hide all..
	$("div.text").hide ();
	// then do the right thing
	oldSelection.slideDown ();
	oldTogglePoint.hide ();
	
	// Attach clikchandlers to toggle-points
	$("span.clickToExpand").each(function() {
		var id = $(this).attr("rel");
		if (id != undefined)
		{
			$(this).click(function () {
				var newSelection = $("div.text#" + id);
				var newTogglePoint = $("span.clickToExpand[rel=" + id + "]");
				
				// If we click on already opened
				if (!listAlwaysOpen && oldSelection !== undefined && oldSelection.attr("id") === newSelection.attr("id"))
				{
					oldSelection.slideToggle();
				}
				else
				{
					oldSelection.slideUp ();
					newSelection.slideDown ();
				}
				newTogglePoint.hide ();
				oldTogglePoint.show ();
				oldSelection = newSelection;
				oldTogglePoint = newTogglePoint;
			});
		}
	});

	/* SIMPLE ROUNDED CORNERS
	---------------------------------------------*/
	$("#banner .column").each(function() {
	    $(this).wrapInner("<div class='rounded-center'></div>");
	    $(this).prepend("<div class='rounded-top'></div>");
	    $(this).append("<div class='rounded-bottom'></div>");
	});
	
	/* DROPDOWN
	-------------------------------------*/
	createDropDown();
	
	$(".dropdown dt").click(function(event) {
		event.preventDefault();
		var dropID = $(this).closest("dl").attr("id");
		$("#" + dropID).find("ul").toggle();
	});

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown")) {
		  $(".dropdown dd ul").hide();
        }
	});


	$(".dropdown dd ul a").click(function(event) {
		event.preventDefault();
		var dl = $(this).closest("dl");
		var dropID = dl.attr("id");
		var sourceID = dl.attr("source");
		var text = $(this).html();
		
		var source = $("select[id='" + sourceID + "']");
		$("#" + dropID + " dt a").html(text);
		$("#" + dropID + " dd ul").hide();
		source.val($(this).find("span.value").html());
		var option = source.find("option[value='" + $(this).find("span.value").html() + "']");
		source.selectedIndex = source.find("option").index(option);
		source.change();
	});

	function createDropDown() {
		$("select[id*='LanguageList']").addClass('selectlist');
		var selects = $("select.selectlist");
		var idCounter = 1;
		selects.each(function() {
			var source = $(this);
			var sourceID;
			var dropID = "dropdown_" + idCounter;
			
			//if (this.hasAttribute('id') && source.attr('id') != "") {
			if (source.attr('id') != "") {
				sourceID = source.attr('id');
			}
			else {
				sourceID = "source_" + idCounter;
				source.attr('id', sourceID);
			}
			
			var selected = source.find("option[selected]");
			var options = $("option", source);

			if(source.hasClass("absolute")) {
				$("#content").prepend('<dl id="' + dropID + '" source="' + sourceID + '" class="dropdown absolute"></dl>');
			}
			else {
				source.after('<dl id="' + dropID + '" source="' + sourceID + '" class="dropdown"></dl>');
			}
			$("#" + dropID).append('<dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt>');
			$("#" + dropID).append('<dd><ul></ul></dd>');
			options.each(function() {
				if($(this).text() !== "") {
					$("#" + dropID + " dd ul").append('<li><a href="#">' + $(this).text() + '<span class="value">' + $(this).val() + '</span></a></li>');
				}
			});
			idCounter++;
		});
	}
		$('#AddButton a').html("");
		$('#SearchArea input.button').val('');
});



