$(function() {
    // Set LivePerson variables when the page loads and attach onclick events
    lpCalculateCost();
    lpAddVars('page', 'Section', 'ProductSelection');
    lpAddVars('page', 'ConversionStage', 'CreateCombination');
    lpAddVars('page', 'ConversionStageNumber', '1'); //[1] [2] [3] [4] [5]
    lpAddVars('page', 'JourneyType', 'AKQA');
    //	if (typeof (lpMTagConfig.dynButton) != 'undefined') {
    //		lpMTagConfig.dynButton[lpMTagConfig.dynButton.length] = { 'name': 'chat-productselection-' + lpUnit + '-' + lpLanguage, 'pid': 'productselectionDiv' };
    //	}

    $("div#skyTVPacks fieldset input").live("click", function() {
        lp_updateEntsPackCartDetails();
    });

    $("div#skyTVPacks div#skyMoviesCont table input").live("click", function() {
        lp_updateMoviesPackCartDetails(this);
    });

    $("div#skyTVPacks div#skySportsCont table input").live("click", function() {
        lp_updateSportsPackCartDetails(this);
    });

    $("div#skyTVPacks div#espnCont table input").live("click", function() {
        lp_updateEspnCartDetails(this);
    });

    $("div#skyTVEquipment input").live("click", function() {
        resetConversionAction();
        lp_updateEquipmentCartDetails(this);
    });

    $("div#skyBroadband input").live("click", function() {
        resetConversionAction();
        lp_updateBroadbandCartDetails(this);
    });

    $("div#skyTalk input:radio").live("click", function() {
        resetConversionAction();
        lp_updateTalkCartDetails(this);
    });

    $("div#skyTalk input:checkbox").live("click", function() {
        resetConversionAction();
        lp_updateLineRentalCartDetails(this);
    });

    // Add hover states to form submit images
    $('div.cta input').live("mouseover", function() {
        $(this).attr('src', '/library/images/buy/btn_confirm_and_buy_on.png');
    });
    $('div.cta input').live("mouseout", function() {
        $(this).attr('src', '/library/images/buy/btn_confirm_and_buy_off.png');
    });

    // Animate the jump from 'Add...' buttons to relevant content
    $("div.costs a").live("click", function() {
        e = $(this).attr("href").split("#")[1];
        ePos = $("div#" + e).position().top;
        $("html, body").animate({ scrollTop: ePos }, 750, "swing");
        return false;
    });

    // Animate the jump from 'Back to top' links to the top of the page.
    // Also animate the basket summary back to it original position.
    $("div.linkCont a.backToTop").live("click", function() { //div.contentPod 
        ePos = $("div#header").position().top;
        $("html, body").animate({ scrollTop: ePos }, 750, "swing");
        $("div#basketSummary").animate({ "top": 0 }, 750, "swing");
        return false;
    });

    // Animate the basket summary down to the top of the current page position
    $("div.contentPod div.linkCont a.showBasket").live("click", function() {
        // Set varibles: 
        // pagePos			The current scrolled-to point of the page
        // basketPos		The start position of the basket when the page loads.
        //					This is calculated using the element directly above in the DOM, to 
        //					accomodate changes in text size and LivePerson images.
        // basketHeight		The height of the basket (not including ad pods)
        // basketEndPos		The position where the basket will end up
        // boundryPos		The max point where the basket should animate to
        // boundryOver		The value of how far the basket will animate past boundryPos
        var pagePos = $("html").scrollTop() == 0 ? $("body").scrollTop() : $("html").scrollTop();
        var basketPos = $("div#shoppingChecklist").offset().top + $("div#shoppingChecklist").outerHeight(true);
        var basketHeight = $("div#basket").outerHeight(true);
        var basketEndPos = pagePos - basketPos;
        var boundryPos = $("div#termsContainer").offset().top;
        var boundryOver;

        if ((pagePos + basketHeight) > boundryPos) {
            boundryOver = Math.abs(boundryPos - (pagePos + basketHeight));
            basketEndPos = basketEndPos - boundryOver;
            $("div#basketSummary").animate({ "top": basketEndPos }, 750, "swing");
        } else {
            boundryOver = 0;
            $("div#basketSummary").animate({ "top": pagePos - basketPos }, 750, "swing");
        }
        return false;
    });

    $("form#aspnetForm").submit(function(event) {
        $("div#basketSummary").animate({ "top": 0 }, 750, "swing");
    });

    $("tr.rowWholesaleLineRental th a").live("click", function() {
        $("td.callFeatures ul").toggleClass("expanded");

        // Toggle the state of both classes
        var activeRow = $("tr.rowWholesaleLineRental th a");

        if (activeRow.hasClass("hideCallFeatures")) {
            activeRow.removeClass("hideCallFeatures");
            activeRow.addClass("viewOrAddCallFeatures")
        } else {
            activeRow.removeClass("viewOrAddCallFeatures");
            activeRow.addClass("hideCallFeatures")
        }
        return false;
    });


    if ($("input[id$='chkWholesaleLineRental']:checked").length > 0) {
        $("td.callFeatures ul").addClass("expanded");
    }

    // Add .net AJAX request handlers
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});

/***************************************************
	function addButtonClicks ()
	--------------------------------------------
	Note: Requires the JQuery library.
***************************************************/
function addButtonClicks(){    

    // Change existing customer pod background if selected
    $('#ctl00_MC_ctl00_rdbExistingCustomer').live("click", function() {
        $('#existingCusomter .inner').addClass('existingCustomer');   
    });
    
    $('#ctl00_MC_ctl00_rdbNewCustomer').live("click", function() {
        $('#existingCusomter .inner').removeClass('existingCustomer');   
    });    
    
    if ($('#ctl00_MC_ctl00_rdbExistingCustomer').is(':checked')) 
    {
        $('#existingCusomter .inner').addClass('existingCustomer');  
    }
    
    // Position the entertainment mix information box to its associated link
    $('div#skyTVPacks a.helpMeChoose:eq(0)').live("click", function() {
        link = $(this);
        renderPopup (25, 288, 'div#entsMixInfo div.content div.content', '/buy/entertainment-mixes.aspx div.container', '/library/css/buy/entertainment-mixes.css');
        return false;
    });
    
   // Position the Movies Pack information box to its associated link
    $('div#skyTVPacks div#skyMoviesCont a.helpMeChoose').live("click", function() {
        link = $(this);
        renderPopup (185, 180, 'div#movieMixInfo div.content div.content', '/buy/premium-mixes.aspx div#addMoviesInfo table', '/library/css/buy/premium-mixes.css');        
        return false;
    });

    // Position the Sports Pack information box to its associated link
    $('div#skyTVPacks div#skySportsCont a.helpMeChoose').live("click", function() {
        link = $(this);
        renderPopup(185, -130, 'div#sportsMixInfo div.content div.content', '/buy/premium-mixes.aspx div#addSportsInfo table', '/library/css/buy/premium-mixes.css');
        return false;
    });
    
    // Position the Sky box information box to its associated link
    $('div#skyTVEquipment tr.rowSkyBox a.info').live("click", function() {
        link = $(this);
        renderPopup (0, -55, 'div#skyBoxInfo div.content div.content', '/buy/equipment.aspx div.cont:eq(0)', '/library/css/buy/equipment.css');
        return false;
    });
    
    // Position the Sky+ box information box to its associated link
    $('div#skyTVEquipment tr.rowSkyPlusBox a.info').live("click", function() {
        link = $(this);
        renderPopup (0, -55, 'div#skyPlusInfo div.content div.content', '/buy/equipment.aspx div.cont:eq(1)', '/library/css/buy/equipment.css');
        return false;
    });
    
    // Position the Sky+HD box information box popup to its associated link
    $('div#skyTVEquipment tr.rowSkyPlusHDBox a.info').live("click", function() {
        link = $(this);
        renderPopup (0, -55, 'div#skyHDInfo div.content div.content', '/buy/equipment.aspx div.cont:eq(2)', '/library/css/buy/equipment.css');
        return false;
    });
    
    // Position the Sky Broadband information box to its associated link
    $('div#skyBroadband a.helpMeChoose').live("click", function() {
        link = $(this);
        renderPopup (25, 533, 'div#broadbandInfo div.content div.content', '');
        return false;
    });
    
    // Position the Sky Talk information box to its associated link
    $('div#skyTalk a.helpMeChoose').live("click", function() {
        link = $(this);
        renderPopup (25, 533, 'div#talkInfo div.content div.content', '');
        return false;
    });
    
    // Position the Sky Talk information box to its associated link (secondary link inside the call features options)
    $('div#skyTalk li.moreInformation a').live("click", function() {
        link = $(this);
        renderPopup (25, 533, 'div#talkInfo div.content div.content', '');
        return false;
    });
    
    // Position the Sports Pack information box to its associated link
    $('div#skyTVPacks p#priceChanges a.priceTable').live("click", function() {
        link = $(this);
        renderPopup (20, 0, 'div#priceChangeInfo div.content div.content', '/buy/premium-mixes.aspx div#PricesInfo table', '/library/css/buy/premium-mixes.css');        
        return false;
    });       
}

/***************************************************
	function EndRequestHandler ()
	Code included in here will run after every AJAX
	response is recevied. E.G after and update panel
	refresh.
	--------------------------------------------
	Note: For use with the .net AJAX framework
***************************************************/
function EndRequestHandler(sender, args){
    if (args.get_error() != undefined && args.get_error().httpStatusCode == '500'){
        var errorMessage = args.get_error().message
        args.set_errorHandled(true);
       }
    lpCalculateCost();
    renderScroller();
    if ($("input[id$='chkWholesaleLineRental']:checked").length > 0) {
    	$("td.callFeatures ul").addClass("expanded");
    }    
}

/***************************************************
	function ExistingCustomerValidator_ClientValidate ()
	--------------------------------------------
	Note: For use with the .net framework
***************************************************/
function ExistingCustomerValidator_ClientValidate(sender, args){
	if ($('div#existingCusomter input:checked').length > 0){
		args.IsValid = true;
	} else {
	    args.IsValid = false;
		$('div.errorSummary').show();
		errorList.push('SkyCustomer');

		//-- site catalyst form tracking -- start
		s.sendFormEvent('e', s.pageName, 'COMBO_PROCEED_CHECKOUT', 'EXISTING_CUSTOMER_Q:EMPTY');
		//-- end site catalyst form tracking 
		
	}
}

/***************************************************
	function MixCountValidator_ClientValidate ()
	--------------------------------------------
	Note: For use with the .net framework
***************************************************/
function MixCountValidator_ClientValidate(sender, args){
	var noOfMixes = $('div#skyTVPacks fieldset:first input:checked').length
	if (noOfMixes > 0){
		args.IsValid = true;
	} else {
		args.IsValid = false;
		$('div.errorSummary').show();
		errorList.push('select' + noOfMixes+ 'Mixes');
	}
    lpErrorNameSet();
}