$(document).ready(function () {

	// COLLAPSABLE WIDGETS (on Dashboard & Profile pages)
	// toggle widget box contents
	$('a.toggle_box_contents').bind('click', toggleContent);

	// toggle widget box edit panel
	$('a.toggle_box_edit_panel').click(function () {
		$(this.parentNode.parentNode).children(".collapsable_box_editpanel").slideToggle("fast");
		return false;
	});

	// toggle customise edit panel
	$('a.toggle_customise_edit_panel').click(function () {
		$('div#customise_editpanel').slideToggle("fast");
		return false;
	});

	// toggle plugin's settings nad more info on admin tools admin
	$('a.pluginsettings_link').click(function () {
		$(this.parentNode.parentNode).children(".pluginsettings").slideToggle("fast");
		return false;
	});
	$('a.manifest_details').click(function () {
		$(this.parentNode.parentNode).children(".manifest_file").slideToggle("fast");
		return false;
	});
	// reusable generic hidden panel
	$('a.collapsibleboxlink').click(function () {
		$(this.parentNode.parentNode).children(".collapsible_box").slideToggle("fast");
		return false;
	});

	// WIDGET GALLERY EDIT PANEL
	// Sortable widgets
	var els = ['#leftcolumn_widgets', '#middlecolumn_widgets', '#rightcolumn_widgets', '#widget_picker_gallery' ];
	var $els = $(els.toString());

	$els.sortable({
		items: '.draggable_widget',
		handle: '.drag_handle',
		forcePlaceholderSize: true,
		placeholder: 'ui-state-highlight',
		cursor: 'move',
		opacity: 0.9,
		appendTo: 'body',
		connectWith: els,
		start:function(e,ui) {

		},
		stop: function(e,ui) {
			// refresh list before updating hidden fields with new widget order
			$(this).sortable( "refresh" );

			var widgetNamesLeft = outputWidgetList('#leftcolumn_widgets');
			var widgetNamesMiddle = outputWidgetList('#middlecolumn_widgets');
			var widgetNamesRight = outputWidgetList('#rightcolumn_widgets');

			document.getElementById('debugField1').value = widgetNamesLeft;
			document.getElementById('debugField2').value = widgetNamesMiddle;
			document.getElementById('debugField3').value = widgetNamesRight;
		}
	});

	// bind more info buttons - called when new widgets are created
	widget_moreinfo();

	// set-up hover class for dragged widgets
	$("#rightcolumn_widgets").droppable({
		accept: ".draggable_widget",
		hoverClass: 'droppable-hover'
	});
	$("#middlecolumn_widgets").droppable({
		accept: ".draggable_widget",
		hoverClass: 'droppable-hover'
	});
	$("#leftcolumn_widgets").droppable({
		accept: ".draggable_widget",
		hoverClass: 'droppable-hover'
	});

}); /* end document ready function */


// List active widgets for each page column
function outputWidgetList(forElement) {
	return( $("input[name='handler'], input[name='guid']", forElement ).makeDelimitedList("value") );
}

// Make delimited list
jQuery.fn.makeDelimitedList = function(elementAttribute) {

	var delimitedListArray = new Array();
	var listDelimiter = "::";

	// Loop over each element in the stack and add the elementAttribute to the array
	this.each(function(e) {
			var listElement = $(this);
			// Add the attribute value to our values array
			delimitedListArray[delimitedListArray.length] = listElement.attr(elementAttribute);
		}
	);

	// Return value list by joining the array
	return(delimitedListArray.join(listDelimiter));
}


// Read each widgets collapsed/expanded state from cookie and apply
function widget_state(forWidget) {

	var thisWidgetState = $.cookie(forWidget);

	if (thisWidgetState == 'collapsed') {
		forWidget = "#" + forWidget;
		$(forWidget).find("div.collapsable_box_content").hide();
		$(forWidget).find("a.toggle_box_contents").html('+');
		$(forWidget).find("a.toggle_box_edit_panel").fadeOut('medium');
	};
}


// Toggle widgets contents and save to a cookie
var toggleContent = function(e) {
var targetContent = $('div.collapsable_box_content', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(400);
		$(this).html('-');
		$(this.parentNode).children(".toggle_box_edit_panel").fadeIn('medium');

		// set cookie for widget panel open-state
		var thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
		$.cookie(thisWidgetName, 'expanded', { expires: 365 });

	} else {
		targetContent.slideUp(400);
		$(this).html('+');
		$(this.parentNode).children(".toggle_box_edit_panel").fadeOut('medium');
		// make sure edit pane is closed
		$(this.parentNode.parentNode).children(".collapsable_box_editpanel").hide();

		// set cookie for widget panel closed-state
		var thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
		$.cookie(thisWidgetName, 'collapsed', { expires: 365 });
	}
	return false;
};

// More info tooltip in widget gallery edit panel
function widget_moreinfo() {

	$("img.more_info").hover(function(e) {
	var widgetdescription = $("input[name='description']", this.parentNode.parentNode.parentNode ).attr('value');
	$("body").append("<p id='widget_moreinfo'><b>"+ widgetdescription +" </b></p>");

		if (e.pageX < 900) {
			$("#widget_moreinfo")
				.css("top",(e.pageY + 10) + "px")
				.css("left",(e.pageX + 10) + "px")
				.fadeIn("medium");
		}
		else {
			$("#widget_moreinfo")
				.css("top",(e.pageY + 10) + "px")
				.css("left",(e.pageX - 210) + "px")
				.fadeIn("medium");
		}
	},
	function() {
		$("#widget_moreinfo").remove();
	});

	$("img.more_info").mousemove(function(e) {
		// action on mousemove
	});
};

// COOKIES
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
	options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
	var expires = '';
	if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
		var date;
		if (typeof options.expires == 'number') {
			date = new Date();
			date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		} else {
			date = options.expires;
		}
		expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
	}
	// CAUTION: Needed to parenthesize options.path and options.domain
	// in the following expressions, otherwise they evaluate to undefined
	// in the packed version for some reason.
	var path = options.path ? '; path=' + (options.path) : '';
	var domain = options.domain ? '; domain=' + (options.domain) : '';
	var secure = options.secure ? '; secure' : '';
	document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');

	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};

// ELGG TOOLBAR MENU
$.fn.elgg_topbardropdownmenu = function(options) {

options = $.extend({speed: 350}, options || {});

this.each(function() {

	var root = this, zIndex = 5000;

	function getSubnav(ele) {
	  if (ele.nodeName.toLowerCase() == 'li') {
		var subnav = $('> ul', ele);
		return subnav.length ? subnav[0] : null;
	  } else {

		return ele;
	  }
	}

	function getActuator(ele) {
	  if (ele.nodeName.toLowerCase() == 'ul') {
		return $(ele).parents('li')[0];
	  } else {
		return ele;
	  }
	}

	function hide() {
	  var subnav = getSubnav(this);
	  if (!subnav) return;
	  $.data(subnav, 'cancelHide', false);
	  setTimeout(function() {
		if (!$.data(subnav, 'cancelHide')) {
		  $(subnav).slideUp(100);
		}
	  }, 250);
	}

	function show() {
	  var subnav = getSubnav(this);
	  if (!subnav) return;
	  $.data(subnav, 'cancelHide', true);
	  $(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
	  if (this.nodeName.toLowerCase() == 'ul') {
		var li = getActuator(this);
		$(li).addClass('hover');
		$('> a', li).addClass('hover');
	  }
	}

	$('ul, li', this).hover(show, hide);
	$('li', this).hover(
	  function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
	  function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
	);

});

};

var submenuLayer = 1000;

function setup_avatar_menu() {

	// avatar image menu link
	$("div.usericon img").mouseover(function() {
		// find nested avatar_menu_button and show
		$(this.parentNode.parentNode).children(".avatar_menu_button").show();
		$(this.parentNode.parentNode).children("div.avatar_menu_button").addClass("avatar_menu_arrow");
		//$(this.parentNode.parentNode).css("z-index", submenuLayer);
	})
	.mouseout(function() { 
		if($(this).parent().parent().find("div.sub_menu").css('display')!="block") {
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_on");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_hover");
			$(this.parentNode.parentNode).children(".avatar_menu_button").hide();
		}
		else {
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_on");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_hover");
			$(this.parentNode.parentNode).children(".avatar_menu_button").show();
			$(this.parentNode.parentNode).children("div.avatar_menu_button").addClass("avatar_menu_arrow");
		}
	});


	// avatar contextual menu
	$(".avatar_menu_button img").click(function(e) { 
		
		var submenu = $(this).parent().parent().find("div.sub_menu");
		
		// close submenu if arrow is clicked & menu already open
		if(submenu.css('display') == "block") {
			//submenu.hide(); 		
		}
		else {
			// get avatar dimensions
			var avatar = $(this).parent().parent().parent().find("div.usericon");
			//alert( "avatarWidth: " + avatar.width() + ", avatarHeight: " + avatar.height() );
			
			// move submenu position so it aligns with arrow graphic
			if (e.pageX < 840) { // popup menu to left of arrow if we're at edge of page
			submenu.css("top",(avatar.height()) + "px")
					.css("left",(avatar.width()-15) + "px")
					.fadeIn('normal');	
			}	
			else {
			submenu.css("top",(avatar.height()) + "px")
					.css("left",(avatar.width()-166) + "px")
					.fadeIn('normal');		
			}	
			
			// force z-index - workaround for IE z-index bug			
			avatar.css("z-index",  submenuLayer);
			avatar.find("a.icon img").css("z-index",  submenuLayer);
			submenu.css("z-index", submenuLayer+1);
						
			submenuLayer++;
			
			// change arrow to 'on' state
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_hover");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").addClass("avatar_menu_arrow_on");
		}
		
		// hide any other open submenus and reset arrows
		$("div.sub_menu:visible").not(submenu).hide();
		$(".avatar_menu_button").removeClass("avatar_menu_arrow");
		$(".avatar_menu_button").removeClass("avatar_menu_arrow_on");
		$(".avatar_menu_button").removeClass("avatar_menu_arrow_hover");
		$(".avatar_menu_button").hide();
		$(this.parentNode.parentNode).children("div.avatar_menu_button").addClass("avatar_menu_arrow_on");
		$(this.parentNode.parentNode).children("div.avatar_menu_button").show();
		//alert("submenuLayer = " +submenu.css("z-index"));
	})
	// hover arrow each time mouseover enters arrow graphic (eg. when menu is already shown)
	.mouseover(function() {
		$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_on");
		$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow");
		$(this.parentNode.parentNode).children("div.avatar_menu_button").addClass("avatar_menu_arrow_hover");
	})
	// if menu not shown revert arrow, else show 'menu open' arrow
	.mouseout(function() { 
		if($(this).parent().parent().find("div.sub_menu").css('display')!="block"){
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_hover");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").addClass("avatar_menu_arrow");
		}
		else {
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow_hover");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").removeClass("avatar_menu_arrow");
			$(this.parentNode.parentNode).children("div.avatar_menu_button").addClass("avatar_menu_arrow_on");
		}
	});
	
	// hide avatar menu if click occurs outside of menu	
	// and hide arrow button						
	$(document).click(function(event) { 		
			var target = $(event.target);
			if (target.parents(".usericon").length == 0) {				
				$(".usericon div.sub_menu").fadeOut();
				$(".avatar_menu_button").removeClass("avatar_menu_arrow");
				$(".avatar_menu_button").removeClass("avatar_menu_arrow_on");
				$(".avatar_menu_button").removeClass("avatar_menu_arrow_hover");
				$(".avatar_menu_button").hide();
			}
	});			   
	

}

$(document).ready(function() {

	setup_avatar_menu();						   
								   
});
function deleteArticleFolder(guid, title){
	if(confirm( MEDIARGUS_SERVICES_ARTICLES_FOLDERS_FOLDER_CONFIRMDELETE + " - " + title)){
		document.location.href= "http://www.mediargus.be/action/articles/remote_article_folder/remove?' + ELGG_GLOBAL_URL_SECURITY + '&guid=" + guid;
	}
}

function togglePortfolio(guid, element){
	if($(element).hasClass("portfolio_added")){
		$.post('http://www.mediargus.be/action/articles/portfolio/remove?' + ELGG_GLOBAL_URL_SECURITY + '&guid=' + guid, function(data){
			$(element).removeClass("portfolio_added");
			$(element).attr("title", MEDIARGUS_SERVICES_ARTICLES_PORTFOLIO_ADD);				
		});
	} else {
		$.post('http://www.mediargus.be/action/articles/portfolio/add?' + ELGG_GLOBAL_URL_SECURITY + '&guid=' + guid, function(data){
			$(element).addClass("portfolio_added");
			$(element).attr("title", MEDIARGUS_SERVICES_ARTICLES_PORTFOLIO_REMOVE);
		});
	}
	
}

function removeArticleLocal(guid){
	document.location.href = "http://www.mediargus.be/action/articles/local_article/remove?" + ELGG_GLOBAL_URL_SECURITY + "&guid=" + guid;
}

function removeArticleFromArticleFolder(folder_guid, article_id){
	$('#remote_article_' + article_id + ' .mediargus_articles_remote_article_remove').removeClass('mediargus_articles_remote_article_remove').addClass('mediargus_services_articles_remote_article_folder_delete_article_ajax_loader').attr('style', 'display:inline-block;');
	
	$.get("http://www.mediargus.be/action/articles/remote_article_folder/remove_article?" + ELGG_GLOBAL_URL_SECURITY + "&folder_guid=" + folder_guid + "&article_id=" + article_id, function(data){
		if(data == "true"){
			$('#remote_article_' + article_id).parents('div.search_listing').remove();
			checkMoveIcons();
		} else {
			document.location.reload(true);
		}
	});
}

function addArticleToArticleFolder(article_guid){
	var folder_guid = $('#add_wrapper_' + article_guid + ' select[name="folder_guid"]').val();
	
	$.get("http://www.mediargus.be/action/articles/remote_article_folder/add_article?" + ELGG_GLOBAL_URL_SECURITY + "&folder_guid=" + folder_guid + "&article_guid=" + article_guid, function(data){
		if(data != "true"){
			alert(MEDIARGUS_SERVICES_ARTICLES_LOCAL_ARTICLE_ADD_TO_FOLDER_ERROR_UNKNOWN);
		}
		$('#add_wrapper_' + article_guid).hide();
	});
}

function checkRemoteArticleFolderContent(){
	var url = "http://www.mediargus.be/pg/remote_article_folder/get_article_ids";
	var folder_guid = $("#remote_article_folder_guid").val();
	
	$("#mediargus_services_articles_remote_article_folder_article_count").html('');
	
	$("input[name^='article_ids[']:checked").attr("checked", "");
	
	$.getJSON(url, "folder_guid=" + folder_guid, function(data){
		$.each($('input[type="checkbox"]'), function(i, item){
        	var article_id = $(item).attr('rel');
        	
			if($.inArray(article_id, data) > -1){
        		$(item).attr('checked', 'checked');
        	}
        	
        	if($(item).attr('onclick') == "undefined" || $(item).attr('onclick') == null){ 
        		$(item).attr('onclick', 'return checkRemoteArticleFolderMax(this);');
        	}
        });
        
        if(typeof checkSelectAllSearchResults == "function"){
        	checkSelectAllSearchResults();
        }
        
        remote_article_folder_article_count = $(data).length;
		changeRemoteArticleFolderArticleCount();
	});
}

function changeRemoteArticleFolderArticleCount(){
	$("#mediargus_services_articles_remote_article_folder_article_count").html("(" + remote_article_folder_article_count + ")");
}

function checkRemoteArticleFolderMax(element){
	var result = false;
	
	if($(element).is(':checked')){
		// element is now checked
		
		if(remote_article_folder_article_count < 100){
			result = true;
			remote_article_folder_article_count++;
		} else {
			alert(remote_article_folder_max_reached_text);
		}
	} else {
		// element is now unchecked
		
		result = true;
		remote_article_folder_article_count--;
	}
	
	changeRemoteArticleFolderArticleCount();
	
	return result;
}

function preNavigateSearchResults(){
	var url = "http://www.mediargus.be/pg/remote_article_folder/get_article_ids";
	var saved_ids = new Array();
	var change = false;
	var ajax_success = false;
	
	var folder_guid = $("#remote_article_folder_guid").val();
	
	$.ajaxSetup({async:false});
	
	$.getJSON(url, "folder_guid=" + folder_guid, function(data){
		ajax_success = true;
		
		$.each(data,  function(i,item){
			saved_ids.push(item);
		});
	});
	
	if(ajax_success){
		$.each($("#mediargus_services_search_results input[type='checkbox']"), function(index, data){
			if(!change){
				var current_id = $(data).attr('rel');
				
				if($(data).is(":checked")){
					if(!($.inArray(current_id, saved_ids) > -1)){
						change = true;
					}
				} else {
					if($.inArray(current_id, saved_ids) > -1){
						change = true;
					}
				}
			}
		});
		
		if(change){
			if(confirm(MEDIARGUS_SERVICES_ARTICLES_JS_PRE_NAVIGATE_CONFIRM_SAVE)){
				saveArticlesToRemoteArticleFolder();
			}
		}
	}
}

function saveArticlesToRemoteArticleFolder(){
	var url = "http://www.mediargus.be/action/mediasearch/save_articles";
	var folder_guid = $("#remote_article_folder_guid").val();
	
	$('#mediargus_services_articles_remote_article_folder_selector_ajax_loader').show();

	$.each($("input[type='checkbox'][name^='article_ids[']"), function(i, item){
		$(item).val($(item).attr("checked")); 
	});
	
	$.post(url, ELGG_GLOBAL_URL_SECURITY + "&folder_guid=" + folder_guid + "&" + $.param($("input[type='checkbox'][name^='article_ids[']")), function(data){
		$('#mediargus_services_articles_remote_article_folder_selector_ajax_loader').hide();
		
		if(data == "true"){
			alert(MEDIARGUS_SERVICES_ARTICLES_REMOTE_ARTICLE_FOLDER_SELECTOR_SAVE_IN_FOLDER_SUCCESS);
		}
		if(data == "false"){
			alert(MEDIARGUS_SERVICES_ARTICLES_REMOTE_ARTICLE_FOLDER_SELECTOR_SAVE_IN_FOLDER_ERROR);
		}
		
		checkRemoteArticleFolderContent();
	});
}

function addArticleToRemoteArticleFolderFromToolbox(article_id){
	var url = "http://www.mediargus.be/action/articles/remote_article_folder/add_article";
	var article_folder_guid = $('#mediargus_services_articles_article_toolbox select[name="remote_article_folder_guid"]').val();
	
	$('#mediargus_services_articles_remote_article_folder_selector_ajax_loader').show();
	
	$.get(url + "?" + ELGG_GLOBAL_URL_SECURITY + "&article_id=" + article_id + "&folder_guid=" + article_folder_guid, function(data){
		$('#mediargus_services_articles_remote_article_folder_selector_ajax_loader').hide();
		
		if(data == "true"){
			alert(MEDIARGUS_SERVICES_ARTICLES_REMOTE_ARTICLE_FOLDER_SELECTOR_SAVE_IN_FOLDER_SUCCESS);
			
			selectArticleInParent(article_id);
		} else {
			alert(MEDIARGUS_SERVICES_ARTICLES_REMOTE_ARTICLE_FOLDER_SELECTOR_SAVE_IN_FOLDER_ERROR);
		}
	});
}

function selectArticleInParent(article_id){
	if(window.opener != null){
		window.opener.$('#mediargus_services_search_results input[type="checkbox"][rel="' + article_id + '"]').attr("checked", "checked");
	}
}

function articlePrint(url) {
	url = encodeURIComponent(url);
	window.open('http://www.mediargus.be/pg/article/print?url=' + url, 'printer', 'width=600,height=400');	
}

function navigateToRemoteArticleFolder(){
	var folder_guid = $("#remote_article_folder_guid").val();
	
	document.location.href = "http://www.mediargus.be/pg/remote_article_folder/" + folder_guid;
}

function showRemoteArticleClippings(article_id, basket_id){
	if($('#remote_article_clippings_' + article_id).is(':visible')){
		$('#remote_article_clippings_' + article_id).hide();
	} else {
		if($('#remote_article_clippings_' + article_id).html() == "&nbsp;") {
			
			var url = "http://www.mediargus.be/pg/article/get_clippings?article_id=" + article_id + "&basket_id=" + basket_id;
	
			$.get(url, function(data){
				$('#remote_article_clippings_' + article_id).html(data);
				
				$('#remote_article_clippings_' + article_id).show();
			});
		} else {
			$('#remote_article_clippings_' + article_id).show();
		}
	}
}

function deleteRemoteArticleClipping(article_id, filename, basket_id){
	var url = "http://www.mediargus.be/pg/article/delete_clippings";
	
	if(confirm(MEDIARGUS_SERVICES_ARTICLES_REMOTE_ARTICLE_DELETE_CLIPPING_CONFIRM)){
		$.post(url, {articleid: article_id, filenames: [filename], basketid: basket_id}, function(data){
			if(data == "success"){
				$('#remote_article_clippings_' + article_id).hide();
				$('#remote_article_clippings_' + article_id).html("&nbsp;");
				
				showRemoteArticleClippings(article_id, basket_id);
			}
		});
	}
}

function checkMoveIcons(){
	$('#remote_article_list .disabled_action').removeClass('disabled_action');

	$('#remote_article_list .mediargus_articles_remote_article_move_top:first').addClass('disabled_action');
	$('#remote_article_list .mediargus_articles_remote_article_move_up:first').addClass('disabled_action');
	$('#remote_article_list .mediargus_articles_remote_article_move_down:last').addClass('disabled_action');
	$('#remote_article_list .mediargus_articles_remote_article_move_bottom:last').addClass('disabled_action');
}

function mediargus_services_remote_article_get_price(article_id){
	var url = "http://www.mediargus.be/pg/article/get_price";
	var $selector = $('#remote_article_' + article_id + ' span.mediargus_articles_remote_article_price');
	
	$selector.removeClass('mediargus_articles_remote_article_price_on_demand');
	$selector.addClass('mediargus_services_articles_remote_article_folder_delete_article_ajax_loader').show();
	
	$.post(url, {articleid: article_id}, function(data){
		if(data != ""){
			$selector.html(data);
		}
		
		$selector.removeClass('mediargus_services_articles_remote_article_folder_delete_article_ajax_loader');
		$selector.attr('onmouseover', '');
	});
}
	$(document).ready(function(){
		$('#press_release_userselection_friends input[type="checkbox"], #press_release_userselection_members input[type="checkbox"]').live('click', function() {
			  if($(this).is(":checked")){
			  	  if(maxUsersReached()){
						$(this).attr("checked", "");
				  } else {
					  // checked
					  if($('#press_release_userselection_selection').find("input[value='" + $(this).attr('value') + "']").size() > 0){
						  $('#press_release_userselection_selection').find("input[value='" + $(this).attr('value') + "']").attr("checked", "checked");
					  } else {
						  	if($('#press_release_userselection_selection').find("div").size() > 0){
					  			$(this).parent().clone().appendTo('#press_release_userselection_selection');
						  	} else {
						  		$('#press_release_userselection_selection').html($(this).parent().clone());
						  	}
					  }
				  }				  
			  } else {
				  // unchecked
				  $('#press_release_userselection_selection').find("input[value='" + $(this).attr('value') + "']").attr("checked", "");
			  }
			  
			  checkSelectedUsers();	
		});

		$('#press_release_userselection_selection input[type="checkbox"]').live('click', function() {
			if($(this).is(":checked") && maxUsersReached()){
				$(this).attr("checked", "");
			} else {
				checkSelectedUsers();
			}				
		});

	});
	
	function maxUsersReached(){
		if($("#press_release_userselection_selection input:checked").size() >= MEDIARGUS_PRESS_RELEASE_LIST_MAX_USERS){
			alert(MEDIARGUS_PRESS_RELEASE_LIST_MAX_REACHED);
			return true;
		} else {
			return false;
		}
	}

	function checkSelectedUsers(){
		$("#press_release_userselection_friends, #press_release_userselection_members").find("input[type='checkbox']").attr("checked", "");
		
		$("#press_release_userselection_selection input:checked").each(function(index){
			$("#press_release_userselection_friends, #press_release_userselection_members").find("input[value='" + $(this).attr("value") + "']").attr("checked", "checked");
		});
	}

// Profile Manager More Info tooltips
$(document).ready(function(){
	$("span.custom_fields_more_info").hover(
		function(e) {
			var tooltip = $("#text_" + $(this).attr('id'));
			$("body").append("<p id='custom_fields_more_info_tooltip'>"+ $(tooltip).html() + "</p>");
		
			if (e.pageX < 900) {
				$("#custom_fields_more_info_tooltip")
					.css("top",(e.pageY + 10) + "px")
					.css("left",(e.pageX + 10) + "px")
					.fadeIn("medium");	
			}	
			else {
				$("#custom_fields_more_info_tooltip")
					.css("top",(e.pageY + 10) + "px")
					.css("left",(e.pageX - 260) + "px")
					.fadeIn("medium");		
			}			
		},
		function() {
			$("#custom_fields_more_info_tooltip").remove();
		}
	);	
});