


$(window).load(function() {

    //TODO attach scroller to pet pics
    //NB! has to be in .ready() handler...
    $("div#makeMeScrollable").smoothDivScroll({
        autoScroll: "onstart",
        autoScrollDirection: "backandforth",
        autoScrollSpeed: 1,
        autoScrollInterval: 50
    });

    

});

$(window).load(function() {


    //    $("#makeMeScrollable").smoothDivScroll({
    //        autoScroll: "onstart",
    //        autoScrollDirection: "backandforth",
    //        autoScrollSpeed: 1
    //    });

    window.setInterval(getLatestActions, 10000);

    function getLatestActions() {

         
        baseUrl = 'http://' + top.location.hostname;
//        baseUrl = 'http://www.grolsch-wg.de';

        var last_fetched = $('.action_msg:first').attr('id');
        if ( parseInt( last_fetched ) > 0 ){
            $.get( baseUrl + "/eventlog/latest/1/"+last_fetched, function(data){
                if ( data.length > 0 ) {
                    newMsg = $(data);
                    animateScroller ( newMsg );
                }
            
            });
        }

    }

    function animateScroller ( newMsg ) {
        $('.action_msg:last').fadeOut(1000, function() {
            // Animation complete.
            $('.action_msg:last').remove();
            $('.action_msg').animate({
                top: '+=36'
            }, 500, function() {
                // Animation complete.
                $('.action_msg').css('top', 0);
                $('.scroller').prepend( newMsg );
                newMsg.fadeIn('slow', function() {
                    // Animation complete
                    });

            });

        });
    }


});




    function hideFlash ()
    {
        $('.movie_container').hide();
    }

    function showFlash ()
    {
        $('.movie_container').show();
    }




//for swf upload ===================================

/**
 * This function places the name of the file in the empty text box we created earlier.
 * This serves no actual functional purpose. It's just a visual effect.
 */
function fileQueued(file) {
    swfu.startUpload();
}

/*
 *
 */

function fileQueueError(file, errorCode, message) {
    
    if (message == 'File size exceeds allowed limit.') {
        alert( 'Diese Datei ist zu groß! 1MB sind erlaubt...');
    } else {
        alert( message );
    }

}
function fileQueueErrorSong(file, errorCode, message) {

    if (message == 'File size exceeds allowed limit.') {
        alert( 'Diese Datei ist zu groß! 8MB sind erlaubt...');
    } else {
        alert( message );
    }

}


/**
 * This function tells SWFUpload to start uploading the queued files.
 */
//function uploadFile(form, e) {
//    try {
//        swfu.startUpload();
//    } catch (ex) {
//        alert(ex);
//    }
//    return false;
//}
/**
 * This function reads progress information.
 * It reveals the progress bar and expands the inner div to reflect upload progress.
 */
function uploadProgress(file, bytesLoaded, bytesTotal) {
    try {
        var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
        $('#upload-progressbar-container').show();
        $('#upload-progressbar').width(percent+'%');
    } catch (e) {
        alert(e);
    }
}

/**
 * This function is huge.
 */
function uploadError(file, errorCode, message) {
    alert( 'error'+ errorCode );
}
/**
 * This function handles data that is returned from the PHP script.
 * That data is just whatever the PHP script echos to the page (no JSON or XML).
 * This function expects an ID representing the new file and places it
 * in the hidden field in the form.
 * It also sets the custom 'upload_successful' parameter to true for use later.
 */
function uploadSuccessThumb (file, serverData, receivedResponse) {
    //    alert(serverData);
    try {
        if ( serverData.substring(0,7) != 'http://') {
            if ( receivedResponse == true ) {
                var response = JSON.parse(serverData);
                alert( response.Filedata );
            }
        } else {
            $('#upload-progressbar-container').hide();
            //replace img src
            var old = $('#pic_img').attr('src');
            var newSrc = '';
            if ( old.indexOf('kein_foto') >= 0 ) { //no pic before
                newSrc = serverData.replace('_full', '_thumb')+ '?' + new Date().getTime();
            } else {
                newSrc = old.substring(0, old.indexOf( '?' )) + '?' + new Date().getTime();
            }
            $('#pic_img').attr('src', newSrc);
            $('#thumb_link').attr('href', serverData + '?' + new Date().getTime());
            $('#btn_pic_delete').show();
            complete_handler();
        }

    } catch (e) {
    }
}
function uploadSuccessSong (file, serverData, receivedResponse) {
    $('#upload-progressbar-container').hide();
    $('#path').val(serverData);
    $('#path_display').val(serverData.substr( serverData.lastIndexOf( '/' ) + 1 ));
}

function uploadSuccessProfil (file, serverData, receivedResponse) {
    try {
        if ( serverData.substring(0,7) != 'http://') {
            if ( receivedResponse == true ) {
                var response = JSON.parse(serverData);
                alert( response.Filedata );
            }
        } else {
            $('#upload-progressbar-container').hide();
            //replace img src
            var old = $('#profile_pic_img').attr('src');
            var newSrc = '';
            if ( old.indexOf('kein_foto') >= 0 ) { //no pic before
                newSrc = serverData.replace('_full', '_profil')+ '?' + new Date().getTime();
            } else {
                newSrc = old.substring(0, old.indexOf( '?' )) + '?' + new Date().getTime();
            }
            $('#profile_pic_img').attr('src', newSrc);
            $('#profile_pic_link').attr('href', serverData + '?' + new Date().getTime());
            $('#btn_pic_delete').show();
            complete_handler();
        }
 
    } catch (e) {
    }
}




/**
 * This file decides what do to after the entire form has been submitted.
 * It expects a JSON object with a 'message' and 'status' value.
 * The message value is always displayed to the user.
 * If the server returns a successful status, the upload and processing of form
 * data is complete and the user is redirected to another page.
 * If the server returns an unsuccessful status, the form submit action
 * is reconfigured to try submitted the form again rather than try the entire upload.
 */
function fileUploaded(json) {
    if (json.status == 1) {
        window.location = 'upload_successful.php';
    } else {
        form.setAttribute('onsubmit','submitForm(this);return false;');
    }
}
//end swfupload =====================
