/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {

    // on ajax start - funzione che viene richiamata durante una chiamata ajax con jquery
    $("#loadingAjaxRequest").ajaxStop(function(){$(this).hide();});
    $("#loadingAjaxRequest").ajaxStart(function(){$(this).show();});


    // inizializza il datepicker nelle input aventi classe = dateInput
    initDatepicker();


    // load degli slider
    $(".scrollable").each(function() {
        $(this).scrollable({
            size: 4,
            hoverClass: "hover",
            keyboard: false,
            items: '#thumbs, #user-media-files',
            prev: "#scrollPrev",
            next: "#scrollNext"
        })
    });
    $(".scrollable_3").each(function() {
        $(this).scrollable({
            size: 3,
            hoverClass: "hover",
            keyboard: false,
            items: '#thumbs, #user-media-files',
            prev: "#scrollPrev",
            next: "#scrollNext"
        })
    });

    $.ui.dialog.defaults.bgiframe = true;

    // dialog segnalazione contenuti
    initDialogReportContent();

    // dialog backend
    initDialogWindows();

    // load di tutti i player
    $(".videoPlayer").each(function() {
        flowplayer($(this).attr("id"), contextPath+"/js/flowplayer-3.1.1.swf", {
//            plugins:  {
//                controls: null
//            },
            clip:  {
                autoPlay: false,
                autoBuffering: true
            }
        });
    });

    $("input").addClass("ui-corner-all");
    $(".button,.smallButton").mouseover(function() {
        $(this).addClass("ui-state-hover")
        });
    $(".button,.smallButton").mouseout(function() {
        $(this).removeClass("ui-state-hover")
        });
    $("select").addClass("ui-corner-all");
//    $(".userPic").addClass("ui-corner-all");
//    $(".userPicHigh").addClass("ui-corner-all");
//    $(".userPicSmall").addClass("ui-corner-all");


    initTooltip(".tooltip");

});

function initTooltip(ref) {
    $(ref).each(function() {
        $(this).tooltip({
            track: true,
            delay: 300,
            showURL: false,
            opacity: 10
        });
    });
}

function initDialogReportContent() {
    $('#dialog-segnalazione').dialog({
        autoOpen: false,
        modal: true,
        bgiframe: true,
        height: 250,
        width: 400,
        draggable: false,
        resizable: false,
        stack: true,
        beforeclose: function() {
            $("object").each(function() {
                $(this).show();
            });
        }
    });

    $('#dialog-messaggio').dialog({
        autoOpen: false,
        modal: true,
        bgiframe: true,        
        height: 400,
        width: 400,
        draggable: false,
        resizable: false,
        stack: true,
        beforeclose: function() {
            $("object").each(function() {
                $(this).show();
            });
        },
        close: function(){
            location.reload(true);
        }
    });

    $('#dialog-richiedi-album').dialog({
        autoOpen: false,
        modal: true,
        bgiframe: true,
        height: 600,
        width: 515,
        draggable: false,
        resizable: false,
        stack: true,
        beforeclose: function() {
            $("object").each(function() {
                $(this).show();
            });
        }
    });
}

function initDialogWindows() {
    
    $(".dialogWindows").each(function() {

        $(this).dialog( 'destroy' );
        $(this).dialog({
            bgiframe: true,
            autoOpen: false,
            width: 700,
            beforeclose: function() {
                $("object").each(function() {
                    $(this).show();
                });
            }
        });
    });
}

function initDatepicker() {
    $('.dateInput').datepicker({
        dateFormat:'dd-mm-yy',
        changeMonth:true,
        changeYear:true,
        yearRange: '-100:+100'
    });
}

function playVideoId(id) {
    flowplayer(id, contextPath+"/js/flowplayer-3.1.1.swf", {
        clip:  {
            autoPlay: true,
            autoBuffering: true
        }
    });
}

/**
 * Apre la dialog dell'oggetto che corrisponde al reference passato in input
 */
function openDialog(reference, title, width, height, modal, draggable) {

    initDatepicker();

    $("object").each(function() {
        $(this).hide();
    });
    var dialog = $(reference).dialog('option', 'title', title);

    //dialog.bind('dialogclose', function(event, ui) {
    //    dialog.dialog('destroy');
    //});

    if (width)
        dialog.dialog('option', 'width', width);
    if (height)
        dialog.dialog('option', 'height', height);
    if (modal != undefined)
        dialog.dialog('option', 'modal', modal);
    if (draggable != undefined)
        dialog.dialog('option', 'draggable', draggable);

    dialog.dialog('open');
}


function segnalaContenuto(id,tipo){
    $("object").each(function() {
        $(this).hide();
    });
    $('#dialog-segnalazione').html('<iframe frameborder="0" style="width:99%;height:99%;" src="utente?action=init_segnala_contenuto&tipo='+tipo+'&contenuto='+id+'"></iframe>').dialog({ title: 'Segnala violazione' }).dialog('open');
}

function inviaMessaggio(to){
    $("object").each(function() {
        $(this).hide();
    });
    var src = "";
    if (typeof to=="undefined"){
        src = "utente?action=init_invia_messaggio";
    } else {
        src="utente?action=init_invia_messaggio&utenteTo="+to;
    }
    $('#dialog-messaggio').html('<iframe frameborder="0" style="width:99%;height:99%;" src='+src+'></iframe>').dialog({title: 'Invia messaggio' }).dialog('open');
}

function richiediAlbum(){
    $("object").each(function() {
        $(this).hide();
    });
    var src = "guest?action=init_richiesta_album";
    $('#dialog-richiedi-album').html('<iframe frameborder="0" style="width:99%;height:99%;" src='+src+'></iframe>').dialog({title: 'Versetto e Dolcetto' }).dialog('open');
}

function viewImage(src, id, showReportLink, showDeleteLink) {
    var html = "<img src='"+src+"' id='_img000' />";

    if (showReportLink)
        html += "<br><a href='javascript:{segnalaContenuto("+id+",\"foto\")}' class='reportContentLink' >Segnala violazione</a>";
    if (showDeleteLink)
        html += '<a href="'+contextPath+'/utente?action=elimina_foto_utente&id='+id+'" class="deleteContentLink" >Elimina</a>';


    // svuota la div playerBox.. se mette un'altra per mantenere l'altezza
    // nasconde la seconda div ed inserisce il nuovo contenuto
    // infine esegue il fadeIn
    $(".playerBoxReference").empty().html("<div class='playerBoxWrapper'></div>");
    $(".playerBoxWrapper").hide().html(html).fadeIn();
}


function playVideo(src, id, showReportLink, showDeleteLink) {
    var html = "<a href='"+$.URLEncode(src)+"' id='_player' class='videoPlayer' ></a>";

    if (showReportLink)
        html += "<br><a href='javascript:{segnalaContenuto("+id+",\"video\")}' class='reportContentLink'>Segnala violazione</a>";
    if (showDeleteLink)
        html += '<a href="'+contextPath+'/utente?action=elimina_video_utente&id='+id+'" class="deleteContentLink" >Elimina</a>';

    $(".playerBoxReference").html(html);
    playVideoId("_player");
}

/* evidenzia di rosso (poi ritorna bianco) l'elemento con id passato in input */
function redHighlighting(objId) {
    fade(objId, 237,28,36, 255,255,255, 30,1,20);
}


/* evidenzia in modalità fade un elemento */
function fade(elId, sr, sg, sb, er, eg, eb, step, current, speed){
    // printfire("----- START fade()");
    if (current <= step){
        setbgColor(elId,Math.floor(sr * ((step-current)/step) + er * (current/step)),Math.floor(sg * ((step-current)/step) + eg * (current/step)),Math.floor(sb * ((step-current)/step) + eb * (current/step)));
        current++;
        setTimeout("fade('"+elId+"',"+sr+","+sg+","+sb+","+er+","+eg+","+eb+","+step+","+current+","+speed+")",parseInt(speed));
    }
    // printfire("----- END fade()");
}

function setbgColor(elId, r, g, b){
    getRef(elId).style.backgroundColor = "rgb("+r+","+g+","+b+")";
}
function getRef(el)
{
    if(typeof el == "string")
    {
        return document.getElementById(el);
    }
    else if(typeof el == "object")
    {
        return el;
    }
    else return null;
}