
function UseAlternativeBestSellersOverlay()
{
    return (document.getElementById('bestSellDetBoxB') ? true : false);
}

function DisplayBestSellerRank(rank)
{
    var useAltOverlay = UseAlternativeBestSellersOverlay();
    var bestSellDetBoxMain = document.getElementById(useAltOverlay ? 'bestSellDetBoxMain' : 'bestSellDetBox');
    var bestSellDetBox     = document.getElementById(useAltOverlay ? 'bestSellDetBoxB'    : 'bestSellDetBox');

    if( bestSellDetBox && bestSellDetBoxMain )
    {
        bestSellDetBox.innerHTML = GetBestSellerDivHTML(rank);
        bestSellDetBoxMain.style.visibility = "visible";

        if( document.getElementById('rotator') )
        {
            HideRotator();
        }
    }
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

function DelayedRestoreBestSellerRank(event)
{
    var bestPromos = document.getElementById('bestPromos');

    if( bestPromos )
    {
        offsetXY = getScrollXY();

        var mouseX = (event.pageX ? event.pageX : (window.event.clientX + offsetXY[0]));
        var mouseY = (event.pageY ? event.pageY : (window.event.clientY + offsetXY[1]));

        if( !IsInsideElement(bestPromos, mouseX, mouseY) )
            RestoreBestSellerRank(); // no delay
    }
}

function RestoreBestSellerRank()
{
    var useAltOverlay = UseAlternativeBestSellersOverlay();
    var bestSellDetBoxMain = document.getElementById(useAltOverlay ? 'bestSellDetBoxMain' : 'bestSellDetBox');

    if( bestSellDetBoxMain )
    {
        bestSellDetBoxMain.style.visibility = "hidden";

        if( document.getElementById('rotator') )
        {
            ShowRotator();
        }
    }
}

function IsInsideElement(obj, x, y)
{
    oTop = GetElementAbsoluteTop(obj);
    oLeft = GetElementAbsoluteLeft(obj);
    oBottom = oTop + obj.offsetHeight;
    oRight = oLeft + obj.offsetWidth;
    return (x > oLeft && x < oRight && y > oTop && y < oBottom);
}

function GetElementAbsoluteLeft(o)
{
    // Get an object left position from the upper left viewport corner
    oLeft = o.offsetLeft;            // Get left position from the parent object
    while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
        oParent = o.offsetParent;    // Get parent object reference
        oLeft += oParent.offsetLeft; // Add parent left position
        o = oParent;
    }
    return oLeft;     // Return left postion
}

function GetElementAbsoluteTop(o)
{
    // Get an object top position from the upper left viewport corner
    oTop = o.offsetTop;            // Get top position from the parent object
    while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
        oParent = o.offsetParent;  // Get parent object reference
        oTop += oParent.offsetTop; // Add parent top position
        o = oParent;
    }
    return oTop; // Return top position
}

function GetRatingBoxString(type, rate)
{
    return '\
        <span class="rateBox">\
            <img src="/img/newFrontEnd/rateBar_bg.gif" width="'+ (rate * 75 / 5.0)  +'" height="15" />\
            <img src="/img/newFrontEnd/rateStars.gif" alt="'+type+' Rating "'+ rate +'/5" title="'+type+' Rating "'+ rate+'/5" />\
        </span>';
}


function GetBestSellerDivHTML(rank)
{
    var bsinfo = new Array();
    switch(rank)
    {
    case 1:
        bsinfo['prId'] = '36853';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1705';
        bsinfo['name'] = 'Motorola Backflip';
        bsinfo['prFriendlyURL'] = 'motorola-backflip-with-motoblur';
        bsinfo['reviewFriendlyURL'] = '/motorola-backflip-with-motoblur-user-reviews';
        bsinfo['desc'] = 'The Motorola Backflip for AT&T isn\'t your average Android smartphone. Equipped with MotoBlur, your Facebook updates, Twitter ... ';
        bsinfo['bulletPoints'] = '<li>Talk & browse the web at the same time                                <li>Rollover unused minutes                                ';
        bsinfo['carrier_corpId'] = '596';
        bsinfo['carrierName'] = 'AT&T';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm596.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/att/motorola/motorola-backflip-with-motoblur_hbs.gif';
        bsinfo['user_rate'] = '3.5';
        bsinfo['expert_rate'] = '3.5';
        bsinfo['price'] = 'FREE';
        bsinfo['prRetailPrice'] = '$449.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$449.99</strike></p>\
                <p class="red"><strong>Your Price: FREE</strong></p>\
            ';
    break;
    case 2:
        bsinfo['prId'] = '35982';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1671';
        bsinfo['name'] = 'BlackBerry Bold 9700';
        bsinfo['prFriendlyURL'] = 'blackberry-bold-9700-t-mobile';
        bsinfo['reviewFriendlyURL'] = '/blackberry-bold-9700-user-reviews';
        bsinfo['desc'] = 'The Blackberry Bold 9700 for T-Mobile is an all powerful and stylish cell phone that will keep you connected everywhere you ... ';
        bsinfo['bulletPoints'] = '<li>3.2 Megapixel Camera with flash                                          <li>32gb expandable MicroSD memory                                               ';
        bsinfo['carrier_corpId'] = '543';
        bsinfo['carrierName'] = 'T-Mobile';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm543.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/t-mobile/blackberry/blackberry-bold-9700-t-mobile_hbs.gif';
        bsinfo['user_rate'] = '4.54';
        bsinfo['expert_rate'] = '4.3';
        bsinfo['price'] = '$49.99';
        bsinfo['prRetailPrice'] = '$529.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$529.99</strike></p>\
                <p class="red"><strong>Your Price: $49.99</strong></p>\
            ';
    break;
    case 3:
        bsinfo['prId'] = '35893';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1670';
        bsinfo['name'] = 'BlackBerry Storm2 9550';
        bsinfo['prFriendlyURL'] = 'blackberry-storm2-verizon-wireless';
        bsinfo['reviewFriendlyURL'] = '/storm2-user-reviews';
        bsinfo['desc'] = 'Stay connected to the people, places and things that matter most with the new BlackBerry Storm2 9550 smartphone from Verizon ... ';
        bsinfo['bulletPoints'] = '<li>Wi-Fi Capable! (802.11 b/g)                                  <li>3.25 inches SurePress Touchscreen with Accelerometer                                                                  ';
        bsinfo['carrier_corpId'] = '660';
        bsinfo['carrierName'] = 'Verizon Wireless';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm660.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/verizonwireless/blackberry/blackberry-storm2-verizon-wireless_hbs.gif';
        bsinfo['user_rate'] = '4.42';
        bsinfo['expert_rate'] = '3.8';
        bsinfo['price'] = 'FREE';
        bsinfo['prRetailPrice'] = '$499.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$499.99</strike></p>\
                <p class="red"><strong>Your Price: FREE</strong></p>\
            ';
    break;
    case 4:
        bsinfo['prId'] = '36583';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1681';
        bsinfo['name'] = 'BlackBerry Curve 8530 - Black';
        bsinfo['prFriendlyURL'] = 'blackberry-curve-8530-sprint';
        bsinfo['reviewFriendlyURL'] = '/blackberry-curve-8530-user-reviews';
        bsinfo['desc'] = 'The BlackBerry® Curve™ 8530 offers an exceptional glossy finish, smooth back, and slimmer size. Grab your 3.5mm headset, the ... ';
        bsinfo['bulletPoints'] = '<li>Wi-Fi and Stereo Bluetooth Capable                                                          <li>2.0 Megapixel Camera                                                                                                                              <li><strong>More Colors Available</strong>';
        bsinfo['carrier_corpId'] = '545';
        bsinfo['carrierName'] = 'Sprint';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm545.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/sprint/blackberry/blackberry-curve-8530-sprint_hbs.gif';
        bsinfo['user_rate'] = '4.11';
        bsinfo['expert_rate'] = '4';
        bsinfo['price'] = 'FREE';
        bsinfo['prRetailPrice'] = '$379.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$379.99</strike></p>\
                <p class="red"><strong>Your Price: FREE</strong></p>\
            ';
    break;
    case 5:
        bsinfo['prId'] = '35946';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1671';
        bsinfo['name'] = 'BlackBerry Bold 9700';
        bsinfo['prFriendlyURL'] = 'blackberry-bold-9700-att';
        bsinfo['reviewFriendlyURL'] = '/blackberry-bold-9700-user-reviews';
        bsinfo['desc'] = 'The Blackberry Bold 9700 for AT&T is an all powerful and stylish cell phone that will keep you connected everywhere you go. ... ';
        bsinfo['bulletPoints'] = '<li>Talk & browse the web at the same time                    <li>Rollover unused minutes                    ';
        bsinfo['carrier_corpId'] = '596';
        bsinfo['carrierName'] = 'AT&T';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm596.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/att//blackberry/blackberry-bold-9700-att_hbs.gif';
        bsinfo['user_rate'] = '4.54';
        bsinfo['expert_rate'] = '4.3';
        bsinfo['price'] = 'FREE';
        bsinfo['prRetailPrice'] = '$499.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$499.99</strike></p>\
                <p class="red"><strong>Your Price: FREE</strong></p>\
            ';
    break;
    case 6:
        bsinfo['prId'] = '35921';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1668';
        bsinfo['name'] = 'DROID by Motorola';
        bsinfo['prFriendlyURL'] = 'droid-by-motorola-verizon-wireless';
        bsinfo['reviewFriendlyURL'] = '/motorola-droid-user-reviews';
        bsinfo['desc'] = 'You\'ve watched the commercials and seen the ads, the <b>DROID by Motorola from Verizon Wireless is the must-have cell phone ... ';
        bsinfo['bulletPoints'] = '<li>5.0 Megapixel camera/video recorder              <li>Google Map\'s turn by turn navigation                                                              ';
        bsinfo['carrier_corpId'] = '660';
        bsinfo['carrierName'] = 'Verizon Wireless';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm660.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/verizonwireless/motorola/droid-by-motorola-verizon-wireless_hbs.gif';
        bsinfo['user_rate'] = '4.27';
        bsinfo['expert_rate'] = '4';
        bsinfo['price'] = '$19.99';
        bsinfo['prRetailPrice'] = '$549.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$549.99</strike></p>\
                <p class="red"><strong>Your Price: $19.99</strong></p>\
            ';
    break;
    case 7:
        bsinfo['prId'] = '35640';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1637';
        bsinfo['name'] = 'Samsung Highlight - Ice';
        bsinfo['prFriendlyURL'] = 'samsung-highlight-ice-tmobile';
        bsinfo['reviewFriendlyURL'] = '/samsung-highlight-user-reviews';
        bsinfo['desc'] = 'The Samsung Highlight brings full web browsing to your fingertips with its large Touchscreen Display - And its 3G Capability ... ';
        bsinfo['bulletPoints'] = '<li>2.5 inch Capacitive Touchscreen Display                                              <li>3G Capable for speed optimization                                              <li><strong>More Colors Available</strong>';
        bsinfo['carrier_corpId'] = '543';
        bsinfo['carrierName'] = 'T-Mobile';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm543.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/t-mobile/samsung/samsung-highlight-ice-tmobile_hbs.gif';
        bsinfo['user_rate'] = '4.15';
        bsinfo['expert_rate'] = '4.3';
        bsinfo['price'] = 'FREE';
        bsinfo['prRetailPrice'] = '$374.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$374.99</strike></p>\
                <p class="red"><strong>Your Price: FREE</strong></p>\
            ';
    break;
    case 8:
        bsinfo['prId'] = '34901';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1519';
        bsinfo['name'] = 'T-Mobile G1 with Google';
        bsinfo['prFriendlyURL'] = 'tmobile-g1-with-google-black-tmobile';
        bsinfo['reviewFriendlyURL'] = '/htc-g1-user-reviews';
        bsinfo['desc'] = 'As the first Android phone to come on the market, the T-Mobile G1 exceeds expectations. The G1 operates on the open-source ... ';
        bsinfo['bulletPoints'] = '<li>Touchscreen & QWERTY Keyboard                                                              <li>Google Web Search with one-click                                             <li><strong>More Colors Available</strong>';
        bsinfo['carrier_corpId'] = '543';
        bsinfo['carrierName'] = 'T-Mobile';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm543.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/t-mobile/htc/tmobile-g1-with-google-black-tmobile_hbs.gif';
        bsinfo['user_rate'] = '4.1';
        bsinfo['expert_rate'] = '0';
        bsinfo['price'] = '$49.99';
        bsinfo['prRetailPrice'] = '$479.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$479.99</strike></p>\
                <p class="red"><strong>Your Price: $49.99</strong></p>\
            ';
    break;
    case 9:
        bsinfo['prId'] = '35213';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1598';
        bsinfo['name'] = 'Samsung Impression';
        bsinfo['prFriendlyURL'] = 'samsung-impression-att';
        bsinfo['reviewFriendlyURL'] = '/samsung-impression-user-reviews';
        bsinfo['desc'] = 'The <b>Samsung Impression</b> for AT&T is one of the only phones to feature a 3.2 inches AMOLED touchscreen, which displays ... ';
        bsinfo['bulletPoints'] = '<li>Receive a $40 Bill credit with purchase of this device!    <li>Talk & browse the web at the same time            ';
        bsinfo['carrier_corpId'] = '596';
        bsinfo['carrierName'] = 'AT&T';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm596.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/att/samsung/samsung-impression-att_hbs.gif';
        bsinfo['user_rate'] = '4.32';
        bsinfo['expert_rate'] = '0';
        bsinfo['price'] = 'FREE';
        bsinfo['prRetailPrice'] = '$349.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$349.99</strike></p>\
                <p class="red"><strong>Your Price: FREE</strong></p>\
            ';
    break;
    case 10:
        bsinfo['prId'] = '35929';
        bsinfo['pgId'] = '100';
        bsinfo['pfId'] = '1673';
        bsinfo['name'] = 'HTC Droid Eris';
        bsinfo['prFriendlyURL'] = 'htc-droid-eris-verizon-wireless';
        bsinfo['reviewFriendlyURL'] = '/htc-droid-eris-user-reviews';
        bsinfo['desc'] = 'HTC\'s Droid Eris is a slim smartphone equipped with Google\'s Android Operating System. Its sleek design combines a 3.2\" 262K ... ';
        bsinfo['bulletPoints'] = '<li>5 Megapixel Camera with Autofocus           <li>3.2” Display                                                 ';
        bsinfo['carrier_corpId'] = '660';
        bsinfo['carrierName'] = 'Verizon Wireless';
        bsinfo['carrierLogoImgURL'] = '/img/corpLogos/plSm660.gif';
        bsinfo['prodImgURL'] = '/img/prod/cell-phones/verizonwireless/htc/htc-droid-eris-verizon-wireless_hbs.gif';
        bsinfo['user_rate'] = '3.98';
        bsinfo['expert_rate'] = '3.8';
        bsinfo['price'] = 'You make $50.00';
        bsinfo['prRetailPrice'] = '$429.99';
        bsinfo['promoSpotPriceDisplay'] = '\
                <p>Product Price: <strike>$429.99</strike></p>\
                <p class="red"><strong>Your Price: You make $50.00</strong></p>\
            ';
    break;
    default:
    break;
    }

    var useAltOverlay = UseAlternativeBestSellersOverlay();

    var userRating = '';
    var expertRating = '';
    var carrierLogo = '';

    var prodDetailURL = '/product/product.htm?prId='+bsinfo['prId'];
    var expertReviewURL = '/reviews/review.htm?pfId='+bsinfo['pfId']+'&rNav=3';
    if(bsinfo['prFriendlyURL'])
    {
        prodDetailURL = bsinfo['prFriendlyURL'];
        expertReviewURL = prodDetailURL+'?expandERV=1&amp;#exptRevs';
        //alert(prodDetailURL);
    }

    var prodImg = '<a href="'+prodDetailURL+'" onclick="UserTrackPageView(\'BSOIM\')"><img src='+bsinfo['prodImgURL']+' border=0 height="170"/></a>';

    var reviewURL = '/reviews/review.htm?pfId='+bsinfo['pfId']+'&rNav=2';
    if(bsinfo['reviewFriendlyURL'])
    {
        reviewURL = bsinfo['reviewFriendlyURL']; 
    }
    
    if( bsinfo['user_rate'] > 0 )
    {
        var ratebox = GetRatingBoxString('User', bsinfo['user_rate']);

        if( useAltOverlay )
        {
            userRating = '\
        <p class="rating">\
        ' + ratebox + '\
        <a href="'+reviewURL+'" onclick="UserTrackPageView(\'BSOUR\')">User Rating '+bsinfo['user_rate']+'</a>\
        </p>';
        }
        else
        {
            userRating = '\
        <a href="'+reviewURL+'" onclick="UserTrackPageView(\'BSOUR\')">\
        <p>User Rating<br>'+bsinfo['user_rate']+'</p>\
        ' + ratebox + '\
        </a>';
        }
    }

    if( bsinfo['expert_rate'] > 0 )
    {
        var ratebox = GetRatingBoxString('Expert', bsinfo['expert_rate']);

        if( useAltOverlay )
        {
            expertRating = '\
        <p class="rating">\
        '+ ratebox + '\
        <a href="'+expertReviewURL+'" onclick="UserTrackPageView(\'BSOER\')">Expert Rating '+bsinfo['expert_rate']+'</a>\
        </p>';
        }
        else
        {
            expertRating = '\
        <a href="'+expertReviewURL+'" onclick="UserTrackPageView(\'BSOER\')">\
        <p>Expert Rating<br>'+bsinfo['expert_rate']+'</p>\
        ' + ratebox + '\
        </a>';
        }
    }

    if( bsinfo['carrierLogoImgURL'] )
    {
        carrierLogo = '<img src="'+ bsinfo['carrierLogoImgURL'] + '" class="'+ ( useAltOverlay ? 'botom' : 'carrLogo') +'" alt="'+bsinfo['carrierName']+'"/>';
    }



    if( useAltOverlay )
    {
        return '\
    <div id="bsPhnPic" class="col">\
        '+ prodImg + '<br />\
        '+ carrierLogo +'\
    </div>\
    <div id="bsDetails" class="col">\
        <h2><a href="'+prodDetailURL+'" onclick="UserTrackPageView(\'BSONM\')">'+ bsinfo['name']+'</a></h2>\
        '+ userRating +'\
        '+ expertRating +'\
        <p>'+ bsinfo['desc']+'</p>\
        <ul>'+ bsinfo['bulletPoints']+'</ul>\
        <div class="bottom">\
            '+ bsinfo['promoSpotPriceDisplay'] +'\
            <form name="detailSelection" action="/upgrades/buytype.htm?prId='+bsinfo['prId']+'&tNav=1&redirect=/plans/shop.htm" method="POST">\
                <input type=image src="/img/home/bs_shopNow_btn.gif" class="right" alt="Shop now" name="buyImage" onclick="UserTrackPageView(\'BSOAC\')" />\
            </form>\
        </div>\
    </div>\
    <a href="'+prodDetailURL+'" onclick="UserTrackPageView(\'BSOMI\')"><img src="/img/home/moreInfo_gfx.gif" id="moreInfo" alt="More Information" border="0"/></a>\
    ';
    }
    else
    {
        return '\
    <div id="bsPhnPic" class="col">\
        '+ prodImg + '<br />\
        <a href="'+prodDetailURL+'" onclick="UserTrackPageView(\'BSOMI\')"><img src="/img/newFrontEnd/moreInfo_btn.gif" class="bottom" border="0"/></a>\
    </div>\
    <div id="bsDetails" class="col">\
        <h2><a href="'+prodDetailURL+'" onclick="UserTrackPageView(\'BSONM\')">'+ bsinfo['name']+'</a></h2>\
        <p>'+ bsinfo['desc']+'</p>\
        <ul>'+ bsinfo['bulletPoints']+'</ul>\
        <div class="bottom">\
            '+ bsinfo['promoSpotPriceDisplay'] +'\
        </div>\
    </div>\
    <div id="bsAction" class="col">\
        '+ carrierLogo +'\
        '+ userRating +'\
        '+ expertRating +'\
        <form name="detailSelection" action="/upgrades/buytype.htm?prId='+bsinfo['prId']+'&tNav=1&redirect=/plans/shop.htm" class="bottom" method="POST">\
              <input type=image src="/img/newFrontEnd/hm_addToCart_btn.gif" alt="Add to Cart" name="buyImage" onclick="UserTrackPageView(\'BSOAC\')" />\
        </form>\
    </div>\
    ';
    }
}
