var TINY = {};

function T$(id) { return document.getElementById(id) }
function T$$$() { return document.all ? 1 : 0 }

TINY.scroller = function () {
    return {
        init: function (a, c, b, s, d, Coeficient) {
            a = T$(a); a.c = c; a.s = s; c = T$(c); b = T$(b); s = T$(s); a.n = d || 0;
            b.style.display = 'block'; a.style.overflow = 'hidden';
            var h = a.offsetHeight, t = c.offsetHeight;
            if (t < h) {
                b.style.display = 'none'
            } else {
                a.m = h - t; a.d = t / h;

                if (h == 0)
                    s.style.height = '0px';
                else
                    s.style.height = (h * (h / t)) + 'px';
                s.style.top = b.style.top = 0;
                s.onmousedown = function (event) { TINY.scroller.st(event, a.id); return false };
                s.onselectstart = function () { return false }
            }
            a.l = b.offsetHeight - s.offsetHeight
            a.l = (a.l * Coeficient)
        },
        st: function (e, f) {
            var a = T$(f), s = T$(a.s); a.bcs = TINY.cursor.top(e); a.bct = parseInt(s.style.top);
            if (a.mv) { this.sp(f) }
            a.mv = function (event) { TINY.scroller.mv(event, f) };
            a.sp = function () { TINY.scroller.sp(f) };
            if (T$$$()) {
                document.attachEvent('onmousemove', a.mv); document.attachEvent('onmouseup', a.sp)
            } else {
                document.addEventListener('mousemove', a.mv, 1); document.addEventListener('mouseup', a.sp, 1)
            }
            if (a.d) { s.className += ' ' + a.n }
        },
        mv: function (e, f) {
            var a = T$(f), m = TINY.cursor.top(e) - a.bcs + a.bct, s = T$(a.s), c = T$(a.c);
            if (m >= 0 && m < a.l) {
                s.style.top = m + 'px'; c.style.top = (m * -1 * a.d) + 'px'
            } else if (m < 0) {
                s.style.top = 0; c.style.top = 0
            } else if (m > a.l) {
                s.style.top = a.l + 'px'; c.style.top = a.m + 'px'
            }
        },
        sp: function (f) {
            var a = T$(f), s = T$(a.s); if (a.d) { s.className = s.className.replace(' ' + a.n, '') }
            if (T$$$()) {
                document.detachEvent('onmousemove', a.mv); document.detachEvent('onmouseup', a.sp)
            } else {
                document.removeEventListener('mousemove', a.mv, 1); document.removeEventListener('mouseup', a.sp, 1)
            }
            a.mv = 0;
        }
    }
} ();

TINY.cursor = function () {
    return {
        top: function (e) {
            return T$$$() ? window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop : e.clientY + window.scrollY
        }
    }
} ();



//Moebelbasen.Web.Ui.MiniVariantCatalog if VariantImageReplace = true
function change_image(targetObjectId, fromImageObjectId) {
    if (targetObjectId == null || fromImageObjectId == null)
        return;

    var targetObject = document.getElementById(targetObjectId);
    var fromImageObject = document.getElementById(fromImageObjectId);
    if (targetObject != null && fromImageObject != null)
        if (targetObject.src != null && fromImageObject.src != null) {
            var targetObjectSrc = targetObject.src;
            targetObject.src = fromImageObject.src;
            fromImageObject.src = targetObjectSrc;
        }
}
//END Moebelbasen.Web.Ui.MiniVariantCatalog if VariantImageReplace = true


//Moebelbasen.Web.Ui.WebShopCatalog if EnableImageChanger = true

var changerImgTimeOut = null;
var changerImgObject = null;
var changerImgUrl = null;
var changerImgArray = null;
var changerImgPosition = 0;
var changerImgDefaultTimeOut = 2000;

function changerImgReset() {

    if (changerImgTimeOut != null)
        clearTimeout(changerImgTimeOut);

    changerImgTimeOut = null;
    changerImgObject = null;
    changerImgUrl = null;
    changerImgArray = null;
    changerImgPosition = 0;
    changerImgDefaultTimeOut = 2000;
}

function changerImgStart(targetObjectImage, imageUrl, stringImages, timeout) {

    if (targetObjectImage == null || imageUrl == null || stringImages == null)
        return false;

    var arrayImages = stringImages.split(",");

    if (arrayImages.length == 0)
        return false;

    //Remove THis After Upload WebShopCatalog
    arrayImages = new Array(arrayImages[0], arrayImages[1]);

    changerImgReset();

    changerImgObject = targetObjectImage;
    changerImgUrl = imageUrl;
    changerImgArray = arrayImages;

    if (timeout > 300)
        changerImgDefaultTimeOut = timeout;

    //changerImgTimeOut = setTimeout('changerImgProcessing()', changerImgDefaultTimeOut);

    //Remove THis After Todor check
    changerImgSetById(targetObjectImage, imageUrl, changerImgArray[1]);

    //alert("DO IT");
}


function changerImgProcessing() {

    if (changerImgObject == null || changerImgUrl == null || changerImgArray == null)
        return false;

    if (changerImgArray.length == 0)
        return false;

    if (changerImgTimeOut != null)
        clearTimeout(changerImgTimeOut);

    if (changerImgArray.length == changerImgPosition + 1)
        changerImgPosition = 0;
    else
        changerImgPosition++;

    changerImgSetById(changerImgObject, changerImgUrl, changerImgArray[changerImgPosition]);

    changerImgTimeOut = setTimeout('changerImgProcessing()', changerImgDefaultTimeOut);
}


function changerImgStop(targetObjectImage, imageUrl, imageId) {

    changerImgReset();

    changerImgSetById(targetObjectImage, imageUrl, imageId);
}


function changerImgProcessingOLD(targetObjectImage, imageUrl, arrayImages) {

    if (targetObjectImage == null || imageUrl == null || arrayImages == null)
        return false;

    if (arrayImages.length == 0)
        return false;

    changerImgSetById(targetObjectImage, imageUrl, arrayImages[1]);
}

function changerImgSetById(targetObjectImage, imageUrl, imageId) {

    if (targetObjectImage == null || imageUrl == null || imageId == null)
        return false;

    changerImgSetByUrl(targetObjectImage, imageUrl + imageId);
}

function changerImgSetByUrl(targetObjectImage, fullImageUrl) {

    if (targetObjectImage == null || fullImageUrl == null)
        return false;

    if (targetObjectImage.tagName.toLowerCase() == "A".toLowerCase()) {

        var imageObjects = targetObjectImage.getElementsByTagName("img");
        if (imageObjects.length > 0)
            imageObjects[0].src = fullImageUrl;
    }
    else
        if (targetObjectImage.tagName.toLowerCase() == "IMG".toLowerCase())
            targetObjectImage.src = fullImageUrl;

}

//END Moebelbasen.Web.Ui.WebShopCatalog if EnableImageChanger = true



/*** MoebelBasen.UserControls.DropDownColor JavaScript ***/

function customDropDownCollapse(box) {
    var text_class_append = "_collapsed";

    if (box == null)
        return;

    if (box.getAttribute('class') != null)
        if (box.getAttribute('class').indexOf(text_class_append) == -1)
            box.setAttribute('class', box.getAttribute('class') + text_class_append);
        else
            box.setAttribute('class', box.getAttribute('class').replace(text_class_append, ''));
}

/*** MoebelBasen.UserControls.DropDownColor JavaScript ***/


/*** MoebelBasen.Web.UI.WebControls.ScrollCatalog JavaScript ***/

function CreateScroll(div_scrool_container_id, div_scrool_arrow_left_id, div_scrool_arrow_right_id, div_scrool_item_class, numberVisibleScrollItems, numberScrollingItems, disableAnimationOnScrollRepeat, scrollDuration, scrollSleepTime) {
    $(function () {
        try {
            var id_scrool_container = "#" + div_scrool_container_id;
            var id_div_scrool_arrow_left = "#" + div_scrool_arrow_left_id;
            var id_div_scrool_arrow_right = "#" + div_scrool_arrow_right_id;
            var class_div_scrool_item = "." + div_scrool_item_class;

            var disableAnimationOnRepeat = false;
            var duration = 500; if (scrollDuration != null && scrollDuration > 0) duration = scrollDuration;
            var enableAutoScrool = true; if (scrollDuration != null && scrollDuration == -1) enableAutoScrool = false;
            var itemsFindString = "div" + id_scrool_container + " " + class_div_scrool_item;

            var scrool_container = $(id_scrool_container);
            var itemsCount = $(itemsFindString).length;
            var itemWidth = $(itemsFindString + ":first").outerWidth();
            var position = 1;
            var numberMoveItems = 1;

            if (scrool_container == null ||
                (itemsCount == null || itemsCount < numberVisibleScrollItems) ||
                (itemWidth == null || itemWidth == 0)
                )
                return;

            //TO DO numberScrollingItems

            if (numberVisibleScrollItems > 1) {

                numberMoveItems = numberVisibleScrollItems;

                //itemsCount = Math.round(itemsCount / numberMoveItems);
                itemsCount = itemsCount / numberMoveItems;
                itemWidth = itemWidth * numberMoveItems;
            }

            //AutoMode Members
            var timeoutId = null;
            var stopAutoMove = false;
            var sleepTime = 4000; if (scrollSleepTime != null && scrollSleepTime > 0) sleepTime = scrollSleepTime;
            var stopTime = 8000;

            //Disable scrool animation on repeat scrool items
            if (disableAnimationOnScrollRepeat != null && disableAnimationOnScrollRepeat == "True") {
                disableAnimationOnRepeat = true;
            }

            $(id_scrool_container).width(itemsCount * itemWidth);
            $(id_scrool_container).css("position", "relative");


            $(id_div_scrool_arrow_left).click(function () {

                moveLeft();

                if (enableAutoScrool)
                    sleepAutoMove(stopTime);

            });

            $(id_div_scrool_arrow_right).click(function () {

                moveRight();

                if (enableAutoScrool)
                    sleepAutoMove(stopTime);

            });


            function moveLeft() {
                if (position > 1) {
                    position--;

                    scrool_container.animate(
                                    { "left": "+=" + itemWidth + "px" },
                                    duration
                                );
                }
                else {

                    position = itemsCount;

                    if (disableAnimationOnRepeat) {
                        scrool_container.animate(
                                    { "left": "+=" + itemWidth + "px" },
                                    duration
                                );
                        scrool_container.animate(
                                    { left: -(position - 1) * itemWidth },
                                    0
                                );
                    }
                    else
                        scrool_container.animate(
                                    { left: -(position - 1) * itemWidth },
                                    duration
                                );
                }
            }

            function moveRight() {

                if (position < itemsCount) {
                    position++;

                    scrool_container.animate(
                                    { "left": "-=" + itemWidth + "px" },
                                    duration
                                );
                }
                else {

                    position = 1;

                    if (disableAnimationOnRepeat) {
                        scrool_container.animate(
                                    { "left": "-=" + itemWidth + "px" },
                                    duration
                                );
                        scrool_container.animate(
                                    { left: 0 },
                                    0
                                );
                    }
                    else
                        scrool_container.animate(
                                    { left: 0 },
                                    duration
                                );
                }
            }

            function startAutoMove(time) {
                if (timeoutId != null) {
                    clearTimeout(timeoutId);
                    timeoutId = null;
                }

                if (stopAutoMove == false)
                    timeoutId = setTimeout(function () { moveRight(); startAutoMove(time); }, time);
            }

            function sleepAutoMove(time) {
                stopAutoMove = true;

                if (timeoutId != null) {
                    clearTimeout(timeoutId);
                    timeoutId = null;
                }

                if (time > 0 && timeoutId == null)
                    setTimeout(function () { stopAutoMove = false; startAutoMove(sleepTime); }, time);
            }

            if (enableAutoScrool)
                startAutoMove(sleepTime);

        }
        catch (ex)
    { }

    });

}

function CreateScrollVertical(div_scrool_container_id, div_scrool_arrow_left_id, div_scrool_arrow_right_id, div_scrool_item_class, numberVisibleScrollItems, numberScrollingItems, disableAnimationOnScrollRepeat, scrollDuration, scrollSleepTime) {
    $(function () {
        try {

            var id_scrool_container = "#" + div_scrool_container_id;
            var id_div_scrool_arrow_left = "#" + div_scrool_arrow_left_id;
            var id_div_scrool_arrow_right = "#" + div_scrool_arrow_right_id;
            var class_div_scrool_item = "." + div_scrool_item_class;

            var disableAnimationOnRepeat = false;
            var duration = 500; if (scrollDuration != null && scrollDuration > 0) duration = scrollDuration;
            var enableAutoScrool = true; if (scrollDuration != null && scrollDuration == -1) enableAutoScrool = false;
            var itemsFindString = "div" + id_scrool_container + " " + class_div_scrool_item;

            var scrool_container = $(id_scrool_container);
            var itemsCount = $(itemsFindString).length;
            var itemHeight = $(itemsFindString + ":first").outerHeight();
            var position = 1;
            var numberMoveItems = 1;

            if (scrool_container == null ||
                (itemsCount == null || itemsCount < 2) ||
                (itemHeight == null || itemHeight == 0) ||
                (itemsCount <= numberVisibleScrollItems)
                )
                return;

            //numberMoveItems from numberScrollingItems
            if (itemsCount > numberScrollingItems && numberScrollingItems > 0) {
                numberMoveItems = numberScrollingItems;
            }

            //AutoMode Members
            var timeoutId = null;
            var stopAutoMove = false;
            var sleepTime = 4000; if (scrollSleepTime != null && scrollSleepTime > 0) sleepTime = scrollSleepTime;
            var stopTime = 8000;

            //Disable scrool animation on repeat scrool items
            if (disableAnimationOnScrollRepeat != null && disableAnimationOnScrollRepeat == "True") {
                disableAnimationOnRepeat = true;
            }

            $(id_scrool_container).height(itemsCount * itemHeight);
            $(id_scrool_container).css("position", "relative");


            $(id_div_scrool_arrow_left).click(function () {

                moveDown();
                if (enableAutoScrool)
                    sleepAutoMove(stopTime);

            });

            $(id_div_scrool_arrow_right).click(function () {

                moveUp();

                if (enableAutoScrool)
                    sleepAutoMove(stopTime);

            });


            function moveDown() {

                var maxPosition = itemsCount - numberVisibleScrollItems + 1;
                var isRepeat = false;

                if (position < 1)
                    position = 1;
                else if (position > maxPosition)
                    position = 1;
                else if (position + numberMoveItems > maxPosition) {

                    if (position != maxPosition)
                        position = maxPosition;
                    else {
                        position = 1;
                        isRepeat = true;
                    }
                }
                else
                    position += numberMoveItems;

                if (isRepeat && disableAnimationOnRepeat) {
                    scrool_container.animate(
                                { "top": "-=" + itemHeight + "px" },
                                duration
                            );
                    scrool_container.animate(
                        { "top": "-" + ((position - 1) * itemHeight) + "px" },
                        0
                    );
                }
                else {
                    scrool_container.animate(
                        { "top": "-" + ((position - 1) * itemHeight) + "px" },
                        duration
                    );
                }

                if (false == true)
                    $("#div_image_catalog_aditional_left_button").html(position);
            }

            function moveUp() {

                var maxPosition = itemsCount - numberVisibleScrollItems + 1;
                var isRepeat = false;

                if (position > maxPosition)
                    position = maxPosition;
                else if (position > maxPosition)
                    position = maxPosition;
                else if (position - numberMoveItems <= 1) {

                    if (position != 1)
                        position = 1;
                    else {
                        position = maxPosition;
                        isRepeat = true;
                    }
                }
                else
                    position -= numberMoveItems;


                if (isRepeat && disableAnimationOnRepeat) {
                    scrool_container.animate(
                                { "top": "+=" + itemHeight + "px" },
                                duration
                            );
                    scrool_container.animate(
                        { "top": "-" + ((position - 1) * itemHeight) + "px" },
                        0
                    );
                }
                else {
                    scrool_container.animate(
                        { "top": "-" + ((position - 1) * itemHeight) + "px" },
                        duration
                    );
                }

                if (false == true)
                    $("#div_image_catalog_aditional_left_button").html(position);
            }

            function startAutoMove(time) {
                if (timeoutId != null) {
                    clearTimeout(timeoutId);
                    timeoutId = null;
                }

                if (stopAutoMove == false)
                    timeoutId = setTimeout(function () { moveDown(); startAutoMove(time); }, time);
            }

            function sleepAutoMove(time) {
                stopAutoMove = true;

                if (timeoutId != null) {
                    clearTimeout(timeoutId);
                    timeoutId = null;
                }

                if (time > 0 && timeoutId == null)
                    setTimeout(function () { stopAutoMove = false; startAutoMove(sleepTime); }, time);
            }

            if (enableAutoScrool)
                startAutoMove(sleepTime);

        }
        catch (ex)
    { }

    });

}

/*** END MoebelBasen.Web.UI.WebControls.ScrollCatalog JavaScript ***/
