try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}

function isPopUp(){
	if( window.opener != null){
		var cWindow = window.location.href.toString();
		if(cWindow.search(/popup=true/) > -1){
			$('body').addClass('popUp').prepend('<a href="javascript:window.close();">Close This Window</a>');
		}
	}
}

function CalcTotals()
{
    setTimeout("CalcTotalsDoIt()", 100);
}


function CalcTotalsDoIt()
{
    var description = jQuery.trim($(".productName").text());
    var selectedTariffRow = $("tr:has(input[name=tariff]:checked)");
    if(selectedTariffRow.length !== 1) return;
            
    var monthlyPrice = parseFloat(jQuery.trim($("td.monthlyPrice", selectedTariffRow).text()).substring(1));
    var devicePrice = parseFloat(jQuery.trim($("span.devicePrice", selectedTariffRow).text()).substring(1));
    description += "<br />with " + jQuery.trim($(".contractName", selectedTariffRow).text());
    //description += " " + jQuery.trim($("td:first", selectedTariffRow).text()) + " Month Term";

    $("tr:has(input[name^=bolton_]:checked)").each(function() {
        monthlyPrice += parseFloat(jQuery.trim($("td.monthlyPrice", this).text()).substring(1));
        if($("input[name^=bolton_][value$=None]", this).length == 0)
        {
        description += "<br />+ " + jQuery.trim($("th:first", this).text());
        }
    });

    $("#totalMonthlyPrice").html("&pound;" + monthlyPrice.toFixed(2));
    $("#totalPhonePrice").html("&pound;" + devicePrice.toFixed(2));
    $("#fullDescription").html(description);
    $("#addToBasket .buyIt").height($("#addToBasket .desc").innerHeight() - 2);
}

$(function () {
    $('#contractTabs').tabs({
        show: function (event, ui) {
            var selectedTariffRowIndex = 0;
            var found = false;
            $("tbody:has(input[name=tariff]:checked) tr:has(input[name=tariff])").each(function () {
                if (!found) selectedTariffRowIndex += 1;
                if ($("input[name=tariff]:checked", this).length == 1) found = true;
            });

            if ($("tr input[name=tariff]", ui.panel).length >= selectedTariffRowIndex) {
                $("tr input[name=tariff]:eq(" + (selectedTariffRowIndex - 1) + ")", ui.panel).click();
            }
            else {
                $("tr input[name=tariff]:first", ui.panel).click();
            }

            CalcTotals();
        }
    });

    $('.contractTypeTitle').hide();

    $("table.clickableRows>tbody>tr")
        .click(
        function (event) {
            document.location.href = jQuery.trim($("a", this).attr("href"));
            return false;
        })
        .hover(
        function (event) {
            $(this).addClass("highlight");
        },
        function (event) {
            $(this).removeClass("highlight");
        });

    $("table.clickableRowSelection tbody tr")
        .click(
        function (event) {
            if (event.target.type !== 'checkbox' && event.target.type !== 'radio') {
                $(':checkbox', this).trigger('click');
                $(':radio', this).trigger('click');
            }
        })
        .hover(
            function (event) {
                $(this).addClass("highlight");
            },
            function (event) {
                $(this).removeClass("highlight");
            }
    );

    $("input[name^=bolton_]").click(function (event) { CalcTotals() });

    $("input[name=tariff]").click(function (event) { CalcTotals() });

    $(".jsOff").removeClass("jsOff");

    $('#slides').slides({
        preload: true,
        play: 5000,
        pause: 2500,
        hoverPause: true
    });
    // Set starting slide to 1
    var startSlide = 1;
    // Get slide number if it exists
    if (window.location.hash) {
        startSlide = window.location.hash.replace('#', '');
    }

    $('#addToBasket').click(function (event) {
        if (event.target.tagName.toLowerCase() != 'input')
            $('input', this).closest('form').submit();
    });

    $('.selectedPrices').removeClass('hidden');
});

