var bahbas={
    init:function(){
        // Ensure DOM-compatibility
        if(!document.getElementById || !document.createTextNode){return;}
        bahbas.initSelectAwardForm();
        bahbas.initNominateForm();
        bahbas.initAutoFade();
        Shadowbox.init(Shadowbox.options);
    },
    initSelectAwardForm:function(){        
        var form = $("selectAwardCategory");
        if (!form){return;}
        var select = $$("#selectAwardCategory select");
        if (!select){return;}
        Event.observe(select[0], 'change', function(){form.submit();});
    },
    initNominateForm:function(){
        var form = $('nominate');
        if (!form){return;}
        form.observe('submit', bahbas.handleNomination);
    },
    handleNomination:function(e){
        // Don't trigger the form submission
        Event.stop(e);

        // Get the form etc.
        var form = Event.findElement(e, 'form');
        var url = form.action;
        var params = form.serialize(true);
        params.type = 'Ajax';
        
        // Disable the form
        form.disable();

        // Insert loading div
        var submit = form.getElementsByTagName('button')[0];
        new Insertion.Before(submit, '<div id="loading">Saving&hellip;</div>');

        // Hide the submit button
        submit.hide();

        // Create the request    
        var request = new Ajax.Request(
            url,
            {
                method: 'post',
                parameters: params,
                onSuccess: function(transport){
                    form.parentNode.innerHTML = transport.responseText;
                    bahbas.initNominateForm();
                },
                onFailure: function(){
                    alert('Sorry, there was a problem. Please try again.');
                    form.enable();
                    submit.show();
                },
                onComplete: function(){
                    var loading = $('loading');
                    if (loading){loading.remove();}
                }
            }
        );
    },
    initAutoFade:function(){
        if (typeof(flickrPhotos) == 'undefined'){return;}
        if (typeof(currentFlickrPhoto) == 'undefined'){currentFlickrPhoto = 0;}
        bahbas.photoReplacer = $$("a.photo_fade_replace");
        if (!bahbas.photoReplacer || !window.setTimeout){return;}
        bahbas.photoReplacer = bahbas.photoReplacer[0];
        // Pre-load the images
        bahbas.preLoads = [];
        for (var i = 0, j = flickrPhotos.length; i < j; i++) {
            bahbas.preLoads[i] = new Image();
            bahbas.preLoads[i].src = flickrPhotos[i].src;
        }
        bahbas.autoFadeDelay = 7000;
        bahbas.timeoutId = window.setTimeout(bahbas.autoFader, bahbas.autoFadeDelay);
    },
    autoFader:function(){
        if (currentFlickrPhoto == flickrPhotos.length - 1) {
            currentFlickrPhoto = 0;
        } else {
            currentFlickrPhoto++;
        }
        var img = bahbas.photoReplacer.firstChild;
        var fade = new Effect.Fade(
            img,
            {'afterFinish': function(){
                img.writeAttribute('src', flickrPhotos[currentFlickrPhoto].src);
                img.writeAttribute('alt', flickrPhotos[currentFlickrPhoto].title);
                bahbas.photoReplacer.writeAttribute('href', flickrPhotos[currentFlickrPhoto].page);
                bahbas.photoReplacer.writeAttribute('title', flickrPhotos[currentFlickrPhoto].title);
                var appear = new Effect.Appear(img);
            }}
        );
        bahbas.timeoutId = window.setTimeout(bahbas.autoFader, bahbas.autoFadeDelay);        
    }
}
Event.observe(window,'load',bahbas.init);
