/// <reference path="jquery-vsdoc2.js" />
var litium = {};

litium.templates = {
	staticPage: (function(litium) {
		var that = {};
		var defaults = { iframeHeight: '600px' };

		that.resize = function(iframe, iframeHeight) {
			try {
				iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 'px';
			} catch (e) {
				iframe.style.height = iframeHeight;
			}
		};

		that.init = function(options) {
			///<summary>
			/// Initialises the resizing of iframes in the document.
			/// Parameters: options: { iframeHeight: height declaration, e.g. '600px' }
			///</summary>
			var i;
			var iframe;
			var iframes = document.getElementsByTagName('iframe');
			var settings = jQuery.extend({}, defaults, options);
			var onloadValue = 'litium.templates.staticPage.resize(this, \'' + settings.iframeHeight + '\');'

			for (i = 0; iframe = iframes[i]; i++) {
				iframe.setAttribute('onload', onloadValue);
			}
		};

		return that;
	})(),
	eventList: (function(litium) {
		var that = {};
		var defaults = { currentMonth: 1 };

		that.init = function(options) {
			var settings = jQuery.extend({}, defaults, options);
			
			that.initEventMenu();
			
			var button = $("#c_linkButton" + settings.currentMonth);
			var month = settings.currentMonth;
			while(!button)
			{
				month++;
				button = $("#c_linkButton" + month);
				
				if(month > 12)
					break;
			}
			
			if(button)
				button.click();
		};
		
		that.initEventMenu = function()
		{
			$("body.p-eventlist .selector .months ul li span a").each(function()
				{
					var href = $(this).attr("href");
					var month = href.replace("?month=", "");
					var count = $(".month_" + month).length;
					if(count == 0){
						var text = $(this).text();
						$(this).parent().empty().text(text).addClass("disabled");
					}
				}
			);
		}

		/// <summary>
		/// Shows all events in provided month and hides the rest
		/// </summary>
		that.showEvents = function(month, sender)
		{
			$("body.p-eventlist .selector .months ul li").removeClass("selected");
			$(sender).parent().parent().addClass("selected");
			
			$(".event").hide();
			$(".month_" + month).show();
		}

		return that;
	})()
	
	
				
};

/// <summary>
/// Init page, when DOM is loaded and before the page content is loaded
/// </summary>
$(document).ready(function() {
    litium.initButtons();
})

/// <summary>
/// Init buttons
/// </summary>
litium.initButtons = function() {
    $("input.btn").wrapperEffects({ wrapperClass: "button" });
    $("a.btn").wrapperEffects({ wrapperClass: "button" });
}

/// <summary>
/// Post form to SL Reseplanerare, encoded as ISO-8859-1 instead of UTF-8
/// </summary>
litium.submitSubwayForm = function() {

    // Get the value user entered
    var stopValue = $("input#subwayStop_utf8").val();
    
    // Set value in SL form
    $("input#subwayStop").val(stopValue);
    
    // Post SL form
    $("form#subwayform").submit();
}
