if (Object.isUndefined(CodeCompany)) { var CodeCompany = {}; }

CodeCompany.Openings = Class.create({

	log: function(str) {
		if (!Object.isUndefined(site)) { site.log('openingSundays: '+str); }
	},

	initialize: function(options) {
		this.options = Object.extend({
		},options || {successCallback: Prototype.emptyFunction,
			overviewType: 'normal',
			async: true});
				this.templates = 
		{
			normal: 
			{
				normal: new Template('<span id="openings"><strong>*</strong> Næste søndagsåbent: #{day}.#{month}.#{year}</span>')
			},
			global:
			{
				normal: new Template('<h1 class="sifr-bliss2-regular sifr-25px">Husk Søndagsåbent #{day}.#{month}.#{year} Kl. 10-16<!-- --></h1>')
			}
		};
	},

	createOpeningsFromJSON: function(jsonStr) {
		var json;
		//alert(jsonStr);
		try { json=jsonStr.evalJSON(); }
		catch (error) {
			this.log('Malformed JSON: '+jsonStr);
			return;
		}
		//this.log("Sundays says: should enter each");
		json.each(function(date){
			if ((undefined===date.year) || (date.year==='')) { return; }
			if ((undefined===date.month) || (date.month==='')) { return; }
			if ((undefined===date.day) || (date.day==='')){ return; }

			// Build link text
			var text = date.day+'.'+date.month+'.'+date.year;
			//alert(text);
			//var test = 
			this.log('sundays tries templateing: ' + this.templates[this.options.overviewType].normal.evaluate(date));
			//alert(this.templates[this.options.overviewType].normal.evaluate(item));
			$('openings').update(this.templates[this.options.overviewType].normal.evaluate(date));
			},this);
		},

	createSundayOpenings: function() {
		this.log('Creating Sundayopenings');

		// Instantiate ImageMap
		//var im = new CodeCompany.ImageMap(element);

		var url = '/systemservice/sundays?json=1';
		new Ajax.Request(url,{
			method: 'get',
			onSuccess: (function(transport){
				this.createOpeningsFromJSON(transport.responseText);
				switch ( this.options.overviewType )
			{
				case 'normal':

				break;
				
				case 'global':
					document.fire('custom:fontreplace');
				break;
				
				default:
				break;
			}
				
				
			}).bind(this),
			onError: (function(transport){
				this.log('JSON request failed: '+url);
			}).bind(this)
		});
	}

});

