/*global YAHOO */
YAHOO.namespace("aos.ee");

YAHOO.aos.ee.Utils = function() {
};

YAHOO.aos.ee.Utils.prototype = {

	/**
	* Show opening hours in YAHOO Panel
	*/
	showHours: function(articleId) {

		var handleSuccess = function(obj) {

			var hourPanel = "AOS_HourPanel" + articleId;
			YAHOO.util.Dom.get('AOS_HoursContainer').innerHTML = "<div id=\"" + hourPanel + "\"><div class=\"hd\"></div><div class=\"bd\"></div><div class=\"ft\"></div></div>";

			var AOS_HoursPanel = new YAHOO.widget.Panel(hourPanel,
				{
					width:"300px",
					height:"300px",
					visible:false,
					draggable:true,
					close:true
				}
			);

			var hours = "";
			if (obj.responseText !== undefined) {
				hours = obj.responseText;
			}

			AOS_HoursPanel.setHeader("Öppettider");
			AOS_HoursPanel.setBody(hours);
			AOS_HoursPanel.render();
			AOS_HoursPanel.center();
			AOS_HoursPanel.show();

		};

		var handleFailure = function(obj) {
			// ignore
		};

		var callback = {
			success: handleSuccess,
			failure: handleFailure,
			timeout: 10000
		};

		var url = "/template/venue/openingHours.jsp";
		if (!YAHOO.lang.isUndefined(articleId)) {
			url += "?articleId=" + articleId;
		}

		YAHOO.util.Connect.asyncRequest("GET", url, callback);

	},


	/**
	* Will show all or the first linkIndex event occurrences in the ul with id id
	*/
	updateEventOccurrences: function(id, tag, linkIndex) {
		var el;

		elements = YAHOO.util.Dom.getElementsBy(function(){return true;}, tag, id);

		/* First three occurrences are shown */
		if (YAHOO.util.Dom.hasClass(elements[linkIndex], 'show')) {
			for(el in elements) {
				// Hide 'show all' link at index linkIndex
				if(el == linkIndex) {
					YAHOO.aos.common.utils.changeClass(elements[linkIndex], 'hide', 'show');
				}
				else {
					YAHOO.util.Dom.removeClass(elements[el], 'hide');
				}
			}
		}
		/* All occurrences are shown */
		else if (!YAHOO.util.Dom.hasClass(elements[elements.length-1], 'hide')) {
			for(el = linkIndex + 1; el < elements.length; el=el+1) {
				YAHOO.util.Dom.addClass(elements[el], 'hide', linkIndex);
			}
			// Show 'show all' link at index linkIndex
			YAHOO.aos.common.utils.changeClass(elements[linkIndex], 'hide', 'show');
		}
	}

};
YAHOO.aos.ee.utils = new YAHOO.aos.ee.Utils();


