var currentCol2 = currentCol3 = currentCol4 = currentCol5 = 30;

$(document).ready(function() {
	// set fontsize from cookies
	if($.cookie("fontsize") != undefined && $.cookie("fontsize") != null) {
		$('#content_lhs, #content_rhs').addClass($.cookie("fontsize"));
		$("#btn_prefs_text_" + $.cookie("fontsize")).addClass("active");
	} else {
		$("#btn_prefs_text_medium").addClass("active");						// default to the medium text size
	}

	// set region from cookies
	if($.cookie("region") != undefined && $.cookie("region") != null) {
		$("#region_pref_" + $.cookie("region")).parent().addClass("active");
		$("#region_pref_" + $.cookie("region")).attr("checked", "checked");
	} else {
		$("#region_pref_1").parent().addClass("active");					// default to the 1st region
		$("#region_pref_1").attr("checked", "checked");
	}

	// set colour style from cookies
	if($.cookie("colour") != undefined && $.cookie("colour") != null) {
		$("#btn_prefs_colour_" + $.cookie("colour")).addClass("active");
	} else {
		$("#btn_prefs_colour_blue").addClass("active");					// default to white!!!!!!!!!!!!! blue?
	}

	for(var i=1; i<=5; i++) {
		$("#sublist" + i + " li").each(function() {
			if($(this).hasClass("col2")) {
				var topValue = parseInt(currentCol2);
				$(this).css("top", topValue);
				currentCol2 += $(this).height() + 10;
			}
			if($(this).hasClass("col3")) {
				var topValue = parseInt(currentCol3);
				$(this).css("top", topValue);
				currentCol3 += $(this).height() + 10;
			}
			if($(this).hasClass("col4")) {
				var topValue = parseInt(currentCol4);
				$(this).css("top", topValue);
				currentCol4 += $(this).height() + 10;
			}
			if($(this).hasClass("col5")) {
				var topValue = parseInt(currentCol5);
				$(this).css("top", topValue);
				currentCol5 += $(this).height() + 10;
			}
		});
		currentCol2 = currentCol3 = currentCol4 = currentCol5 = 30;
	}
});

jQuery.fn.styleSwitcher = function() {
	$(this).click(function(){
		loadStyleSheet(this);
		return false;
	});
	function loadStyleSheet(obj) {
		$(obj).parent().parent().find("a").removeClass("active");
		$(obj).addClass("active");

		$('body').append('<div id="overlay" />');
		$('body').css({height:'100%'});
		$('#overlay')
			.css({
				display: 'none',
				position: 'absolute',
				top:0,
				left: 0,
				width: '100%',
				height: '100%',
				zIndex: 1000,
				background: 'black url(images/loading.gif) no-repeat center'
			})
			.fadeIn(500,function(){
				$.get( obj.href+'&js',function(data){
					//alert(data);
					$('#colourStyle').attr('href','css/' + data + '.css');
					cssDummy.check(function(){
						$('#overlay').fadeOut(500,function(){
							$(this).remove();
						});	
					});
				});
			});
	}
	var cssDummy = {
		init: function(){
			$('<div id="dummy-element" style="display:none" />').appendTo('body');
		},
		check: function(callback) {
			if ($('#dummy-element').width()==2) callback();
			else setTimeout(function(){ cssDummy.check(callback) }, 200);
		}
	}
	cssDummy.init();
}

jQuery.fn.fontSizeSwitcher = function() {
	$(this).click(function(){
		changeFontSize(this);
		return false;
	});
	function changeFontSize(obj) {
		$(obj).parent().parent().find("a").removeClass("active");
		$(obj).addClass("active");
		switch(obj.name) {
			case "large":
				$('#content_lhs, #content_rhs').removeClass("medium").removeClass("small").addClass("large");
				break;
			case "medium":
				$('#content_lhs, #content_rhs').removeClass("large").removeClass("small").addClass("medium");
			break;
		}
		$.cookie("fontsize", obj.name);
	}
}



jQuery.fn.resetStyle = function() {
	$(this).click(function(){
		$("#btn_prefs_colour_blue").trigger("click");
		$("#btn_prefs_text_medium").trigger("click");
		$.cookie("fontsize", null);
		$.cookie("colour", null);
		return false;
   });
}

jQuery.fn.switchRegion = function() {
	$(this).change(function(){
		changeRegion(this);
		return false;
	});
	function changeRegion(obj) {
		$(obj).parent().parent().find("li").removeClass("active");
			
		$(obj).parent().addClass("active");

		var regionId = $(obj).attr("id").replace("region_pref_", "");
		if($("#waterWidget").length > 0) {
			document.getElementById("waterWidget").changeRegion(regionId);
		} else {
			// fallback html here
		}
		$.cookie("region", regionId);
	}
}
