$(document).ready(function(){

    if(typeof Cufon != "undefined")
    {
        Cufon.replace('h1', {
                hover: true
            });
    }

    createScrollbar();

    $('#body-front-carousel').cycle({
        fx: 'fade',
        cleartype:true,
        cleartypeNoBg: true,
        timeout: 6000
    });

    ddsmoothmenu.init({
        mainmenuid: "header-nav > div", //menu DIV selector
        orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
        classname: 'ddsmoothmenu', //class added to menu's outer DIV

        contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
    });

    var path = window.location.pathname.split('/'), pathLength = path.length;

    if(window.location.pathname == "/")
    {
        $(document.body).addClass('default-bg');
    }
    else if(path.length > 2)
    {   //product page
        var className = path[1];
        $(document.body).addClass(className + ' product');
    }
    else if(path.length == 2)
    {
        var className = path[1];
        $(document.body).addClass(className + ' catalog');
    }

    $('.productLarge .product-price img').hover(function(){
        $(this).attr('src', '/images/add-to-cart-hover.png');
    },function(){
        $(this).attr('src', '/images/add-to-cart.png');
    });
    // subcat view
    $('.list-subcat .title').click(function(){
        if($(this).next().css('display') == 'none')
        {
            $(this).next().css('display', 'block');
            $(this).parent().addClass('expanded');
        }
        else
        {
            $(this).next().css('display', 'none');
            $(this).parent().removeClass('expanded');
        }

        createScrollbar();
    });

    if(typeof path[2] != 'undefined')
    {
        $('.list-subcat.'+path[2] +' div.content').css('display', 'block');
        $('.list-subcat.'+path[2]).addClass('expanded');
        createScrollbar();
    }

    // create log in / log out link
    var $login = (parseInt($('#logged-in').html()) != 1) ?
        $('<li><a href="/login">Login</a></li>') :
        $('<li><a href="/LogOutProcess.aspx">Log out</a></li>');

    $('div.footer-links ul').append($login);

    if(!$('.cartSummaryLink').length)
    {
        createViewCart();
    }

    $('#no-spam').remove();
    var realEmail = $('#footer-email').html();
    $('#footer-email').html('<a href="mailto:'+realEmail+'">'+realEmail+'</a>');

    // add classes to each li element in product list
    $('.body-right-col-list-body ul li, #header-nav ul li').each(function(){
        var _class = $(this).children('a').attr('href').split('/');
        $(this).addClass(_class[_class.length-1]);
    });

    // scroll product list to correct element
    var curPage = path[path.length-1];
    if(curPage != '' && $('.body-right-col-list-body ul li.'+curPage).length)
    {
        $('.body-right-col-list-body ul li.'+curPage).addClass('selected');
    }

    if($(".jspPane li.selected").length)
    {
        $('.body-right-col-list-body').data('jsp').scrollToY($(".jspPane li.selected")[0].offsetTop);
    }

    // add selected class to top nav menu
    if(path.length > 2)
    {
        $('#header-nav .'+path[1]).addClass('selected');
        var title = $('#header-nav li.selected a').html();
        var newBreadcrumb = '<a href="'+
            $('#header-nav li.selected a').attr('href')+'">'+ title + '</a> / ' +
            $('.body-right-col-list-body ul li.selected a').html();
        $('#body-breadcrumb-holder').html(newBreadcrumb);
    }
    else
    {
        $('#body-breadcrumb-holder').html($('#header-nav li.selected a').html());
    }

    if($('#body-breadcrumb-holder').html() == '')
    {
        $('#body-breadcrumb-holder').html('Cart');
    }

    // keep title up to date
    if(curPage != '')
    {
        document.title = 'Viva la Body - ' + $('#header-nav li.selected a').html().replace('&amp;', '&')    ;
    }

    if($(document.body).hasClass('product'))
    {
        $('.body-col-content-body .description, .body-col-content-body .details').append('<fb:like href="" show_faces="false" width="450" font="arial"></fb:like>');

        $('#orig_price').html($('#orig_price').html().replace('$', ''));
    }
});

function createScrollbar()
{
    if($('.body-right-col-list-body, .body-product-description, .productLarge .description').length > 0)
    {
        $('.body-right-col-list-body').jScrollPane(
        	{
        		verticalDragMinHeight: 61,
        		verticalDragMaxHeight: 61,
        		showArrows: true,
        		horizontalGutter: 10
        	});
    }
}

function createViewCart()
{
    var $viewCart = $('<br/><a href="/OrderRetrievev2.aspx" class="cartSummaryLink">View Cart</a>');
    $('.cartSummaryItem').append($viewCart);
}

function template_checkout()
{
    if(parseInt($('#logged-in').html()) == 1)
    {
        $("#user-functions-col").css('display', 'block');
    }
    else
    {
        $('.body-checkout-middle div.grid_9').css('width', '100%');
    }

    $('#catshopclear').click(function(){
        $('#catCartSummary').html('<table cellspacing="0" class="cartSummaryTable"><tbody><tr><td class="cartSummaryItem">Shopping cart is empty.<br><a class="cartSummaryLink" href="/OrderRetrievev2.aspx">View Cart</a></td></tr></tbody></table>');
    });
}





























