(function($){

$.fn.buttonify = function(prepends, appends, settings){
 var _p = (settings && settings.prefix ? settings.prefix : '');
 var _s = (settings && settings.suffix ? settings.suffix : '');
 var make_img = function(_class, _url) {
	return '<img class="'+_class+' buttonify-edge" src="'+_p+_url+_s+'" />';
 }
 var not_already_added = function(_class) {
	return $(this).find("."+_class).length ? false : true;
 }
 var _ = this;
 var _prepend;
 if (settings && settings['prepend-after']) {
	var _pa = _.find(settings['prepend-after']).filter(not_already_added);
	var _op = _.filter(":not("+settings['prepend-after']+")").filter(not_already_added);
	if (_pa.length == _.length)
		$.each(prepends, function(_class, _url){
			_pa.filter(function(){ return not_already_added(_class) }).after(make_img(_class, _url));
		});
	else//???
		$.each(prepends, function(_class, _url){
			_op.filter(function(){ return not_already_added(_class) }).prepend(make_img(_class, _url));
		});
 }
 else 
	$.each(prepends, function(_class, _url){ _.filter(function(){ return not_already_added(_class) }).prepend(make_img(_class, _url)) });
 $.each(appends, function(_class, _url){ if (not_already_added(this, _class)) _.filter(function(){ return not_already_added(_class) }).append(make_img(_class, _url)) });
 //_.focus().blur();window.scrollTo(0,0);
 return this;
}

$(document).ready(function(){
 var _button_prepends = {
	'before': "L",
	'active-before': "active-L"
 };
 var _button_appends = {
	'active-after': "active-R",
	'after': "R"
 };
 var _topnav_firstlink_prepends = $.extend({}, _button_prepends, {
	'hover-before': "hover-L",
	'visiting-before': "visiting-L"
 });
 var _topnav_settings = {
	'prefix': "/images/b/topnav-",
	'suffix': "-edge.png"
 };

 // header and footer bars
 $("#header #topnav, .pagespanner").buttonify({'before': "L"}, {'after': "R"},
	_topnav_settings);

 // topnav buttons (the L selector was #topnav > .search + a before) #topnav > a:first does not work in css, where #topnav > a.first is needed instead
 $("#topnav > a:first").buttonify({
	'visiting-before': "visiting-L",
	'active-before': "active-L",
	'hover-before': "hover-L",
	'before': "L"
 }, {}, _topnav_settings).buttonify({}, {'active-after': "/images/b/topnav-active-R-button-side.png"});
 $("#topnav a").filter(":not(#topnav > a:first)").buttonify({
	'active-before': "active-L"
 }, {	'active-after': "active-R"
 }, {	'prefix': "/images/b/topnav-", 'suffix': "-button-side.png"
 });
 // !!! sessionchanged? other loaded blocks?

 // body content buttons
 if (typeof console != 'undefined') $(document).bind('body-content-changed', function(e){
	var _x = this;
	var _t = e.target;
	var name = _t.nodeName;
	if (typeof _t.id != 'undefined') name += "#"+_t.id;
	if (typeof _t.className != 'undefined') name += "."+_t.className;
	if (typeof console != 'undefined') console.info("Body content changed in "+name);
 });
 $(document).bind('body-content-changed', function(e, c){
	var _ = c ? $(c) : $(e.target);
	var _sec_styled = ".show-more,.discard-import,.secondary";
	_.find(":not(.button-nav, .button-views) > a.button:not("+_sec_styled+",.nodeco), button:not("+_sec_styled+",.nodeco)").buttonify(_button_prepends, _button_appends, {
		'prefix': "/images/b/button-",
		'suffix': "-edge.png"
	});

	_.find("#add .show-more, .discard-import, button.secondary").buttonify(
	_button_prepends, _button_appends, {
		'prefix': "/images/b/button-secondary-",
		'suffix': "-edge.png"
	});

	_.find(".button-nav a.button:not("+_sec_styled+",.nodeco), .button-views a.button:not("+_sec_styled+",.nodeco)").buttonify(_button_prepends, _button_appends, {
		'prefix': "/images/b/button-big-",
		'suffix': "-edge.png",
		'prepend-after': ".geo-icon"
	});

	_.find(".button-nav a.button.secondary:not(.nodeco), .button-views a.button.secondary:not(.nodeco)").buttonify(_button_prepends, _button_appends, {
		'prefix': "/images/b/button-big-secondary-",
		'suffix': "-edge.png"
	});
 }).trigger('body-content-changed');

 // popup content buttons (need to handle as loaded)
 $(document).bind('popup-content-changed', function(e, c){
	var _ = $(c);//.parent();// so find can check for .popup-body, below
	_.find("a.button:not(.secondary), button:not(.secondary)").buttonify(_button_prepends, _button_appends, {
		'prefix': "/images/b/p/button-",
		'suffix': "-edge-popup.png"
	});
	_.find("a.button.secondary, button.secondary").buttonify(_button_prepends, _button_appends, {
		'prefix': "/images/b/p/button-secondary-",
		'suffix': "-edge-popup.png"
	});
 });

 // style tweaks (for crossplatform differences btwn the same browser
 if (/firefox/i.test(navigator.userAgent)
  && /linux/i.test(navigator.userAgent)) {
	function button_adjust(dom_node) {
		//$(dom_node).find("button .before, button .after").css('top', "-5px");
		$(dom_node).find("button .active-before, button .active-after").css('top', "-6px");
		$(dom_node).find("button .before, button .after").css('top', "-5px");
		$(dom_node).find("#zoom-to-fit button .before, #zoom-to-fit button .after, #zoom-to-fit button .active-before, #zoom-to-fit button .active-after").css('top', function(i, a){ return (parseInt(a) - 1)+"px" });
		$(dom_node).find(".dialog-body .sharers button .active-before, .dialog-body .sharers button .active-after").css('top', "-5px");
		$(dom_node).find(".dialog-body .sharers button .before, .dialog-body .sharers button .after").css('top', "-4px");
		// slower but more flexible
		var raise_edge = function(i, a){ return (parseInt(a) - 1)+"px" }
		//$(dom_node).find("button .active-before, button .active-after").filter(":not([style])").css('top', raise_edge);
		//$(dom_node).find("button .before, button .after").filter(":not([style])").css('top', raise_edge);
		//$(dom_node).find("#zoom-to-fit button .before, #zoom-to-fit button .after, #zoom-to-fit button .active-before, #zoom-to-fit button .active-after").filter(":not([style])").css('top', raise_edge);
	 if (false) {
		$(dom_node).find("button .active-before, button .active-after").css('top', raise_edge);
		$(dom_node).find("button .before, button .after").css('top', raise_edge);
		$(dom_node).find("#zoom-to-fit button .before, #zoom-to-fit button .after, #zoom-to-fit button .active-before, #zoom-to-fit button .active-after").css('top', raise_edge);
	 }
	}
	button_adjust(document.body);
	var adjust_changed_content = function(e, c){ button_adjust(e.target) };
	$(document).bind('body-content-changed', adjust_changed_content);
	$(document).bind('popup-content-changed', adjust_changed_content);
	// !!! also for sessionchanged?
	//var raise_edge = function(i, a){ return (parseInt(a) - 1)+"px" };$(".popup-body").find("button .before, button .after").css('top', raise_edge);
 }

});

})(jQuery);

