//INITIALISE HOMEPAGE SHUFFLE MODULE

$TORA.whenReady(function() {
	$TORA.Requirement(["shuffle::ShuffleWidget", "environment"])(function() {

		var shufflePackage = $TORA("shuffle"),
				addEventListener = $TORA("common").dom.addEventListener,
				dataProvider = shufflePackage.ShuffleDataProvider();
				
		var container = document.getElementById("hp-shuffle-wrapper"),
				whatsThis = document.getElementById("hp-what-link"),
				whatsThisText = document.getElementById("hp-discover-desc");

		dataProvider.requestShuffleData(function() {

			var config = { dataProvider: dataProvider, smallImages: true },
					report = $TORA("PAGE").getProperty("pageReport");

			if (report) config.report = report;
			var shuffleWidget = shufflePackage.ShuffleWidget(config);

			var translations = dataProvider.getTranslations();
			var hpShuffleWidget = document.createDocumentFragment();
			var shuffleWidgetHandlers = {
			     "refresh" : function() { shuffleWidget.refresh();},
			     "goToRandom" : function() { shuffleWidget.gotoRandom();}
			};

			// Create elements
			var form = document.createElement("form"),
				shuffleRandomItems = shuffleWidget.getNode(),
				shuffleBarActions = document.createElement("fieldset"),
				shuffleBarOptions = document.createElement("legend"),
				shuffleBarOptionsText = document.createElement("strong"),
				actions = document.createElement("ul"),
				submit = document.createElement("li"),
				random = document.createElement("li"),
				submitButton = document.createElement("input"),
				randomInputTag = document.createElement("input");
			
			// Create attributes
			form.className = "shuffle-bar-options";
			form.method = "GET";
			form.action = "javascript:void(0);";
			shuffleBarActions.className = "shuffle-actions-container";
			actions.className = "shuffle-actions";
			submit.className = "shuffle-submit";
			random.className = "shuffle-random";
			submitButton.type = "submit";
			submitButton.name = "Shuffle";
			submitButton.value = translations["bar-discover"];
			randomInputTag.type = "submit";
			randomInputTag.name = "random";
			
			// Create text nodes
			shuffleBarOptionsText.innerHTML = translations["bar-title"]; 
			randomInputTag.value = translations["bar-feeling-lucky"];
			
			// Event listeners
			addEventListener.call(submitButton, "click", shuffleWidgetHandlers["refresh"]);
			addEventListener.call(randomInputTag, "click", shuffleWidgetHandlers["goToRandom"]);
			addEventListener.call(whatsThis, "mouseover", function() {
				whatsThisText.className = "hp-discover-show"; 
			});
			addEventListener.call(whatsThis, "mouseout", function() {
				whatsThisText.className = ""; 
			});
			
			// Add items to container
			form.appendChild(shuffleRandomItems);
			submit.appendChild(submitButton);
			actions.appendChild(submit);
			random.appendChild(randomInputTag);
			actions.appendChild(random);
			shuffleBarOptions.appendChild(shuffleBarOptionsText);
			shuffleBarActions.appendChild(shuffleBarOptions);
			shuffleBarActions.appendChild(actions);
			form.appendChild(shuffleBarActions);
			hpShuffleWidget.appendChild(form);
			container.appendChild(hpShuffleWidget);
		});
		
	});	
});

