var HowDoI = {
  load : function(selector) {
    var content = $(selector);
	var containerHeight = $('#content-full').height();
	$.address.change(function(event) {
		   var section = $.address.parameter('sect');
		   var question = $.address.parameter('q');
		   
		   if (section) {
		       $('h2.how-do-i-heading').text(section.substring(0, 1).toUpperCase() + section.substring(1));
		       $('.how-do-i-nav a').each(function() {
			     var $this = $(this);
				 $this.removeClass('selected');
				 if ($this.attr('href').indexOf('sect=' + section) > -1) {
				   $this.addClass('selected');
				 }
				 
			   });
			   
			   HowDoI.readData(function(entries) {
			     content.find('>li').remove();
				 var sectionEntries = [];
				 $.each(entries, function(i, entry) {
					if (entry.section == section) {
					  sectionEntries.push(entry);
					}
				 });
				 
				 $.each(sectionEntries, function(i, entry) {
				   var isLast = (i == sectionEntries.length-1);
				   var frag = entry.fragment || '';
				   var li = content.append('<li class="clearfix" style="display:none;"><a name="' + frag + '"/><h3>' + entry.title + '</h3><div class="answer"></div></li>');
				   if (entry.fragment) {
				     li.find('li:last .answer').load('/fragments/how-do-i/' + entry.section + '/' + entry.fragment + '.html',
					   function() {
					     if (!question) {
						 if (isLast) {
						   setTimeout(function() {
						   $('#content-full').height(content.height() + 100);
						   }, 500);
						 }
					     }
					   }
					 );
					 
				   }
				   
				   
				 });
				 
				 if (question) {
				   var anchor = $("a[name='" + question + "']").parent().show();
				 } else {
				   content.find('>li').show();
				 }

				 
			   });
		   }
	 });
	 
  },
  
  loadBlock : function(selector, section, max) {
    var listElem = $(selector);
	
	if (listElem.length == 0) {
	  return;
	}
	
	listElem.find("li").remove();
    HowDoI.readData(function(entries) {
	  var sectionEntries = [];
	  $.each(entries, function(i, entry) {
        if (section == 'landing') {
		  sectionEntries.push(entry);
	    } else if (section == entry.section) {
		  sectionEntries.push(entry);
		}
	  });
	  
	  var selected = HowDoI.selectRandom(sectionEntries, max);
	  
	  $.each(selected, function(i, entry) {
	      listElem.append('<li><a href="/how-do-i/index.html#/?sect=' + entry.section + '&q=' +entry.fragment+ '">' + entry.title + '</a></li>');
	  });
	  setTimeout(function() {
	    Portal.alignHeights();
	  }, 100);
	});
  },
  
  readData : function(callback) {
	  var t = new Date().getTime();
	  $.getJSON('/json/how-do-i.json?t=' + t, callback);
  },
  
  selectRandom : function(entries, max) {
 
	  var selectedIndexes = [];
	  
	  var len = max > entries.length ? entries.length : max;
	  while (selectedIndexes.length < len) {
	    var rand = Math.floor(Math.random()*entries.length);
		
		if ($.inArray(rand, selectedIndexes) == -1) {
		  selectedIndexes.push(rand);
		}
	  }
	  
	  var selectedEntries = [];
	  
	  $.each(selectedIndexes, function(i, selIndex) {
	      selectedEntries.push(entries[selIndex]);
	  });
	  
	  return selectedEntries;
  }
  
};
