﻿

var changeImageObject = null;
var timeOutObject = null;
var selectedImage = null;

String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
};


function FindProductsPanel(element) {
    while (element != null && (element.className == null || !element.className.startsWith('ProductsPanel'))) {
        element = element.parentNode;
    }
    return element;
}

function LoadProductsJavascript(sender) {
    var productsPanel = FindProductsPanel(sender);

    $('img.RotatingProductImage', productsPanel).mouseover(function (event) {
        var sender = event.target;
        changeImageObject = sender.id;
        changeImage();
    });

    $('img.RotatingProductImage', productsPanel).mouseout(function (event) {
        if (timeOutObject != null) {
            clearTimeout(timeOutObject);
        }
        document.images[changeImageObject].src = document.images[changeImageObject].src.substring(0, document.images[changeImageObject].src.indexOf("item"));
        changeImageObject = null;
    });

    $('img.MediumDetailImage', productsPanel).click(function (event) {
        var sender = event.target;
        var productsPanel = FindProductsPanel(sender._element);

        if (selectedImage != null) {
            //selectedImage.parentNode.style.borderWidth = '1px';
            selectedImage.parentNode.style.borderColor = '#a3a3a3';
            selectedImage.parentNode.style.borderStyle = 'dotted';
        }

        var iArray = $('.MediumDetailImage', sender.parentNode.parentNode);
        var i = 0;
        var element = iArray[i];
        var newHref = jQuery("*[id$='HyperLinkLargePopupImage']", productsPanel)[0].href.replace("/original/", "/small/");
        while (element != null && element.src != newHref) {
            i++;
            element = iArray[i];
        }

        if ($('.highslide', sender.parentNode).length > 0) {
            $('.highslide', sender.parentNode)[0].href = jQuery("*[id$='HyperLinkLargePopupImage']", productsPanel)[0].href;
            element.parentNode.appendChild($('.highslide', sender.parentNode)[0]);
        }

        //sender.parentNode.style.borderWidth = '2px';
        sender.parentNode.style.borderColor = 'gray';
        sender.parentNode.style.borderStyle = 'solid';
        selectedImage = sender;
        jQuery("*[id$='MediumDetailImage']", productsPanel)[0].src = sender.src.replace("/small/", "/medium/");
        jQuery("*[id$='HyperLinkLargePopupImage']", productsPanel)[0].href = sender.src.replace("/small/", "/original/");
        jQuery("*[id$='MediumDetailImageURL']", productsPanel)[0].value = sender.src;
    });

    $('.ImageProductButton', productsPanel).click(function (event) {
        var sender = event.target;
        var children = sender.parentNode.children;
        var articleID;
        var value;
        var articleControls = $("*[id$='HiddenFieldArticleID']", sender.parentNode.parentNode.parentNode);
        var radNumericTextBoxQtyControls = $("*[id$='RadNumericTextBoxQty']", sender.parentNode.parentNode.parentNode);
        
        if (articleControls.length > 0) {
            articleID = articleControls[0].value;
        }
        if (radNumericTextBoxQtyControls.length > 0) {
            var item = radNumericTextBoxQtyControls[0];
            value = $find(item.id.substring(0, item.id.indexOf('RadNumericTextBoxQty', 0)) + 'RadNumericTextBoxQty').get_value();
        }
        __doPostBack('AddToCart:' + articleID, value);  // Do postback with articleID and value
        return false;                                   // Disable the normal postback
    });
}

function changeImage() {
    if (changeImageObject != null && document.images[changeImageObject] != null) {
        if (document.images[changeImageObject].src.indexOf("item") != -1) {
            document.images[changeImageObject].src =
                document.images[changeImageObject].src.substring(0, document.images[changeImageObject].src.indexOf("item")) +
                    "item/" +
                    (parseInt(document.images[changeImageObject].src.substring(document.images[changeImageObject].src.indexOf("item") + 5)) + 1);
        }
        else {
            document.images[changeImageObject].src = document.images[changeImageObject].src + "item/1";
        }
        timeOutObject = setTimeout("changeImage()", 500);
    }
}


function RadListViewArticlesCreated(sender) {
    //    var productsPanel = FindProductsPanel(sender._element);
    //    var imageButtonList = jQuery("*[id$='ImageButtonAddToCart']", productsPanel);
    //    for (var b = 0, a = imageButtonList.length; b < a; b++) {
    //        imageButtonList[b].src = '/App_Themes/HBBMain/images/addToShoppingCart.png';
    //    }
    //
}

function OnClientValueChanging(sender, args) {
    // Show the tooltip only while the slider handle is sliding. In case the user simply clicks on the track of the slider to change the value
    // the change will be quick and the tooltip will show and hide too quickly.
    if (!isSliding) return;

    var tooltip = $find(jQuery("*[id$='RadToolTip1']")[0].id);

    ResetToolTipLocation(tooltip);
    tooltip.set_text(args.get_newValue());
}

var isSliding = false;
function OnClientSlideStart(sender, args) {
    isSliding = true;

    var tooltip = $find(jQuery("*[id$='RadToolTip1']")[0].id);
    ShowRadToolTip(tooltip, sender);
}

function OnClientSlideEnd(sender, args) {
    isSliding = false;

    var tooltip = $find(jQuery("*[id$='RadToolTip1']")[0].id);
    tooltip.hide();

    var header = encodeURIComponent(sender._element.attributes['filterheader'].nodeValue).revertPercentage().replace('%2F', '%C3%97S');
    var startvalue = encodeURIComponent($(sender)[0]._value).revertPercentage().replace('%2F', '%C3%97S');
    var endvalue = encodeURIComponent($(sender)[0]._selectionEnd).revertPercentage().replace('%2F', '%C3%97S');
    var uniqueToken = CreateUniqueToken(location.href.replacePercentage());
    var currentHref = encodeURI(decodeURI(location.href.replacePercentage().replaceSlash(uniqueToken))).revertPercentage().replaceSlash(uniqueToken);

    var fillInValue = '/' + header + '/' + startvalue + '-' + endvalue;
    if (startvalue == $(sender)[0]._minimumValue && endvalue == $(sender)[0]._maximumValue) {
        fillInValue = '';
    }

    if (currentHref.indexOf('/' + header) != -1) {
        var startOfNextValue = currentHref.indexOf('/', currentHref.indexOf('/' + header) + 1);
        var endOfNextValue = currentHref.indexOf('/', startOfNextValue + 1);

        var nextValue;
        if (endOfNextValue != -1) {
            nextValue = currentHref.substring(startOfNextValue + 1, endOfNextValue);
        }
        else {
            nextValue = currentHref.substring(startOfNextValue + 1);
        }
        if (nextValue.indexOf('-') != -1) {
            window.location.href = (currentHref.replace('/' + header + '/' + nextValue, fillInValue)).split('//').join('/').replace('http:/', 'http://').replace('https:/', 'https://');
        }
        else {
            window.location.href = (currentHref.replace('/' + header, fillInValue)).split('//').join('/').replace('http:/', 'http://').replace('https:/', 'https://');
        }
    }
    else {
        window.location.href = (currentHref + fillInValue).split('//').join('/').replace('http:/', 'http://').replace('https:/', 'https://');
    }
}

function ShowRadToolTip(tooltip, slider) {
    var activeHandle = slider.get_activeHandle();
    if (!activeHandle) return;

    tooltip.set_targetControl(activeHandle);
    ResetToolTipLocation(tooltip);
}

function ResetToolTipLocation(tooltip) {
    if (!tooltip.isVisible())
        tooltip.show();
    else
        tooltip.updateLocation();
}

function openWin(url, towindow) {
    window.radopen(url, towindow);
}

var combo = null;
function CloseActiveToolTip() {
    var tooltip = Telerik.Web.UI.RadToolTip.getCurrent();
    if (tooltip) tooltip.hide();
}

function OnClientHide(sender, args) {
    if (combo) {
        $find(combo.id).hideDropDown();
    }
}

function CreateUniqueToken(sender) {
    var i = 0;
    while (sender.indexOf('T' + i) != -1)
    {
        i++;
    }
    return 'T' + i;
}

function CheckboxClick(sender) {
    var header = encodeURIComponent(sender.parentElement.attributes['filterheader'].nodeValue).revertPercentage().replace('%2F', '%C3%97S');
    var value = encodeURIComponent(sender.parentElement.textContent).revertPercentage().replace('%2F', '%C3%97S');
    var uniqueToken = CreateUniqueToken(location.href.replacePercentage());
    var currentHref = encodeURIComponent(decodeURIComponent(location.href.replacePercentage().replaceSlash(uniqueToken))).revertPercentage().revertSlash(uniqueToken);

    if (sender.checked) {
        currentHref = currentHref.split('%2F').join('/').split('%3A').join(':').replace('%EF%BF%BDS', '%C3%97S').replace('%EF%BF%BDP', '%C3%97P');
        if (currentHref.indexOf('/' + header) != -1) {
            window.location.href = currentHref.replace('/' + header, '/' + header + '/' + value).split('//').join('/').replace('http:/', 'http://').replace('https:/', 'https://');
        }
        else {
            window.location.href = (currentHref + '/' + header + '/' + value).split('//').join('/').replace('http:/', 'http://').replace('https:/', 'https://');
        }
    }
    else {
        var ownHeaderFound = false;
        var nextHeaderFound = false;
        var endOfValue = currentHref.indexOf('%2F' + value + '%2F');
        if (endOfValue == -1) {
            endOfValue = currentHref.lastIndexOf('%2F');
            nextHeaderFound = true;
        }
        else {
            var listHeaders = $(sender).closest(":has(div#filter-repeater-item div.textbar.gray)").find('div#filter-repeater-item div.textbar.gray');
            var startOfNextValue = currentHref.indexOf('%2F', endOfValue + 3);
            var endOfNextValue = currentHref.indexOf('%2F', startOfNextValue + 3);

            var nextValue;
            if (endOfNextValue != -1) {
                nextValue = currentHref.substring(startOfNextValue + 3, endOfNextValue);
            }
            else {
                nextValue = currentHref.substring(startOfNextValue + 3);
            }

            for (var i = 0; i < listHeaders.length; i++) {
                if (nextValue == listHeaders[i].textContent.trim()) {
                    nextHeaderFound = true;
                }
            }
        }

        var startOfValue = currentHref.lastIndexOf('%2F', endOfValue - 3);
        posibleHeader = currentHref.substring(startOfValue + 3, endOfValue);
        if (header == posibleHeader) {
            ownHeaderFound = true;
        }

        currentHref = currentHref.split('%2F').join('/').split('%3A').join(':').replace('%EF%BF%BDS', '%C3%97S').replace('%EF%BF%BDP', '%C3%97P');
        if (ownHeaderFound && nextHeaderFound) {
            window.location.href = currentHref.replace('/' + header + '/' + value, '').split('//').join('/').replace('http:/', 'http://').replace('https:/', 'https://');
        }
        else {
            window.location.href = currentHref.replace('/' + value, '').split('//').join('/').replace('http:/', 'http://').replace('https:/', 'https://')
        }
    }
}

String.prototype.replaceSlash = function (uniqueToken) {
    return this.replace('%C3%97S', uniqueToken).replace('%EF%BF%BDS', uniqueToken);
}

String.prototype.revertSlash = function (uniqueToken) {
    return this.replace(uniqueToken, '%C3%97S').replace(uniqueToken, '%EF%BF%BDS');
}

String.prototype.replacePercentage = function () {
    return this.replace('%C3%97P', '%25').replace('%EF%BF%BDP', '%25');
}

String.prototype.revertPercentage = function () {
    return this.replace('%25', '%C3%97P').replace('%25', '%EF%BF%BDP');
}

String.prototype.revertPercentageSmall = function () {
    return this.replace('%', '%C3%97P').replace('%', '%EF%BF%BDP');
}

hs.graphicsDir = '/Shopfinity/General/js/highslide/graphics/';
hs.align = 'center';
hs.transitions = ['expand', 'crossfade'];
hs.outlineType = 'rounded-white';
hs.fadeInOut = true;
hs.dimmingOpacity = 0.75;

// define the restraining box
//hs.useBox = true;
hs.width = 640;
hs.height = 480;

// Add the controlbar
hs.addSlideshow({
    //slideshowGroup: 'group1',
    interval: 5000,
    repeat: false,
    useControls: true,
    fixedControls: 'fit',
    overlayOptions: {
        opacity: 1,
        position: 'bottom center',
        hideOnMouseOut: true
    }
});
