﻿(function(j$) {
    j$(document).ready(function() {
		Cufon.replace('h1, h2, .subnav-title');
		j$('body').randomClass( [ 'bg1', 'bg2', 'bg3', 'bg4' ] );
								
		//Popup window code
        j$('a').each(function() {
            var popupWindowRegex = /\{[\w\d\s:,&;/\."\[\]\-]*\}/g;
            if (j$(this).attr('class').match('popUp')) {
                var paramsJson = j$(this).attr('class').match(popupWindowRegex); 
                j$(this).click(function() {
                    popUp(paramsJson);
                    return false;
                });
            }
        });
        
    });
		// Home Video Animation
		j$('#open-video').click(function() {
			j$('#welcome-video').animate({top: '0px'},600, 'swing');
			j$('#open-video').hide();
			j$('#close-video').show();
		});
		j$('#close-video').click(function() {
			j$('#welcome-video').animate({top: '-233px'},600, 'swing');
			j$('#open-video').show();
			j$('#close-video').hide();
		});

    j$(document).ready(function() {
        /*var payment_type = j$('input[name="ctl00$cphContent$ctl00$service_plans"]');
        var stepButton = null;
        if(payment_type.length != 0) {
            stepButton = j$('div.button-container input');
            payment_type.click(function(e) {
                var $this = j$(this);
                var hidden_val = $this.parent().parent().next().children(':hidden:eq(0)').val();
                j$('#app_option_select').text($this.parent().parent().next().children(':hidden:eq(1)').val());
                //console.log(hidden_val);
                var tax_gst = (5/100) * (parseFloat(hidden_val));
                tax_gst = tax_gst.toFixed(2);
                //console.log(tax_gst);
                var total_val = parseFloat(hidden_val) + parseFloat(tax_gst);
                total_val = total_val.toFixed(2);
                //console.log(total_val);
                /** Calculate the installments for the particular plan **/
                /*var fourMonth = (total_val * 0.24) / (12 * 4);
                var tenMonth = (total_val * 0.24) / (12 * 10);*/
                /*if($this.val() != 'check_standard_service') {
                    var foutMonthInterest = total_val * 0.24 / 12 * 4;
                    var tenMonthInterest = total_val * 0.24 / 12 * 10;
                    var fourMonth = (parseFloat(total_val) + parseFloat(foutMonthInterest)) / 4;
                    var tenMonth = (parseFloat(total_val) + parseFloat(tenMonthInterest)) / 10;
                j$('#4-month').html('$'+fourMonth.toFixed(2)+' per month');
                j$('#10-month').html('$'+tenMonth.toFixed(2)+' per month');
                }
                else {
                    j$('#4-month').html('<br />');
                j$('#10-month').html('<br />');
                }
                
                j$('#p_base_price').text('Service Fee: $'+hidden_val);
                j$('#p_gst').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GST: $'+tax_gst);
                j$('#p_total_price').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Total</b>: $'+total_val);
                
            });
        }*/
        /**
        * Attach the step links to steps
        */
        var stepLinks = j$('div.number-steps ul li a');
        var stepButton = null;
        if(stepLinks.length != 0) {
            stepButton = j$('div.button-container input');
            stepLinks.click(function(e) {
                /*var $this = j$(this);
                if($this.hasClass('step-1')) {
                    j$('form').formwizard('show','#firstStep');
                    stepButton.attr('class','proceed-2');
                }
                else if($this.hasClass('step-2')) {
                    j$('form').formwizard('show','#secondStep');
                    stepButton.attr('class','proceed-3');
                }
                else {
                    j$('form').formwizard('show','#thirdStep');
                    stepButton.attr('class','proceed-end');
                }*/
                return false;
            });
        }
    });
})(jQuery);

/********************************************************
    * HST Calculations
    ********************************************************/
    var interestLabel = null;
    function pardonHst(curDate) {
        var payment_type = $('input[name="ctl00$cphContent$ctl00$service_plans"]');
        var province = $('#ctl00_cphContent_ctl00_DropDownList2');
        var interestRate = null;
        var hidden_val = null;
        var values = null;
        $.data(document.body, 'provinceHST', {
            'New Brunswick': parseFloat(13/100),
            'Nova Scotia': parseFloat(15/100),
            'Newfoundland and Labrador': parseFloat(13/100),
            'British Columbia': parseFloat(12/100),
            'Ontario': parseFloat(13/100),
            'GST': parseFloat(5/100)
        });

        if(payment_type.length == 0)
            return false;   //No such elements found

        //Are we using HST
        if(parseInt(curDate) != -1) {
            interestRate = province.val();
            //Was a province changed
            province.change(function(e) {
                interestRate = $(this).val();
                interestRate = ($.data(document.body, 'provinceHST')[interestRate])?$.data(document.body, 'provinceHST')[interestRate]:$.data(document.body, 'provinceHST').GST;
                values = calcHst(hidden_val, interestRate);
                dispHst(values);
                //console.log(values);
            });
            interestRate = ($.data(document.body, 'provinceHST')[interestRate])?$.data(document.body, 'provinceHST')[interestRate]:$.data(document.body, 'provinceHST').GST;
            interestLabel = "HST";
        }
        else {
            interestRate = $.data(document.body, 'provinceHST').GST;
            interestLabel = "GST";
        }
        //Select payment type values
        payment_type.click(function(e) {
            var radTypes = $(this);
            hidden_val = radTypes.parent().parent().next().children(':hidden:eq(0)').val();
            $('#app_option_select').text(radTypes.parent().parent().next().children(':hidden:eq(1)').val());
            values = calcHst(hidden_val, interestRate);
            dispHst(values);
            //console.log(values);
        });
    }

    function calcHst(cost, interest) {
        var foutMonthInterest=null;
        var tenMonthInterest=null;
        var fourMonth=null;
        var tenMonth=null;

        var tax_gst = (interest) * (parseFloat(cost));
        tax_gst = tax_gst.toFixed(2);

        var total_val = parseFloat(cost) + parseFloat(tax_gst);
        total_val = total_val.toFixed(2);

        foutMonthInterest = total_val * 0.24 / 12 * 4;
        tenMonthInterest = total_val * 0.24 / 12 * 10;
        fourMonth = (parseFloat(total_val) + parseFloat(foutMonthInterest)) / 4;
        tenMonth = (parseFloat(total_val) + parseFloat(tenMonthInterest)) / 10;

        return {
            'tax_gst': tax_gst,
            'total_val': total_val,
            'fourMonth': fourMonth.toFixed(2),
            'tenMonth': tenMonth.toFixed(2),
            'hidden_val': cost
        };
    }

    function dispHst(objVals) {
        var rads = $('input[name="ctl00$cphContent$ctl00$service_plans"]:checked');
        $('#4-month').html('$'+objVals.fourMonth+' per month');
        $('#10-month').html('$'+objVals.tenMonth+' per month');
        $('#p_base_price').text('Service Fee: $'+objVals.hidden_val);
        $('#p_gst').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+interestLabel+': $'+objVals.tax_gst);
        $('#p_total_price').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Total</b>: $'+objVals.total_val);
        if(rads.val() == 'check_standard_service') {
            $('#4-month').html('<br />');
            $('#10-month').html('<br />');
        }
    }
