
/*
* Main nav menu hover action
*/
var makeNav = function () {
    $('.subnav').hide();
    $('li.on .subnav').show();
    $('#nav > li').hover(function () {
        if (!$(this).hasClass('on')) {
            $(this).addClass('temp-on'); //give the class to highlight link
            $oldActive = $('#nav > li.on');
            $oldActive.find(' > ul').hide(); //Hide any currently showing links
            $oldActive.find(' > a').css('borderWidth', '7px');
            $(this).find('> ul').show(); //Show the links under the hover item
        }
    }, function () {
        if (!$(this).hasClass('on')) {
            $(this).removeClass('temp-on');
            $(this).find('> ul').hide();
            $oldActive = $('li.on');
            $oldActive.find('.subnav').show();
            $oldActive.find(' > a').css('borderWidth', '10px');
        }
    });
    if (!$('#nav > li.on').length) {
        $('#c_products').addClass('on');

        makeNav();


    }
};
/*
* Set the active item in the menu
*/
var setActiveItem = function (id) {
    $('#nav > li').removeClass('on');
    $("#c_" + id).addClass('on');
    $("#c_" + id).parent().parent().addClass('on');
    makeNav();
};

/*
*   Remove base URL in images for dev site
*/
var imgFix = function () {
    if(!window.location.href.indexOf('.shoplocal.com')) {
        $('img').each(function () {
            var src = $(this).attr('src');
            if (src.indexOf('.com') > -1) {
                newSrc = src.substring((src.indexOf('.com') + 4), src.length);
                $(this).attr('src', newSrc);
            }
        });
    }
}
$(document).ready(function () {

    imgFix();
    $('a.enlarge-modal').each(function () {
        var href = $(this).attr('href');
        if (href.indexOf('.com') > -1) {
            newHref = href.substring((href.indexOf('.com') + 4), href.length);
            $(this).attr('href', newHref);
        }
    });

    makeNav(); //Make the navigation menu initally

    /* Fudge the nav menu for the pop ups */
    $('#c_deliveryAreas > a').click(function (event) {
        event.preventDefault();
        $('#nav > li').removeClass('on');
        $(this).parent().addClass('on');
        //alert('c_deliveryAreasd Script.js' + this.href);
        window.open(this.href, 'DeliveryAreas', 'height=400,width=500,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no');
        makeNav();
        return false;
    });

    $('#c_yourArea > a').click(function (event) {
        event.preventDefault();
        $('#nav > li').removeClass('on');

        $('#c_deliveryAreas').addClass('on');
        //alert('c_yourArea Script.js' + this.href);
        window.open(this.href, 'DeliveryAreas', 'height=400,width=500,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no');
        makeNav();
        return false;
    });
    /****
    * Sub category list/grid view
    *****/
    // Change cursor
    $('.grid-view').hover(function () {
        $(this).css('cursor', 'pointer');
    }, function () {
        $(this).css('cursor', 'auto');
    });
    $('.list-view').hover(function () {
        $(this).css('cursor', 'pointer');
    }, function () {
        $(this).css('cursor', 'auto');
    });

    //Change view from/to list or grid
    $('.grid-view').click(function () {
        if ($('.product-list').hasClass('list')) {
            $('.product-list').removeClass('list');
            $(this).addClass('active');
            $('.list-view').removeClass('active');
            $('.catListView').css('display', 'none');
            //$('.catListView').css('visibility', 'hidden');
            $('.catGridView').css('display', 'inline');
            //$('.catGridView').css('visibility', 'visible');
        }
    });
    $('.list-view').click(function () {
        if (!$('.product-list').hasClass('list')) {
            $('.product-list').addClass('list');
            $(this).addClass('active');
            $('.grid-view').removeClass('active');
            $('.catListView').css('display', 'inline');
            //$('.catListView').css('visibility', 'visible');
            $('.catGridView').css('display', 'none');
            //$('.catGridView').css('visibility', 'hidden');


        }
    });


    /****
    * Temp: resize misc tables
    *****/
    if ($('.sidemenu').length) {
        $('table[width="760"]').width("600px");
    }

    /*****
    * Default text in textboxes
    *****/
    $(".defaultText").focus(function (srcc) {
        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });

    $(".defaultText").blur(function () {
        if ($(this).val() == "") {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });

    $(".defaultText").blur();

    /* Search and Category back to top link */

    if ($('#toTop').length) {
        $(window).scroll(function () {
            if ($(this).scrollTop() != 0) {
                $('#toTop').fadeIn();
            } else {
                $('#toTop').fadeOut();
            }
        });

        $('#toTop').click(function () {
            $('body,html').animate({ scrollTop: 0 }, 800);
        });
    }

    if ($('#toTopTwo').length) {
        var offset = $('.product-list .thumb-item:first').offset();
        if (offset) $('#toTopTwo').css('left', (offset.left - 125));
        $(window).scroll(function () {
            if ($(this).scrollTop() != 0) {
                $('#toTopTwo').fadeIn();
            } else {
                $('#toTopTwo').fadeOut();
            }
        });

        $('#toTopTwo').click(function () {
            $('body,html').animate({ scrollTop: 0 }, 800);
        });
    }
});



