﻿$(document).ready(function () {

    //variables for navigating images on category pages
    var folder = "/App_Themes/Skin_1/images/";
    var images = [
        folder + "Alchemy_hero_images_crop_pages_Page_1.jpg",
        folder + "Alchemy_hero_images_crop_pages_Page_2.jpg",
        folder + "Alchemy_hero_images_crop_pages_Page_3.jpg",
        folder + "Alchemy_hero_images_crop_pages_Page_4.jpg",
        folder + "Alchemy_hero_images_crop_pages_Page_5.jpg",
    ];
    function preload(arrayOfImages) {
        $(arrayOfImages).each(function () {
            $('<img/>')[0].src = this;
        });
    }

    // Usage:

    preload([
     images
     ]);

    //indicate first phone field as required with asterisk
    $("#ctl00_PageContent_pnlAccountInfo span:eq(13)").prepend("*");

    //align thumbnails on product page to the left
    $(".changeChildImage > div").attr("align", "left");

    //remove extraneous tags after thumb nails
    if ($(".changeChildImage > div").has("br")) {
        $(".changeChildImage > div br").remove();
    }
    if ($(".changeChildImage > div img[src='images/spacer.gif']")) {
        $(".changeChildImage > div img[src='images/spacer.gif']").remove();
    }

    //create large image as opposed to medium images for product page
    $(".changeChildImage img").attr('src', function (index, attr) {
        return attr.replace('medium', 'large');
    });

    //disable popup for main image
    $(".changeChildImage > div > div > img").attr({ "style": "", "onclick": "" })

    //disable multiimages appearing as main image on product page
    $(".changeChildImage > div > img").attr("onclick", "");

    //create popup window when multiimages are clicked
    $(".changeChildImage > div > img").click(function () {
        window.open(this.src.replace('micro', 'large').replace('.jpg', '_.jpg'), '', 'width=500, height=500');
    });

    //slider for homepage
    $("#slides").cycle({
        fx: 'scrollHorz',
        speed: '750',
        timeout: '5000',
        next: '#nextarrow',
        prev: '#prevarrow'
    });

    //hide second search bar after first search
    $("#ctl00_PageContent_ctrlPageSearch").hide();

    //style footer based on content
    //$("#ctl00_PageContent_pnlContent > b ").parent().css("marginBottom", "200px");
    $("#ctl00_PageContent_pnlContent").not(":has('td')").not(":has('img')").css({ 'marginBottom': '200px', 'float': 'none', "paddingLeft": "115px" })

    //disable switching main image on product page when the "color" e.g. one of the drop-downs is changed
    $("select").attr("onchange", "");

    //hide first three levels of left navigation
    $(".aspnetMenu_Level1, .aspnetMenu_Level2, .aspnetMenu_Level3").css("display", "none");

    //adding classes to table elements in left navigation
    $('table tbody tr td a').filter(function () {
        return this.style.marginLeft == "32px";
    }).addClass('topLevel');
    $('table tbody tr td a').filter(function () {
        return this.style.marginLeft == "48px";
    }).addClass('secondLevel');
    $('table tbody tr td a').filter(function () {
        return this.style.marginLeft == "64px";
    }).addClass('thirdLevel');
    $(".secondLevel").parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().attr("class", "layerTwo");
    $(".thirdLevel").parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().attr("class", "layerThree");

    //adding additional classes to left nav
    $("#ctl00_aspnetMenu a").each(function () {
        if ($(this).text() == 'lounge chairs' || $(this).text() == 'occasional tables' || $(this).text() == 'media storage' || $(this).text() == 'ottomans') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                $(this).addClass("livingSubcategory");
            };
        }
        else if ($(this).text() == 'dining chairs' || $(this).text() == 'buffets' || $(this).text() == 'stools') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                $(this).addClass("diningSubcategory");
            }
        }
        else if ($(this).text() == 'beds') {
            $(this).addClass("bedroomSubcategory");
        }
        else if ($(this).text() == 'storage') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                if ($(this).attr("href") == "/c-31-storage.aspx") {
                    $(this).addClass("bedroomSubcategory");
                }
                else if ($(this).attr("href") == '/c-61-storage.aspx') {
                    $(this).addClass("officeSubcategory")
                }
                else {
                    $(this).addClass('livingSubcategory');
                }
            }
        }
        else if ($(this).text() == 'desks' || $(this).text() == 'office chairs') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                $(this).addClass("officeSubcategory");
            }
        }
        else if ($(this).text() == 'bedroom' || $(this).text() == 'dining' || $(this).text() == 'media' || $(this).text() == 'office') {
            /*if ($(this).hasClass("secondLevel")) {
            $(this).addClass("storageSubcategory");
            }*/
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                $(this).addClass("storageSubcategory");
            }
        }
        else if ($(this).text() == 'living') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                if ($(this).attr("href") == "/c-41-living.aspx") {
                    $(this).addClass("storageSubcategory");
                }
            }
        }
        else if ($(this).text() == 'lighting' || $(this).text() == 'mirrors' || $(this).text() == 'rugs' || $(this).text() == 'coat racks') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                $(this).addClass("accessoriesSubcategory");
            }
        }
        else if ($(this).text() == 'lounge' || $(this).text() == 'chairs') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                $(this).addClass("outdoorSubcategory");
            }
        }
        else if ($(this).text() == 'tables') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                if ($(this).attr("href") == "/c-50-tables.aspx") {
                    $(this).addClass("outdoorSubcategory");
                }
                else if ($(this).attr("href") == '/c-14-tables.aspx') {
                    $(this).addClass("diningSubcategory")
                }
                else {

                }
            }
        }
        else if ($(this).text() == 'sofas') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                if ($(this).attr('href') == '/c-9-sofas.aspx') {
                    $(this).addClass("livingSubcategory");
                }
                else if ($(this).attr("href") == '/c-52-sofas.aspx') {
                    $(this).addClass("outdoorSubcategory")
                }
                else { }
            }
        }
        else if ($(this).text() == 'sectional' || $(this).text() == 'console' || $(this).text() == 'consoles') {
            $(this).remove();
        }
        else if ($(this).text() == 'wall units') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                if ($(this).attr("href") == "/c-25-wall-units.aspx") {
                    $(this).addClass("livingSubcategory");
                }
                else {
                    $(this).addClass("storageSubcategory");
                }
            }
        }
        else if ($(this).text() == 'bookshelves') {
            if ($(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().hasClass("layerTwo")) {
                if ($(this).attr("href") == "/c-54-bookshelves.aspx") {
                    $(this).addClass("livingSubcategory");
                }
                else {
                    $(this).addClass("storageSubcategory");
                }
            }
        }
        else if ($(this).text() == 'cabinets' && $(this).attr('href') == '/c-69-cabinets.aspx') {
            $(this).remove();
        }
        else { }
    });

    //variables for hiding left navigation
    var pathname = window.location.pathname;
    var dynamicMenu = "#ctl00_aspnetMenu";
    var allSubcategories = ".livingSubcategory,  .diningSubcategory, .bedroomSubcategory, .officeSubcategory, .storageSubcategory, .accessoriesSubcategory, .outdoorSubcategory";
    $(allSubcategories).hide();

    //hide parts of the left navigation
    $("tr.layerThree").each(function () {
        $(this).hide();
    });

    //if the url address contains 'c-' which indicates a category page
    if (pathname.indexOf("/c-") > -1) {

        //create header image for category pages
        $("#content > img:first-child").attr("src", images[Math.round(Math.random() * images.length)]);
        $("#content > img:first-child").show();
        //display left navigation depending on page
        var id = parseInt(pathname.match(/\d+/));
        switch (id) {
            case 1: case 9: case 10: case 18:
            case 19: case 11: case 13: case 20:
            case 21: case 22: case 23: case 25:
            case 53: case 54: case 65:
                $('.livingSubcategory').show();
                break;
            case 2: case 14: case 26: case 27:
            case 28: case 29: case 66:
                $('.diningSubcategory').show();
                break;
            case 3: case 30: case 31: case 32:
            case 33: case 34: case 35:
                $('.bedroomSubcategory').show();
                break;
            case 4: case 15: case 36: case 61:
                $('.officeSubcategory').show();
                break;
            case 5: case 38: case 39: case 40: case 41:
            case 56: case 57: case 58: case 59:
            case 62: case 63: case 64: case 70:
                $('.storageSubcategory').show();
                break;
            case 6: case 49: case 50: case 51: case 52:
                $(".outdoorSubcategory").show();
                break;
            case 7: case 42: case 43: case 44:
            case 45: case 46:
            case 47: case 48: case 55:
                $('.accessoriesSubcategory').show();
                break;
            /*
            case "/c-1-living.aspx": case "/c-9-sofas.aspx": case "/c-10-lounge-chairs.aspx?pagenum=1": case "/c-10-lounge-chairs.aspx?pagenum=2": case "/c-18-occasional-tables.aspx":
            case "/c-19-storage.aspx": case "/c-11-sleepers.aspx": case "/c-13-sofas.aspx": case "/c-20-sectionals.aspx":
            case "/c-21-coffee-tables.aspx": case "/c-22-side-tables.aspx": case "/c-23-console-tables.aspx": case "/c-25-wall-units.aspx":
            case "/c-53-media-storage.aspx": case "/c-54-bookshelves.aspx": case "/c-65-ottomans.aspx":
            $('.livingSubcategory').show();
            break;
            case "/c-2-dining.aspx": case "/c-14-tables.aspx": case "/c-26-dining-chairs.aspx": case "/c-27-buffets.aspx":
            case "/c-28-extension.aspx?pagenum=1": case "/c-28-extension.aspx?pagenum=2": case "/c-29-non-extension.aspx": case "/c-66-stools.aspx":
            $('.diningSubcategory').show();
            break;
            case "/c-3-bedroom.aspx": case "/c-30-beds.aspx": case "/c-31-storage.aspx": case "/c-32-nightstands.aspx":
            case "/c-33-dressers.aspx": case "/c-34-wardrobes.aspx": case "/c-35-closets.aspx":
            $('.bedroomSubcategory').show();
            break;
            case "/c-4-office.aspx": case "/c-15-desks.aspx": case "/c-36-office-chairs.aspx": case "/c-61-storage.aspx":
            $('.officeSubcategory').show();
            break;
            case "/c-5-storage.aspx": case "/c-38-dining.aspx": case "/c-39-bedroom.aspx": case "/c-40-office.aspx": case "/c-41-living.aspx":
            case "/c-56-nightstands.aspx": case "/c-57-dressers.aspx": case "/c-58-wardrobes.aspx": case "/c-59-closets.aspx":
            case "/c-62-bookshelves.aspx": case "/c-63-media-storage.aspx": case "/c-64-wall-units.aspx": case "/c-70-cabinets.aspx":
            $('.storageSubcategory').show();
            break;
            case "/c-6-outdoor.aspx": case "/c-49-lounge.aspx": case "/c-50-tables.aspx": case "/c-51-chairs.aspx": case "/c-52-sofas.aspx":
            $(".outdoorSubcategory").show();
            break;
            case "/c-7-accessories.aspx": case "/c-42-rugs.aspx": case "/c-43-lighting.aspx": case "/c-44-mirrors.aspx":
            case "/c-45-suspension.aspx?pagenum=1": case "/c-45-suspension.aspx?pagenum=2": case "/c-46-floor.aspx?pagenum=1": case "/c-46-floor.aspx?pagenum=2":
            case "/c-47-table.aspx": case "/c-48-wallceiling.aspx": case "/c-55-coat-racks.aspx":
            $('.accessoriesSubcategory').show();
            break;
            */ 
            default:
                $(allSubcategories).hide();
        }

        $("#bodyWrapper").hide();

        //css to float elements to the left
        //$("#ctl00_aspnetMenu, #ctl00_PageContent_pnlContent").css("float", "left");

        //other css
        //$("#ctl00_PageContent_pnlContent").css({ "width": "80%", "marginLeft": "5%" });

        //more css
        $("#content > div > td").width("20%");

        $(dynamicMenu).children().css({ "fontSize": "1.1em", "lineHeight": "1.5em" });

        //$("a.subcatDrilldown").css("fontSize", "1.1em");
    }
    //display left nav for product pages
    else if (pathname.indexOf("/p-") > -1) {
        //display left navigation
        //variables
        var referer = $(".referer").text();
        var id = parseInt(referer.match(/\d+/));
        switch (id) {
            case 1: case 9: case 10: case 18:
            case 19: case 11: case 13: case 20:
            case 21: case 22: case 23: case 25:
            case 53: case 54: case 65:
                $('.livingSubcategory').show();
                break;
            case 2: case 14: case 26:
            case 27:
            case 28: case 29: case 66:
                $('.diningSubcategory').show();
                break;
            case 3: case 30: case 31: case 32:
            case 33: case 34: case 35:
                $('.bedroomSubcategory').show();
                break;
            case 4: case 15: case 36: case 61:
                $('.officeSubcategory').show();
                break;
            case 5: case 38: case 39: case 40:
            case 41: case 56: case 57: case 58:
            case 59: case 62: case 63: case 64:
            case 70:
                $('.storageSubcategory').show();
                break;
            case 6: case 49: case 50:
            case 51: case 52:
                $(".outdoorSubcategory").show();
                break;
            case 7: case 42: case 43: case 44:
            case 45: case 46: case 47: case 48:
            case 55:
                $('.accessoriesSubcategory').show();
                break;
            /*
            case baseUrl + "/c-1-living.aspx": case baseUrl + "/c-9-sofas.aspx": case baseUrl + "/c-10-lounge-chairs.aspx": case baseUrl + "/c-18-occasional-tables.aspx":
            case baseUrl + "/c-19-storage.aspx": case baseUrl + "/c-11-sleepers.aspx": case baseUrl + "/c-13-sofas.aspx": case baseUrl + "/c-20-sectionals.aspx?pagenum=1":
            case baseUrl + "/c-20-sectionals.aspx?pagenum=2":
            case baseUrl + "/c-21-coffee-tables.aspx?pagenum=1": case baseUrl + "/c-22-side-tables.aspx": case baseUrl + "/c-23-console-tables.aspx": case baseUrl + "/c-25-wall-units.aspx":
            case baseUrl + "/c-53-media-storage.aspx": case baseUrl + "/c-54-bookshelves.aspx": case baseUrl + "/c-65-ottomans.aspx": case baseUrl + "/c-21-coffee-tables.aspx?pagenum=2":
            $('.livingSubcategory').show();
            break;
            case baseUrl + "/c-2-dining.aspx": case baseUrl + "/c-14-tables.aspx": case baseUrl + "/c-26-dining-chairs.aspx?pagenum=1":
            case baseUrl + "/c-26-dining-chairs.aspx?pagenum=2": case baseUrl + "/c-26-dining-chairs.aspx?pagenum=3": case baseUrl + "/c-27-buffets.aspx":
            case baseUrl + "/c-28-extension.aspx": case baseUrl + "/c-29-non-extension.aspx": case baseUrl + "/c-66-stools.aspx":
            $('.diningSubcategory').show();
            break;
            case baseUrl + "/c-3-bedroom.aspx": case baseUrl + "/c-30-beds.aspx": case baseUrl + "/c-31-storage.aspx": case baseUrl + "/c-32-nightstands.aspx":
            case baseUrl + "/c-33-dressers.aspx": case baseUrl + "/c-34-wardrobes.aspx": case baseUrl + "/c-35-closets.aspx":
            $('.bedroomSubcategory').show();
            break;
            case baseUrl + "/c-4-office.aspx": case baseUrl + "/c-15-desks.aspx": case baseUrl + "/c-36-office-chairs.aspx": case baseUrl + "/c-61-storage.aspx":
            $('.officeSubcategory').show();
            break;
            case baseUrl + "/c-5-storage.aspx": case baseUrl + "/c-38-dining.aspx": case baseUrl + "/c-39-bedroom.aspx": case baseUrl + "/c-40-office.aspx":
            case baseUrl + "/c-41-living.aspx": case baseUrl + "/c-56-nightstands.aspx": case baseUrl + "/c-57-dressers.aspx": case baseUrl + "/c-58-wardrobes.aspx":
            case baseUrl + "/c-59-closets.aspx": case baseUrl + "/c-62-bookshelves.aspx": case baseUrl + "/c-63-media-storage.aspx": case baseUrl + "/c-64-wall-units.aspx":
            case baseUrl + "/c-70-cabinets.aspx":
            $('.storageSubcategory').show();
            break;
            case baseUrl + "/c-6-outdoor.aspx": case baseUrl + "/c-49-lounge.aspx": case baseUrl + "/c-50-tables.aspx":
            case baseUrl + "/c-51-chairs.aspx?pagenum=1": case baseUrl + "/c-51-chairs.aspx?pagenum=2": case baseUrl + "/c-52-sofas.aspx":
            $(".outdoorSubcategory").show();
            break;
            case baseUrl + "/c-7-accessories.aspx": case baseUrl + "/c-42-rugs.aspx": case baseUrl + "/c-43-lighting.aspx": case baseUrl + "/c-44-mirrors.aspx":
            case baseUrl + "/c-45-suspension.aspx": case baseUrl + "/c-46-floor.aspx": case baseUrl + "/c-47-table.aspx": case baseUrl + "/c-48-wallceiling.aspx":
            case baseUrl + "/c-55-coat-racks.aspx":
            $('.accessoriesSubcategory').show();
            break;
            */ 
            default:
                $(allSubcategories).hide();
                $(dynamicMenu).hide();
        }

        //hide some of the "buy" elements on the product page
        $("a[onclick]").hide();

        $("td > br").remove();

        $("#bodyWrapper").hide();

        //css to float elements to the left
        //$("#ctl00_aspnetMenu, #ctl00_PageContent_pnlContent").css("float", "left");

        //other css
        //$("#ctl00_PageContent_pnlContent").css({ "width": "80%", "marginLeft": "5%" });

        //more css
        //$("#content > div > td").width("20%");

        $(dynamicMenu).children().css({ "fontSize": "1.1em", "lineHeight": "1.5em" });

        //$("a.subcatDrilldown").css("fontSize", "1.1em");
        $("table#ctl00_aspnetMenu").css("width", "20%");

        var pageIndex = referer.indexOf("pagenum") - 1;
        if (pageIndex > 0) {
            referer = referer.substring(33, pageIndex);
        }
        else {
            referer = referer.substring(33);
        }
        $("#ctl00_aspnetMenu a").each(function () {
            if ($(this).attr("href") == referer) {
                if ($(this).hasClass("thirdLevel")) {
                    $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().show().addClass("currentLink");
                    $('.currentLink').find('a').show();
                    $(this).addClass("currentLink");
                }
                else if ($(this).hasClass('secondLevel')) {
                    $(this).addClass('currentLink');
                    $('.currentLink').parent().parent().parent().parent().parent().parent().next().find('a').show();
                }
            }
        });
    }
    else if (pathname.indexOf('/t-') > -1) {
        //create header image for category pages
        $("#content > img:first-child").attr("src", images[Math.round(Math.random() * images.length)]);
        $("#content > img:first-child").show();

        //remove 200px margin-bottom
        $("#ctl00_PageContent_pnlContent").css("marginBottom", "0");
    }
    /*else if (pathname.indexOf('manufacturer') > -1 )
    {
    //create header image for category pages
    $("#content > img:first-child").attr("src", images[Math.round(Math.random() * images.length)]);
    $("#content > img:first-child").show();
    }*/
    else if (pathname.indexOf('search') > -1) {
        $("#ctl00_PageContent_pnlContent").css("width", "100%");
        $(dynamicMenu).hide();
    }
    else if (pathname.indexOf('ContactUs.aspx') > -1) {
        $("#content > img:first-child").attr("src", images[Math.round(Math.random() * images.length)]);
        $("#content > img:first-child").show();
    }
    else {
        //hide the leftnav
        $(dynamicMenu).css("display", "none");
    }

    //disable classes in the left navigation
    /*$("table tr a").attr({
    "class": ""
    });*/

    //variable
    var currentLevel = 1;


    //more variables
    var currentTop;
    var currentSecond;
    var currentThird;

    //control left navigation functionality
    /*$(".topLevel").click(function () {
    var lastTop = currentTop;
    currentTop = $(this).parent().parent().parent().parent().parent().parent();
    if (lastTop && lastTop[0] == currentTop[0]) {
    if (currentSecond && currentSecond.hasClass("layerTwo")) {
    currentSecond.hide();
    if (currentThird) {
    currentThird.hide();
    }
    }
    else {
    currentSecond = currentTop.next().hide();
    if (currentThird) {
    currentThird.hide();
    }
    currentThird = null;
    }
    currentSecond = null;
    currentTop = null;
    return false;
    }
    else {
    if (currentTop.next().hasClass("layerTwo")) {
    if (currentSecond) {
    lastTop.next().hide();
    if (currentThird) {
    currentThird.hide();
    currentThird = null;
    }
    }

    currentSecond = currentTop.next();
    currentSecond.show();
    return false;
    }
    return true;
    }
    });*/

    $(".secondLevel").click(function () {
        var lastSecond = currentSecond;
        currentSecond = $(this).parent().parent().parent().parent().parent().parent();
        if (lastSecond && lastSecond[0] == currentSecond[0]) {
            currentSecond = null;
            if (currentThird) {
                //currentThird.hide();
            }
            return false;
        }
        else {
            if (currentSecond.next().hasClass("layerThree")) {
                if (currentThird) {
                    //currentThird.hide();
                }
                /*currentThird = currentSecond.next();
                currentThird.show();*/
                return true;
            }
            return true;
        }
    });

    //show third level categories based on current page
    $("#ctl00_aspnetMenu a").each(function () {
        if ($(this).attr("href") == pathname) {
            if ($(this).hasClass("secondLevel")) {
                currentSecond = $(this).parent().parent().parent().parent().parent().parent();
                currentThird = currentSecond.next();
                currentThird.show();
                $(this).addClass("currentLink");
                $('.currentLink').parent().parent().parent().parent().parent().parent().next().find('a').show();
            }
            else if ($(this).hasClass("thirdLevel")) {
                $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().show().addClass("currentLink");
                $('.currentLink').find('a').show();
                $(this).addClass("currentLink");
            }
        }
    });

});

//watermark text for search field
$.watermark.options =
{
    className: 'searchWatermark',
    useNative: false
}

jQuery(function ($) {
    $("#ctl00_ctrlSearch_SearchText").watermark("search");
});
