Event.observe(window, 'load', function() {
    if ($('emptyAlbum')) {
        $('emptyAlbum').observe('click', function(event) {
            Event.stop(event);
            var texte = translate("Êtes-vous sûr(e) de vouloir vider votre album ?");
            var response = !confirm(texte);
            if (response == false) {
                var url = '/visualprints/ajax/empty-session-vars/album/' + response + '/layout/true';
                new Ajax.Request(url, {
                    method: 'get',
                    onComplete: function () {
//                         setTop20Listeners();
                    },
                    asynchronous: false
                });
                var url = document.URL;
                var tmp = url.split('\/');
                var xprints = tmp[3];
                if (xprints != 'visualprint' &&  xprints != 'default' &&
                xprints != 'index' && xprints != 'edit' && xprints != 'visualprints'
                && xprints != 'ecommerce' && xprints != 'xprints'
                && xprints != 'maps' && xprints != 'admin' && xprints != 'page'
                && xprints != 'lang' && xprints != 'error' && xprints != 'block') {
                    xprints = '/' + xprints;
                } else {
                    xprints = '';
                }
                if (document.URL.match(/create-publication/)) {
                  document.location = xprints + '/visualprints/index/choose-pictures';
                } else if (document.URL.lastIndexOf('choose-pictures') != -1) {
                    var url = xprints + '/visualprints/ajax/update-right-col/setAlbum/1';
                } else {
                    var url = xprints + '/visualprints/ajax/update-right-col';
                }
                new Ajax.Updater('rightCol', url, {
                    method: 'get',
                    onComplete: function () {
                        setTop20Listeners();
                        if ($('okCreatePublication')) {
                            $('okCreatePublication').remove();
                        }
                    },
                    asynchronous: false
                });
            }
        });
    }

    if ($('top20')) {
        setTop20Listeners();
//         var projects = $('top20').getElementsByClassName('top20Picture');
//         for (var i = 0; i < projects.length; i++) {
//             $(projects[i]).observe('click', function(event) {
//                 Event.stop(event);
//                 var element = Event.findElement(event, 'a');
//                 var publicationId = element.readAttribute('publication');
//                 var url = '/visualprints/ajax/navigate-through-top20/id/' + publicationId + '/page/1';
//                 popupTop20(url);
//             });

//             $('seePreview').observe('click', function(event) {
//                 popupSlideshow('');
//                 var url_create_preview = '/visualprints/ajax/publication-create-preview';
//                 new Ajax.Request(url_create_preview, {
//                     method: 'post',
//                     onComplete: function(transport) {
//                         var response = transport.responseText;
//                         if (response == 'OK') {
//                             var url_preview = '/visualprints/ajax/publication-preview';
//                             popupSlideshow(url_preview);
//                         }
//                     }/*,
//                     asynchronous: false*/
//                 });
//                 Event.stop(event);
//             });
    }

    if ($('viewExampleContainer')) {
        setViewExampleListeners();
    }

    if ($('colorSelector')) {
        $('colorSelector').observe('change', function(event) {
            if ($('colorSelector').readAttribute('type') == 'font') {
                $('colorSelector').setStyle({color: $($F('colorSelector')).getStyle('color')});
            } else {
                $('colorSelector').setStyle({backgroundColor: $($F('colorSelector')).getStyle('background-color')});
            }
        });
    }
});

setTop20Listeners = function() {
    var projects = $('top20').getElementsByClassName('top20Picture');
    for (var i = 0; i < projects.length; i++) {
        $(projects[i]).observe('click', function(event) {
            Event.stop(event);
            var element = Event.findElement(event, 'a');
            var publicationId = element.readAttribute('publication');
            var url = '/visualprints/ajax/navigate-through-top20/id/' + publicationId + '/page/1';
            popupTop20(url);
        });
    }
};

setViewExampleListeners = function() {
    var projects = $('viewExampleContainer').getElementsByClassName('viewExample');
    for (var i = 0; i < projects.length; i++) {
        $(projects[i]).observe('click', function(event) {
            Event.stop(event);
            var element = Event.findElement(event, 'a');
            var publicationTypeId = element.readAttribute('publicationTypeId');
            var publicationId = getRandomExample(publicationTypeId);
            if (publicationId > 0) {
                var url = '/visualprints/ajax/navigate-through-top20/id/' + publicationId + '/page/1';
                popupTop20(url);
            } else {
                var publicationTypeName = element.readAttribute('publicationTypeName');
                var msg = publicationTypeName + ' : ' + translate("aucun exemple disponible");
                alert(msg);
            }
        });
    }
};

getRandomExample = function(publicationTypeId) {
    var url = '/visualprints/ajax/get-random-example/publicationTypeId/' + publicationTypeId;
    var publicationId = 0;
    new Ajax.Request(url, {
      method: 'get',
      onComplete: function(transport) {
          publicationId = transport.responseText;
      },
      asynchronous: false
    });
    return publicationId;
};

loadPublication = function(event, id) {
    var url = document.URL;
    var tmp = url.split('\/');
    var xprints = tmp[3];
    if (xprints != 'visualprint' && xprints != 'visualprints' && xprints != '')
        var url = '/'+xprints+'/ecommerce/account/load-ordered-publication/id/' + id;
    else
        var url = '/ecommerce/account/load-ordered-publication/id/' + id;
    new Ajax.Request(url, {
        method: 'get',
        onComplete: function() {
            if (xprints != 'visualprint' && xprints != 'visualprints' && xprints != '')
                document.location = '/'+xprints+'/visualprints/index/create-publication';
            else
                document.location = '/visualprints/index/create-publication';
        },
        asynchronous: false
    });
    Event.stop(event);
};

Event.observe(window, 'beforeunload', function(event) {
//     if (gStayOnSite == false) {
//         var texte = '';
//
//         texte += translate("Êtes-vous certain(e) de vouloir quitter le site de Visualprints ?");
//         texte += "\n";
//         texte += translate("Les projets non enregistrés ou partiellement réalisés ne seront pas conservés dans la mémoire pas votre navigateur");
//
//         var response = confirm(texte);
//     }
//     var url = '/visualprints/ajax/empty-session-vars/album/' + response + '/layout/' + response;
//     new Ajax.Request(url, {
//         method: 'get',
//         asynchronous: false
//     });
});
// window.onclose = alert('test');

popupTop20 = function(url) {
//     <div id="popup">
//         <div id="popupBackground"></div>
//         <div id="popupContent">
//             <a onclick="popupClose(event);"></a>
//              <div id="popupAjaxContent"></div>
//         </div>
//     </div>
//     if (url == '') { // init
//         var center3ColHeight = $('center3Col').getHeight();
//
//         var popupBgHeight = center3ColHeight + 3;
//         var popupHeight = center3ColHeight - 43;
//
//         var popup = Builder.node('div', {id: 'popup'}, [
//             Builder.node('div', {id: 'popupBackground', style: 'height: ' + popupBgHeight + 'px'}),
//             Builder.node('div', {id: 'popupContent', style: 'height: ' + popupHeight + 'px;'}, [
//                 Builder.node('a', {id: 'popupClose', onclick: 'popupClose(event)'}),
//                 Builder.node('div', {id: 'popupAjaxContent', style: 'height: 450px'}, [
//                     Builder.node('span', {id: 'waitingMessage'}, translate("Merci de patienter pendant la création de l'aperçu."))
//                 ])
//             ])
//         ]);
//
//         setTimeout(function() { new Effect.ScrollTo('pageHeader'); }, 1000);
//         $('center3Col').appendChild(popup);
//     }
//     else { // populate
//         new Ajax.Updater('popupAjaxContent', url, {
//             method: 'get'/*,
//             onComplete: function() {
//                 $('waitingMessage').remove();  supprime le message d'attente
//             }*/
//         });
//      }
    if ($('popup')) {
        $('popup').remove();
    }
    if ($('flashcontent')) {
        $('flashcontent').hide();
    }
    var centerCol = null;
    var popupContentStyle = '';
    var popupCloseStyle = '';
    if ($('center2Col')) {
        centerCol = $('center2Col');
        popupContentStyle = 'margin-left: 100px;';
        popupCloseStyle = 'right: 0;';
    } else if ($('center3Col')) {
        centerCol = $('center3Col');
        popupCloseStyle = 'position:absolute; top:0; right: 40px;';
    }
    var popupBgHeight = centerCol.getHeight() + 3;
    var popupBgWidth = centerCol.getWidth();
    var popupHeight = centerCol.getHeight() - 43;
    if ($('center2Col')) {
        var popupContentWidth = popupBgWidth - 120;
    } else if ($('center3Col')) {
        var popupContentWidth = popupBgWidth;
    }
    var popup = Builder.node('div', {id: 'popup'}, [
        Builder.node('div', {id: 'popupBackground', style: 'height: ' + popupBgHeight + 'px; width: ' + popupBgWidth + 'px;'}),
        Builder.node('div', {id: 'popupContent', style: 'height: ' + popupHeight + 'px; width: ' + popupContentWidth + 'px;' + popupContentStyle}, [
            Builder.node('a', {id: 'popupClose', onclick: 'popupClose(event)', style: popupCloseStyle}),
            Builder.node('div', {id: 'popupAjaxContent', style: 'width:560px;'})
        ])
    ]);
    centerCol.appendChild(popup);
    new Ajax.Updater('popupAjaxContent', url, {
        method: 'get',
        onComplete: function() {
            setTimeout(function() { new Effect.ScrollTo('pageHeader'); }, 1000);
        }
     });
};

loadTop20Page = function(event, url) {
    new Ajax.Updater('popupAjaxContent', url, {
        method: 'get'
    });
    Event.stop(event);
};

popupClose = function(event) {
    $('popup').remove();
    if ($('flashcontent')) {
        $('flashcontent').show();
    }
    Event.stop(event);
};
