/*
 * aisa.js - site-wide JavaScript functions for use with jQuery.js
 *
 * @author: Martin Bean <martin@azure-design.com>
 * @copyright: http://www.azure-design.com
 * @created: 05 December 2008
 *
 * IMPORTANT!
 *   All jQuery functions shuold use the jQuery namespace rather than the dollar ($) namespace.
 *   This is because using dollar conflicts with the Prototype framework, which is used to power Lightbox on the site.
 *   They now co-exist on the site peacefully despite an larger impact on load time - boo! :'(
 *
 * @ref: http://docs.jquery.com/Using_jQuery_with_Other_Libraries
 */

//
jQuery.noConflict();

// invokes various JS functions on jQuery(document).ready()
jQuery(document).ready(function() {
    jsEnabled();
    formDefaults();
    targetBlank();
    sifrText();
    numbersOnly();
    updatePrice();
});

// adds a class of 'js' if JS is enabled
function jsEnabled() {
    jQuery('body').addClass('js-enabled');
}

// sets default value of form fields
function formDefaults() {
    jQuery('#name').DefaultValue('');
    jQuery('#message').DefaultValue('');
    jQuery('#email_address').DefaultValue('');
    jQuery('#username').DefaultValue('Username');
    jQuery('#password').DefaultValue('Password');
    jQuery('#additional-donation').DefaultValue('0.00');
}

// opens external links in new windows, as target attribute is deprecated in XHTML 1.x Strict
function targetBlank() {
    jQuery('a[rel="external"]').click(function() {
        window.open(jQuery(this).attr('href'));
        return false;
    });
}

// sIFR used for the majority of heading titles - RTFM: http://jquery.thewikies.com/sifr/
function sifrText() {
    jQuery.sifr({
        font: 'themixbold.swf',
        path: '/cms/includes/fonts/',
        textTransform: 'uppercase',
        link: '#bb2014',
        hover: '#e10000'
    });
    jQuery('#main-column h2').sifr({
        color: '#000000'
    });
/*    jQuery('#main-column h3').sifr({
        color: '#bb2014',
		  width: '480'
    });*/
    jQuery('#home #main-column #featured-articles h3').sifr({
        color: '#001f46'
    });
    /*jQuery('#home #main-column #featured-articles h4').sifr({
        color: '#bb2014',
        width: '240'
    });*/
    jQuery('#right-column #editable-area h3').sifr({
        color: '#000000'
    });
    jQuery('#who-are-aisa-inner h3').sifr({
        color: '#ffffff'
    });
    //jQuery('#left-column #site-of-the-week span.title').sifr({
        //color: '#000000'
    //});
    // elements caught by sIFR definitions above but don't need to be sIFR-ized
    jQuery('#main-column #main-story-body h2, #main-column .latest h2, #main-column .headlines h3').sifr({
        unsifr: true
    });
}

// only allows input of numbers on form elements with class of .numeric
function numbersOnly() {
    jQuery('.numeric').keypress(function(e) {
        if(e.which != 8 && e.which != 0 && e.which != 46 && (e.which < 48 || e.which > 57)) {
            alert('Please enter numbers only');
            return false;
        }
    });
}

// updates price on join form when a membership option is selected/changed
function updatePrice() {
    jQuery('#membership-option').change(function() {
        total = parseFloat((jQuery(this).val())) + parseFloat((jQuery('#additional-donation').val()));
        total = total.toFixed(2);
        jQuery('#price').html(jQuery(this).val()).show();
        jQuery('#total-price').html(total).show();
        jQuery('.membership-value').val(total);
        jQuery(':selected').each(function() {
            str = jQuery(this).attr('title');
        });
        jQuery('.membership-title').val(str);
    });
    jQuery('#additional-donation').change(function() {
        total = parseFloat((jQuery(this).val())) + parseFloat((jQuery('#membership-option').val()));
        total = total.toFixed(2);
        jQuery('#total-price').html(total).show();
        jQuery('.membership-value').val(total);
    });
}

// opens external links in a new window
// removed as was causing windows to open twice - maybe a confict with scriptalicious/prototype?
// does that have a default action on links with rel[external]?
//function externalLinks() {
//    jQuery('a[rel="external"]').click(function() {
//        window.open(jQuery(this).attr('href'));
//        return false;
//    });
//}