function log() {
	if ( typeof console != "undefined" ) {
		console.log(arguments)
	} else if ( typeof opera != "undefined" ) {
		opera.postError(arguments)
	}
}

//
// Make main layout fill the height of the screen if it's too small
//
function adjust_layout() {
	var footer = $("#footer")
	if ( footer.length )
	{
		var diff = $(window).height() - (footer.position().top + footer.outerHeight())

		if ( diff > 20 )
		{
			$("#body").height( $("#body").height() + diff )
			$("#content").height( $("#content").height() + diff )
		}
	}
}

//
// Make two elements the same height if they're in the same position
// (using the greater of the two heights)
//
function match_height( el1, el2 )
{
	el1.css('height','auto');
	el2.css('height','auto');
	if ( el1.position().top == el2.position().top )
	{
		var tallest = Math.max(el1.height(), el2.height())
		$(el1).height(tallest)
		$(el2).height(tallest)
	}
}

function size_adjustments() {
	adjust_layout()

	if ( $("#pg_plans_kvm").length || $("#pg_plans_hybrid-plans").length ) {
		match_height( $("#benefits"), $("#distros") )
		match_height( $("#panels"), $("#extras") )
	}

	if ( $("#pg_plans_panels").length ) {
		match_height( $("#directadmin"), $("#webmin") )
	}

	if ( $("#pg_home").length ) {
		match_height( $("#plan_summary"), $("#contact") )
		match_height( $("#news"), $("#cool") )
	}
}

$(window).resize( function() {
	size_adjustments();
} )


$(document).ready(function() {

	//
	// Close messages
	//
	$(".closable").prepend( "<a href='#' title='Close this message' class='close'>x</a>" ).click( 
		function() {
			$(this).fadeOut("slow", function() { $(this).hide(); location.href='?' })
		}
	)

	// 
	// Close the current window
	//
	$(".close_window").click(
		function() {
			window.close()
		}
	)

	//
	// Toggle expandy sections
	//
	$(".expander").click( 
		function() {
			var contents = $("#" + this.id.substr(8) + "_contents")
			contents.toggle()

			if ( contents.is(':hidden') )
			{
				$(this).addClass("closed")
				$(this).removeClass("open")
				$(this).title = 'Click to expand section'
			}
			else
			{
				$(this).addClass("open")
				$(this).removeClass("closed")
				$(this).title = 'Click to hide section'
			}
		})


	//
	// Show submenus on menu hover
	// But only if menu is in horizontal configuration
	//
	if ( $("#mainmenu #menu_home").position().top == $("#mainmenu #menu_plans").position().top ) {
		$("#mainmenu a").hover( 
			function()
			{
				$(".submenu").hide()
				var submenu = $('#submenu_' + this.id.substr(5))
				if ( submenu )
				{
					submenu.show()
				}
			}
		)
	}

	//
	// When mouse leaves the navigation area, restore the right submenu for the page
	//
	$("#nav").hover(
		null,
		function()
		{
			$(".submenu").hide()
			$(".submenu.selected").show()
		}
	)


	if ( $("#su_to").length )
	{
		var su_to = $("#su_to")
		$.getJSON(secure_path+"/ajax/active-users.pxl",
							null,
							function (data)
							{
								su_to.empty()

								// Populate the dropdown box
								$.each( data, 
												function(i,u)
												{
													var opt = $('<option />').val(u.id).text(u.email)
													if ( u.id == auth.user_id ) opt.prepend("* ")
													if ( u.id == auth.effective_id ) opt.attr( "selected", "1" )
													su_to.append(opt)
												})

								// Change effective id when dropdown changes
								su_to.change( 
									function( )
									{
										$.post(rel_path+'/su/set_effective_id.pxl', { effective_id: $("#su_to").val() },
												function() { 
													window.location.reload()
												})
									}
								)
							} )
	}

	//
	// EVENTS PAGE: submit form when dropdown changes
	//
	$("#user_events").change(
		function( ) {
			$(this)[0].parentNode.submit()
		}
	)


	//
	// MY SERVERS: lazy-load traffic graphs
	//
	$("#expandy_traffic_graphs").click(
		function()
		{
			$("#fourhours").attr('src', 'traffic-graph.pxl?frame=fourhours&guest_id=' + guest_id)
			$("#day").attr('src', 'traffic-graph.pxl?frame=day&guest_id=' + guest_id)
			$("#week").attr('src', 'traffic-graph.pxl?frame=week&guest_id=' + guest_id)
			$("#month").attr('src', 'traffic-graph.pxl?frame=month&guest_id=' + guest_id)
			$("#year").attr('src', 'traffic-graph.pxl?frame=year&guest_id=' + guest_id)
		} )


	//
	// ORDER FORM
	//
	if ( $("form#order").length )
	{
		// 
		// Update total monthly cost based on order form choices
		//
		function update_price( )
		{
			$.getJSON(rel_path+'/ajax/prices.pxl',
				{ plan_id: $("#plan_id").val(),
				  panel_id: $("#panel_id").val(),
				  distro_id: $("#distro_id").val(),
				  num_ips: $("#num_ips").val(),
				  extra_disk: $("#extra_disk").val(),
				  extra_bandwidth: $("#extra_bandwidth").val(),
				  offer_code: $("#offer_code").val() },
					
				function update_price_complete(data)
				{
					if ( data.cost )
					{
						$("#total_cost span").html( data.cost )
						$("#total_cost").show()
					}
					else
					{
						$("#total_cost").hide()
					}

					if ( data.offer )
					{
						$("#offer_description span").html(data.offer)
						$("#offer_description").show()
					}
					else
					{
						$("#offer_description").hide()
					}

					if ( data.num_ips != undefined  )
					{
						$("#num_ips").val(data.num_ips)
					}

					if ( data.extra_disk != undefined )
					{
						$("#extra_disk").val(data.extra_disk)
					}

					if ( data.extra_bandwidth!= undefined  )
					{
						$("#extra_bandwidth").val(data.extra_bandwidth)
					}
				})
		}


		//
		// Update panel choices based on chosen distro
		//
		function update_panels( )
		{
			var old_choice = $.trim( $("#panel_id option:selected").text() )

			$.getJSON(rel_path+'/ajax/panels.pxl',
				{ distro_id: $("#distro_id").val() },

				function update_panels_complete(panels)
				{
					var dropdown = $("#panel_id")
					dropdown.empty()

					dropdown.append( "<option value=''>No panel please</option>" )

					$.each( panels, function( ) {
						id = this[0]
						name = this[1]
						price = this[2]
						if ( price > 0 )
							price = "(&pound;" + price + "/month)"
						else
							price = "(free)"
						if ( name == old_choice )
							dropdown.append( "<option selected='1' value='" + id + "'>" + name + price + "</option>" )
						else
							dropdown.append( "<option value='" + id + "'>" + name + price + "</option>" )
					} )
					
					// enable or disable dropdown depending on whether any panels available
					$("#panel_id").attr("disabled",panels.length ? "" : "disabled")

					// re-select old selection if still available
					if ( $("#panel_id option:selected").is(':hidden') )
					{
						opt0 = $($("#panel_id option").get(0))
						opt0.attr("selected", "1")
					}
				} )

			update_price( )
		}


		//
		// Update distro choices based on chosen plan
		// TODO: obsolete now we're KVM only
		//
		function update_distros( )
		{
			var old_choice = $.trim( $("#distro_id option:selected").text() )

			$.getJSON(rel_path+'/ajax/distros.pxl',
				{ plan_id: $("#plan_id").val() },

				function update_distros_complete(distros)
				{
					var dropdown = $("#distro_id")
					dropdown.empty()
					dropdown.append( "<option value=''>&nbsp;</option>" )

					$.each( distros, function( ) {
						id = this[0]
						name = this[1]
						if ( name == old_choice )
							dropdown.append( "<option selected='1' value='" + id + "'>" + name + "</option>" )
						else
							dropdown.append( "<option value='" + id + "'>" + name + "</option>" )
					} )
				})
		}

		update_distros()
		update_panels()

		$("#plan_id").change(update_price).change(update_distros)
		$("#panel_id").change(update_price)
		$("#num_ips").change(update_price)
		$("#extra_disk").change(update_price)
		$("#extra_bandwidth").change(update_price)
		$("#offer_code").change(update_price)
		$("#distro_id").change(update_panels)
	}


	//
	// CONSOLE BUTTONS - open new window when buttons clicked
	//
	if ( $("#console").length )
	{
		$("#changecd").click( 
			function()
			{
				window.open("change-cd.pxl?guest_id=" + guest_id, "Change CD", "toolbar=no,menubar=no,status=no,directories=no,scrollbars=yes");
			}
		)

		$("#reset").click(
			function()
			{
				window.open("reset.pxl?guest_id=" + guest_id, "Reset", "toolbar=no,menubar=no,status=no,directories=no,scrollbars=yes");
			}
		)
	}

	size_adjustments();
});
