/* General application-wide functions and settings */

//site-wide page load functions
function pageInit() {
    
    //convert 'popup' links to open in new window
    $$('a.popup').each( function(link) {
        if (!link) return;
        link.observe('click', makePopup.bindAsEventListener(link) );
    });
    
    //decode server-encoded email address
    DecodeEmailAddresses();
    
}

//run the init function as soon as the page is ready
document.observe('dom:loaded', pageInit);


//opens a new window using the URL and settings provided
function popup(url, settings) {
    var defaults = $H({
        height:     550,
        width:      650,
        scrollBars: 1,
        toolbar:    1,
        resizable:  1,
        location:   1,
        status:     1
    });
    
    //override defaults if settings provided
    var options = defaults.merge(settings);
    //create a string of all options (height=550, width=650, toolbar=1...)
    options = options.invoke('join', '=');
    
    window.open(url, 'popup_win', options);
}

//event-handler to create a popup from links on the page
//NOTE: uses 'this' as the actual link element, so bind it!
function makePopup(e) {
    e.stop();
    
    //check for overriding settings
    overrideSettings = getSettingsFromClassNames(this, ['width', 'height']);
    
    popup(this.href, overrideSettings);
}

//given a list of possible setting overrides, returns a hash object with the name
//of the setting as the key and the value found on the CSS class names as the value. 
//example: given settingName 'height' and class name height-750, returns {height: 750}
function getSettingsFromClassNames(obj, settingNames) {
    //store override values in a hash
    var overrideSettings = new Hash();
    
    settingNames.each(function(name) {
        var setting = getSettingFromClassName(obj, name);
        if (setting) {
            overrideSettings.set(name, setting);
        }
    });
    
    return overrideSettings;
}

//finds the value of a single setting given a CSS class name, 
//i.e. class=width-750 sets the width property to "750"
function getSettingFromClassName(obj, settingName) {
    var setting = obj.classNames().findAll(function(name) {
        return name.startsWith(settingName+'-');
    });
    
    if (setting != null) {
        return setting.toString().sub(settingName+'-', '');
    } else {
        return null;
    }
}


//decode email addresses that were ASCII-encoded on the server to prevent spam harvesting
function decode(ServerEncoded) {
    // The ServerEncoded parameter is a string that contains the encoded data.
    // Each character in the ServerEncoded parameter has been converted into 
    // a two digit number (hex / base16). This function converts the
    // series of numbers back into the normal form and returns the 
    // decoded string to the client
    // holds the decoded string

    var res = "";
    
    // remove the '-' from the ASCII-encoding that happens on the server
    ServerEncoded = ServerEncoded.split('-').join('');

    // go through and decode the full input server encoded string
    for (i=0; i < ServerEncoded.length;) {
	    // holds each letter (2 digits)
	    var letter = "";
	    letter = ServerEncoded.charAt(i) + ServerEncoded.charAt(i+1)

	    // build the real decoded value
	    res += String.fromCharCode(parseInt(letter,16));
	    i += 2;
    }
	
    //return the new decoded string to allow it to be rendered to screen
    return res;
}

/*
====================================================================
This function gets a reference to the server encrypted string and
then decrypts this using the decode() function and sets the
txtDecrypted value to the value return by the decode() function
====================================================================
*/
function DecodeEmailAddresses() {

	//get each email address on the page
	$$('.emailAddress').each(function(emailField) {
	    
	    //reverse the encoding done on the server
	    if (emailField.href && emailField.href != null) {
	        //remove the 'mailto:' part of the link so we can just work with the encrypted string
	        var emailAddress = emailField.href.replace('mailto:', '');
	        //decrypt the email and add the 'mailto:' back on so the link will work
	        emailField.href = 'mailto:'+ decode(emailAddress);
	    } else {
            emailField.update( decode(emailField.innerHTML) );
        }
        
        //email text hidden by default
        //re-display it once it's decrypted
        emailField.show();
    });
}
