$(document).ready(function () {

    // soToolTip JS

    $('.showsoToolTip').each(function () {
        var linkHasFocus = false;

        $('.specialOfferToolTip').hover(function () {
            $('.soToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.soToolTip').hide();
            }
        });

    });

    // fiToolTip JS

    $('.showfiToolTip').each(function () {
        var linkHasFocus = false;
        $('.filterInfoToolTip').hover(function () {
            $('.fiToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.fiToolTip').hide();
            }
        });

    });

    // sdToolTip JS

    $('.showsdToolTip').each(function () {
        var linkHasFocus = false;
        $('.showDealToolTip').hover(function () {
            $('.sdToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.sdToolTip').hide();
            }
        });

    });

    // ctToolTip JS

    $('.showctToolTip').each(function () {
        var linkHasFocus = false;

        $('.cappedTariffToolTip').hover(function () {
            $('.ctToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.ctToolTip').hide();
            }
        });

    });

    // ctToolTip JS

    $('.showctProdToolTip').each(function () {
        var linkHasFocus = false;

        $('.cappedTariffProdToolTip').hover(function () {
            $('.ctProdToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.ctProdToolTip').hide();
            }
        });

    });

    /* homeCarousel */
    if ($('#homeCarousel #homeCarouselFrames').length != '0') {
        $('#homeCarousel #homeCarouselFrames').cycle({
            fx: 'scrollHorz',
            timeout: 6000,
            speed: 500,
            pause: true,
            // next:'#homepageCarouselNext',
            // prev:'#homepageCarouselPrev',
            pager: '.frameControl',
            pagerAnchorBuilder: function (idx, slide) {
                // return selector string for existing anchor 
                return '.frameControl a:eq(' + idx + ')';
            }
        });
    }

    $('.nojs').removeClass('nojs');


    /* homeProductPanel show/hide */

    $(".homeProdModule .slideContainer").hide();

    $(".homeProdModule").hover(function () {
        $(this).children(".slideContainer").slideToggle("slow");
    });

    /* deal panel height */

    $('.dealRow').each(function () {
        var $biggerPanel = 0;
        $('.dealDetails', this).each(function () {
            if ($(this).height() > $biggerPanel) {
                $biggerPanel = $(this).height();
            }
        });
        $('.dealDetails', this).height($biggerPanel);
    });

    $('.dealRow').each(function () {
        var $biggerPanel = 0;
        $('.dealPanelContent', this).each(function () {
            if ($(this).height() > $biggerPanel) {
                $biggerPanel = $(this).height();
            }
        });
        $('.dealPanelContent', this).height($biggerPanel);
    });

    /* productCarousel */

    $(document).ready(function () {
        $('#productCarousel .noJS').removeClass("noJS");
        if ($('#productCarousel .carouselFrames').length != '0') {
            $('#productCarousel .carouselFrames').cycle({
                fx: 'scrollHorz',
                timeout: 6000,
                speed: 500,
                pause: true,
                next: '#productCarousel .next',
                prev: '#productCarousel .prev',
                cleartypeNoBg: true,
                pager: '#productCarousel .carouselPaging',
                pagerAnchorBuilder: function (idx, slide) {
                    // return selector string for existing anchor 
                    return '#productCarousel .carouselPaging li:eq(' + idx + ') a';
                }
            });
        }
    });

    /* accordion container */

    $(".accordionContainer").addClass('js');

    $('.accordionContainer h2 a').click(function () {
        if ($(this).hasClass('open')) {
            $(this).removeClass('open').addClass('close');
            $(this).addClass('clickOpen');
            setTimeout(
                function () {
                    blurAccordion();
                }, 200);
        }
        else {
            $(this).removeClass('close').addClass('open');
            $(this).addClass('clickClose');
            setTimeout(
                function () {
                    blurAccordion();
                }, 200);
        }
        $(this).closest('.accordionContainer').find('.accordionContent').slideToggle();
        return false;
    }
    );

    $('.accordionContainer').each(function () {
        $(this).find('h3 a').removeClass('close').addClass('open');
        $(this).find('.accordionInnerContent').hide();
    }
    );

    $('.accordionContainer h3 a').click(function () {
        if ($(this).hasClass('open')) {
            $(this).removeClass('open').addClass('close');
            $(this).addClass('clickOpen');
            setTimeout(
                function () {
                    blurAccordion();
                }, 200);
        }
        else {
            $(this).removeClass('close').addClass('open');
            $(this).addClass('clickClose');
            setTimeout(
                function () {
                    blurAccordion();
                }, 200);
        }
        $(this).closest('h3').next('.accordionInnerContent').slideToggle();
        return false;
    });

});

function blurAccordion() {
    $('.accordionContainer h2 a').removeClass('clickOpen').removeClass('clickClose');
    $('.accordionContainer h3 a').removeClass('clickOpen').removeClass('clickClose');
}


function RadioButtonClicked(cost, totalCostSpanClientId, hfRadioButtonSelection) {

    var totalCostSpan = $('#' + totalCostSpanClientId);
    var totalCost = parseFloat(totalCostSpan.text());

    totalCost += parseFloat(cost);
    totalCost -= parseFloat($('#' + hfRadioButtonSelection).val());
    totalCostSpan.text(totalCost.toFixed(2));
    $('#' + hfRadioButtonSelection).val(cost.toString());
}


function CheckBoxClicked(checkBoxId, cost, totalCostSpanClientId, checkBoxGroupId, minAmount, MaxAmount, reset, errorId) {

    var txtError = $('#' + errorId);
    txtError.text(' ');

    var chkBoxList = document.getElementById(checkBoxGroupId);
    var chkBoxCount = chkBoxList.getElementsByTagName("input");

    var $infoId = '';


    //when none option is selected
    if (reset == 'true') {

        //get total selected cost
        $infoId = '#' + checkBoxGroupId + ' input[type=checkbox]:checked';
        var values = 0;
        $($infoId).each(function () {

            var currentValue = $(this).parent().attr('Charge');
            if (currentValue !== '')
                values += parseFloat(currentValue);
        });

        //substract total selected cost from display cost
        var totalCostSpan = $('#' + totalCostSpanClientId);
        var totalCost = parseFloat(totalCostSpan.text());


        totalCost -= parseFloat(values);
        totalCostSpan.text(totalCost.toFixed(2));

        //deselect all the options and select none option
        for (var i = 0; i < chkBoxCount.length; i++) {
            if (i === 0) {
                chkBoxCount[i].checked = true;
            }
            else {

                chkBoxCount[i].checked = false;
            }
        }

        txtError = $('#' + errorId);
        txtError.text(' ');
    }
    //when none option is not selected
    else {

        var error = false;
        // deselect none when min==0
        if (minAmount === 0) {
            chkBoxCount[0].checked = false;
        }

        var totalCount = 0;
        for (var i = 0; i < chkBoxCount.length; i++) {
            if (chkBoxCount[i].checked) {
                totalCount = totalCount + 1;
            }

        }

        if (totalCount < minAmount) {

            var txtError = $('#' + errorId);
            txtError.text('You must select at least ' + minAmount);

            //allow user to deselect option when mincount==totalcout
            if (!(minAmount - 1 == totalCount)) {
                var chkBoxId = document.getElementById(checkBoxId);
                chkBoxId.checked = true;
                error = true;
            }

        }

        //a deselect option when count is greater than max
        if (totalCount > MaxAmount) {
            var txtError = $('#' + errorId);
            txtError.text('The maximum number of items you can select is ' + MaxAmount);
            var chkBoxId = document.getElementById(checkBoxId);
            chkBoxId.checked = false;
            error = true;
        }

        //when min==0 and nothing selected select none option
        if (minAmount == 0 && totalCount == 0) {
            chkBoxCount[0].checked = true;
        }


        if (!error) {
            error = false;
            var cbx = $('#' + checkBoxId);
            var totalCostSpan = $('#' + totalCostSpanClientId);
            var totalCost = parseFloat(totalCostSpan.text());

            if (cbx.is(':checked')) {
                totalCost += parseFloat(cost);
            }
            else {
                totalCost -= parseFloat(cost);
            }
            totalCostSpan.text(totalCost.toFixed(2));

        }
    }

}

function ExecuteOnPostBack() {

    var txtjavaScript = $('#hidJavascriptEnable');
    txtjavaScript.val('javascript enabled');

    $('.lightBox11 .closeButton, #overlay a').click(function () {
        $('.lightBox11, #overlay1').hide();
        $('.overlay').hide();
        return false;
    });

    $('.continueShop').click(function () {
        $('.lightBox8, #overlay1').hide();
        $('.overlay').hide();
        return false;
    });

    $('.lightBox4 .closeButton, #overlay a').click(function () {
        $('.lightBox4, #overlay1').hide();
        $('.lightBox6, #overlay').hide();
        $('.overlay').hide();
        return false;
    });

    $('.dealRow').each(function () {
        var $biggerPanel = 0;
        $('.dealDetails', this).each(function () {
            if ($(this).height() > $biggerPanel) {
                $biggerPanel = $(this).height()
            }
        });
        $('.dealDetails', this).height($biggerPanel);
    });


    $('.showsdToolTip').each(function () {

        $(this).attr("span", "javascript: void(0)");

        var linkHasFocus = false;
        $('.showDealToolTip').hover(function () {
            $('.sdToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.sdToolTip').hide();
            }
        });

    });

    // ctToolTip JS

    $('.showctToolTip').each(function () {
        var linkHasFocus = false;

        $('.cappedTariffToolTip').hover(function () {
            $('.ctToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.ctToolTip').hide();
            }
        });

    });
    // ctToolTip JS

    $('.showctProdToolTip').each(function () {
        var linkHasFocus = false;

        $('.cappedTariffProdToolTip').hover(function () {
            $('.ctProdToolTip', this).show();
        },
        function () {
            if (!linkHasFocus) {
                $('.ctProdToolTip').hide();
            }
        });

    });

    //To Set Bottom Paging control
    alter_pag_margin();

}

function alter_pag_margin() {
    var simsTableContainerHeight = $('.simsTableContainer').outerHeight(true);
    var newFilterContainerContentHeight = $('.newFilterContainerContent').outerHeight(true);

    if (simsTableContainerHeight > newFilterContainerContentHeight) {
        var newHeight = ((simsTableContainerHeight - newFilterContainerContentHeight) - $('.pagingContainerSimOBottom').height()) + 5;
    } else {
        var newHeight = 5;
    }

    if ($.browser.msie && $.browser.version.substr(0, 1) == '6') {
        $('.pagingContainerSimOBottom').css({ 'position': 'absolute', 'left': '0', 'margin-top': (newHeight - 25) + 'px', 'display': 'block' });
        $('.simsTableContainer').css({ 'margin-bottom': '65px' });
    } else {
        $('.pagingContainerSimOBottom').css({ 'margin-top': newHeight + 'px', 'display': 'block' });
    }
}

