/** 
 * @fileOverview Main site code that should follow the namespace UKISA.site.*
 * @version 0.0.1
 * @changeLog Created.
 */

UKISA.namespace("site");

/**
 * Custom implementation of the simple drop down and the weather widget to replace the use of a search form.
 */
UKISA.site.WeatherWidget = function() {
	var dd, ww;

	
	ww = new UKISA.widget.Weather("weather-search-form", "#footer-nav-weather div.forecast");

	//dd = new UKISA.widget.SimpleCssDropDown("weather-region-filter-selector");
	//dd.onTextChange.subscribe(function(e, args) {
	//	
	//}, this);

	dd = new YAHOO.widget.Button({  
		id: "weather-widget-location-dd",  
		name: "weather-widget-location-dd", 
		label: "Choose a county...", 
		type: "menu",   
		menu: "weather-widget-location",  
		container: "weather-region-filter-selector",
		menuminscrollheight: 120
	}); 
	dd.on("selectedMenuItemChange", function(e) {
		ww.search(e.newValue.value, e.newValue.srcElement.text);
	}); 

};

/**
 * Navigation related methods such as creating modals/dialogs or enhancing normal links.
 */
 UKISA.site.Navigation = {
	/**
	 * Create a modal box for the terms.
	 *
	 * @requires UKISA.site.Modal
	 */
	terms: function(o) {
		var modal, load, success, failure, callback, link;

		// Used for closures;
		link = o;

		load = function() {
			var a = document.createElement("div");
			a.id = "terms-modal";
			a.className = "busy";

			var b = document.createElement("p");
			b.innerHTML = "Please wait&hellip;";

			a.appendChild(b);

			return a;
		};

		success = function(text) {
			var a = document.createElement("div");
			a.id = "terms-modal";
			a.innerHTML = text;
		
			return a;
		};

		failure = function() {
			var a = document.createElement("div");
			a.id = "add-to-basket-modal";

			var b = document.createElement("p");
			b.className = "error";
			b.innerHTML = "Sorry, there was a problem and we could not fetch the page for you.";

			a.appendChild(b);

			return a;
		};
		
		modal = new UKISA.widget.Modal("terms-modal", load());
		modal.show();
		
		callback = {
			success: function(o) {
				var q;

				var footer = [
					"<p class=\"print-modal\"",
						"<a class=\"print\" href=\"#\" onclick=\"return UKISA.util.print('modal-terms-print-reference');\">Print</a>",
					"</p>"
				];

				modal.setBody(success(o.responseText));
				modal.setFooter(footer.join(""));

				// Create an IFRAME to allow printing of the contents.
				var iframe = document.createElement("iframe");
				iframe.id = "modal-terms-print-reference";
				iframe.name = "modal-terms-print-reference";
				iframe.src = link.href;
				iframe.width = "1";
				iframe.height = "1";
				iframe.style.position = "absolute";
				iframe.style.left = "-999em";
				iframe.style.top = "-999em";
				document.body.appendChild(iframe);
			},
			failure: function(o) {
				modal.setBody(failure());
			}
		};	

		// Send the Ajax request.
		ajax = YAHOO.util.Connect.asyncRequest("GET", o.href, callback);

		return false;
	}
};
