//addDOMLoadEvent(setupSlideDisplay);
addDOMLoadEvent(setupNiftyCorners);
addDOMLoadEvent(setupAdminTrigger);
addDOMLoadEvent(setupLinkDeleteButtons);
addDOMLoadEvent(setupSortableSlides);
addDOMLoadEvent(setupSlideDisplay);

function setupSortableSlides()
{
    if ($('sortableSlides'))
    {
        $('sortableSlides').insert({after: '<p class="adminText">You can sort these slides - click and drag from the right side of each slide (the cursor will turn into a move cursor)</p>'});
        Sortable.create("sortableSlides", {
            onUpdate: function() {
                new Ajax.Request("ajax/sortableSlides.php", {
                    method: "post",
                    parameters: { data: Sortable.serialize("sortableSlides") }
                });
            }
        });
    }
}

function setupAdminTrigger()
{
    if($('showAdmin'))
    {
        $('showAdmin').insert({bottom: '&nbsp;| <a href="#" id="adminTrigger">Admin Login</a>'});
        Event.observe($('adminTrigger'), 'click', function(e)     {
            $('loginPanel').show();
            Event.stop(e);
        }, false);
    }
}

function setupSlideDisplay()
{
    setupNextSlideLink();
    setupPreviousSlideLink();
    //setupPreviousCategory();
    //setupNextCategory();
}

function setupNiftyCorners()
{
    var x = $$('ul.slideNav li');
    thisColor = $(x[0]).getStyle("background-color");
    $(x[0].setStyle("background-image: none"));
    if(!NiftyCheck())
        return;
    Rounded("ul.slideNav","all","#FFFFFF",thisColor,"smooth");
}

function setupNextSlideLink()
{
    if ($('nextSlide').down("a"))
    {
        Event.observe($('nextSlide').down("a"), 'click', function(e)     {
            Event.stop(e);
            hideArrows();
            new Effect.Parallel([
              new Effect.Move('slideArea', { sync: true, x: -100, y: 0, mode: 'relative' }),
              new Effect.Opacity('slideArea', { sync: true, from: 1, to: 0 })
            ], { 
              duration: 0.4
            });
            new Effect.Move('slideArea', { x: 200, y: 0, mode: 'relative', duration: 0, queue: 'end' });
            
            setTimeout('new Ajax.Updater(\'slideArea\', \'ajax/loadSlide.php\', { delay: 0.5, method:\'post\', evalScripts: true, parameters: {task: \'nextslide\', categoryId: $(\'currentCategory\').getValue(), slideId: $(\'currentSlide\').getValue()} }); hideArrows();', 400);
            new Effect.Parallel([
              new Effect.Move('slideArea', { sync: true, x: -100, y: 0, mode: 'relative' }),
              new Effect.Opacity('slideArea', { sync: true, from: 0, to: 1 })
            ], {
              duration: 0.4,
              delay: 0.1,
              queue: 'end'
            });

            setTimeout('showArrows();', 900);

            //new Effect.Opacity('slideArea', { from: 1, to: 0, duration: 0.2 });

        }, false);
    }
}

function hideArrows() {
    if ($('previousSlide')) $('previousSlide').setStyle({visibility: "hidden"});
    if ($('previousCategory')) $('previousCategory').setStyle({visibility: "hidden"});
    if ($('nextSlide')) $('nextSlide').setStyle({visibility: "hidden"});
    if ($('nextCategory')) $('nextCategory').setStyle({visibility: "hidden"});
}

function showArrows() {
    if ($('previousSlide')) $('previousSlide').setStyle({visibility: "visible"});
    if ($('previousCategory')) $('previousCategory').setStyle({visibility: "visible"});
    if ($('nextSlide')) $('nextSlide').setStyle({visibility: "visible"});
    if ($('nextCategory')) $('nextCategory').setStyle({visibility: "visible"});
}

function setupPreviousSlideLink()
{
    if ($('previousSlide').down("a"))
    {
        Event.observe($('previousSlide').down("a"), 'click', function(e)     {
            Event.stop(e);
            hideArrows();
            new Effect.Parallel([
              new Effect.Move('slideArea', { sync: true, x: 100, y: 0, mode: 'relative' }),
              new Effect.Opacity('slideArea', { sync: true, from: 1, to: 0 })
            ], {
              duration: 0.4
            });
            new Effect.Move('slideArea', { x: -200, y: 0, mode: 'relative', duration: 0, queue: 'end' });

            setTimeout('new Ajax.Updater(\'slideArea\', \'ajax/loadSlide.php\', { delay: 0.5, method:\'post\', evalScripts: true, parameters: {task: \'previousslide\', categoryId: $(\'currentCategory\').getValue(), slideId: $(\'currentSlide\').getValue()} })', 400);

            new Effect.Parallel([
              new Effect.Move('slideArea', { sync: true, x: 100, y: 0, mode: 'relative' }),
              new Effect.Opacity('slideArea', { sync: true, from: 0, to: 1 })
            ], {
              duration: 0.4,
              delay: 0.1,
              queue: 'end'
            });
            setTimeout('showArrows();', 900);

        }, false);
    }
}

function setupPreviousCategory()
{
    if ($('previousCategory').down("a"))
    {
        Event.observe($('previousCategory').down("a"), 'click', function(e)     {
            new Effect.Opacity('slideArea', { from: 1, to: 0, duration: 0.2 });
            setTimeout("new Ajax.Updater('slideArea', 'ajax/loadSlide.php', { method:'post', evalScripts: true, parameters: {task: 'previouscat', categoryId: $('currentCategoryId').getValue(), slideId: $('currentSlideId').getValue()} }); new Effect.Opacity('slideArea', { from: 0, to: 1, duration: 0.2 });", 200);
            Event.stop(e);
        }, false);
    }
}

function setupNextCategory()
{
    if ($('nextCategory').down("a"))
    {
        Event.observe($('nextCategory').down("a"), 'click', function(e)     {
            new Effect.Opacity('slideArea', { from: 1, to: 0, duration: 0.2 });
            setTimeout("new Ajax.Updater('slideArea', 'ajax/loadSlide.php', { method:'post', evalScripts: true, parameters: {task: 'nextcat', categoryId: $('currentCategoryId').getValue(), slideId: $('currentSlideId').getValue()} }); new Effect.Opacity('slideArea', { from: 0, to: 1, duration: 0.2 });", 200);
            Event.stop(e);
        }, false);
    }
}
