﻿
function buyItWithScroll()  // scroll thumbs for product page
{
    var btnPrev = $('dl.buy-it-with dd.prev-btn');
    var btnNext = $('dl.buy-it-with dd.next-btn');

    var scrollUl = $("dl.buy-it-with ul");
    var countLi = $(scrollUl).children().size();
    var liWidth = $(scrollUl).children().width() + 12;
    var ulWidth = countLi * liWidth;
    $(scrollUl).css("width", ulWidth);

//    if (countLi > 7) {
//        $('dl.buy-it-with dd.inner').css("width", " ulWidthBig");
//    }
    
    var currentPos = Math.abs(parseInt($(scrollUl).css('left')));

    if (currentPos == 0) 
    {
        var btnPrev = $('dl.buy-it-with dd.btn-prev');
    }

    $(btnNext).click(function() 
    {
        var scrollWidth = $(scrollUl).width();
        var scrollAmount = scrollWidth - $(scrollUl).parent().width();
        var currentPos = Math.abs(parseInt($(scrollUl).css('left')));
        var remainingScroll = scrollAmount - currentPos;

        // Scroll liWidth + 4 px by default
        var nextScroll = liWidth - 4;

        if (remainingScroll < nextScroll) 
        {
            btnNext.fadeTo('800', 0.3); //hide("600");
        }
        if (currentPos < scrollAmount) 
        {
            // Scroll left
            $(scrollUl).animate({ 'left': '-=' + nextScroll }, '600');
            btnPrev.fadeTo('800', 1);
        }
        if (currentPos > 0) {
            btnPrev.show("600");
        }
    });

    $(btnPrev).click(function() 
    {
        var scrollWidth = $(scrollUl).width();
        var scrollAmount = scrollWidth - $(scrollUl).parent().width();
        var currentPos = Math.abs(parseInt($(scrollUl).css('left')));
        var remainingScroll = scrollAmount - currentPos;

        // Scroll liWidth - 4 px by default
        var nextScroll = liWidth - 4;

        if (currentPos > scrollAmount || currentPos !== 0) {
            // Scroll right
            $(scrollUl).animate({ 'left': '+=' + nextScroll }, '600');
            btnNext.fadeTo('800', 1); //hide("600");
        }
        if (currentPos == 0) {
            btnPrev.fadeTo('800', 0.3); //hide("600");
        }
    });
}

$(document).ready(function() {
    // add srolling for thumbs in product page
    buyItWithScroll();
    /***********************************************************/

    // HIDE THUMBS ARROWS AND SHOW WITH RIGHT POSITION
    var thumbsPrev = $(".thumb-box span.btn-prev");
    var thumbsNext = $(".thumb-box span.btn-next");
    thumbsPrev.hide();
    thumbsNext.hide();

    var countThumbs = $(".thumb-box li").length;
    switch (countThumbs) {
        case 1:
            break;
        case 2:
            //alert("dwa");
            thumbsPrev.css("top", "35px");
            thumbsNext.css("top", "35px");
            thumbsPrev.delay(1100).show();
            thumbsNext.delay(1100).show();
            break;
        default:
            thumbsPrev.delay(1100).show();
            thumbsNext.delay(1100).show();
    }
    /***********************************************************/

    var display = $("dd.next-dd").css("display");
    if (display == "none" ? true : false) {
        $("dd.low-dd").css("border-right", "none");
    }

    // PUTTING EQUAL HEIGHT FOR CONTENT & SIDEBAR
    var MainContentHeight = $(".menu-content").height();
    if (MainContentHeight == null) {
        MainContentHeight = $(".content").height();
    }
    var SidebarHeight = $(".sidebar").height();
    var CollectionMenuHeight = $("ul.collections-nav").height();
    var TafBoxHeight = 160;

    if ((SidebarHeight) < MainContentHeight) {
        $(".sidebar").css("height", MainContentHeight);
        $("div.taf-box").delay(800).show().css({ 'position': 'absolute', 'bottom': '0', 'left': '0' });
    }
    if (SidebarHeight > MainContentHeight) {
        $("div.taf-box").show();
    }

    // taf-box fadeTo 0.5 opacity onmouseover
    $("div.taf-box").hover(function() {
        $(this).fadeTo('800', 0.5);
    }, function() {
        $(this).fadeTo('800', 1);
    });
    /***********************************************************/

    var buyItWithWidthInner = $(".buy-it-with .inner").width();
    $(".buy-it-with").css("width", buyItWithWidthInner);

    /***********************************************************/
    // ul.chose-briefcase align to left 
    // if there is less than three 'li'
    var sizeUl = $("ul.chose-briefcase").children().size();
    if (sizeUl < 3) {
        $("ul.chose-briefcase li.select-concierge").css("margin", "0 0 10px");
    }
    /***********************************************************/

    // a.info-icon hide toolpit
    $('a.info-icon').find('span').hide();

    // info icon toolpit for tailoring
    // slideDown and fadeOut
    $('a.info-icon').hoverIntent(function() {
        $(this).find('span')
               .slideDown('slow');
    }, function() {
        $(this).find('span')
	           .fadeOut('fast');
    });

    // TABS NAVIGATION FOR PRODUCT PAGE
    $("ul.info-tab-body li").hide();
    $("ul.info-tab-body li:first").show();

    $("a.technical-click").click(function() {
        $("ul.info-tab-body li").hide();
        $("ul.info-tab-nav li.active").removeClass("active");
        $("ul.info-tab-body").find("li.technical").fadeIn();
        $(this).parent().addClass("active");
        //Ed, why do you hide li?
        $("li.ser").show(); //I can see the attributes now :)(Victor Antofica)
        return false;
    });

    $("a.details-click").click(function() {
        $("ul.info-tab-body li").hide();
        $("ul.info-tab-nav li.active").removeClass("active");
        $("ul.info-tab-body").find("li.details").fadeIn();
        $(this).parent().addClass("active");
        return false;
    });

    $("a.delivery-click").click(function() {
        $("ul.info-tab-body li").hide();
        $("ul.info-tab-nav li.active").removeClass("active");
        $("ul.info-tab-body").find("li.delivery").fadeIn();
        $(this).parent().addClass("active");
        return false;
    });
    /***********************************************************/

    // ADD SCROLLBAR FOR TAB NAVIGATION CONTENT
    $('ul.info-tab-body div.inner').jScrollPane({ scrollbarWidth: 8, scrollbarMargin: 15 });
    /***********************************************************/

    // PRIMARY NAVIGATION SLIDING DOWN/TOP
    function addMega() {
        $(this).find("ul")
               .stop(true, true)
               .slideDown('500')
               .end()
               .find("a:first")
               .addClass("selected");
    }

    function removeMega() {
        $(this).find("ul")
               .stop(true, true)
               .slideUp('500')
               .end()
               .find("a:first")
               .removeClass("selected");
    }

    var megaConfig = {
        interval: 200,
        sensitivity: 4,
        over: addMega,
        timeout: 200,
        out: removeMega
    };

    $(".primary-nav").find("> li").hoverIntent(megaConfig);
    /***********************************************************/
});
