(function(inputgenres) {
	var features = new Array();
	var dataobj = {};
	var features_to_load = 0;
	var number_of_features = 0;
	var number_of_pages = 0;
	var currentpage = 0;
	var currentwidth = 0;
	var is_mobile = $('#aoc').attr('data-ismobile');
	var feature_width = parseInt($('#aoc').attr('data-featurewidth'));
	var page_width = parseInt($('#aoc').attr('data-windowwidth'));
	var features_per_page = Math.floor(page_width / feature_width);
	var sitelist_url = "/tv/includes/_also_on_cbc_sitelist";
  var genres = inputgenres.split('|');

  //console.log(genres);
	//console.log("set variables, executing... ["+sitelist_url+"]");
	
	function shuffleArray(array) {
		for (var i = array.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
		return array;
	}	

 	
  function writeFeatures() {
    //console.log("writeFeatures()");
		var random_features = shuffleArray(features);
		$('#also-on-cbc-features-holder').html('');
		for (var i=0;i<random_features.length;i++) {
      currentwidth += feature_width;
      var currentfeature = random_features[i];
      var featurediv = '<figure class="also-on-cbc-feature-item"><a href="'+currentfeature.url+'" target="_top"><img src="'+currentfeature.image+'" title="'+currentfeature.name+'" alt="'+currentfeature.name+'"></a>';
      switch(currentfeature.phase) {
        case "premiering" :
          featurediv += '<figcaption>NEW SERIES</figcaption>';
          break;
        case "pre-season" :
          featurediv += '<figcaption>RETURNING</figcaption>';
          break;
        default :
          break;
      }
      featurediv += '</figure>';
      $('#also-on-cbc-features-holder').width(currentwidth).append(featurediv);
		}
	}
  
	$(dataobj).load(sitelist_url,function(response, status, xhr) {
    //console.log("loading XML");
		if (status==="success") {
			var myxml = $.parseXML(response);
      var showlist = $(myxml).find('show');
			features_to_load = number_of_features = showlist.length;
			for (var i=0;i<number_of_features;i++) {
        var newshow = $(showlist[i]);
        var newshowObject = {};
        newshowObject.url = newshow.find('show_url').text();
        newshowObject.name = newshow.find('show_name').text();
        newshowObject.description = newshow.find('show_description').text();
        newshowObject.image = newshow.find('show_image').text();
        newshowObject.genre = newshow.find('show_genre').text();
        newshowObject.phase = newshow.find('show_phase').text().toLowerCase();
        //console.log("loading ["+newshowObject.genre+" : "+newshowObject.name+"]");

        for (var g=0;g<genres.length;g++) {
          //console.log("["+genres[g].toLowerCase()+"] ["+newshowObject.genre.toLowerCase()+"]")
          if (genres[g].toLowerCase()===newshowObject.genre.toLowerCase()){
            //console.log("genres match, adding a "+genres[g])
            features.push(newshowObject);
          }
        }
			}
      //console.log(features);
      writeFeatures();
		} else {}});
	
})("Comedy");