jQuery.noConflict();

jQuery(document).ready(function () {
	// Get the collapsed news elements and tha language labels
	ttnews.expandNews.getStatus();

	//If there is a click to an arrow expand or collapse the news element
	jQuery('div.newsItem div.title div.header span.arrow img').click (function() {
		newsItem = jQuery(this).parent().parent().parent().parent();

		if (ttnews.expandNews.isHidden(newsItem)) {
			ttnews.expandNews.expand(newsItem, true);
		} else {
			ttnews.expandNews.collapse(newsItem, true);
		}
	});
});

// Extension namespace
var ttnews = ttnews || {};

// Plugin namespace
ttnews.expandNews = ttnews.expandNews || {};

ttnews.expandNews = {
	/**
	 * The language labels
	 */
	LLL: null,

	/**
	 * Checks whether a news element is hidden
	 * @param newsItem
	 */
	isHidden: function(newsItem) {
		return jQuery('>div.content',newsItem).hasClass('newsContentHidden');
	},

	/**
	 * Get the collapsed news items and the language labels from the server
	 */
	getStatus: function () {
		jQuery.ajax({
			url:'ajax.php?eID=spickttnewslist_controllerajax&action=getStatus',
			dataType: 'json',
			success: function(response) {
				ttnews.expandNews.LLL = response.LLL;
				ttnews.expandNews.addHoverText(response.LLL);
				ttnews.expandNews.collapseElements(response.data);
			}
		});
	},

	/**
	 * Collapse all the collapsed elements when the page is loaded
	 *
	 * @param elementsArray the elements to collapse
	 */
	collapseElements: function (elementsArray) {
		var nrElements = elementsArray.length;
		for (var i=0; i<nrElements; i++) {
			ttnews.expandNews.collapse(jQuery('#'+elementsArray[i]));
		}
	},

	/**
	 * Change the title and alt of an image
	 *
	 * @param LLL an array containing language keys
	 */
	addHoverText: function(LLL) {
		jQuery.each(jQuery('.newsItem .arrow>img'), function(key,value) {
			jQuery(value).attr('alt',LLL['spickttnewslist.collapse']);
			jQuery(value).attr('title',LLL['spickttnewslist.collapse']);
		});
	},

	/**
	 * Expand a news item
	 * 
	 * @param newsItem a jQuery newsItem Object
	 * @param report a flag whether to report back to the server by ajax
	 */
	expand: function(newsItem, report) {
		jQuery('>div.content',newsItem).removeClass('newsContentHidden');
		jQuery('.arrow>img',newsItem).attr('alt',ttnews.expandNews.LLL['spickttnewslist.collapse']);
		jQuery('.arrow>img',newsItem).attr('title',ttnews.expandNews.LLL['spickttnewslist.collapse']);
		jQuery('.arrow>img',newsItem).attr('src','/fileadmin/spick/bilder/up.gif');

		if (report) {
			jQuery.ajax({
				url:'ajax.php?eID=spickttnewslist_controllerajax&action=expand&newsItem='+jQuery(newsItem).attr('id')
			});
		}
	},

	/**
	 * Collapse a news item
	 *
	 * @param newsItem a jQuery newsItem Object
	 * @param report a flag whether to report back to the server by ajax
	 */
	collapse: function(newsItem, report) {
		jQuery('>div.content',newsItem).addClass('newsContentHidden');
		jQuery('.arrow>img',newsItem).attr('alt',ttnews.expandNews.LLL['spickttnewslist.expand']);
		jQuery('.arrow>img',newsItem).attr('title',ttnews.expandNews.LLL['spickttnewslist.expand']);
		jQuery('.arrow>img',newsItem).attr('src','/fileadmin/spick/bilder/down.gif');

		if (report) {
			jQuery.ajax({
				url:'ajax.php?eID=spickttnewslist_controllerajax&action=collapse&newsItem='+jQuery(newsItem).attr('id')
			});
		}
	}
};
