$(document).ready(function () {

    if(Modernizr.csstransforms)
    {
        
        $('.gallery .media').scroller({itemClass: 'galleryline', perPage: 12, auto: false});
        $('.gallery').galleryDialog({});
        
        /*
        $('.galleryline a').click(function () {
            var dlg = $('<div class="dialog-container" style="display: none"></div>'),
                a = $(this),
                widget = null,
                nextButton = $('<a href="#">Next</a>');
                prevButton = $('<a href="#">Previous</a>');
                titlebar = null;
            $(document).append(dlg);

            dlg.load(a.attr('href'), {}, function (data){
                bindDialogEvents(dlg);
                dlg.dialog({
                    title: a.attr('title'),
                    modal: true,
                    dialogClass: 'gallery-dialog',
                    width: 700,
                    height: 700
                    });
                
                widget = dlg.dialog('widget');
                titlebar = $('.ui-dialog-titlebar', widget);
                
                if($('.next-image-link', dlg))
                {
                    titlebar.prepend(nextButton);
                    $(nextButton).button({icons: {secondary: 'ui-icon-triangle-1-e'}});
                    
                }
                
                if($('.prev-image-link', dlg))
                {
                    titlebar.prepend(prevButton);
                    $(prevButton).button({icons: {primary: 'ui-icon-triangle-1-w'}});
                }

                
                
                bindPageEvents(dlg);
            });            
            return false;

        });
        */

    }    
    
});

jQuery.fn.galleryDialog = function (opts)
{
    var nextLink = opts.nextLink || '.next-image-link',
        prevLink = opts.prevLink || '.previous-image-link',
        linkSelector = opts.linkSelector || 'a',
        dialogWidth = opts.dialogWidth || 700,
        dialogHeight = opts.dialogHeight || 700,
        dialogClass = opts.dialogClass || 'gallery-dialog',
        dlg = null,
        nextButton = $('<a href="#">Next</a>');
        prevButton = $('<a href="#">Previous</a>');
        obj = this;
        
    $(linkSelector, this).click(function () {
        obj.openDialog($(this).attr('href'));
        return false;
    });
        
    this.openDialog = function (href){
        var a = $(this),
            widget = null,
            titlebar = null;
            
        if(dlg === null)
        {
            dlg = $('<div class="dialog-container" style="display: none"></div>'),            
            $(document).append(dlg);
            dlg.dialog({
                    modal: true,
                    dialogClass: dialogClass,
                    width: dialogWidth,
                    height: dialogHeight,
                    autoOpen: false
            });

            widget = dlg.dialog('widget');
            titlebar = $('.ui-dialog-titlebar', widget);
            
            titlebar.prepend(nextButton);
            $(nextButton).button({icons: {secondary: 'ui-icon-triangle-1-e'}});
            
            $(nextButton).click(function () {
                obj.loadDialogContent($(this).attr('href'));
                return false;
            });
            
            titlebar.prepend(prevButton);
            $(prevButton).button({icons: {primary: 'ui-icon-triangle-1-w'}});
            
            $(prevButton).click(function () {
                obj.loadDialogContent($(this).attr('href'));
                return false;
            });
        }
        
        obj.loadDialogContent(href);

    };
    
    this.loadDialogContent = function (href){
        dlg.load(href, null, function (data){
            if($(nextLink, dlg).length > 0)
            {
                $(nextButton).show();
                $(nextButton).attr('href', $(nextLink, dlg).attr('href'));
                $(nextLink, dlg).hide();
            }
            else
            {
                $(nextButton).hide();
            }
            
            if($(prevLink, dlg).length > 0)
            {
                $(prevButton).show();
                $(prevButton).attr('href', $(prevLink, dlg).attr('href'));
                $(prevLink, dlg).hide();
            }
            else
            {
                $(prevButton).hide(); 
            }
            
            $(nextLink, dlg).hide();
            $(prevLink, dlg).hide();
            
            dlg.dialog('open');
            
        });
        
    };
}
