/*global Prototype, Class, Effect */
var MobiQuote = Class.create({

    initialize: function (max, idname, current, delay) {
        this.idname = idname;
        this.max = max;
        this.current = current;
        this.delay = (delay * 1000);
        this.duration = 2;
        if (Prototype.Browser.IEbefore7)
        {
            this.duration = 0;
        }
    },

    delayedDisplayNext: function () {
        var func = this.idname + ".displayNext()";
        window.setTimeout(func, this.delay);
    },

    displayNext: function () {

        var old = this.current;
        this.current++;
        if (this.current > this.max)
        {
            this.current = 1;
        }
        var tmp = new Effect.Parallel([
            new Effect.Fade(this.idname + '-' + old, { sync: true }),
            new Effect.Appear(this.idname + '-' + this.current, { sync: true })
        ], { duration: this.duration, afterFinish: this.delayedDisplayNext() });
    },

    start: function () {
        this.delayedDisplayNext();
    }

});

function updateContactPhones()
{
    var opt = {
        postBody: 'contact_uid=' + $F('contact_uid'),
        onSuccess: function(t) {
            $('callphone1').enable();
            $('callphone1').length = 0;
            var response = ('(' + t.responseText + ')').evalJSON();
            
            var telCount = 0;
            
            // Adds Phone entries for this contact
            if (response.contact.tel.size() > 0) 
            {
                response.contact.tel.each(function(phone, index) {
                    var optionValue = $F('contact_uid') + '-' + phone.phone_type_id + '@' + phone.full_number;
                    $('callphone1').options[ telCount ] = new Option(phone.phone_type_name + ' - ' + phone.formatted_number, optionValue);
                    
                    telCount++;
                });
            }
            
            // Adds IM entries
            if(response.contact.im.size() > 0) 
            {
                response.contact.im.each( function(im,index) {
                    var optionValue = $F('contact_uid') + '-' + im.phone_type_id + '@' + im.body_name;
                    $('callphone1').options[ telCount ] = new Option(im.phone_type_name + ' - ' + im.body_name, optionValue);
                
                    telCount++;
                } );
            }
            
            
            // If no Phone or IM found, we disable
            if( telCount==0 )
            {
                $('callphone1').options[0] = new Option(trad_no_phone, '');
                $('callphone1').disable();
            }
        },
        onFailure: function (t)
        {
            display_error_window(trad_error_function, trad_error_title_server);
        }
    }
    
    var tmp = new Ajax.Request('/contact/ajaxgetcontact/', opt);
}

function validate_webcal()
{
    $('callphone_err1').hide();
    $('userphone_err1').hide();
    
    if ($F('contact_uid') == '' || $F('callphone1') == '')
    {
        $('callphone_err1').show();
        return false;
    }
    
    if ($F('userphone') == '')
    {
        $('userphone_err1').show();
        return false;
    }
    
    return true;
}

function webcall_submit()
{
    if (validate_webcal())
    {
       ajax_webcall_submit();
    }
    
    /*
    opt = {
        parameters: $('webcall').serialize() + '&webcall_type=call&nb_clid=1',
        onSuccess: function (t) {
            console.log(t);
        },
        onFailure: function (t) {
            console.log(t);
        }
    };
    
    var tmp = new Ajax.Request('/webcall/callgo/', opt);
    */
}