﻿$(document).ready(function () {
    var type = 'c';
    var codeCount = 1;
    $('.homelargebox .cat_text_link ul').makeacolumnlists({ cols: 4, colWidth: 140, equalHeight: false, startN: 1 });
    // break up ul of 4 li in a div
    $('.homelargebox .cat_img_link ul').each(function () {
        var id = $(this).attr('class');
        var idnum = codeCount;
        var $bigList = $('.homelargebox .cat_img_link ul.' + id),
            group = $bigList.find('li:lt(4)').remove();
        $('.homelargebox .cat_img_link').append('<div class="' + id + '" ><div class="coda-nav-left" id="coda-nav-left-' + idnum + '"><a href="#">&nbsp;</a></div><div class="coda-nav-right" id="coda-nav-right-' + idnum + '"><a href="#">&nbsp;</a></div><div class="slider" id="coda-slider-' + idnum + '"></div></div>'); //<div class="coda-nav" id="coda-nav-' + idnum + '"><ul/></div>
        var count = 1;
        while (group.length) {
            var ulgroup = $('<ul/>').append(group);
            var panel = $('<div class="panel" />').append(ulgroup);
            panel.appendTo('.homelargebox .cat_img_link .' + id + ' .slider');
            group = $bigList.find('li:lt(4)').remove();
            //$('<li><a href="#' + count + '">&nbsp;</a></li>').appendTo('#coda-nav-' + idnum + ' ul');
            //count = count + 1;
        }
        $('.homelargebox .cat_img_link ul.' + id).remove();
        $('#coda-slider-' + idnum).codaSlider({ dynamicArrows: false, dynamicTabs: true });
        codeCount++;
    });
    /*$('.homelargebox .cat_img_link img').each(function () {
    var text = $(this).attr('alt');
    $(this).parent().append('<div class="img_block"><span>' + text + '</span></div>');
    });*/
    $('.homelargebox .brand_text_link ul').makeacolumnlists({ cols: 4, colWidth: 140, equalHeight: false, startN: 1 });
    // break up ul of 4 li in a div
    $('.homelargebox .brand_img_link ul').each(function () {
        var id = $(this).attr('class');
        var idnum = codeCount;
        var $bigList = $('.homelargebox .brand_img_link ul.' + id),
            group = $bigList.find('li:lt(4)').remove();
        $('.homelargebox .brand_img_link').append('<div class="' + id + '" ><div class="coda-nav-left" id="coda-nav-left-' + idnum + '"><a href="#">&nbsp;</a></div><div class="coda-nav-right" id="coda-nav-right-' + idnum + '"><a href="#">&nbsp;</a></div><div class="slider" id="coda-slider-' + idnum + '"></div></div>'); //<div class="coda-nav" id="coda-nav-' + idnum + '"><ul/></div>
        var count = 1;
        while (group.length) {
            var ulgroup = $('<ul/>').append(group);
            var panel = $('<div class="panel" />').append(ulgroup);
            panel.appendTo('.homelargebox .brand_img_link .' + id + ' .slider');
            group = $bigList.find('li:lt(4)').remove();
            //$('<li><a href="#' + count + '">&nbsp;</a></li>').appendTo('#coda-nav-' + idnum + ' ul');
            //count = count + 1;
        }
        $('.homelargebox .brand_img_link ul.' + id).remove();
        $('#coda-slider-' + idnum).codaSlider({ dynamicArrows: false, dynamicTabs: true });
        codeCount++;
    });
    /*$('.homelargebox .brand_img_link img').each(function () {
    var text = $(this).attr('alt');
    $(this).parent().append('<div class="img_block"><span>' + text + '</span></div>');
    });*/


    var selectionChanged = function (e) {
        $('.homelargebox').show();
        $('.homeContent').hide();
        if (type == 'b') {
            $('.cat_img_link').hide();
            $('.cat_text_link').hide();
            $('.brand_img_link').show();
            $('.brand_text_link').show();
            $('.cat_link').removeClass('selected');
            $('.brand_link').addClass('selected');
        } else {
            $('.cat_img_link').show();
            $('.cat_text_link').show();
            $('.brand_img_link').hide();
            $('.brand_text_link').hide();
            $('.brand_link').removeClass('selected');
            $('.cat_link').addClass('selected');
        }
        // reset hidden
        $('.homelargebox .cat_img_link > div').hide();
        $('.homelargebox .cat_text_link ul').hide();
        $('.homelargebox .brand_img_link > div').hide();
        $('.homelargebox .brand_text_link ul').hide();
        $('.homelistbox .img_cat img').hide();
        if (e === undefined) {
            e = $('.homelistbox .active a');
        }
        if (e.length == 0) {
            e = $('a.link_1');
        }

        if (e.length != 0) {
            var id = $(e).attr('class');
            var text = $(e).text();
            id = parseInt(id.substring(id.indexOf("link_") + 5, id.length));
            $('.homelistbox li').attr('class', 'inactive');
            $('.homelistbox a.link_' + id).parent().attr('class', 'active');
            $('.homelargebox .cat_name').text(text);
            $('.homelistbox .img_cat img.img_' + id).show();
            if (type == 'b') {
                $('.homelargebox .brand_img_link div.img_' + id).show();
                $('.homelargebox .brand_text_link ul.ul_' + id).css('display', 'inline-block');
            } else {
                $('.homelargebox .cat_img_link div.img_' + id).show();
                $('.homelargebox .cat_text_link ul.ul_' + id).css('display', 'inline-block');
            }

            //alert(text);
        }
    }
    $('.homelargebox').hide();
    if ($('.homelistbox .active').length > 0)
        selectionChanged();

    $('.homelistbox a').click(function (e) {
        selectionChanged(this);
        e.preventDefault();
        return false;
    });

    $('.homelargebox .cat_link').click(function (e) {
        type = 'c';
        selectionChanged();
        e.preventDefault();
        return false;
    });
    $('.homelargebox .brand_link').click(function (e) {
        type = 'b';
        selectionChanged();
        e.preventDefault();
        return false;
    });

    $('.homelargebox .cat_img_link .panel a').hover(function () {
        var id = $(this).attr('class');
        $('.homelargebox .cat_text_link a.' + id).addClass('selecting');
        //alert($('.homelargebox .cat_text_link a.' + id).attr('class'));
    }, function () {
        var id = $(this).attr('class');
        $('.homelargebox .cat_text_link a.' + id).removeClass('selecting');
    });
    $('.homelargebox .brand_img_link .panel a').hover(function () {
        var id = $(this).attr('class');
        $('.homelargebox .brand_text_link a.' + id).addClass('selecting');
        //alert($('.homelargebox .cat_text_link a.' + id).attr('class'));
    }, function () {
        var id = $(this).attr('class');
        $('.homelargebox .brand_text_link a.' + id).removeClass('selecting');
    });

    $('.homelargebox .box_close').click(function (e) {
        $('.homelargebox').hide();
        $('.homeContent').show();
        $('.homelistbox .img_cat img').hide();
        $('.homelistbox .active').each(function () {
            $(this).removeClass('active');
            $(this).addClass('inactive');
        });
        e.preventDefault();
        return false;
    });
});
