﻿/* BANNER AD FUNCTIONS */
function bannerOnClientLoadHandler(sender, args) {
    var spotimgId = '#' + sender.get_id() + '_SpotImage0';
    $(spotimgId).addClass("selected");
    $(spotimgId).attr("src", "/images/iq/ad-spot-on.png");
}

function bannerOnClientItemShownHandler(sender, args) {

    var count = sender.get_items().length;
    var item = sender.get_currentItem();

    var itemId = $('#' + sender.get_id() + '_i' + item.get_index() + '_BannerItemId').val();
    var bannerId = $('#' + sender.get_id() + '_BannerId').val();
    var unique = $('#' + sender.get_id() + '_BannerIdUnique').val();

    if (unique == 1) {
        $('#' + sender.get_id() + '_BannerIdUnique').val(0)
    }

    var paramList = '{' + '"bannerId":"' + bannerId + '",';
    paramList += '"bannerItemId":"' + itemId + '",';
    paramList += '"unique":"' + unique + '"}';

    $.ajax({
        type: "POST",
        url: "/WebServices/BannerAdTracker.asmx/RegisterImpression",
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: function(msg) { },
        error: function(XMLHttpRequest, textStatus, errorThrown) { }
    });

};

function bannerOnClientItemShowingHandler(sender, args) {

    var controlID = sender.get_id();
    var itemIndex = sender.get_currentItem().get_index() + 1;
    renderSpotSwaps(controlID, itemIndex);

}

function renderSpotSwaps(controlID, itemIndex) {

    var rotator = $find(controlID);
    var count = rotator.get_items().length;

    if (itemIndex == rotator.get_items().length) {
        itemIndex = 0;
    }

    for (i = 0; i <= count; i++) {
        var spotimgId = '#' + rotator.get_id() + '_SpotImage' + i;

        if (i == itemIndex) {
            $(spotimgId).addClass("selected");
            $(spotimgId).attr("src", "/images/iq/ad-spot-on.png");
        } else {
            $(spotimgId).removeClass("selected");
            $(spotimgId).attr("src", "/images/iq/ad-spot-off.png");
        }

        $(function() {
            $(spotimgId).hover(
	            function() {
	                $(this).attr("src", "/images/iq/ad-spot-on.png");
	            },
	            function() {
	                if ($(this).hasClass("selected")) {
	                    $(this).attr("src", "/images/iq/ad-spot-on.png");
	                } else {
	                    $(this).attr("src", "/images/iq/ad-spot-off.png");
	                }
	            }
            );
        });
    }
}

function showAd(controlID, newItemIndex) {

    var rotator = $find(controlID);
    var currImgIndex = rotator.get_currentItem().get_index();

    if (newItemIndex > currImgIndex) {
        while (currImgIndex < newItemIndex) {
            rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
            currImgIndex++;
        }
    }
    else {
        while (currImgIndex > newItemIndex) {
            rotator.showNext(Telerik.Web.UI.RotatorScrollDirection.Right);
            currImgIndex--;
        }
    }

    rotator.set_scrollDirection(Telerik.Web.UI.RotatorScrollDirection.Left);
    rotator.set_animationDirection(Telerik.Web.UI.RotatorScrollDirection.Left);

    renderSpotSwaps(controlID, newItemIndex);
};

function showQuickNav() {

    $(this).animate({ "width": 980 }, 200);
    $("div.quickNavButton").delay(2000).css("background-image", "url(/images/iq/quicknav-button-on.png)");
    $("div.quickNav").show("slide", { direction: "left" }, 1000);
};

function hideQuickNav() {
    $("div.quickNav").hide("slide", { direction: "left" }, 1000, function() {
        $("div.quickNavButton").css("background-image", "url(/images/iq/quicknav-button-off.png)");
    });
    $(this).delay(1000).animate({ "width": 239 }, 200);
};

$(document).ready(function() {

    /*if ($("div.quickNavContainer").length) {
    $("div.quickNavContainer").hover(showQuickNav, hideQuickNav);
    }*/

    if ($("a.vehiclePopup").length) {
        $("a.vehiclePopup").fancybox({ 'width': 350, 'height': 600, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

    if ($("a.vehicleTestDrivePopup").length) {
        $("a.vehicleTestDrivePopup").fancybox({ 'width': 350, 'height': 600, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

    if ($("a.sendToFriendPopup").length) {
        $("a.sendToFriendPopup").fancybox({ 'width': 350, 'height': 590, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

    if ($("a.contactDealershipPopup").length) {
        $("a.contactDealershipPopup").fancybox({ 'width': 350, 'height': 500, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

    if ($("a.testDrivePopup").length) {
        $("a.testDrivePopup").fancybox({ 'width': 350, 'height': 500, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

    if ($("a.pdfPopup").length) {
        $("a.pdfPopup").fancybox({ 'width': 800, 'height': 600, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

    if ($("a.afterSalesPopup").length) {
        $("a.afterSalesPopup").fancybox({ 'width': 350, 'height': 450, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

    if ($("a.servicePopup").length) {
        $("a.servicePopup").fancybox({ 'width': 350, 'height': 660, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': false });
    }

});

function qsOnClientItemShownHandler(sender, args) {

    var item = sender.get_currentItem();
    var stockTypeId = $('#' + sender.get_id() + '_i' + item.get_index() + '_StockTypeId').val();

    if (stockTypeId == 7) {  // Exclusive Vehicle
        $('#ctl00_ContentPlaceHolder1_QuickSearch1_lblTitle').html('<a title="New" href="/search/new.aspx">New</a> | <span style="color:#cc0033;">Exclusive</span>');
        }
    else {
        $('#ctl00_ContentPlaceHolder1_QuickSearch1_lblTitle').html('<a title="New" href="/search/new.aspx">New</a>');        
    }
    
    //alert(stockTypeId);
}
