var GLOBALMAILTIMER;
function _overlayClose(id,rel) {
    try {
        var marginT = parseInt( $('#'+rel).css('top').replace(/px/,'') );
        $('#'+rel)
        .slideUp(500,function() {
            $('#'+id).fadeOut('fast',function() {
                $('body').children().remove('#'+id);
                $('body').children().remove('#'+rel);
            });
        });
    }
    catch(e) {
    }
    document.onkeyup = document.onkeypress = document.onkeydown = '';
    $('select').show();
    if ( GLOBALMAILTIMER )
        clearInterval(GLOBALMAILTIMER);
}

function _overlayShow(id,rel) {
    $('select').hide();
    try {
        var w = $('#'+rel).width();
        var h = $('#'+rel).height();
        var marginL = parseInt( ($(window).width() - w) / 2  );
        var marginT = parseInt( ($(window).height() - h) / 2  );
        if ( marginL <= 0 ) { marginL = 20; }
        if ( marginT <= 0 ) { marginT = 20; }
        marginL += $(window).scrollLeft();
        marginT += $(window).scrollTop();

        $('#'+id).fadeIn('fast', function() {
            $('#'+rel).css({
                'position': 'absolute',
                'top': marginT + 'px',
                'left': marginL + 'px',
                'color': '#fff',
                'z-index': '10000'
            })
            .slideDown(500);
        });

        // close event
        $('#'+id).click(function() {
            _overlayClose(id,rel);
        });
        $('#pageback').click(function() {
            _overlayClose(id,rel);
        });
        document.onkeyup = document.onkeypress = document.onkeydown = function(e) {
            var keycode;
            if ( e == null ) // ie
                keycode = event.keyCode;
            else
                keycode = e.which;
            if ( keycode == 27 ) // esc
                _overlayClose(id,rel);
            else
                $('#mail').focus();
        };
    }
    catch(e) {
    }
}

function _overlayCreate(id) {
    var bgW = ( $('body').width() > $('#container').width() ) ? $('body').width() : $('#container').width();
    var bgH = ( $('body').height() > $('#container').height() ) ? $('body').height() : $('#container').height();

    $('body').append(
        $('<div id="'+id+'"></div>')
        .css({
            'position':'absolute','top':'0px','left':'0px', 'z-index':'9999',
            'width': bgW,
            'height': bgH,
            'background-color':'#000000','opacity':'0.7'
        })
        .hide()
    );
}

function changeLabelImg(){
	if(document.getElementById('label').className == "normal"){
		document.getElementById('label').className = "click";
		$('#label1').attr('checked','true');
	}else{
		document.getElementById('label').className = "normal";
		$('#label1').attr('checked','false');
		var expire = new Date();
		expire.setTime(expire.getYear() - 1);
		document.cookie = '_eapUserMailToiPhone=; path=/; expires='+expire.toGMTString();
	}
	return false;
}

function addHostname(host) {
    if ( host != 'i.softbank.jp' && host != 'me.com' )
        return false;

    var mailaddress = $('#mail').val();
    if ( mailaddress && mailaddress.match(/@/) ) {
        mailaddress = mailaddress.replace(/\@.*$/, '@' + host);
    }
    else if (mailaddress && mailaddress == 'メールアドレス' ) {
        return false;
    }
    else {
        mailaddress += '@' + host;
    }
    $('#mail').val(mailaddress);

    return false;
}

function send2iPhone(eid) {
    try {
        $.post('http://www.elegant-apps.com/eapps-sys/eappsmail.cgi', {'eid':eid,'type':'ajax'},
              function(data) {
                  var html = eval('('+data+')');
                  var overlayId = '_fOverlay';
                  var innerId = '_fMailDiv';
                  $('body').append( 
                      $('<div id="'+innerId+'"></div>').append(html.content).hide()
                      //$(html).hide() 
                  );
                  $('#MailArea').append(
                      $('<a href="#" id="pageback" title="閉じる">閉じる</a>').click(function() {
                          _overlayClose(overlayId,innerId);return false;
                      })
                  );

                  _overlayCreate(overlayId);
                  _overlayShow(overlayId, innerId);
              });
    }
    catch(e) {
    }
    return false;
}

function send2iPhone_send(eid, sess) {
    try {
        $.post('http://www.elegant-apps.com/eapps-sys/eappsmail.cgi', 
               {
                   'eid':eid,
                   'key':sess,
                   'type':'ajax',
                   '__mode':'send',
                   'mailaddress':$('#mail').val(),
                   'saveaddress':$('#label1').attr('checked')
               },
               function(data) {
                   var html = eval('('+data+')');
                   $('#_fMailDiv').html(html.content);
                   $('#MailArea').append(
                       $('<a href="#" id="pageback" title="閉じる">閉じる</a>').click(function() {
                           _overlayClose('_fOverlay','_fMailDiv');return false;
                       })
                   );

                   GLOBALMAILTIMER = setInterval( function() {
                       if ( html.stat == 'success' ) {
                           _overlayClose('_fOverlay','_fMailDiv');
                       }
                   },5000);
               });
    }
    catch(e) {
    }
    return false;
}
