if (!Clip) var Clip = {};
Clip.History = Class.create();


Clip.History.prototype = {
    initialize: function(args) {
        args = args || {};
        this.current_page = 1;
        this.perpage = 20;
        this.sort = 1;
        this.next = 0;
        this.prev = 0;
        this.jsr;
        this.host = args.host;
    },
    changeSort: function(){
        if(this.sort){
            this.sort='';
            $('sort_new1').src = '/image/common/sort_new2_on.gif';
            document.MM_sr[0].oSrc = '/image/common/sort_new2_on.gif';
            $('sort_new1').onmouseout = function() { MM_swapImgRestore(); return false; };
            $('sort_new1').onmouseover = function() { MM_swapImage('sort_new1','', '/image/common/sort_new1_on.gif',1); return false; };
        }else{
            this.sort='desc';
            $('sort_new1').src = '/image/common/sort_new1_on.gif';
            document.MM_sr[0].oSrc = '/image/common/sort_new1_on.gif';
            $('sort_new1').onmouseout = function() { MM_swapImgRestore(); return false; };
            $('sort_new1').onmouseover = function() { MM_swapImage('sort_new1','', '/image/common/sort_new2_on.gif',1); return false; };
        }
        this.get();
    },
    changePerpage: function(n){
        this.current_page =  Math.floor(((this.current_page - 1) * this.perpage + 1)/n) + 1;
        this.perpage = n;
        if(this.perpage == 100){
            $('sort_view_20').src = '/image/common/sort_view_20.gif';
            $('sort_view_100').src = '/image/common/sort_view_100_on.gif';
        }else{
            $('sort_view_20').src = '/image/common/sort_view_20_on.gif';
            $('sort_view_100').src = '/image/common/sort_view_100.gif';
        }
        this.get();
    },
    swapPerpageRestore: function(n){
        if(this.perpage != n){
            MM_swapImgRestore();
        }
    },
    next_page: function() {
        this.current_page = this.next;
        if(this.current_page){
            this.get();
        }
    },
    previous_page: function() {
        this.current_page = this.prev;
        if(this.current_page){
            this.get();
        }
    },
    get_subwindow: function() {
        //        var url = '/rest/history/?format=json&perpage=4&page=1&sort=desc&t='+(new Date().getTime());
        //new Ajax.Request(
        //                 url, { method: 'get', onSuccess: this.got_subwindow.bind(this) }
        //                );
        this.jsr = new JSONscriptRequest(this.host + '/rest/history/?format=json&callback=history_clip.got_subwindow&perpage=4&page=1&sort=desc&t='+(new Date().getTime()));
        this.jsr.buildScriptTag();
        this.jsr.addScriptTag();
        

    },
        //    got_subwindow: function(request) {
            //        var json =  eval("("+request.responseText+")");
    got_subwindow: function(json) {
        if(json.clips){
            $('history_clip_list').style.display = '';
            Jemplate.process( 'subwindow.tt', json, $('history_clip_list'));
        }else{
            $('history_clip_list').style.display = 'none';
            Jemplate.process( 'subwindow.tt', null, $('history_clip_list'));
        }
        this.jsr.removeScriptTag();
    },
    get: function() {
        var url = '/rest/history/?format=json&perpage='+this.perpage+'&page='+this.current_page+'&sort='+this.sort+'&t='+(new Date().getTime());
        new Ajax.Request(
                         url, { method: 'get', onSuccess: this.got.bind(this) }
                         );
    },
    got: function(request) {
        var json =  eval("("+request.responseText+")");
        if(json.clips){
            $('result_list').style.display = '';
            Jemplate.process( 'main.tt', json, $('result_list'));
            $('navi1').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            $('navi2').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            this.prev = json.pager.previous_page;
            this.next = json.pager.next_page;
        }else{
            $('result_list').style.display = 'none';
            Jemplate.process( 'main.tt', null, $('result_list'));
            $('navi1').innerHTML = '0-0(0件中)';
            $('navi2').innerHTML = '0-0(0件中)';
            this.prev  = '';
            this.next = '';
        }
        if(!json.pager.next_page){
            $('arrow_next1').style.display = 'none';
            $('arrow_next2').style.display = 'none';
            $('arrow_next1_none').style.display = '';
            $('arrow_next2_none').style.display = '';
        }else{
            $('arrow_next1').style.display = '';
            $('arrow_next2').style.display = '';
            $('arrow_next1_none').style.display = 'none';
            $('arrow_next2_none').style.display = 'none';
        }
        if(!json.pager.previous_page){
            $('arrow_prev1').style.display = 'none';
            $('arrow_prev2').style.display = 'none';
            $('arrow_prev1_none').style.display = '';
            $('arrow_prev2_none').style.display = '';
        }else{
            $('arrow_prev1').style.display = '';
            $('arrow_prev2').style.display = '';
            $('arrow_prev1_none').style.display = 'none';
            $('arrow_prev2_none').style.display = 'none';
        }
    }
}

var history_list = new Clip.History();