var _LIST_OF_NEW_SEGNALATIONS_ID = "";
var _COLOR = 1;

function startup(list_sgn) {
    _LIST_OF_NEW_SEGNALATIONS_ID = list_sgn;
    var myInterval = window.setInterval(function () {
        animate_segnalations();
    },1500);

}
function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function animate_segnalations ()
{
    var list = _LIST_OF_NEW_SEGNALATIONS_ID.split(",");
    var color1;
    var color2;
    if (_COLOR == 1)
    {
        color1 = '000000';
        color2 = 'FF0000';
        _COLOR = 2;
    } else {
        color2 = '000000';
        color1 = 'FF0000';
        _COLOR = 1;
    }
    for (var i = 0; i < list.length; i++)
    {
        if(list[i] != "") {
            colorFade(list[i],'color',color1,color2,25,50);
        }
    }
}
function switch_flag_on(img)
{
    var bg = img.src;
    bg = bg.split("/")[5];
    img.src = "images/canton_flags/"+bg.split("bw_")[1];
}
function switch_flag_off(img)
{
    var bg = img.src;
    bg = bg.split("/")[5];
    img.src = "images/canton_flags/bw_"+bg;
}

function colorFade(id,element,start,end,steps,speed, reverse) {
    var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
    var target = document.getElementById(id);
    steps = steps || 20;
    speed = speed || 20;
    clearInterval(target.timer);
    endrgb = colorConv(end);
    er = endrgb[0];
    eg = endrgb[1];
    eb = endrgb[2];
    // tolto perche se no non lampeggia dopo che ha puntato un altro
    //if(!target.r) {
    startrgb = colorConv(start);
    r = startrgb[0];
    g = startrgb[1];
    b = startrgb[2];
    target.r = r;
    target.g = g;
    target.b = b;
    // }
    rint = Math.round(Math.abs(target.r-er)/steps);
    gint = Math.round(Math.abs(target.g-eg)/steps);
    bint = Math.round(Math.abs(target.b-eb)/steps);
    if(rint == 0) {
        rint = 1;
    }
    if(gint == 0) {
        gint = 1;
    }
    if(bint == 0) {
        bint = 1;
    }
    target.step = 1;
    target.timer = setInterval( function() {
        animateColor(id,element,steps,er,eg,eb,rint,gint,bint)
    }, speed);

}

// incrementally close the gap between the two colors //
function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
    var target = document.getElementById(id);
    var color;
    if(target.step <= steps) {
        var r = target.r;
        var g = target.g;
        var b = target.b;
        if(r >= er) {
            r = r - rint;
        } else {
            r = parseInt(r) + parseInt(rint);
        }
        if(g >= eg) {
            g = g - gint;
        } else {
            g = parseInt(g) + parseInt(gint);
        }
        if(b >= eb) {
            b = b - bint;
        } else {
            b = parseInt(b) + parseInt(bint);
        }
        color = 'rgb(' + r + ',' + g + ',' + b + ')';
        if(element == 'background') {
            target.style.backgroundColor = color;
        } else if(element == 'border') {
            target.style.borderColor = color;
        } else {
            target.style.color = color;
        }
        target.r = r;
        target.g = g;
        target.b = b;
        target.step = target.step + 1;
    } else {
        clearInterval(target.timer);
        color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
        if(element == 'background') {
            target.style.backgroundColor = color;
        } else if(element == 'border') {
            target.style.borderColor = color;
        } else {
            target.style.color = color;
        }
    }
}

// convert the color to rgb from hex //
function colorConv(color) {
    var rgb = [parseInt(color.substring(0,2),16),
    parseInt(color.substring(2,4),16),
    parseInt(color.substring(4,6),16)];
    return rgb;
}


function submit_abuse()
{
    var abuse_type =  $("input[name='abuse_type']:checked").val();
    var sgn_id =  document.getElementById("abuse_sgn").value;
    $.get( "access/submit_abuse.php", {abuse_type: abuse_type, sgn_id: sgn_id}, function(data) {

        var code = data.split("#")[0];
        var description = data.split("#")[1];
        if (code == "-100")
        {
            hide_abuse();
            setTimeout("show_login()",1000);
        }
        else if (code == "400")
         {
            hide_abuse();
            setTimeout("document.location.href=document.location.href",900);

        } else {
            document.getElementById("abuse_message").innerHTML = "Error: "+description;
        }


    } );

}

function show_abuse(id_sgn)
{
    document.getElementById("abuse_sgn").value = id_sgn;
    var screen_div = document.getElementById("screen_fade");
    screen_div.onclick = function(){ hide_abuse() };
    var div = document.getElementById("screen_fade");
    div.style.height = getDocHeight()+"px";
    div.style.visibility = "visible";
    opacity("screen_fade",0,50,1000);

    setTimeout("show_abuse_div()",900);

}
function show_abuse_div()
{
    var div = document.getElementById("abuse");
    div.style.visibility = "visible";
    opacity("abuse",0,100,500);
}
function hide_abuse()
{
    opacity("screen_fade",50,0,900);
    opacity("abuse",100,0,200);
    setTimeout("hide_abuse_div()",900);
}
function hide_abuse_div() {
        var abuse_div = document.getElementById("abuse");
    var screen_div = document.getElementById("screen_fade");
    abuse_div.style.visibility = "hidden";
    screen_div.style.visibility = "hidden";
}