// Uses http://jQuery.com

$(function()
{
   // Show/Hide vintage details
   $('.mod-producer-vintages > div.item > div.summary').toggle(
        function()
        {
            ShowVintage($(this));
        },
        function()
        {
            HideVintage($(this));
        }
    );
    
    // Prevent links from bubbling, but we still want to follow any links
    $('a.open-all').toggle(
        function(event)
        {
            event.stopPropagation();
            $('.mod-producer-vintages > div.item > div.summary').each(
                function() 
                {
                    $('a.open-all').addClass('close-all');
                    ShowVintage($(this));
                }
             );
        },
        function(event)
        {
            event.stopPropagation();
            $('.mod-producer-vintages > div.item > div.summary').each(
                function() 
                {
                    $('a.open-all').removeClass('close-all');
                    HideVintage($(this));
                }
             );
        }
     );
    
    // Hide/Show Producer
    $('h2.producer').toggle(
        function()
        {
            $(this).siblings('.mod-producer').slideUp(200);
            $(this).removeClass('open');
        },
        function()
        {
            $(this).siblings('.mod-producer').slideDown(200);
            $(this).addClass('open');
        }
    );
    
    // Hide/Show Products
    $('h2.products').toggle(
        function()
        {
            $(this).siblings('div.mod-producer-vintages').slideUp(200);
            $(this).removeClass('open');
        },
        function()
        {
            $(this).siblings('div.mod-producer-vintages').slideDown(200);
            $(this).addClass('open');
        }
    );
    
    // Show Vintage if pasted in on query line
    // See http://plugins.jquery.com/project/query-object for example
    var vintage = $.query.get('v');
    if (vintage > 0)
    {
        var target = $('.VintageId[value=' +vintage +']').parent();
        //Open Details
        target.click();
        //Scroll To
        $('html,body').animate({scrollTop: target.offset().top}, 100);
    }
});

function ShowVintage(objSummary)
{
    var objDetails = objSummary.siblings('div.details');
    objDetails.slideDown(400);
    objSummary.parent().addClass('open');

    // Check if we need load content
    if ( objDetails.children('div.vintage-image').length == 0 )
    {
        var vintageId = objSummary.find('.VintageId').val();
        var winaryId = $('#WineryId').val();
        objDetails.load("VintageDetail.aspx div.details > *", {w: winaryId, v: vintageId});
        
    }
}

function HideVintage(objSummary)
{
    objSummary.siblings('div.details').slideUp(400);
    objSummary.parent().removeClass('open');
}
