window.addEvent('domready', function() {
    
    //*
    (function() {
        var quotes = $('quotes').getElements('p');
        var currQuote = 0;
        var nextQuote = function() {
            hideQuotes();
            currQuote = (currQuote+1)%quotes.length;
            quotes[currQuote].setStyle('display', 'block');
        };
        var hideQuotes = function() {
            quotes.each(function(item, index) {
                item.setStyle('display', 'none');
                item.addEvent('click', nextQuote);
            });
        }
        currQuote = Math.floor(Math.random() * quotes.length);
        nextQuote();
    })();
    //*/
    
    (function() {
        $('buy-cd').addEvent('click', function() {
            //location.href = 'http://ninemilerecords.mybisi.com/product/parlour-steps--the-hidden-names';
            window.open('http://ninemilerecords.mybisi.com/product/parlour-steps--the-hidden-names');
        });
    })();
    
    var key = 'ad9203LKA9024OLKERDW0VFaslkdfgduw';
    
    $(document.body).getElements('span.email').each(function(item, index) {
        decodeEmail(item);
        //encodeEmail(item);
    });
    
    $('left').getElements('a.menu-item').each(function(item, index) {
        var img = item.getElement('img');
        var outSrc = img.src;
        var overSrc = img.src.replace(/\.png$/, '_over.png');
        item.addEvents({
            'mouseover': function() {
                img.src = overSrc;
            },
            'mouseout': function() {
                img.src = outSrc;
            }
        });
    });
    
    function encodeEmail(item) {
        var addr = item.get('text');
        var salt = 10 + Math.floor(Math.random()*90);
        var enc = encode(addr, key, salt);
        var lnk = new Element('a', {'href': 'mailto:' + decode(enc, key, salt)});
        lnk.set('text', enc + salt);
        item.empty().grab(lnk);
    }
    
    function decodeEmail(item) {
        var enc = item.get('text');
        var salt = enc.substr(-2);
        var addr = decode(enc.substr(0, enc.length-2), key, salt);
        var lnk = new Element('a', {'href': 'mailto:' + addr});
        lnk.set('text', addr)
        item.empty().grab(lnk);
    }
    
    function encode(val, key, salt) {
        if (!salt) salt = 0;
        var enc = '';
        var c = -1;//
        while (++c < val.length) {
            enc += String.fromCharCode(val.charCodeAt(c) + key.charCodeAt((c+salt)%key.length));
        }
        return enc;
    }
    
    function decode(val, key, salt) {
        if (!salt) salt = 0;
        var dec = '';
        var c = -1;
        while (++c < val.length) {
            dec += String.fromCharCode(val.charCodeAt(c) - key.charCodeAt((c+salt)%key.length));
        }
        return dec;
    }
    
});

function joinList( form ) {
    var req = myRequest( 'scripts/joinlist.php?email=' + form.email.value );
    if( !req ) {
        alert( 'Sorry, you have an incompatible brower!' );
    }
    return false;
}

function myRequest(myUrl, callBack) {
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest(); // Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera
	}
	else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer
	}
	else {
		return false;
	}
	
	xmlhttp.open( "GET", myUrl, true ); // Open a connection. Replace GET with HEAD in order to do a HEAD request.
	
	//xmlhttp.callBack = callBack ? callBack : showMessage;
	callBackFunction = callBack ? callBack : showMessage;
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) { // Wait until everything is fetched!
			//xmlhttp.callBack( xmlhttp.responseText );
			callBackFunction(xmlhttp.responseText);
		}
	}
	
	xmlhttp.send(null); // send() is used to initiate the transfer. No actual data needs to be sent in this case
					    // because it's all in the GET/POST
	return true;
	//*/
}

function showMessage( msg ) {
	alert( msg );
}

function selectafield(targ) {
    targ.select();
    targ.focus();
    targ.onclick = null;
    targ.onblur = function() {
        this.onclick = function() {
            selectafield(this);
        }
    }
}


