﻿var HideAllSubMenus = function () { };
var DisplaySubMenu = function () { };

// The number of thumbnails to display at any one time in the image gallery
var galleryWindowSize = 3;
var productWindowSize = 5;
var thumbOnDisplay;

var productListApi;
var galleryApi;

var doTransition = true;
var refreshBtnID;

var cleanStringForUrlHash = function (obj) {
    var text = obj.html();

    // jquery corners may add wrapper divs to this object
    if (obj.hasClass('rounded_by_jQuery_corners')) {
        if (obj.children('div').size() == 3) {
            var objDivs = obj.children('div').eq(1);
            text = objDivs.html();
        }
    }

    return text.toLowerCase().trim().replace(/ /g, "-"); //will turn "Portable Toilets" into "portable-toilets", which is something we can pass over #hash in the URL.
}

var imgTitle = '';

//Change the product main image when you click one of the thumbnails.
var showProductImage = function (obj) {
    var src = $(obj).attr('href');
    imgTitle = '';

    obj = $(obj).children('img');

    if (obj.attr('id') == thumbOnDisplay)
        return false;

    thumbOnDisplay = obj.attr('id');

    // Reset the description
    imgTitle = obj.attr('alt');
    $('.pictureDescription').fadeOut(400, function () {
        $(this).html('');

        if (imgTitle != '') {
            $(this).html("<span id=\"fancybox-title-over\">" + imgTitle + "</span>");
        }
    });

    // Fade it out
    $('.productHeroWrapper img').fadeOut(400, function () {
        // When complete, swap the source and bring it back
        $(this)
        .attr('src', src)
        .attr('alt', imgTitle)
        .load(function () {
            var maxWidth = 320; // Max width for the image
            var maxHeight = 320;       // Max height for the image
            var width = $(this).width();    // Current image width
            var height = $(this).height();  // Current image height
            var newWidth = width;
            var newHeight = height;

            if (width > maxWidth || height > maxHeight) {
                var xScale = width / maxWidth;
                var yScale = height / maxHeight;

                if (yScale > xScale) {
                    newWidth = Math.round(width * (1 / yScale));
                    newHeight = Math.round(height * (1 / yScale));
                } else {
                    newWidth = Math.round(width * (1 / xScale));
                    newHeight = Math.round(height * (1 / xScale));
                }
            }

            // loading complete, fadein please
            $('.productHeroWrapper img').fadeIn();
            if (imgTitle != '') {
                $('.pictureDescription').fadeIn();
            }
        })
    });

    return false;
}

var closeFancyBox = function () {
    $.fancybox.close();
}

var closeFancyBoxAndRedirect = function (strRedirectTo) {
    $.fancybox.close();
    //we only want to redirect if we are on the products page (if we're already on the quotes page, the fancybox declaration will take care of the onClosed argument separately)
    if ($('.order-summary-outer-wrapper').length == 0) {
        //no summary, send to quotes page where there IS a summary
        window.location = strRedirectTo; //'/construction/20/quote.aspx'
    }
}

var deleteLinkObj;
var confirmDeleteShoppingCartItem = function (obj) {
    deleteLinkObj = obj;
    jConfirm('Are you sure you want to remove this item?', 'Confirmation required', function (r) {
        if (r) {
            var deleteLinkId = deleteLinkObj.id;            //ctl00_ctl00_cph1_cph1_ctl00_OrderSummary1_rptShoppingCart_ctl00_lbRemove
            deleteLinkId = deleteLinkId.replace(/_/g, "$"); //ctl00$ctl00$cph1$cph1$ctl00$OrderSummary1$rptShoppingCart$ctl00$lbRemove
            __doPostBack(deleteLinkId, '');
        }
    });
}

/* --- Image Gallery --- */



// Fade the gradient in and out
var gallerySeek = function (seekEvent, index) {
    try {
        if ((!index) || index == 0) {
            $('#westGhost').animate({ width: '0px' });
        } else {
            $('#westGhost').animate({ width: '27px' }); /* 81px width 3-state image / 3 */
        }

        if (index == galleryApi.getSize() - galleryWindowSize) {
            $("#eastGhost").animate({ width: '0px' });
        } else {
            $("#eastGhost").animate({ width: '27px' }); /* 81px width 3-state image / 3 */
        }
    } catch (e) {
    }
}


// Setup event handlers to pass on the click event when
// the ghosted gradient is clicked
var horizClick = function (index) {
    var target = galleryApi.getItems()[index];
    target = $(target).children('a');
    target.trigger('click');
}

var initFancyCorners = function () {

    // Lightbox
    $(".openFancybox").fancybox({
        'overlayOpacity': 0.65,
        'overlayColor': '#000000',
        'type': 'iframe',
        'titleShow': false,
        'width': 900,
        'height': 550,
        'onClosed': function () {
            if (window.parent.refreshBtnID != undefined) {
                __doPostBack(window.parent.refreshBtnID, '');
            }
        }
    });


    /* grab any title links that have a "rel" attribute and look for content to display in a jQuery tooltip */
    $(".order-summary-item .product-title a").tooltip({
        bodyHandler: function () {
            return $($(this).attr("rel")).html();
        },
        showURL: false
                , delay: 10
    });

    /* grab anything in the product attributes */
    $(".attributes *").tooltip({
        showURL: false
                , delay: 10
    });

    // Setup rounded corners
    
    $('.rounded').corners();
    $('.section-product h3').corners("transparent");
}

$('document').ready(function () { 
    // Work out which image is currently being shown
    thumbOnDisplay = $("#imageGallery .items img").first().attr('id');

    // Preload all the gallery images
    $('#imageGallery .items a').each(function (index) {
        var imgPreload = $('<img />').attr('src', $(this).attr('href'));
    });

    $('#eastGhost').click(function () {
        horizClick(galleryWindowSize + galleryApi.getIndex() - 1);
    });

    $('#westGhost').click(function () {
        horizClick(galleryApi.getIndex());
    });

    // ie6 can't handle the png gradients
    if (!ie6) {
        // Show the gradients
        $("#eastGhost, #westGhost").show();
        gallerySeek();
    }

    // initialize scrollable gallery
    var galleryCount = $("#imageGallery .items").children().size();
    if (galleryCount > galleryWindowSize) {
        $("#gallery .btnLeft").addClass("prev").removeClass("disabled");
        $("#gallery .btnRight").addClass("next").removeClass("disabled");

        $("#imageGallery").scrollable();

        // Show the navigation buttons
        $('#gallery a.prev, #gallery a.next').show();

        // Set the number of thumbnails to display at any one time
        galleryApi = $("#imageGallery").data("scrollable");
        galleryApi.getConf().itemCount = galleryWindowSize;
        galleryApi.onBeforeSeek(gallerySeek);
    }

    refreshBtnID = $('.btnRefresh').attr('id');
    if (refreshBtnID != undefined) {
        refreshBtnID = refreshBtnID.replace(/_/g, '$');
    }

    initFancyCorners();

});




