// DF1.1 :: domFunction
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
//******************************************************




//DOM-ready watcher
function domFunction(f, a)
{
	//initialise the counter
	var n = 0;

	//start the timer
	var t = setInterval(function()
	{
		//continue flag indicates whether to continue to the next iteration
		//assume that we are going unless specified otherwise
		var c = true;

		//increase the counter
		n++;

		//if DOM methods are supported, and the body element exists
		//(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1]
		//in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
		{
			//set the continue flag to false
			//because other things being equal, we're not going to continue
			c = false;

			//but ... if the arguments object is there
			if(typeof a == 'object')
			{
				//iterate through the object
				for(var i in a)
				{
					//if its value is "id" and the element with the given ID doesn't exist
					//or its value is "tag" and the specified collection has no members
					if
					(
						(a[i] == 'id' && document.getElementById(i) == null)
						||
						(a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
					)
					{
						//set the continue flag back to true
						//because a specific element or collection doesn't exist
						c = true;

						//no need to finish this loop
						break;
					}
				}
			}

			//if we're not continuing
			//we can call the argument function and clear the timer
			if(!c) { f(); clearInterval(t); }
		}

		//if the timer has reached 60 (so timeout after 15 seconds)
		//in practise, I've never seen this take longer than 7 iterations [in kde 3
		//in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time]
		if(n >= 60)
		{
			//clear the timer
			clearInterval(t);
		}

	}, 250);
};

// End domFunction


// Cookies functions by http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days) {
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// End Cookies functions


// IE bar by Clement T : http://chiunam.net/blog/archives/36


function showIEbar() {
	if (readCookie("IEbar") == "closed") return;
	if (document.all) {
		var d = document.createElement("div");
		var c = document.createElement("div");
		var a = document.createElement("a");

		d.setAttribute("id", "IEbar");
		c.style.textAlign = "right";
		a.href = "javascript:closeIEbar();"
		a.title = "Comon' I love dump browser! Don't bug me!";
		a.innerHTML = "[Close]";
		c.appendChild(a);

		var t = "<a href='http://getfirefox.com/' title='Get Firefox - Web browsing redefined.'>";
		t += "<img src='http://www.mozilla.org/products/firefox/buttons/getfirefox_large2.png' style='border: none; float: right' alt='Get Firefox' /></a>";
		t += "<strong>You see this message cause you're using Internet Explorer. Try Firefox, it's much better.</strong>";
		t += "<ul><li>Firefox blocks pop-up windows.</li>";
		t += "<li>It protects your privacy and security.</li>";
		t += "<li>Most important, Internet Explorer cannot display this site correctly cause it sucks. It's not my fault, dude.</li></ul>";
		t += "<p>Take back the web. Try <a href='http://getfirefox.com/'>Firefox</a>.</p>";

		d.innerHTML = t;
		d.appendChild(c);
		d.style.textAlign = "left";
		d.style.backgroundColor = "#ffb";
		d.style.font = "12px tahoma, arial, serif #000";
		d.style.padding = "20px 50px";
		document.body.insertBefore(d, document.body.childNodes[0]);
	}
}

function closeIEbar() {
	if (document.all.IEbar) {
		document.all.IEbar.style.display = "none";
	}
	createCookie('IEbar','closed',30);
}

var foobar = new domFunction(showIEbar);

