function swap(tag)
{
    var regexp = new RegExp(tag+' ?'); 
    var s = $('entry_form_tag').value.match(regexp);
    if (null == s) {
	
        if (0 < $('entry_form_tag').value.length && !$('entry_form_tag').value.match(/ $/)) {
            $('entry_form_tag').value = $('entry_form_tag').value+' ';
        }
        $('entry_form_tag').value += tag+' ';
        $('entry_form_tag').focus();
    } else {
        $('entry_form_tag').value = $('entry_form_tag').value.replace(s, '');
    }
}


Event.observe(window, 'load', init, false);

function init() {
    makeSubmitForm('entry_form');
}


function makeSubmitForm(id) {
    if( $(id+'_submit') != null ) {
        Event.observe(id+'_submit', 'click', function(){formSubmit(id)}, false);
    }
    
    if( $(id+'_title') != null ) {
        Event.observe(id+'_title', 'focus', function(){formFocus(id+'_title')}, false);
        Event.observe(id+'_title', 'blur', function(){formBlur(id+'_title')}, false);
    }
    if( $(id+'_description') != null ) {
        Event.observe(id+'_description', 'focus', function(){formFocus(id+'_description')}, false);
        Event.observe(id+'_description', 'blur', function(){formBlur(id+'_description')}, false);
    }
}


function formSubmit(id) {
    
    var err = 0;
    var obj = $(id);
    
    var title       = $(id+'_title').value;
    var description = $(id+'_description').value;
    
    title = title.replace(/^\s+|\s+$/g, "");
    description = description.replace(/^\s+|\s+$/g, "");
    
    if (title == '') {
        /* alert('タイトルを入力してください。'); */
        $(id+'_title_err').innerHTML = 'タイトルを入力してください。';
        err = 1;
    }
    
    if (description == '') {
        /* alert('本文を入力してください。'); */
        $(id+'_description_err').innerHTML = '本文を入力してください。';
        err = 1;
    }
    
    if (err) {
        return;
    }
    
    obj.action = '/diary/entry';
    obj.method = 'post';
    
    obj.submit();
    
}


function formFocus(id) {
    Element.addClassName($(id), 'editable');
//    $(id+'_err').innerHTML = '';
}


function formBlur(id) {
    Element.removeClassName($(id), 'editable');
}

function deleteEntry(id)
{
	var txt = '記事を削除してもよろしいですか？';
	
	if (!confirm(txt)) {
		return;
    }
    
    requestAPI('delete', id);

}


function requestAPI(action, id)
{
	
	var url  = '/api/ajax/entry.php';
	var pars = 'action=' + action + '&id=' + id;
	
	var success = function(t) {requestSuccess(t, id);}
	var failure = function(t) {requestFailure(t, id);}
	
	var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
	
}

function requestSuccess(t, id) {
	new Effect.Fade($('diary_' + id));
	return;
}

function requestFailure(t, id) {
	$('diary_err').innerHTML = t.responseText;
	return;
}

function popupImage(imgUrl,w,h) {
    outerW = w+7;
    outerH = h+7;

    imgHTML =
        '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + "\n" +
        '<html xmlns="http://www.w3.org/1999/xhtml">' + "\n" +
        '<head>' + "\n" +
        '<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" />' + "\n" +
        '<meta http-equiv="Content-Script-Type" content="text/javascript" />' + "\n" +
        '<meta http-equiv="Content-Style-Type" content="text/css" />' + "\n" +
        '<link rel="shortcut icon" href="/favicon.ico" />' + "\n" +
        '<style type="text/css">' + "\n" +
        'body {' + "\n" +
        '   margin: 0;' + "\n" +
        '   padding: 10px 0 0;' + "\n" +
        '   text-align: center;' + "\n" +
        '   background: #fff;' + "\n" +
        '}' + "\n" +
        'img {' + "\n" +
        '   border: none;' + "\n" +
        '}' + "\n" +
        '</style>' + "\n" +
        '<title>Grouptube</title>' + "\n" +
        '</head>' + "\n" +
        '<body>' + "\n" +
        '<a href="javascript:window.close();" title="CLOSE WINDOW">' + "\n" +
        '  <img src="' +imgUrl+ '" alt="CLOSE WINDOW" />' + "\n" +
        '</a>' + "\n" +
        '</body>' + "\n" +
        '</html>';

    w_sizeX = w+20;
    w_sizeY = h+22;
    s_width = screen.width;
    s_height = screen.height;
    posX = (s_width-w_sizeX)/2;
    posY = (s_height-w_sizeY)/2;

    if ((navigator.appName) == "Netscape" && (navigator.appVersion) > 3){
        iWin = window.open('',"ImageWindow","width=" + w_sizeX + ",height=" + w_sizeY + ",screenX=" + posX + ",screenY=" + posY);
    } else {
        iWin = window.open('',"ImageWindow","width=" + w_sizeX + ",height=" + w_sizeY + ",left=" + posX + ",top=" + posY);
    }
    iWin.focus();
    iWin.document.write(imgHTML);
    iWin.document.close();
}
