/* $Id: global.js,v 1.13.2.2 2008-12-12 22:26:30 dsacramore Exp $ */

/*
=pod

=head1 NAME

global.js - functions and classes used globally across all pages

=head1 FUNCTIONS and CLASSES

=cut

*/


//For nav - probably will change/delete

//////////////////////////////////////////////////////////////////////////
// Find the location of the main nav tab for proper placement of the dropdown
//////////////////////////////////////////////////////////////////////////
function findPos(obj) {
    obj = document.getElementById(obj);
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return [curleft,curtop];
}
//////////////////////////////////////////////////////////////////////////
// Close all of th emain nav drop downs
//////////////////////////////////////////////////////////////////////////
function closeAll() {
 $('music_menu').style.display = "none";
 $('sports_menu').style.display = "none";
 $('arts_menu').style.display = "none";
 $('family_menu').style.display = "none";
 $('especiales_menu').style.display = "none";
 $('toros_menu').style.display = "none";
}
//////////////////////////////////////////////////////////////////////////
// Open a main nav dropdown - deprecated, do NOT use
//////////////////////////////////////////////////////////////////////////
function menu( cat, effect, delay ) {
    if (typeof(effect_queue) != 'undefined'){ clearTimeout(effect_queue); };
    menu_name = cat + '_menu';
    myEffect = effect;
    timeDelay = delay;
    if( effect == "show_menu" && $(menu_name) ){
        menu_height = $(menu_name).getDimensions().height ? $(menu_name).getDimensions().height : 3;
        menu_width = $(menu_name).getDimensions().width ? $(menu_name).getDimensions().width : 4;
        $(menu_name).setStyle({display: "block"});
        var coord = $(cat).cumulativeOffset();
        var h_space = coord[0];
        var v_space = coord[1] + $(cat).getHeight();
        $(menu_name).setStyle({top: v_space+'px', left: h_space+'px'});
        $('drop_shim').setStyle({display: "block", top: v_space+'px', left: h_space+'px', height: menu_height - 3 + 'px', width: menu_width - 4 + 'px'});
        $(cat).addClassName('hover');

    }
    else if( effect == "hide_menu" && $(menu_name) ){
        $(cat).removeClassName('hover');
        $(menu_name).hide();
        $('drop_shim').hide();
    }
}
//////////////////////////////////////////////////////////////////////////
// Apply a scriptaculous effect to the main nav dorp down
// (Currently unused, due to side effect of "Flashing" and JS breaking
// when too many are rolled over)
//////////////////////////////////////////////////////////////////////////
function runEffect() {
    switch( myEffect ) {
        case "appear":
            new Effect.Appear( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "fade":
            new Effect.Fade( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "puff":
            new Effect.Puff( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "blind_down":
            new Effect.BlindDown( $(menu_name), {queue: {position: 'end', scope: 'topnavscope', limit:2}, delay: timeDelay} );
        break;
        case "blind_up":
            new Effect.BlindUp( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "switch_off":
            new Effect.SwitchOff( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "slide_down":
            new Effect.SlideDown( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "slide_up":
            new Effect.SlideUp( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "drop_out":
            new Effect.DropOut( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "shake":
            new Effect.Shake( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "pulsate":
            new Effect.Pulsate( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "squish":
            new Effect.Squish( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "fold":
            new Effect.Fold( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "grow":
            new Effect.Grow( $(menu_name), {direction: 'top-left', queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "shrink":
            new Effect.Shrink( $(menu_name), {direction: 'top-left', queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "highlight":
            new Effect.Highlight( $(menu_name), {queue: {position: 'end', scope: 'topnavscope'}, delay: timeDelay} );
        break;
        case "show":
            $( $(menu_name) ).show();
        break;
        default:
            $('music_menu','sports_menu','arts_menu','family_menu').invoke('hide');
        break;
    }
}
//////////////////////////////////////////////////////////////////////////
// USed FOR "CHANGE LOCATION" BOX AND LEFT NAV
//////////////////////////////////////////////////////////////////////////
function toggleA(box_id,input_id){
    var box = $(box_id);
    var input = $(input_id);

    if( input.visible() ) {
        box.className = 'more';
        input.hide();
    }
    else {
        box.className = 'less';
        input.show();
    }
}
//////////////////////////////////////////////////////////////////////////
// Ren's Script for Browse Box dropdowns
//////////////////////////////////////////////////////////////////////////
function setHidden(field_name, field_value, field_select){
    // Hide the error text, if previously displayed
    $('browse_error_us_national_90_days').hide();
    $('date_range_too_large').hide();
    // Test for form error
    if (browse_us_national && (field_value == "n93") && (($('browse_area').value == "") || ($('browse_area').value == browse_area_default_text))){
        $('browse_error_us_national_90_days').show();
    }

    // If the being clicked is either major or minor category, then the hidden var is called "category"
    if ((field_name == 'minor_cat') || (field_name == 'major_cat')){
        $("category").value = field_value;
        if (field_name == 'major_cat'){
            if (((current_domain == 'au') || (current_domain == 'nz')) && (field_value == 8)) {
                $('root').value = 10004;
            }else{
                $('root').value = field_value;
            }
            $('minor_cat_dropdown').update();
        }
    }else {
        $(field_name).value=field_value;
    }

    // Do not try to reset the simulated input field for the range dates
    if ((field_name != 'rdc_smonth') &&
        (field_name != 'rdc_sday') &&
        (field_name != 'rdc_syear') &&
        (field_name != 'rdc_emonth') &&
        (field_name != 'rdc_eday') &&
        (field_name != 'rdc_eyear') && $(field_name + '_title') && $(field_name + '_dropdown')){
            $(field_name + '_title').update(decodeURIComponent(field_select));
            $(field_name + '_dropdown').hide();
    }

    //console.log($(field_name).value);
}
//////////////////////////////////////////////////////////////////////////
// Build the Minor Category dropdown based on selecting a major cat
//////////////////////////////////////////////////////////////////////////
function build_minor_cat_dropdown(major_cat_id){
    title_span = eval('minor_cat_title_span_' + major_cat_id);

    $('minor_cat_title').update(title_span);
    $('minor_cat_title').removeClassName('off');
    $('minor_cat_dropdown').removeClassName('off');
    if(major_cat_id == '10012'){
    $('minor_cat_title').removeClassName('on');
    $('minor_cat_dropdown').removeClassName('on');
    $('minor_cat_title').addClassName('off');
    $('minor_cat_dropdown').addClassName('off');
    $('rdc_select_dropdown').toggle(); $('browse_calendar_picker').hide();  setHidden('rdc_select', 'n1095', 'Show all'); return false;
    }

    for( k in genres[major_cat_id] ) {
        var min_cat_anchor = new Element('a', { title: genres[major_cat_id][k].replace("&#39;","'"),
                                                href: httpServer
                                            }
                                        );
    $(min_cat_anchor).onclick = new Function("setHidden('minor_cat', '" + k + "', '" + genres[major_cat_id][k] + "'); return false;");
    $(min_cat_anchor).update( genres[major_cat_id][k] );
        $('minor_cat_dropdown').appendChild( min_cat_anchor );
    }
}
//////////////////////////////////////////////////////////////////////////
// Change Location JS for Top Nav
//////////////////////////////////////////////////////////////////////////
function verifyLocationString(form) {
    $('location_eror_blank','location_eror_multiple','location_eror_multiple_dropdown','location_eror_not_found').invoke('hide');
    if( $F('location_input').strip().empty() || $F('location_input') == location_text ){
        $('location_eror_blank').show();
        $('location_eror_multiple').hide();
        $('location_eror_multiple_dropdown').hide();
        $('location_eror_not_found').hide();
        $('location_input').focus();
        $F('location_input').value = location_text;
        return false;
    } else {
        $(form).request({
            parameters: { uri: page_context_uri, use_json: 1 },
            onSuccess: function(t) {
                var loc_data = t.responseText.evalJSON();
                if( loc_data.redirect == 1 ) {
                    wt_stringscrape($F('location_input'), '1');
                    self.location.href = loc_data.location;
                }
                else if( loc_data.count > 1 ) {
                    $('location_multiple_dropdown').childElements().invoke('remove');
                    $('location_results').update( $F('location_input') );
                    $('location_eror_multiple').show();
                    buildMulti( loc_data );
                    $('location_eror_multiple_dropdown').show();
                    wt_stringscrape($F('location_input'));
                }
                else {
                    $('location_eror_blank').hide();
                    $('location_eror_multiple').hide();
                    $('location_eror_multiple_dropdown').hide();
                    $('location_eror_not_found').show();
                    wt_stringscrape($F('location_input'));
                }
            },
            onFailure: function(t) {
            }
        });
        return false;
    }
}


function verifyDmaString(form) {
    $('dma_eror_blank','dma_eror_not_found').invoke('hide');
    if( $F('dma_area').strip().empty() || $F('dma_area') == location_text ){
        $('dma_eror_blank').show();
        $('dma_eror_not_found').hide();
        $('dma_area').focus();
        return false;
    } else {
        $(form).request({
            parameters: { uri: page_context_uri, use_json: 1 },
            onSuccess: function(t) {
                var loc_data = t.responseText.evalJSON();
                if( loc_data.redirect == 1 ) {
                    wt_stringscrape($F('dma_area'), '1');
                    document.changelocation.submit();
                }
                else if( loc_data.count > 1 ) {
                    wt_stringscrape($F('dma_area'), '1');
                    document.changelocation.submit();
                }
                else {
                    wt_stringscrape($F('dma_area'));
                    $('dma_eror_blank').hide();
                    $('dma_eror_not_found').show();
                }
            },
            onFailure: function(t) {
            }
        });
        return false;
    }
}


//////////////////////////////////////////////////////////////////////////
// Location Module - Builds the faux select box of found cities
//////////////////////////////////////////////////////////////////////////
function buildMulti( theData, theType ) {
    var to_insert;
    var tm_link = theData.tm_link;
    if( theType == 'browse' ) {
        to_insert = 'browse_location_multiple_dropdown';
    }
    else if( theType == 'search' ) {
        to_insert = 'search_location_multiple_dropdown';
    }
    else {
        to_insert = 'location_multiple_dropdown';
    }
    theData.countries.each(
        function( c ) {
            if( c != theData.countries.first() ) {
                var dv = new Element('div', {'className': 'locDivide'} ).update( theData.country[c] );
                Element.insert($(to_insert), dv);
            }
            theData.dmas[c].sortBy( function( dma ) { return dma.desc; } ).each(
                function( dma ) {
                    var a_link;
                    if( theType == 'browse' ) {
                        a_link = new Element('a', { title: dma.desc, href: '/' }).update( dma.desc );
                        $(a_link).onclick = new Function( 'browse_loadDMA("' + dma.desc + '", ' + dma.dma + '); return false;' );
                    }
                    else {
                        a_link = new Element('a', { title: dma.desc, href: '/change_area?uri=' + page_context_uri + '&dma=' + dma.dma + '&desc=' + encodeURI(dma.desc) + '&tm_link=' + tm_link }).update( dma.desc );
                    }
                    Element.insert($(to_insert), a_link);
                }
            );
        }
    );
}
//////////////////////////////////////////////////////////////////////////
// updates fields upon selection from city faux-select box
//////////////////////////////////////////////////////////////////////////
function browse_loadDMA( theDesc, theID ) {
    $('browse_area').value = theDesc;
    $('browse_location_eror_multiple_dropdown').hide();
    $('browse_location_eror_multiple').hide();
    $('browse_go_button').disable();
    $('browse_go_button').className = 'go_wait';
    new Ajax.Request('/change_area', {
        method: 'get',
        parameters: { dma: theID },
        onSuccess: function(t) {
            multiSelect = true;
            $('browse_go_button').enable();
            $('browse_go_button').className = 'go';
        },
        onFailure: function(t) {
            multiSelect = false;
            $('browse_go_button').enable();
            $('browse_go_button').className = 'go';
        }
    } );
    if( $('rdc_select') && $('rdc_select').value != 'range' ) {
        $('calendar_from_input').disable().setStyle({backgroundColor: '#ffffff'});
        $('calendar_to_input').disable().setStyle({backgroundColor: '#ffffff'});
    }
    if( $('rdc_select') && $('rdc_select').value == 'range' ) {
        $('rdc_select').remove();
    }
}

function convertDate( date_from, date_to ) {
    if( Object.isElement($(date_from)) && Object.isElement($(date_to)) ) {
        calendar_from_input_str = $(date_from).value;
        calendar_to_input_str = $(date_to).value;

        if (date_string_format == '%d/%m/%Y'){
            date_separater = date_string_format.substr(2,1);
            calendar_from_input_str = calendar_from_input_str.split(date_separater);
            calendar_from_input_str = calendar_from_input_str[1] + date_separater + calendar_from_input_str[0] + date_separater + calendar_from_input_str[2];
            calendar_to_input_str = calendar_to_input_str.split(date_separater);
            calendar_to_input_str = calendar_to_input_str[1] + date_separater + calendar_to_input_str[0] + date_separater + calendar_to_input_str[2];
        }
        return true;
    }
    else {
        return false;
    }
}

//////////////////////////////////////////////////////////////////////////
// Misc form preprocessing and validation before submit
//////////////////////////////////////////////////////////////////////////
function preProcessBrowse() {
    $('browse_go_button').disable();
    $('browse_go_button').className = 'go_wait';
    // First we hide all errors
    $$('.error.padV5.txt11.lh13').invoke('hide');
    if( $('browse_location_eror_multiple_dropdown') ) {
        $('browse_location_eror_multiple_dropdown').hide();
    }

    // If we have no category error out right away
    if( $('category').value == "NULL" ){
        $('major_cat_missing').show();
        $('browse_go_button').enable();
        $('browse_go_button').className = 'go';
        return false;
    }
    if( browse_us_national && ($('rdc_select').value == "n93") && (($('browse_area').value == "") || ($('browse_area').value == browse_area_default_text)) ) {
        $('browse_error_us_national_90_days').show();
        $('browse_go_button').enable();
        $('browse_go_button').className = 'go';
        return false;
    }
    if( ($('rdc_select') && $('rdc_select').value == 'range') || $('type').value == 'range' ) {
        // Re-enable the form fields just in case
        $('rdc_smonth','rdc_sday','rdc_syear','rdc_emonth','rdc_eday','rdc_eyear').invoke('enable');
        // We convert date formats to the one we want
        var date_check = convertDate( $('calendar_from_input'), $('calendar_to_input') );
        if( !date_check ) {
            alert("Calendar input field are blank");
            $('browse_go_button').enable();
            $('browse_go_button').className = 'go';
            return false;
        }
        // We check for a date range that is too long
        if( $D(calendar_to_input_str).diff(calendar_from_input_str, 'days') > 60 && browse_us_national == 1 && (($('browse_area').value == "") || ($('browse_area').value == browse_area_default_text)) ) {
            $('date_range_too_large').show();
            $('browse_go_button').enable();
            $('browse_go_button').className = 'go';
            return false;
        }
        else {
            // Since we are ranged we have to set the type parameter
            $('type').value = 'range';
            // We also have to disable of the rdc_select input
            if( $('rdc_select') ) {
                $('rdc_select').disable();
            }
            // Since we are ranged we also need the teVal parameter for some reason
            if( !Object.isElement($('teVal')) ) {
                var teval_field = new Element('input', { type: "hidden", name: "teVal", id: "teVal" });
                $('ccs_form').appendChild( teval_field );
            }
            // We set up the values in the form now and get ready to submit
            $('rdc_smonth').value   = $D(calendar_from_input_str).getMonthNumber();
            $('rdc_sday').value     = $D(calendar_from_input_str).getDate();
            $('rdc_syear').value    = $D(calendar_from_input_str).getFullYear();
            $('rdc_emonth').value   = $D(calendar_to_input_str).getMonthNumber();
            $('rdc_eday').value     = $D(calendar_to_input_str).getDate();
            $('rdc_eyear').value    = $D(calendar_to_input_str).getFullYear();
            // Disable the values for the date range faux input boxes
            $('calendar_from_input').disable().setStyle({backgroundColor: '#ffffff'});
            $('calendar_to_input').disable().setStyle({backgroundColor: '#ffffff'});
        }
    }
    else {
        // If the date is not range, change the hidden 'type' field to selected
        $('type').value = 'selected';
        // The the hidden field to be set is not "range", remove "teval", which may exist if the user selected range then hit the back button
        if( Object.isElement($('teVal')) ){
            $('teVal').remove();
        }
        // Disable the date value fields since we don't need those in the form
        $('rdc_smonth','rdc_sday','rdc_syear','rdc_emonth','rdc_eday','rdc_eyear').invoke('disable');
    }

    // Okay time to handle location now
    // If we are nationalized then empty out the area parameter and send the form
    if( $('browse_area').value.indexOf(checkUni(browse_area_default_text)) !=-1 && !browse_area_default_text.empty() ) {
        $('browse_area').value = "";
    }
    else {
        if( multiSelect || (current_location == $('browse_area').value ) ) {
            if( multiSelect != 'browse' ) {
                $('browse_area').disable().setStyle({backgroundColor: '#ffffff'});
            }
            $('browse_go_button').enable();
            $('browse_go_button').className = 'go';
            return true;
        }
        else {
            multiSelect = false;
            var return_value = new Ajax.Request('/find_area', {
                method: 'get',
                parameters: { uri: page_context_uri, use_json: 1, area: $F('browse_area') },
                onSuccess: function(t) {
                    var browse_loc_data = t.responseText.evalJSON();
                    if( browse_loc_data.redirect == 1 ) {
                        if( $('rdc_select') && $('rdc_select').value != 'range' ) {
                            $('calendar_from_input').disable().setStyle({backgroundColor: '#ffffff'});
                            $('calendar_to_input').disable().setStyle({backgroundColor: '#ffffff'});
                        }
                        if( $('rdc_select') && $('rdc_select').value == 'range' ) {
                            $('rdc_select').remove();
                        }
                       if($('browse_area') && $('browse_area').type != 'hidden' ){
                          wt_stringscrape($('browse_area').value, '1');
                        }
                        $('ccs_form').submit();
                    }
                    else if( browse_loc_data.count > 1 ) {
                        if($('browse_area') && $('browse_area').type != 'hidden' ){
                           wt_stringscrape($('browse_area').value);
                        }
                        $('browse_location_multiple_dropdown').childElements().invoke('remove');
                        $('browse_location_results').update( $F('browse_area') );
                        $('browse_location_eror_multiple').show();
                        buildMulti( browse_loc_data, 'browse' );
                        $('browse_location_eror_multiple_dropdown').show();
                    }
                    else {
                        if($('browse_area') && $('browse_area').type != 'hidden' ){
                             wt_stringscrape($('browse_area').value);
                         }
                        $('browse_location_eror_not_found').show();
                    }
                    $('browse_go_button').enable();
                    $('browse_go_button').className = 'go';
                },
                onFailure: function(t) {
                }
            });
            return false;
        }
    }
}
// With this implementation, you don't have to set event handlers on the HTML level (no onclick, onmouseover, or onmouseout)
// This is DropMenu Class Constructor
var DropMenu = Class.create( {
    initialize: function(title, dropdown){
            var timer = null;
            $(title).observe('click', function(e) {
                    $(dropdown).toggle();
            });
            $(dropdown).observe('mouseover', function(e) {
                    clearTimeout(timer);
            });
            $(dropdown).observe('mouseout', function(e) {
                    timer = setTimeout( function(){$(dropdown).hide();},500);
            });
    } // end of initialize
});

function initLoc() {
    var ndma_c = GetCookie('NDMA');
    var npdma_c = GetCookie('NPDMA');
    if( ( npdma_c && ndma_c ) && stored_dma != ndma_c && stored_dma != npdma_c ) {
        self.location.reload();
    }
}

function initHPB() {
    if( $F('root') != 'NULL' && $F('category') != 'NULL' ) {
        var cat = $F('category');
        var root = $F('root');
        if( current_domain == 'au' || current_domain == 'nz' ) {
            if( cat == '711' || cat == '8' ) {
                root = '8';
            }
        }
        setHidden('major_cat', root, $(root).title );
        build_minor_cat_dropdown( root );
        setHidden('minor_cat', cat, genres[root][cat] );
    }
    if( $F('browse_area').empty() && !browse_area_default_text.empty() ) {
        $('browse_area').value = browse_area_default_text;
    }
    if( $('browse_area').present() && $F('browse_area') != current_location ) {
        if( browse_area_default_text.empty() ) {
            $('browse_area').value = current_location.unescapeHTML();
        }
        else {
            $('browse_area').value = browse_area_default_text;
        }
    }
}

function add_did_to_links() {
    if ( spcOffer_did ) {
        var links = Element.extend(document.body).select('a.link_needs_did');

        links.each( function(link) {
            delim = link.href.search ( /[?]/ ) == -1 ? '?' : '&';
            link.href += delim + "did=" + spcOffer_did;
        });
    }
}

function add_query_args ( url, args ) {
    var delim = url.search( /\?/ ) < 0 ? '?' : '&';

    for ( key in args ) {
        if ( args[key] ) {
            url += delim + key + "=" + escape(args[key]);
            delim = '&';
        }
    }

    return url;
}

CookieStore = Class.create( {
    initialize: function( store_name ) {
        this.store_name = store_name;

        this._load();
    },

    _load: function() {
        var json = GetCookie( this.store_name );
        if ( json )
            this.data = unescape(json).evalJSON();
        else
            this.clear();
    },

    clear: function() {
        this.data = {};
        this._store();
    },

    _store: function() {
        document.cookie = this.store_name + "=" + escape(Object.toJSON( this.data )) + "; path=/;expires=";
    },

    get: function( key ) {
        return this.data[key];
    },

    set: function( key, value ) {
        this.data[key] = value;
        this._store();
    },

    remove: function ( key ) {
        delete this.data[key];
        this._store();
    }
} );

CookieTree = Class.create( {
    initialize: function () {
        this.keys = $A(arguments);
        this.store = new CookieStore( this.keys.shift() );
    },

    _get_data: function() {
        var datakey = this.keys[0];
        var data = this.store.get( datakey ) || {};

        for( var i = 1 ; i < this.keys.length ; i ++ ) {
            var key = this.keys[i];
            data = data[key] || {};
        }

        return data;
    },

    _set_data_for_key: function ( ikey, storedata, newdata ) {
        storedata = storedata || {};

        if ( ikey == this.keys.length - 1 )
            storedata[this.keys[ikey]] = newdata;
        else if ( ikey < this.keys.length )
            // only recurse if there are more and we are not at the end
            storedata[this.keys[ikey]] = this._set_data_for_key( ikey + 1, storedata[this.keys[ikey]], newdata );

        return storedata;
    },

    _set_data: function( data ) {
        var datakey = this.keys[0];
        var storedata = this.store.get( datakey );

        storedata = this._set_data_for_key( 1, storedata, data );

        this.store.set( datakey, storedata );
    },

    get: function( key ) {
        if ( this.keys.length ) {
            var data = this._get_data();
            return data[key];
        }
        else
            return this.store.get( key );
    },

    set: function( key, value ) {
        if ( this.keys.length ) {
            var data = this._get_data();
            data[key] = value;
            this._set_data( data );
        }
        else
            this.store.set( key, value );
    },

    remove: function( key ) {
        if ( this.keys.length ) {
            var data = this._get_data();
            delete data[key];
            this._set_data( data );
        }
        else
            this.store.remove( key );
    }
} );

var Discrete = {
    setup: function( event_id ) {
        var url_did = new $H( location.search.toQueryParams() ).get('did');
        var cookie = new CookieTree( '_E' );
        var did = cookie.get( 'did' );

        // reset the saved DID if not for this event
        if ( !did || did[0] != event_id ) {
            did = [ 0, '' ];
            Discrete.clear();
        }

        // the discrete is either passed in or, if we are on the same event,
        // fetched from the cookie
        Discrete.id = (!url_did || url_did == "null") ? did[1] || '' : url_did.toLowerCase();

        // store the did if we got one
        if ( Discrete.id )
            cookie.set( 'did', [ event_id, Discrete.id ] );
    },

    clear: function() {
        new CookieTree( '_E' ).remove( 'did' );
        delete Discrete.id;
    }
};

var EventState = {
    touch: function( event_id ) {
        var events = new CookieTree( '_E' ).get( 'el' ) || [];
        events = events.without( event_id );
        events.push( event_id );

        if ( events.length > 10 ) {
            var oldest = events.shift();
            new CookieTree( "_E" ).remove( oldest );
        }
        new CookieTree( '_E' ).set( 'el', events );
    }
};

function getSearch( uri, h, k, args ) {
    new Ajax.Request(uri, {
        method: 'get',
        parameters: args,
        onSuccess: function(t) {
            var data = t.responseText.evalJSON();
            if( k == 'histogram' ) {
                h.set( k, data.facet_counts.facet_fields );
            }
            else if( k == 'didyoumean' ) {
                h.set( k, data.spellcheck );
                h.set( 'seealso', data.response.docs );
            }
            else {
                h.set( k, data.response );
                h.set( 'total', h.get('total') + h.get( k ).numFound );
                h.set( 'total_docs', h.get('total_docs') + h.get( k ).docs.length );
            }
        },
        onFailure: function(t) {
            h.set( 'response_errors', 1 );
        }
    } );
}

function getMenu( uri, h, k, args ) {
    new Ajax.Request(uri, {
        method: 'get',
        parameters: args,
        onSuccess: function(t) {
            if( k == 'popular_events' ) {
                h.set( k, t.responseText.evalJSON() );
            }
            else {
                h.set( k, t.responseText.evalJSON().facet_counts.facet_fields );
            }
        }
    } );
}

function getMember( uri, h, k, args ) {
    new Ajax.Request(uri, {
        method: 'get',
        parameters: args,
        onSuccess: function(t) {
            h.set( k, t.responseText.evalJSON().member );
        }
    } );
}

function anchorClick()
{
    var theAnchor = $$('a') ;
    for(var i=0;i<theAnchor.length;i++)
    {
	theAnchor[i].onfocus = function () { if(this.blur)this.blur(); };
    }
}

Currency = Class.create( {
    amount_regexp: new RegExp( '333\(.\)222\(.\)11', '' ),

    initialize: function( template ) {
        this._template = template;
        var results = template.match( this.amount_regexp );
        this._thou = results[1];
        this._dec = results[2];
    },

    format: function( amount ) {
        var dollars = Math.floor( amount );
        // Math.round to deal with javascript precision errors
        var cents = Math.round( (amount - dollars) * 100 );
        cents = cents.toString();
        if ( cents.length == 1 )
            cents = '0' + cents;

        var formatted_amount = this._dec + cents;

        dollars = dollars.toString();
        while ( true ) {
            var hundreds = dollars.slice( -3 );
            formatted_amount = hundreds + formatted_amount;
            dollars = dollars.slice( 0, -3 );

            if ( dollars == '' )
                // no more digits.  we're done
                break;
            else
                // add a hundreds separator
                formatted_amount = this._thou + formatted_amount;
        }

        // replace the amount in the template with the formatted amount and return it
        return this._template.replace( this.amount_regexp, formatted_amount );
    }
} );

/*
package global::OnWindowLoad;

=pod

=over 4

=item Class: B<OnWindowLoad>

Manages and call functions after the window.onload event in sequence

=over 4

=item SYNOPSIS

  new OnWindowLoad( function() {
    // do some stuff
  } );

  new OnWindowLoad( function() {
    // do some stuff
  }, { load_last: true } );


=item DESCRIPTION

Different browsers execute events attached to window.onload in different order.  For example, IE executes them in the reverse order compared to Firefox.  This class manages lists of functions and calls them squentially in the order that they were added.

Optionally you can specify a flag to have the function executed last (or nearly last).

=back

=back

=cut

*/

OnWindowLoad = Class.create( {
    to_load: [],
    to_load_last: [],
    loaded: [],
    flags: {
        is_running: false,
        window_loaded: false
    },

    initialize: function( f, options ) {
        this.f = f;

        options = Object.extend( {
            load_last: false
        }, options );

        if ( options.load_last ) {
            this.to_load_last.push( this );
        }
        else {
            this.to_load.push( this );
        }
    },

    run: function() {
        if ( !this.flags.window_loaded || this.flags.is_running ) {
            return;
        }
        else {
            this.flags.is_running = true;
        }

        // run it
        this.f();

        this.flags.is_running = false;

        // shift the loader object to the finish list
        this.loaded.push( this.to_load.shift() );

        // run the next one
        this.next();
    },

    next: function() {
        this.flags.window_loaded = true;

        if ( !this.to_load.length && this.to_load_last.length ) {
            this.load_load = this.to_load_last;
            this.to_load_last = [];
        }

        // run 'em if we got 'em
        if ( this.to_load.length ) {
            this.to_load[0].run();
        }
    }
} );
Event.observe( window, 'load', OnWindowLoad.prototype.next.bind( OnWindowLoad.prototype ) );

