/*
 * Common expandable panel functions.
 */

/*
 * Checks if the close icon is showing.
 *
 * @param element the element which contain the icon.
 */  
function isCloseActivatorIconShowing(activatorClass, widgetId) {
    var backgroundImage = $(widgetId + " " + activatorClass).css("background-image");
    if (backgroundImage.indexOf("close.png") > -1) {
        return true;
    }
    return false;
}

/*
 * Checks if the close icon is showing when clicking on a specific element.
 *
 * @param activatorElement the element which contain the icon.
 */  
function isCloseActivatorIconShowingForCurrentObject(activatorElement) {
    var backgroundImage = $(activatorElement).css("background-image");
    if (backgroundImage.indexOf("close.png") > -1) {
        return true;
    }
    return false;
}

/*
 * Sets the icon depending on the state.
 * 
 * @param element the element which contain the icon.
 * @param widgetId the id for the current expandable panel element.
 * @param state the state of which icon who should be shown (open/close).
 */ 
function setActivatorIcon(activatorClass, widgetId, state) {
	$(widgetId + " " + activatorClass).css({backgroundImage: "url(https://www.kth.se/img/icon/" + state + ".png)"});
}

/*
 * Sets the icon depending on the state.
 * 
 * @param activeElement the element which contain the icon.
 * @param state the state of which icon who should be shown (open/close).
 */ 
function setCurrentActivatorIcon(activeElement, state) {
	$(activeElement).css({backgroundImage: "url(https://www.kth.se/img/icon/" + state + ".png)"});
}

/*
 * Toggles the open/close icon when clicking on the activation element.
 *
 * @param element the element which contain the icon.
 */
function toogleExpandablePanelActivatorIcon(activatorClass, widgetId) {	 
	if (isCloseActivatorIconShowing(activatorClass, widgetId)) {
		setActivatorIcon(activatorClass, widgetId, "open");
    } else {
        setActivatorIcon(activatorClass, widgetId, "close");
    }    
}

/*
 * Toggles the open/close icon when clicking on the activation element.
 *
 * @param activatorElement the element which contain the icon.
 * @param widgetId the id for the current expandable panel element.
 */
function toogleCurrentExpandablePanelActivatorIcon(activatorElement, widgetId) {	 
	if (isCloseActivatorIconShowingForCurrentObject(activatorElement)) {
		setCurrentActivatorIcon(activatorElement, "open");
    } else {
        setCurrentActivatorIcon(activatorElement, "close");
    }    
}

/*
 * Expand or collapse the chosen panel element.
 *
 * @param widgetId the id for the expandable panel element.
 */
function toogleExpandablePanel(widgetId) {
	$(widgetId + " " + ".expandablePanelWidgetActivator").click(function (event) {
		$(this).parent().next(".expandablePanel").toggle();
		toogleCurrentExpandablePanelActivatorIcon(this, widgetId);
		event.stopPropagation();
	});
}

/*
 * Initates the rss views.
 *
 * @param widgetId the id for the current rss paragraph.
 */
function registerExpandablePanelWidget(widgetId) {
	toogleExpandablePanelActivatorIcon(".expandablePanelWidgetActivator", widgetId);
	toogleExpandablePanel(widgetId);
	$(".expandablePanel").hide();
}