// 
//  bundle.js
//  svn://.../northwestel/branches/code_on_lyris
//  $Revision: 19047 $

//  
//  Copyright 2009 Kellett Communications. All rights reserved.
// 
// This script is responsible for calculating bundle prices and 
// updating the bundle page to show discounts, etc.
// 
// All prices are in cents.
// 
// Eg.
//     $29.95 would be 2995

var Bundle = { // namespace

// @var monthly_costs list of undiscounted monthly charges
// 
// The property name must match up with the ID of a form input.
// 
// Eg.
//     The input with an ID of internet_light relates to the monthly_costs 
//     property internet_light.
//     
//     <input type="radio" name="internet" value="light"   id="internet_light"  />
//     ...
//     internet_light:    3995
// 
monthly_costs: {
    // Internet
    internet_light:    4195,
    internet_classic:  6295,
    internet_ultra:    8395,
    internet_extreme: 12495,

    // digital cable, aka "Essentials"
    digital_cable:     3695,

    // receivers
    receiver_2500:      495,	// Standard Digital Receiver
    receiver_6200:     1495,	// Home Theatre HD Receiver
    receiver_6416:     1995,	// Advanced HD DVR Receiver

    // packages            ,
    package_family:    1095,
    package_music:     1095,
    package_news:      1095,
	package_living:    1095,
    package_movies:    2095,
    package_french:    1095,
    package_sports:    1095,
    package_supermovies:1695,
    package_setanta:   1699,
    package_canadiana:  995,
    package_hd_premium:1095,
	package_hd_world:   595,
    
    // analog (NW only)
    package_A_basic:   6790,

    // specialty channels
    specialty_fairchild: 1995,
    specialty_playboy:   1595,
    specialty_hustler:   1995,
    specialty_red_hot:   1995,

    extra_outlets:       1295,
    
    home_phone:          4995
},

// @var one_time_costs list of undiscounted one-time charges
// 
// The property name must match up with the ID of a form input.
one_time_costs: {
    // subscriptions       ,
    subscription_NFL: 17900,
    subscription_NHL: 19900,
    installation:      7495,
    wireless:         10000
},

// @var savings_one_time list of cost-saved against one-time charges
// 
// The property name may match up with the ID of a form input.
savings_one_time: {
},

// @var savings list of cost-saved against monthly charges
// 
// The property name may match up with the ID of a form input.
savings: {
    // Internet
    internet_light:    1000,
    internet_classic:  1500,
    internet_ultra:    2000,
    internet_extreme:  2500,

    specialty_bundle:   995,
    super_plus_movies: 395
},

// @var duration the number of months used to calculate "Total Savings"
duration: 12,


/************************************************
 * Initialize the Bundle Calculator
 */
init: function(){
    Bundle.setup_specialty_channels();
    Bundle.calculate_price();
    jQuery('#bundleform input').change(Bundle.calculate_price).click(Bundle.calculate_price);
},

/************************************************
 * Crunch the numbers and display costs and savings
 */
calculate_price: function(){
    var B = Bundle;
    monthly_cost    = 0;
    monthly_savings = 0;
    one_time_cost   = 0;
    one_time_savings= 0;

    // clear out fields
    jQuery('#savings_monthly_cost').text('');
    jQuery('#savings_annual_cost').text('');
    jQuery('#your_cost_cost').text('');
    jQuery('.selected').removeClass('selected');

    Bundle.force_order();

    
    // // only show bundle cost if internet and receiver
    // if (jQuery('input[name=receiver]').length && // if no reciever input (NW)
    //     (
    //         !jQuery('input[name=receiver]:checked').length || 
    //         !jQuery('input[name=receiver]:checked').length
    //     )
    // ) {
    //     return;
    // };
    
    // if Digital Receiver chosen, automatically select "Essential" TV package
    if (jQuery('fieldset.receiver :checked').length && jQuery('fieldset.receiver :checked').val() != 'none') {
        jQuery('#digital_cable').attr('checked', true);
    } else {
        jQuery('#digital_cable').attr('checked', false);
    }

    jQuery('input:checked').each(function(){
        if (B.monthly_costs[this.id]) {
            var saving_on_this = B.savings[this.id] || 0;
            monthly_cost    += B.monthly_costs[this.id] - saving_on_this;
            monthly_savings += saving_on_this;
        };
        if (B.one_time_costs[this.id]) {
            var saving_on_this = B.savings_one_time[this.id] || 0;
            // console.log(saving_on_this);
            // console.log(one_time_savings);
            one_time_cost    += B.one_time_costs[this.id] - saving_on_this;
            one_time_savings += saving_on_this;
        };
    }).parents('tr').addClass('selected');

    // Combo deals
    //
    // Movies + Super Movies = savings
    if (jQuery('.super_plus_movies:checked').length == 2 ) {
        monthly_cost    -= B.savings.super_plus_movies; // both costs already calculated
        monthly_savings += B.savings.super_plus_movies;
        jQuery('.super_plus_movies:checked').parents('tr').addClass('bundled');
    } else {
        jQuery('.super_plus_movies').parents('tr').removeClass('bundled');
    }
    // specialty bundle -- Hustler + Red Hot for 29.95
    if (jQuery('.specialty_bundle:checked').length == 2 ) {
        monthly_cost    -= B.savings.specialty_bundle; // both costs already calculated
        monthly_savings += B.savings.specialty_bundle;
        jQuery('.specialty_bundle:checked').parents('tr').addClass('bundled');
    } else {
        jQuery('.specialty_bundle').parents('tr').removeClass('bundled');
    }
    
    var custom = Bundle.customCosting(monthly_cost, monthly_savings);
    monthly_cost    = custom[0];
    monthly_savings = custom[1];

    jQuery('#savings_annual_cost').text('$' + B.centNotation((monthly_savings * B.duration) + one_time_savings) );
    jQuery('#savings_monthly_cost').text('$' + B.centNotation(monthly_savings) + '/mo.' );

    jQuery('#your_cost_cost').html('$' + B.centNotation(monthly_cost) + "/mo." + (one_time_cost ? ('<br /> + $' + one_time_cost/100 + ' one time cost'): ''));

    // console.log(monthly_cost, one_time_cost, monthly_savings);
    jQuery('#monthly_cost').attr('value', monthly_cost);
    jQuery('#one_time_cost').attr('value', one_time_cost);
    jQuery('#monthly_savings').attr('value', monthly_savings);
    jQuery('#rate_2009').attr('value', monthly_cost + monthly_savings);

},

/************************************************
 * Replace this function to account for custom cost structures
 *
 * @param monthly_cost integer
 * @param monthly_savings integer
 * @return array of monthly cost and savings
 */
customCosting: function(monthly_cost, monthly_savings){
    return [monthly_cost, monthly_savings];
},

/************************************************
 * Control which section is visible when.
 */
force_order: function(){
    // return;
    if (jQuery('fieldset.internet :checked').length) {
        jQuery('fieldset.home_phone').fadeIn();
        jQuery('fieldset.receiver').fadeIn();

        var digital_tv = (jQuery('fieldset.receiver :checked').val() != 'none');
        if (!digital_tv) {
            jQuery('fieldset.packages_subscriptions').hide();
        } else if (jQuery('fieldset.receiver :checked').length) {
            jQuery('fieldset.packages_subscriptions').fadeIn();
            jQuery('fieldset.installation').fadeIn();
            jQuery('fieldset.wireless').fadeIn();
            jQuery('fieldset.payperview').fadeIn();
        }
        if ((jQuery('fieldset.receiver :checked').length && digital_tv) || jQuery('fieldset.home_phone :checked').length) {
            jQuery('fieldset.totals').fadeIn();
            jQuery('fieldset.order').fadeIn();
        } else {
            jQuery('fieldset.totals').hide();
            jQuery('fieldset.order').hide();
        }
    } else {
        jQuery('fieldset.home_phone').hide();
        jQuery('fieldset.receiver').hide();
        jQuery('fieldset.packages_subscriptions').hide();
        jQuery('fieldset.payperview').hide();
        jQuery('fieldset.installation').hide();
        jQuery('fieldset.wireless').hide();
        jQuery('fieldset.totals').hide();
        jQuery('fieldset.order').hide();
    }

},

/************************************************
 * Convert cents to dolars
 *
 * @param value integer cost in cents
 * @return value in decimal, to two digits
 */
centNotation: function(value) {
    value = value/100;
    return value.toFixed(2);
},

/************************************************
 * Setup the behaviour specific to the Playboy channel.
 *
 * Ordering Playboy channels requires subscription to the Movie package.
 * Deselecting the Movie package deselects Playboy channel
 */
setup_specialty_channels: function () {
    // 2008-11-07 JT requested not to collapse Specialty channels
    // jQuery("#specialty_table caption").append(' <a class="show">(show)</a> <a class="hide">(hide)</a>').click(function(){
    //     jQuery("#specialty_table tr").toggle();
    //     jQuery(this).toggleClass("revealed")
    // });
    // // only collapse if none selected
    // if (jQuery('#specialty_table :checked').length) {
    //     jQuery('#specialty_table caption').addClass("revealed")
    // } else {
    //     jQuery("#specialty_table tr").hide();
    // };

    // enforce Movie package for certain specialty channels
    var must_have_movie = function(){
        if (jQuery(this).attr('checked')) {
            jQuery('#package_movies').attr('checked', 'checked');
        };
    }; 
    jQuery('#specialty_playboy').change(must_have_movie).click(must_have_movie);
    // if Movies deselected, uncheck dependants
    var no_movie_no_playboy = function(){
        if (!jQuery(this).attr('checked')) {
            jQuery('#specialty_playboy').attr('checked', false);
        };
    }; 
    jQuery('#package_movies').change(no_movie_no_playboy).click(no_movie_no_playboy);
}
} // end of Bundle

// shim jQuery until it's loaded
if (typeof $ === 'undefined') {
    var $ = function(and_then){
        if (typeof jQuery == 'function') {
            $ = jQuery;
            jQuery(and_then);
        } else {
            setTimeout(function(){$(and_then)}, 50);
        }    
    }
};

$(Bundle.init);
;

