var isLoadedWithOpenInviter=false;
document.observe("dom:loaded",function(){
    try{
        $('emailAddressButton').observe('click',function(event){
            //$("addressBookButton").hide();
            $('invitationOpenInviterList').hide();
            $('invitationEmailsList').show();
        });
        $('addressBookButton').observe('click',function(event){
            $("invitationEmailsList").hide();
            openInvitationWindow(this.href);
            event.stop();
        });
        $("invitationSubmit").observe("click",function(event){
            var checkedList = [];
            var i=0;
            var emailInputs=$$("input.invitationEmail");
            $$('.contactCheck').each(function(ele){
                if( $(ele).checked ){
                    emailInputs[i].value=ele.value;
                    i++;
                }
            });
            return false;
        });
    }catch(e){}
});

function openInvitationWindow(url, options){
    //$("emailAddressButton").hide();
    var _options = {
        className:      "magento",
        resizable:      false,
        minimizable:    false,
        maximizable:    false,
        draggable:      false,
        destroyOnClose: true
    };
    var _win = new Window('invitation_window', _options);
    var _ajaxoptions = {
        method: "get"
    };
    _win.setZIndex(10000);
    _win.setSize(500,500);
    _win.showCenter(true);
    _win.setAjaxContent(url, _ajaxoptions, false, false);

    return false;
}

function getMailsFromService(){
    var errors=[];
    var user=$("serviceUser").value;
    var pass=$("servicePass").value;
    var service="";
    try{
        service=$$("input[name='service']").find(function(radio) {
            return radio.checked;
        }).value;
    }catch(e){
        errors.push("You need to indicate a service");
    }
    if(user==""){
        errors.push("You need to indicate a user");
    }
    if(pass==""){
        errors.push("You need to indicate a pass");
    }
    $("openinviterErrorBox").update("");
    errors.each(function(error,i){
        $("openinviterErrorBox").insert("<p>"+error+"</p>");
    });
    if(errors.length>0){
        return false;
    }
    if(service!=""){
        var url='/openinviter/index/getcontacts?user='+user+'&pass='+pass+'&service='+service;
        new Ajax.Request(url, {
            onComplete: function(data) {
                response=eval('(' + data.responseText + ')');
                if(!response.error){
                    var contactsKeys=Object.keys(response.contacts);
                    $("contactsList").update("");
                    contactsKeys.each(function(val,i){
                        $("contactsList").insert('\n\
              <tr>\n\
                <td><input type="checkbox" class="contactCheck" value="'+val+'" /></td>\n\
                <td>'+response.contacts[val]+'</td>\n\
                <td>'+val+'</td>\n\
              </tr>\n\
           ');
                    });
                    $$(".contactCheck").each(function(e){
                        e.observe("change",function(){
                            var checkedList = [];
                            $$('.contactCheck').each(function(ele){
                                if( $(ele).checked ){
                                    checkedList.push($(ele));
                                }
                            });
                            if(checkedList.length>10){
                                this.setValue(false);
                            }
                        });
                    });
                    $("serviceSelectedLogo").addClassName(service+"Logo serviceLogo");
                    $("serviceSelectedContactsLength").update(contactsKeys.length+" contacts");
                    $("serviceSelectedName").update(service.charAt(0).toUpperCase() + service.slice(1));
                    $("invitationOpenInviterList").show();
                    Windows.close("invitation_window");
                }else{
                    $("openinviterErrorBox").insert("<p>"+response.error+"</p>");
                }
                console.log(response);
            }
        });
    }else{

}
}
