$(document).ready( function()
{
    /**
     * 画面制御
     */
    $( function()
    {
        $('#base').css('visibility', 'hidden');
//        $('html').scrollTop($.url.param('_y'));
//        $('html').scrollLeft($.url.param('_x'));
        $('#estimate #vote').show();
        $('#blog_comment #input').show();
        $('#base').css('visibility', 'visible');
    });

    /**
     * 投票制御
     */
    $( function()
    {
        var vote = false;
        var complete = false;
        var message = '';
        var messageTimer = 0;
        var messageFlg = false;
        var successCallback = function(response)
        {
            if (response == 'true') {
                vote = true;
            } else {
                vote = false;
            }
        };

        $.ajax( {
            'type' :'POST',
            'url' :'../user/vote.php',
            'data' : {
                'aid' :$('#aid').val(),
                'type' :'get'
            },
            'success' :successCallback,
            'async' :false,
            'cache' :false
        });
        var ratingFocus = function(value, link)
        {
            clearTimeout(messageTimer);
            if (messageFlg == false) {
                if (vote == false) {
                    $('#estimate .message').text('すでに投票済みです');
                } else {
                    if (complete == false) {
                        $('#estimate .message').text('クリックで投票できます');
                    }
                }
                messageFlg = true;
            }
        };
        var ratingBlur = function(value, link)
        {
            messageTimer = window.setTimeout( function()
            {
                $('#estimate .message').text(message);
                messageFlg = false;
            }, 1000);
        };
        var ratingCallback = function(value, link)
        {
            var successCallback = function(response)
            {
                if (response == 'true') {
                    message = '投票を受け付けました';
                    $('input').rating('disable');
                    $('#estimate .message').text(message);
                    complete = true;
                } else {
                    $('input').rating('enable');
                }
            }
            $.ajax( {
                'type' :'POST',
                'url' :'../user/vote.php',
                'data' : {
                    'aid' :$('#aid').val(),
                    'point' :value,
                    'type' :'add'
                },
                'success' :successCallback,
                'async' :false,
                'cache' :false
            });
            return false;
        };

        message = $('#estimate .message').text();
        if (vote == true) {
            $('##estimate #vote .point').rating( {
                'focus' :ratingFocus,
                'blur' :ratingBlur,
                'required' :true,
                'callback' :ratingCallback
            });
        } else {
            $('#estimate #vote .point').rating( {
                'required' :true,
                'readOnly' :true
            });
            $('#estimate #vote').mouseover( function()
            {
                ratingFocus();
            });
            $('#estimate #vote').mouseout( function()
            {
                ratingBlur();
            });
        }
    });

    /**
     * コメント制御
     */
//    $( function()
//    {
//        $('#blog_comment .page .move').click( function(event)
//        {
//            event.target.href += '?_x=' + $('html').scrollLeft() + '&_y=' + $('html').scrollTop();
//        });
//
//        var cmtObj = document.getElementById('cmt');
//        alert(cmtObj);
//        if (cmtObj != null) {
//        	$('#blog_comment #input #cmt').textlimit('#blog_comment #input .count span', 300);
//        }
//
//        $('#blog_comment #input #cmt').focus( function()
//        {
//        	$('#blog_comment #input .message').text('');
//        });
//
//        // ajax.jsに吸収予定　いずれ消す。
//        $('#blog_comment #input .submit').click( function(event)
//        {
//            var value = $('#blog_comment #input textarea').val();
//            if (value == null || value == '') {
//                $('#blog_comment #input .message').text('この画像についてコメントを入力してください');
//            } else {
//                $.alerts.okButton = 'は　い';
//                $.alerts.cancelButton = 'いいえ';
//                $.alerts.dialogClass = 'mystyle';
//                jConfirm('コメントを投稿しますか？', 'コメント投稿ダイアログ', function(r)
//                {
//                    if (r == true) {
//                        $('#form')[0].action = './post.php?_x=' + $('html').scrollLeft() + '&_y=' + $('html').scrollTop();
//                        $('#form')[0].method = 'POST';
//                        $('#form')[0].submit();
//                        return false;
//                    }
//                });
//            }
//        });
//    });

    // ajax.jsに吸収予定　いずれ消す。
    /* コメント削除処理 */
//    $('#delete_button').click( function()
//    {

//    	alert('選択数:' + document.form.elements['seq[]'].length);

//    	 if (document.form.elements['seq[]'].length) {
//    		 var count = 0;
//    		 for (var i=0;i<document.form.elements['seq[]'].length;i++){
//
//    			 if ((document.form.elements[i].name=='seq[]') && (document.form.elements[i].checked)) {
//    				 count ++;
//    			 }
//    		 }
//    		 alert(count+"個のチェックボックスがチェックされています。");
//    		 if (count == 0) {
// 	            $('#blog_comment #blog_delete #input .message').text('少なくとも１つ削除するコメントを選択してください。');
// 	            return;
//    		 }
//    	 }
//    	 else {
//	    	if (document.form.elements['seq[]'].checked) {
//	    		alert("単一1個のチェックボックスがチェックされています。");
//	    	} else {
//	    		alert("単一0個のチェックボックスがチェックされています。");
//	            $('#blog_comment #blog_delete #input .message').text('少なくとも１つ削除するコメントを選択してください。');
//	    	}
//    	 }

//        $.alerts.okButton     = 'は　い';
//        $.alerts.cancelButton = 'いいえ';
//        $.alerts.dialogClass  = 'mystyle';
//        jConfirm('チェックしたコメントを削除しますか？', 'コメント削除ダイアログ', function(r)
//        {
//            if (r == true) {
//                $('#form')[0].action = './delete.php?_x=' + $('html').scrollLeft() + '&_y=' + $('html').scrollTop();
//                $('#form')[0].method = 'POST';
//                $('#form')[0].submit();
//                return false;
//            }
//        });
//    });

    /**
     * スクロール制御
     */
//    $( function()
//    {
//        $('#link_top').click( function()
//        {
//            if (!$.browser.safari) {
//                $(this).blur();
//                $('html,body').animate( {
//                    scrollTop :0
//                }, 'normal');
//            }
//        });
//    });
});
