/**
 * TRIDENT WINES
 *
 * Mario Parris <mario@brightsquareinc.com>
 */


/**
 * Function: popupItem()
 *
 *      Fetches the item Details via Ajax and shows a popup preview.
 */
function popupItem(element, itemID, useShortDescription) {

    // Check for simplemodal plugin.     
    if (!jQuery.modal) { return false; }

    $.ajax({
        type: 'POST',
        url: '/item_preview.php',
        data: {id: itemID },     
        dataType: 'json',
        success: function(data, textStatus, XMLHttpRequest) {
            var html = '';
            if ('image' in data) {
                html +=  '<img src="/ItemImages/' + data.image.name  + '">';
                html += '<div class="description"><h4>' + data.name + '</h4>';
            }
            else {
                html += '<div class="description" style="margin-left: 15px;"><h4>' + data.name + '</h4>';
            }


            if (useShortDescription) {
                html += data.shortdescription;
            }
            else {
                html += data.description;
            }
            
            html += '<div class="price">PRICE $' + data.price + ' (VAT INC) per bottle </div></div>'; 
           
            html = '<div id="item-preview">' + html + '</div>';

            // Build offset for popup.
            offset = $(element).offset();
            tableOffset = $('#items').offset();
            bottleSizeOffset = $('#items td.item-size').offset();

            //alert(offset.top);

            if (offset.top <= 615) {
                offset.top = tableOffset.top + 10;
            }
            else { 
                offset.top = "25%"; 
            }

            offset.left = bottleSizeOffset.left;

            // Show the popup.
            $.modal(html, {
                position: [offset.top, offset.left],
                transient: true,
                closeHTML: '<div>Close <span class="close-button">&times;</span></div>'
            });
        }
    });
}


/**
 * Function: showItem()
 *      Fetches the item details via AJAX, and displays a preview for the user.
 *
 * Parameters:
 *      itemid - _integer_ the ID of the item to display.
 *      useShortDescription - _boolean_ true shows the short description of the item (For Staff Picks etc.); false, uses the long description.  
 *
 */
function showItem(element, itemid, useShortDescription) {
    if (jQuery) {
        popupItem(element, itemid, useShortDescription);
        /*$.ajax({
            type: 'POST',
            url: '/item_preview.php',
            data: {id: itemid },     
            dataType: 'json',
            success: function(data, textStatus, XMLHttpRequest) {
                $preview = $('#item-preview');

                var html = '';
                if ('image' in data) {
                    html +=  '<img src="/ItemImages/' + data.image.name  + '">';
                    html += '<div class="description"><h4>' + data.name + '</h4>';
                }
                else {
                    html += '<div class="description" style="margin-left: 15px;"><h4>' + data.name + '</h4>';
                }


                if (useShortDescription) {
                    html += data.shortdescription;
                }
                else {
                    html += data.description;
                }
                html += '<div class="price">PRICE $' + data.price + ' (VAT INC) per bottle </div></div>'; 
                if ($('#item-preview').length) {
                    // The preview box exists, just update the contents.
                    $preview.html(html);    
                }
                else {
                    // It doesn't, boo!
                    html = '<div id="item-preview">' + html + '</div>';
                    $('#store').after(html);
                }
            }

        });*/
    }
}


function showProducer(producerid) {
    if (jQuery) {
        $.ajax({
            type: 'POST',
            url: 'producer_preview.php',
            data: {id: producerid},
            dataType: 'json',
            success: function(data, textStatus, XMLHttpRequest) {
                $preview = $('#producer-preview');
                var html = '<img src="/SiteAssets/"' + data.image + '">';
                html += '<div class="description"><h4>' + data.SECTIONINFO_NAME + '</h4>' + data.SECTIONINFO_DETAILS1;

                if ($preview.length) {
                    $preview.html(html);
                }
                else {
                    html = '<div id="producer-preview">' + html + '</div>';
                    $('#producers-list').after(html);
                }
            }
        });
    }
}

function createSortPopup() {
    if (jQuery) {

        $('#toggle-sort').bind('click', function() {
            var $menu = $('#sort-menu');

            if ($menu.css('display') == 'block') {
                $menu.hide();
            }
            else {
                $menu.show();
            }
        });

        /*
        $('#sort-popup').bind('click', function () {
            $popup = $('#popup-list');
            $(this).after($popup);
            $popup.show();
        });
        */
        /*
        $('#sort-popup > ul > li').bind('click', function (eventObject) {
            // Remove previous popped up elements.

            $('.popped').remove();
            // Needs to be 'outside' its container.
            $this = $(this);
            $child = $this.children('ul:first');
            $clone = $child.clone().addClass('popped');
            
            $clone.bind('click', function () {
                $(this).remove();    
            });

            $this.parent().after($clone).show();

        });
        */
    }
}

