if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

// FUNCTIONAL

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}

function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}


// DOM

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}

function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}

// DOM EVENTS

function listen(event, elem, func) {
    elem = getElem(elem);
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    else throw 'cannot add event listener';
}

function mlisten(event, elem_list, func) {
    map(elem_list, function(elem) { listen(event, elem, func) } );
}

function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    return this;
}

function isUndefined(v) {
    var undef;
    return v===undef;
}

// END LIB.JS


/** HEADER REPLACEMENT (JIR) **/

function replaceTags(tag) {
	
	var ta = document.getElementsByTagName(tag);
	
	for (var i=0;i<ta.length;i++) {
		if (ta[i].id) {

			ta[i].innerHTML = '<img alt="' + ta[i].firstChild.nodeValue + '" src="/headers/' + tag + '_' + ta[i].id + '.gif" />';
			//ta[i].style.background = 'url(headers/' + tag + '_' + ta[i].id + '.gif) left no-repeat';
			//ta[i].title = ta[i].firstChild.nodeValue;
		} //else {
		//	ta[i].className = 'noID-' + tag;
	//	}
	}
}

function replacePrice() {
	try {
	var price = getElem("price");
	} catch (Exception) { return; }
	var dollars = /(\$\d*\.\d{2,})/gi;
	dollars = dollars.exec(price.innerHTML)[0];
	price.innerHTML += '<img alt="' + dollars + '" src="/prices/' + unescape(dollars) + '.gif" />';
	
	//price.style.background = 'url(prices/' + unescape(dollars) + '.gif) no-repeat';
	

}

document.getElementsByTagName('html')[0].className = 'doReplace';


/**

As seen on http://www.alistapart.com/articles/popuplinks/

**/

var _BLANK_0 = 'left=0,top=0,location=0,statusbar=0,menubar=0,width=600,height=' + (screen.height-37) + ',scrollbars=1';
var _POPUP_FEATURES = 'left=0,top=0,location=1,statusbar=1,menubar=1,width=' + screen.width - 10 + ',height=' + (screen.height-37) + ',scrollbars=1';


// These defaults should be changed the way it best fits your site
//var _POPUP_FEATURES = '';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function event_popup(e) {
    // to be passed as an event listener
    // pops up a window grabbing the url from the event source's href
    link_popup(e.currentTarget);
    e.preventDefault();
}

function event_popup_features(features) {
    // generates an event listener similar to event_popup, but allowing window features
    return function(e) { link_popup(e.currentTarget, features); e.preventDefault() }
}


function showPic (whichpic, placeholder) { 
	if (document.getElementById) { 
		d = document.getElementById(placeholder);
		d.src = '';
		d.src = whichpic.href;
		whichpic.className = 'visited';
		return false; 
	} else { 
		return true; 
	} 
}


function prepareGallery() {

  if ((!document.getElementsByTagName) || (!getElementsByClass)) return false;
  if (!getElementsByClass("pics")) return false;
  var gallery = getElementsByClass("pics");
  
  for ( var j=0; j < gallery.length; j++) {
	  var links = gallery[j].getElementsByTagName("a");

	  for ( var i=0; i < links.length; i++) {
	  	if (links[i].target != '_blank') {
		links[i].onclick = function() {
		  return showPic(this, 'placeholder');
		}
		}
	  }
	}
}

function init() {

	replaceTags('h1');
	prepareGallery();

}
window.onload = init;
