/* Accessible Popups 
	http://www.alistapart.com/articles/popuplinks/ */

var _POPUP_FEATURES = 'location=0,statusbar=1,menubar=0,width=540,height=440,resizable,scrollbars=yes';

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() }
}


/*
	Standards Compliant Rollover Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/

function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

var searchCleared = false;

function clearSearch(e) {
		if (!searchCleared) {
				e.currentTarget.value = "";
				searchCleared = true;
		}
}

function toggle_billing() {
		var fieldset = document.getElementById('billing_fields');
		var inputs = fieldset.getElementsByTagName('input');
		var selects = fieldset.getElementsByTagName('select');
		var fields = concat_collection(inputs, selects);
		var len = fields.length;
		var field;
		for(var i = 0; i < len; i++) {
			field = fields[i];
			if((field.getAttribute('type') == 'checkbox') || (field.getAttribute('type') == 'submit')) {
				
			}
			else {
				if(field.getAttribute('disabled')) {
					field.removeAttribute('disabled');
				}
				else {
					field.setAttribute('disabled', 'dsiabled');
				}
			}
		}
}

// from http://www.codingforums.com/showthread.php?t=50611
function concat_collection(obj1, obj2) {
    var i;
    var arr  = new Array();
    var len1 = obj1.length;
    var len2 = obj2.length;
    for (i=0; i<len1; i++) {
        arr.push(obj1[i]);
    }
    for (i=0; i<len2; i++) {
        arr.push(obj2[i]);
    }
    return arr;
}

function init()
{
	try {
		initRollovers();
		// make all links inside a "slideshow_index" element open in popup windows
		if(document.getElementById && document.getElementById('slideshow_index'))
		{
				mlisten(
				'click',
				document.getElementById('slideshow_index').getElementsByTagName('a'),
				event_popup
			);
		}
		if(document.getElementById) {
			// make all links with class "popup" open in popup windows
			mlisten(
				'click',
				getElementsByClass('popup','a'),
				event_popup
			);
			// searchbox stuff
			mlisten(
				'click',
				[document.getElementById('searchbox')],
				clearSearch
			);
			// donation form checkbox / disabling stuff
			var mail_s_ship = document.getElementById('mail_s_ship');
			if(mail_s_ship && mail_s_ship.getAttribute('checked')) {
					toggle_billing();
					mail_s_ship.onclick = toggle_billing;
			}
		}
		
	}
	catch (e) {
	}
}

window.onload = init;