// Copyright (C) 2008 Stephane Lavergne <http://www.imars.com/>
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.

// AltSrc: Wait until document is loaded before replacing an img.src.
//
// A modern alternative to img.lowsrc of times past. Images can have low
// bandwidth content in their regular src and be replaced with optional (if
// JavaScript is enabled) new src when the page has fully loaded.
//
// Every image of class "altsrc" will have their src replaced with the
// non-whitespace initial part of its alt.

imars.require("imars.dom");

imars.dom.altsrc = function () {
	var is = imars.dom.getElementsByClassName(document, 'img', 'altsrc');
	var iCount = is.length;
	for (var i=0; i < iCount; i++) {
		var newsrc = is[i].alt;
		var toks = newsrc.split(' ');
		if (toks.length > 0) newsrc = toks[0];  // This assumes that split() fails instead of returning one argument.
		if (toks.length > 0  &&  newsrc != is[i].src) is[i].src = newsrc;  // Don't write to .src unless it's truly warranted.
	};
};

imars.onLoad(imars.dom.altsrc);
