$(document).ready(function() {
	var lang = (typeof n_lang != 'undefined' && n_lang != null) ? n_lang : 'en-us';
	loadLocalStr(lang, initPage);
	
	//reduce font size of languages that are verbose
	if($("#banner_right .block p").html() && $("#banner_right .block p").html().length > 200){
		$("#banner_right .block p").css("font-size", "12px");
		$("#banner_right .block p").css("line-height", "13px");
		$("#banner_right .block a").css("font-size", "10px");
	}

});

function loadLocalStr(lang, callback) {
	var localStr;

	//get language, and fetch appropriate file
	$.ajax({
		type: "GET",
		url: "/common/inc/local/solution/solution_" + lang + ".json",
		dataType: "json",
		success:function(data, textStatus) {
			callback(data);
		},
		error: function(XMLHttpRequest, textStatus) {
			if(lang != 'en-us') {
				loadLocalStr('en-us', callback);
			}
			else {
				callback({more: "More", less: "Less" })	
			}
		}
	});		
}

function initPage(localStr) {
	$(".extra").css("display", "none");

	$(".toggle a").each(function () {
		var linkTitle = $(this).parent().prevAll("h5:first").text();
		$(this).attr("href","#"+linkTitle.replace(/ /g, "-").toLowerCase());
	}).click (function() {
		var extra = $(this).parent().prev();
		if($(extra).css("display") == "none"){
			$(extra).css("display", "block");
			$(extra).prev().children(".extra").css("display", "inline");
			$(this).html("<strong>-</strong> "+localStr.less);
		}
		else{
			$(extra).css("display", "none");
			$(extra).prev().children(".extra").css("display", "none");
			$(this).html("<strong>+</strong> "+localStr.more);
		}
		return false;
	});


	if(location.hash){
		//when the page first loads and goes to the hash, the above code shrinks all the toggle boxes, thus decreasing the height,
		//and messing up the correct browser viewport location. so, here below we'll smooth scroll to the hash
		var targetOffset = $('a[name='+location.hash.replace(/#/, '')+']').offset().top;
		var target = location.hash;
		// event.preventDefault();
		$("html, body").animate({
			scrollTop: targetOffset
			}, 500,
			function() {
				location.hash = target;
			}
		);

		//now, expand the corresponding hash's toggle box
		$("a[name="+location.hash.replace(/#/, '')+"]").nextAll(".extra:first").css("display", "block").next().children(":first-child").html("<strong>-</strong> "+localStr.less);
	}

}