/**
 * Common social bookmarks functions
 */

/**
 * Checks if the close icon is showing.
 *
 * @param element the element which contain the icon.
 */  
function isCloseIconShowing(activatorId) {
    var backgroundImage = $(activatorId).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 name the state of which icon who should be shown (share-close/share-open).
 */ 
function setIcon(activatorId, name) {
	$(activatorId).css({backgroundImage: "url(/img/icon/" + name + ".png)"});
}

/**
 * Toggles the open/close icon when clicking activation element.
 *
 * @param element the element which contain the icon.
 */
function toogleIcon(activatorId) {	 
	if (isCloseIconShowing(activatorId)) {
		setIcon(activatorId, "share-open");
    } else {
        setIcon(activatorId, "share-close");
    }    
}

/**
 * Show/Hide the social bookmarks if the activator element is clicked.
 *
 * @param activatorElement the element to activate the widget with.
 * @param left the left position relative to the parent element.
 * @param top the top position relative to the parent element.
 * @param widgetId the id of the element containing the widget.
 */     
function addActivatorClickEventListner(activatorId, widgetId) {
    $(activatorId).click(function (event) {
        $(widgetId).toggle();
        toogleIcon($(activatorId));
    	event.stopPropagation();
    });
}

/**
 * Position the social bookmarks relative the the activator element.
 *
 * @param widgetId the id of the element containing the widget.
 * @param left the left position relative to the parent element.
 * @param top the top position relative to the parent element.
 */
function positionSocialBookmarks(widgetId, left, top) {
    $(widgetId).css("left", left);
    $(widgetId).css("top", top);
}

/**
 * Disables propagation of the event if one of the inner elements 
 * of the box is clicked, preventing it from closing.
 */
function addSocialBookMarkClickEventListner() {
    $(".socialBookMarks,.socialBookMarks img,.socialBookMarks a").click(function (event) {
    	event.stopPropagation();
    });
}
  
/**
 * Activate listeners related to a given widget element depending on 
 * if the user has java script enabled or not.
 *
 * @param widgetId the id of the element containing the widget.
 * @param fallbackId the id of the fall back element.
 * @param activatorId the element to activate the widget with.
 * @param left the left position relative to the parent element.
 * @param top the top position relative to the parent element.
 */
function registerSocialBookmarkWidget(widgetId, fallbackId, activatorId, left, top) {
	$(widgetId).hide();
	addActivatorClickEventListner(activatorId, widgetId);
	positionSocialBookmarks(widgetId, left, top);
	toogleIcon(activatorId);
	addSocialBookMarkClickEventListner();
	if (fallbackId !== "") {
		$(fallbackId).show();
	}
	
}