﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="/inc/jquery/jquery-1.4.2.js"/>

    var showCartConfirm = true;

    $(document).ready(function () {
        // Popup/entire-page elements normally moved on Page_Init, but they don't get translated because of page lifecycle (they get moved before master.vb can TranslateControls())
        // Move Order Message elements on the client side so text gets translated first by codebehind.
        $("#idMask").prependTo("body");
        $("#idOrderMessage").prependTo("body");
    });

    function changeImage(id,shtml) {
       if (document.getElementById || document.all) {
          var el = document.getElementById? document.getElementById(id): document.all[id];
          if (el && typeof el.innerHTML != "undefined") el.innerHTML = shtml;
       }
    }	

    var sMoreProdModels = 'idMoreProductModels';
    var sShowHideText = 'idShowHideText';
    
    function toggleProdModels(){
        $get(sMoreProdModels).style.display=="none"? showProdModels() : hideProdModels();
    }
    
    function showProdModels(){
        $get(sMoreProdModels).style.display = "";
        $get(sShowHideText).innerHTML = showLessText + " <img src='/img/i_red-arrow-up.gif' border='0'/>";
    }
    
    function hideProdModels(){
        $get(sMoreProdModels).style.display = "none";
        $get(sShowHideText).innerHTML = showAllText + " <img src='/img/i_red-arrow-down.gif' border='0'/>";
    }


    function scrollToAnchor(anchorName) {
        var forcedScrolling = false;
        // IE versions prior to 8 did not allow location.hash updates, so for any IE less than 8, we force the scroll.
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
            var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
            if (ieversion < 8) { forcedScrolling = true; };
        }
        if (forcedScrolling) {
          window.scrollTo(0,findPosY($get(anchorName)));
        } else  {
          location.hash = anchorName;
        }
    }

    function findPosY(obj)
    {
        var curtop = 0;
        if (obj != null)
        {
            if (obj.offsetParent)
            {
                while (obj.offsetParent)
                {
                    curtop += obj.offsetTop;
                    obj = obj.offsetParent;
                }
                curtop += obj.offsetTop;
            }
            else if (obj.y)
                curtop += obj.y;
        }
        return curtop;
    }

    
//## Begin Region: AJAX Order Button Functions
    
    function orderPart(whichButton,whichPartNum,whichImgSrc) {
    
        //Disable button so it can't be clicked twice
        $('#'+whichButton).attr("disabled",true);
        
/*
        //Clone button to make a proxy for fly-to-cart Motion
        var startloc = $('#' + whichButton).offset();
        var endloc = $('#idHeaderCart').offset();
        var proxyButton = $('#' + whichButton).clone();      
        
        // Hide the original button
        $('#' + whichButton).css("visibility","hidden");

        // Give cloned button a new id, add it to the body, set the start position.
        $(proxyButton).attr('id', 'idOrderShape').appendTo('body').css({width: 50, "position": "absolute", "top": startloc.top, "left": startloc.left });

        // Animate the button, call quietBasket
        $(proxyButton).animate({ "top": endloc.top, "left": endloc.left}, 300, "swing",
            function () {
                quietBasket(whichButton, whichPartNum, whichImgSrc);
                $(proxyButton).remove();
                //shake cart
                $('#idHeaderCart > img').effect('bounce', { direction: "up", distance: 3, times: 2 }, 300);
            }
        );
*/
        quietBasket(whichButton, whichPartNum, whichImgSrc);

    }

    function quietBasket(whichButton, whichPartNum, whichImgSrc) {
        $.ajax({
            type: "GET",
            url: "/dealeronly/quietbasket.aspx?partlist=" + whichPartNum + "&qtylist=1",
            dataType: "text",
            success: function (data) {
                if (data.search("message value='Items added'") > -1) { orderSuccess(whichButton, whichPartNum, whichImgSrc); }
                else { alert('Your session has timed out. Please log back in before adding items to your cart.'); window.location.reload(true); } 
                },
            error: function () { $('#' + whichButton).css("visibility", "visible").attr("disabled", false); alert('An error occurred. Please verify your cart contents.'); },
            timeout: 10000
        });
        
    }
    
    function orderSuccess (whichButton,whichPartNum,whichImgSrc) {
        
        //Re-Enable clicked button
        $('#' + whichButton).css("visibility", "visible").attr("disabled",false);

        //Don't continue with the confirmation dialog if showCartConfirm is disabled
        if (!showCartConfirm) { return false };

        //Set Order Message Content
        $('#idOrderMessagePartNum').html(whichPartNum);
        $('#idOrderMessageImg').attr("src", (whichImgSrc == '') ? mainImage : whichImgSrc); // mainImage value set via codebehind.
        $('#idOrderMessageNm').html($('#idModelNm' + whichPartNum).html());
        $('#idOrderMessageVr').html($('#idModelVr' + whichPartNum).html());

        //Check for Universal AC Outlet
        $.getJSON("/product/prodsettings.ashx?t=UniversalACOutlet&f=partnumber&rv=" + whichPartNum, function (result) {
            if (result == true) {
                $("#idUAOutletMsg").show();
            } else {
                $("#idUAOutletMsg").hide();
            }
        });

        //Enable Mask
        $('#idMask').fadeIn(200);
        $('#idMask').height($(document).height());

        //Fade in order msg
        $('#idOrderMessage').dialog({ width: 500, closeText: "", draggable: false, position: "top", resizable: false }).css("margin-top",200);
    }

    function hideMsg() {
        $('#idOrderMessage').dialog("close");
        $('#idMask').fadeOut(200);
    }

    
//## End Region: AJAX Order Button Functions


//## Begin Region: SWF Popup Functions

    function swfPopup() {
        $('#idSwfPopup').dialog({ width: 1010, closeText: "", draggable: false,  position: "top", resizable: false});
        $('#idSwfMask').fadeIn(200);
        $('#idSwfMask').height($(document).height());
    }

    function hideSwfBox() {
        $('#idSwfPopup').dialog("close");
        $('#idSwfMask').fadeOut(200);
    }

//## End Region: SWF Popup Functions

//## Begin Region: Video Player Functions

    function PlayVideo(serverUrl, serverClip, videoId, caller, anchorTag) {
        extronVideo("idVideoPlayer", videoId, true, false, false, "idVideoBookmarks", "Chapter List", "0", null, "idVideoDesc");
        $(".prodvid").removeClass("prodvidactive");
        $(caller).addClass("prodvidactive");
        //$(window).scrollTop($("#idVideoBlock").offset().top - 40);
    }

    function noVidTn(el) {
        el.src = "/img/movie_reel.png";
        el.style.width = 80;
        el.style.height = 46;
        el.onerror = "";
        return true;
    }

    function noVidSplash(el) {
        el.src = "/product/img/home/video_tab.jpg";
        el.onerror = "";
        return true;
    }

    function ShowFirstVideo() {
        var vidSplash = $("#idVideoSplash");
        var firstVid = $(".prodvid:first");
        vidSplash.attr("src", firstVid.attr("data-thumbnail").replace(".png", "-lg.jpg"));
        vidSplash.bind("click", function () { firstVid.click(); $(this).unbind("click"); });
        vidSplash.css("cursor", "pointer");
        firstVid.addClass("prodvidactive");
    }

//## End Region: Video Player Functions
