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


Clip.TownImpression.prototype = {
    initialize: function() {
        this.current_page = 1;
        this.perpage = 5;
        this.sort = 'desc';
        this.modified_sort = 'desc';
        this.sort_flag = '';
        this.sort_type = '';
        this.is_open = false;
    },
    swapSortImage: function( sort_type ) {
        if( sort_type == 'renew'){
            if( this.sort_type == 'renew' ){
                if(this.modified_sort == 'desc'){
                    $('sort_renew1').src = '/image/common/sort_renew2_on.gif';
                }else{
                    $('sort_renew1').src = '/image/common/sort_renew1_on.gif';
                }
            }else{
                $('sort_renew1').src = '/image/common/sort_renew1_on.gif';
            }
        }else{
            if( this.sort_type == 'renew' ){
                $('sort_new1').src = '/image/common/sort_new1_on.gif';
            }else{
                if(this.sort == 'desc'){
                    $('sort_new1').src = '/image/common/sort_new2_on.gif';
                }else{
                    $('sort_new1').src = '/image/common/sort_new1_on.gif';
                }
            }

        }
        //if(sort_type == 'renew') {
        //    if(this.modified_sort == 'desc'){
        //        $('sort_renew1').src = '/image/common/sort_renew2_on.gif';
        //    }else{
        //        $('sort_renew1').src = '/image/common/sort_renew1_on.gif';
        //    }
        //}else{
        //    if(this.sort == 'desc'){
        //        $('sort_new1').src = '/image/common/sort_new2_on.gif';
        //    }else{
        //        $('sort_new1').src = '/image/common/sort_new1_on.gif';
        //    }
        //}
    },
    swapSortImageRestore: function( sort_type) {
        if( this.sort_flag != 1 ){
            if(sort_type == 'renew') {
                if(this.sort_type == 'renew' ){
                    if(this.modified_sort == 'desc'){
                        $('sort_renew1').src = '/image/common/sort_renew1_on.gif';
                    }else{
                        $('sort_renew1').src = '/image/common/sort_renew2_on.gif';
                    }
                }else{
                    if(this.modified_sort == 'desc'){
                        $('sort_renew1').src = '/image/common/sort_renew1.gif';
                }else{
                        $('sort_renew1').src = '/image/common/sort_renew2.gif';
                    }
                }
            }else{
                if(this.sort_type == 'renew'){
                    if(this.sort == 'desc'){
                        $('sort_new1').src = '/image/common/sort_new1.gif';
                    }else{
                        $('sort_new1').src = '/image/common/sort_new2.gif';
                    }
                }else{
                    if(this.sort == 'desc'){
                        $('sort_new1').src = '/image/common/sort_new1_on.gif';
                    }else{
                        $('sort_new1').src = '/image/common/sort_new2_on.gif';
                    }
                }
            }
        }else{
            this.sort_flag = 0;
        }
    },
    changeSort: function( sort_type ){
        this.sort_flag = 1;
        if(sort_type == 'renew'){
            if(this.sort_type == 'renew'){
                if(this.modified_sort == 'desc'){
                    this.modified_sort='asc';
                    $('sort_renew1').src = '/image/common/sort_renew2_on.gif';
                }else{
                    this.modified_sort='desc';
                    $('sort_renew1').src = '/image/common/sort_renew1_on.gif';
                }
            }else{
                this.modified_sort='desc';
                this.sort = 'desc';

                $('sort_renew1').src = '/image/common/sort_renew1_on.gif';
                $('sort_new1').src = '/image/common/sort_new1.gif';
            }
            this.sort_type  = 'renew';
        }else{
            if(this.sort_type == 'renew'){
                this.sort='desc';
                this.modified_sort = 'desc';
                $('sort_new1').src = '/image/common/sort_new1_on.gif';
                $('sort_renew1').src = '/image/common/sort_renew1.gif';
            }else{
                if(this.sort){
                    this.sort='';
                    $('sort_new1').src = '/image/common/sort_new2_on.gif';
                }else{
                    this.sort='desc';
                    $('sort_new1').src = '/image/common/sort_new1_on.gif';
                }
            }
            this.sort_type  = '';
        }
        this.render();
    },
    next_page: function() {
        this.current_page = $('form').next.value;
        if(this.current_page){
            this.render();
        }
    },
    previous_page: function() {
        this.current_page = $('form').prev.value;
        if(this.current_page){
            this.render();
        }
    },
    login_page: function(town_info, impression) {
        var href = location.href;
        location.href = '/town/voted/' + town_info + '/' +  impression + '/';
    },
    avail: function(impression) {
        $('memo_informative_' + impression).style.display = "none";
        $('memo_informative_' + impression + '_finish').style.display = "";
        var url = '/rest/town/avail/'+impression+'?format=json&'+"&"+ new Date().getTime();
        new Ajax.Request( url, { method: 'get', onSuccess: this.availed});
    },
    availed: function(request){
        //        var json = JSON.parse(request.responseText);
        var json = eval("("+request.responseText+")");
        $('availed_'+json.id).innerHTML = json.count;
        $('availed2_'+json.id).innerHTML = json.count;
    },
    render: function(town_info) {
        if(town_info){
            this.town_info = town_info;
        }
        if(this.sort_type == 'renew'){
            sort = "modified_sort=" + this.modified_sort;
        }else{
            sort = "sort=" + this.sort;
        }
        var url = '/rest/town/impression/'+this.town_info+'?format=json&' + sort+'&perpage='+this.perpage+'&page='+this.current_page+'&t='+(new Date().getTime());
        new Ajax.Request(
                         url, { method: 'get', onSuccess: this.rendered }
                         );
    },
    rendered: function(request) {
        //var json = JSON.parse(request.responseText);
        var json =  eval("("+request.responseText+")");
        if(json){
            Jemplate.process( 'town_impression.tt', json, $('cliptipBOX'));
            $('navi1').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            $('navi2').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            $('form').prev.value = json.pager.previous_page;
            $('form').next.value = json.pager.next_page;
            new Lightbox().setup_anchors();
        }else{
            $('navi1').innerHTML = '0-0(0件中)';
            $('navi2').innerHTML = '0-0(0件中)';
            $('form').prev.value = '';
            $('form').next.value = '';
        }
        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';
        }
        
    },
    switchingAll : function() {
        if(this.is_open){
            var comments = document.getElementsByClassName('commentBOX1open');
            comments.each(function( comment ){
                    comment.className = 'commentBOX1';
                });
        }else{
            var comments = document.getElementsByClassName('commentBOX1');
            comments.each(function( comment ){
                    comment.className = 'commentBOX1open';
                });
        }
        this.is_open = !this.is_open;
    }

}

var impression = new Clip.TownImpression();
