String.prototype.trim = function() {//全角半角空白を取り除く
	return this.replace(/^[\s　]+|[\s　]+$/g, "");
}
Function.prototype.bind  = function(obj){
    var th = this;
    return function(){
        return th.apply(obj)
    }
}
var URLUtil = {};
URLUtil.URLRegExp = new RegExp('(http(s)?:\/\/[A-Za-z0-9%&=~?+-_/.#]+)',"gi");
URLUtil.urlToLink = function(s){
	return s.replace(URLUtil.URLRegExp,'<a href="$1" target="_blank">$1</a>');
}
URLUtil.AnchorRegExp = new RegExp('<a href="[A-Za-z0-9%&=~?+-_/.#]+" target="?_blank"?>([A-Za-z0-9%&=~?+-_/.#]+)</a>','gi');
URLUtil.unLink = function(s){
	return s.replace(URLUtil.AnchorRegExp,'$1');
}
URLUtil.isURL = function(s) {
    if (s.match(URLUtil.URLRegExp)) {
        return true;
    } else {
        return false;
    }
}
var EmailUtil = {};
EmailUtil.EmailRegExp = new RegExp('([a-zA-Z0-9./+_]+@[a-zA-Z0-9./+_]+)','gi')
EmailUtil.toLink = function(s){
	return s.replace(EmailUtil.EmailRegExp,'<a href="mailto:$1">$1</a>');
}
EmailUtil.AnchorRegExp = new RegExp('<a href="mailto:([a-zA-Z0-9./+_]+@[a-zA-Z0-9./+_]+)">([a-zA-Z0-9./+_]+@[a-zA-Z0-9./+_]+)</a>','gi');
EmailUtil.unLink = function(s){
	return s.replace(EmailUtil.AnchorRegExp,'$1');
}

var ArrayUtils = {};
ArrayUtils.is_array = function(obj){
  return !(
    !obj || 
    (!obj.length || obj.length == 0) || 
    typeof obj !== 'object' || 
    !obj.constructor || 
    obj.nodeType || 
    obj.item 
  );
};

var ApplieUtils = {};

var InputUtils={};
InputUtils.get_value = function(ele){
    if(!ele)return null;
    if(ele.type == 'checkbox' || ele.type =='radio' ){
        return ele.checked ? ele.value : null;
    }else if(ele.length){
        var value_list = null;
        for(var i=0; i<ele.length; i++){
            if(ele[i].checked){
                //return ele[i].value;
                if (!value_list) {
                    value_list = [ele[i].value];
                } else {
                    value_list.push(ele[i].value);
                }
            }
        }
        return value_list;
        //return null;
    }
    return ele.value;
};

var HTMLUtil = {};
HTMLUtil.escape = function(s){
	s = s.replace(/&/g,"&amp;");
	s = s.replace(/</g,"&lt;");
	s = s.replace(/>/g,"&gt;");
	s = s.replace(/"/g,"&quot;");
	s = s.replace(/'/g,"&#39;");
	return s;
}
HTMLUtil.unescape = function(s){
	s = s.replace(/&amp;/g,"&");
	s = s.replace(/&lt;/g,"<");
	s = s.replace(/&gt;/g,">");
	s = s.replace(/&quot;/g,'"');
	s = s.replace(/&#39;/g,"'");
	return s;
}
HTMLUtil.undisplay = function(s){
	s = s.replace(/\n/ig,"");
	s = HTMLUtil.unescape(s);
	s = URLUtil.unLink(s); // add
	s = EmailUtil.unLink(s); // add
	s = s.replace(/<br\s*\/?>/ig,"\n");
	return s;
}
HTMLUtil.display= function(s){
  if(ArrayUtils.is_array(s)){
    s = "[" + s + "]";
  }
	s = HTMLUtil.escape(s);
	s = URLUtil.urlToLink(s); // add
	s = EmailUtil.toLink(s); // add
	s = s.replace(/\n/g,"<br />");
	return s;
}

var HTTPConnection={};
HTTPConnection.post = function(url,data,on_success,on_error){
    data["csrf_token"] = $("#base_csrf_token").text();
    $.ajax({
        type:'POST',
        url: url,
        processData: true,
        data: data ,
        success:function(data,status){
            on_success(eval("("+data+")")); //TODO
        },
        error:function(xhr,status,err){
            var data = {};
            if(status == 'timeout'){
                data['msg'] = "サーバーと接続できません.";
            }else{
                try{
                    data = eval("("+xhr.responseText+")")
                }catch(e){
                    data['msg'] = "サーバー上で動作が不安定になっています。しばらくたってからやり直してみてください";
                }
            }
            on_error(data)
        }
    });
};
HTTPConnection.get = function(url,data,on_success,on_error){
    $.ajax({
        type:'GET',
        url: url,
        processData: true,
        data: data ,
        cache: false,
        success:function(data,status){
            on_success(eval("("+data+")"));
        },
        error:function(xhr,status,err){
            var data = {};
            if(status == 'timeout'){
                data['msg'] = "サーバーと接続できません.";
            }else{
                try{
                    data = eval("("+xhr.responseText+")")
                }catch(e){
                    data['msg'] = "サーバー上で動作が不安定になっています。しばらくたってからやり直してみてください";
                }
            }
            on_error(data)
        }
    });
};

function PostForm(disp_id, fields, src_prefix, defaultparams){
    this.disp_id = disp_id;
    this.fields = fields;
    this.src_prefix = src_prefix;
    this.area = $("#"+this.disp_id);
    this.params = defaultparams ? defaultparams : {};
};
$.extend(PostForm.prototype,{
    show_form : function(){
        this.area.hide();
        var disp_params = {};
        for(var i=0;i<this.fields.length;i++){
            if(this.params[this.fields[i]]){
                disp_params[this.fields[i]] = HTMLUtil.display(this.params[this.fields[i]]);
            }
        }
        this.area.html( TrimPath.processDOMTemplate(this.src_prefix+"1",disp_params));
        //this.area.html( $("#"+this.src_prefix+"1").html() );
        var frm = this.find("form").get(0); 
        for(var k=0;k<this.fields.length;k++){// initial form value
            var nm = this.fields[k];
            if(!this.params[nm])continue;
            var f = frm[nm];
            if (f.type == 'checkbox' || f.type == 'radio'){
                f.checked ='true'; 
            }else if(f.length){ //checkboxes,radio buttons
                for(var i=0;i<f.length;i++){
                    for(var j=0;j<this.params[nm].length;j++){
                        if(f[i].value == this.params[nm][j]){
                            f[i].checked="true";
                        }
                    }
                }
            }else{
                f.value = this.params[nm];
            }
        }
        this.find("[name=next]").bind("click",{},function(e){
            var frm = this.validate();
            if(!frm){
                return false;
            }
            for(var i=0;i<this.fields.length;i++){
                var nm = this.fields[i];
                this.params[nm] = InputUtils.get_value(frm[nm]);
            }
            this.show_confirm();
            return false;
        }.bind(this));
        this.find("[name=prev]").bind("click",function(){
            this.cancel_form();
            return false;
        }.bind(this));
        this.area.fadeIn();
    }
    ,cancel_form : function(){
    }
    ,validate : function(){
        return this.find("form").get(0);
    }
    ,show_confirm : function(){
        var disp_params = {};
        for(var i=0;i<this.fields.length;i++){
            if(this.params[this.fields[i]]){
                disp_params[this.fields[i]] = HTMLUtil.display(this.params[this.fields[i]]);
            }
        }
        disp_params['_params'] = this.params;
        disp_params._MODIFIERS = {'show_selecttag': function(s) {
            for(var i=0; i<ApplieSelectTags.length; i++){
                if (ApplieSelectTags[i].id == parseInt(s)){
                    return ApplieSelectTags[i].name;
                }
            }
        }};
        this.area.html( TrimPath.processDOMTemplate(this.src_prefix+"2",disp_params));
        this.find("[name=prev]").bind("click",function(){
            this.back_form();
            return false;
        }.bind(this));
        var th = this
        this.find("[name=next]").bind("click",function(e){
            if(PostForm.post_wait)return false;
            PostForm.post_wait=true;
            th.post();
            setTimeout(function(){PostForm.post_wait=false;},2000);
            e.preventDefault(); 
        });
    }
    ,back_form : function(){
        this.show_form();
    }
    ,post : function(){
        return true;
    }
    ,find : function(s){
        return this.area.find(s);
    }
    ,get_hash : function(s){
        var ret = {}
        for(p in this.params){
            var val = this.params[p]
            if(val!=null && val!=undefined)ret[p]=val;
        }
        return ret;
    }
});

function WomToApplicationForm(disp_id, src_prefix, app_id, post_url){
    this.post_url = post_url || "/post_wom_to_app";
    this.disp_id = disp_id;
    this.app_id = app_id;
    PostForm.apply(this, [disp_id, ['content', 'selecttags','freetags', 'rating1', 'rating2', 'rating3', 'rating4', 'rating5', "post_to_twitter"], src_prefix]);
};
$.extend(WomToApplicationForm.prototype,PostForm.prototype,{
    post_url : this.post_url
    ,show_form : function(){
        PostForm.prototype.show_form.apply(this);
        var frm = this.find("form").get(0);
    }
    ,validate : function(){
        this.find(".errors").html("");
        var errors = [];
        var frm = this.find("form").get(0);
        var checked = false;
        for(var i=1; i<=5; i++){
            var elem = frm['rating'+i];
            if(elem){
                for(var j=0; j<elem.length; j++){
                    if(elem[j].checked){
                        checked = true;
                        break;
                    }
                }
            }
            if(checked == true){
                break;
            }
        }
        if( !checked && !frm.content.value ){
            errors.push('クチコミもしくはアプリケーションの評価は必須入力です');
        }
        if(errors.length){
            this.find(".errors").html( errors.join("<br>") ) ;
            return false;
        } 
        return frm;
    }
    ,show_confirm :function(){
        PostForm.prototype.show_confirm.apply(this);
    }
    ,back_form : function(){
        this.show_form();
        $('input.rating-star').rating();
    }
    ,do_when_post_success: function(data){
        this.area.html( $("#"+this.src_prefix+"3").html() );
        location.reload(); //TODO no reload
    }
    ,do_when_post_failure : function(data){
        this.area.html( TrimPath.processDOMTemplate(this.src_prefix+"_error",{'msg':data.msg}));
    }
    ,post : function(){
        var params = this.get_hash();
        params['app_id'] = this.app_id;
        var th = this;
        HTTPConnection.post(this.post_url,params
            ,function(data){
                th.do_when_post_success(data);
            }
            ,function(data){
                th.do_when_post_failure(data);
            }
        );
    }
});

function WomToReviewForm(disp_id, src_prefix, review_id, post_url){
    this.post_url = post_url || "/post_rating_and_wom_to_review";
    this.disp_id = disp_id;
    this.review_id= review_id;
    PostForm.apply(this, [disp_id, ['evaluate', 'content', 'is_public', "post_to_twitter"], src_prefix]);
};
$.extend(WomToReviewForm.prototype,WomToApplicationForm.prototype,{
    post_url : this.post_url
    ,validate : function(){
        this.find(".errors").html("");
        var errors = [];
        var frm = this.find("form").get(0);
        if((frm.evaluate && !frm.evaluate[0].checked && !frm.evaluate[1].checked && !frm.content.value) || (!frm.evaluate && !frm.content.value)){
            errors.push('内容が入力されていません');
        }
        if(errors.length){
            this.find(".errors").html( errors.join("<br>") ) ;
            return false;
        } 
        return frm;
    }
    ,post : function(){
        var params = this.get_hash()
        params['review_id'] = this.review_id;
        var th = this;
        HTTPConnection.post(this.post_url,params
            ,function(data){
                th.do_when_post_success(data);
            }
            ,function(data){
                th.do_when_post_failure(data);
            }
        );
    }
});

function WomToReviewUpdateForm(disp_id, src_prefix, wom_id, post_url){
    this.post_url = post_url || "/update_wom_to_review";
    this.disp_id = disp_id;
    this.wom_id= wom_id;
    PostForm.apply(this, [disp_id, ['content', 'is_public'], src_prefix]);
};
$.extend(WomToReviewUpdateForm.prototype,WomToApplicationForm.prototype,{
    post_url : this.post_url
    ,post : function(){
        var params = this.get_hash()
        params['wom_id'] = this.wom_id;
        var th = this;
        HTTPConnection.post(this.post_url,params
            ,function(data){
                th.do_when_post_success(data);
            }
            ,function(data){
                th.do_when_post_failure(data);
            }
        );
    }
});

function WomToAppUpdateForm(disp_id, src_prefix, wom_id, post_url){
    this.post_url = post_url || "/update_wom_to_app";
    this.disp_id = disp_id;
    this.wom_id= wom_id;
    PostForm.apply(this, [disp_id, ['content', 'selecttags','freetags', 'rating1', 'rating2', 'rating3', 'rating4', 'rating5'], src_prefix]);
};
$.extend(WomToAppUpdateForm.prototype,WomToApplicationForm.prototype,{
    post_url : this.post_url
    ,post : function(){
        var params = this.get_hash()
        params['wom_id'] = this.wom_id;
        var th = this;
        HTTPConnection.post(this.post_url,params
            ,function(data){
                th.do_when_post_success(data);
            }
            ,function(data){
                th.do_when_post_failure(data);
            }
        );
    }
});


function ReviewForm(disp_id, src_prefix, app_id, post_url){
    this.post_url = post_url || "/post_review_to_app";
    this.disp_id = disp_id;
    this.app_id = app_id;
    PostForm.apply(this, [disp_id, ['title', 'content', 'selecttags','freetags', 'rating1', 'rating2', 'rating3', 'rating4', 'rating5'], src_prefix]);
};
$.extend(ReviewForm.prototype, WomToApplicationForm.prototype, {
    post_url : this.post_url
    ,validate : function(){
        this.find(".errors").html("");
        var errors = [];
        var frm = this.find("form").get(0);
        var checked = false;
        for(var i=1; i<=5; i++){
            var elem = frm['rating'+i];
            if(elem){
                for(var j=0; j<elem.length; j++){
                    if(elem[j].checked){
                        checked = true;
                        break;
                    }
                }
            }
            if(checked == true){
                break;
            }
        }
        if( !checked ){
            errors.push('評価が入力されていません');
        }
        if( !frm.title.value ){
            errors.push('タイトルが入力されていません');
        }
        if( !frm.content.value ){
            errors.push('本文が入力されていません');
        }
        if(errors.length){
            this.find(".errors").html( errors.join("<br>") ) ;
            return false;
        } 
        return frm;
    }
    ,show_confirm : function(){
        var disp_params = {};
        for(var i=0;i<this.fields.length;i++){
            if(this.params[this.fields[i]]){
                disp_params[this.fields[i]] = HTMLUtil.display(this.params[this.fields[i]]);
            }
        }
        disp_params['_params'] = this.params;
        // content だけそのまま表示
        if (this.params['content']){
          disp_params['content'] = this.params['content'];
        }
        disp_params._MODIFIERS = {'show_selecttag': function(s) {
            for(var i=0; i<ApplieSelectTags.length; i++){
                if (ApplieSelectTags[i].id == parseInt(s)){
                    return ApplieSelectTags[i].name;
                }
            }
        }};
        this.area.html( TrimPath.processDOMTemplate(this.src_prefix+"2",disp_params));
        this.find("[name=prev]").bind("click",function(){
            this.back_form();
            return false;
        }.bind(this));
        var th = this
        this.find("[name=next]").bind("click",function(e){
            if(PostForm.post_wait)return false;
            PostForm.post_wait=true;
            th.post();
            setTimeout(function(){PostForm.post_wait=false;},2000);
            e.preventDefault(); 
        });
    }
    ,back_form : function(){
        this.show_form();
        $('input.rating-star').rating();
        PostReviewPage.nicEditor.panelInstance('review_text');
        nicEditors.findEditor('review_text').setContent(this.params['content']);
    }
    ,post : function(){
        var params = this.get_hash();
        params['app_id'] = this.app_id;
        var th = this;
        HTTPConnection.post(this.post_url,params
            ,function(data){
                th.do_when_post_success(data);
            }
            ,function(data){
                th.do_when_post_failure(data);
            }
        );
    }
});


function SimplePaginator(src_prefix, render_info, callback) {
    this.src_prefix = src_prefix;
    this.num_current = 1;
    this.render_info = render_info;
    this.callback = callback;
};
$.extend(SimplePaginator.prototype, {
    show : function() {
        this.go_page(this.num_current);
    },
    render : function(data) {
        var context = data;
        var th = this;
        $('#' + this.src_prefix + '_paginator').html(
            TrimPath.processDOMTemplate(this.src_prefix + '_paginator_tpl', context)
        );
        if (th.render_info) {
            $('#' + this.src_prefix + '_paginator_info').html(
                TrimPath.processDOMTemplate(this.src_prefix + '_paginator_info_tpl', context)
            );
        }
        $('#' + this.src_prefix + '_paginator_prev_link').bind('click', function(){
            th.go_prev();
        });
        $('#' + this.src_prefix + '_paginator_next_link').bind('click', function(){
            th.go_next();
        });
        $('.' + this.src_prefix + '_paginator_page_link').bind("click", function(){
            th.go_page(parseInt($(this).text()));
        });
        th.callback(data);
    },
    go_page : function(p) {
        var page = this._lazy_value(p);
        this.render(this.get_data(p));
        this.num_current = p;
    },
    get_data : function(p) {
        return {'paginator': null};
    },
    go_prev : function() {
        if (this.num_current <= 1) {
            this.go_page(1);
        } else {
            this.go_page(this.num_current - 1);
        }
    },
    go_next : function() {
        var data = this.get_data();
        if (data.paginator == null) {
            this.show();
        } else if (this.num_current >= data.paginator.num_pages) {
            this.go_page(data.paginator.num_pages);
        } else {
            this.go_page(this.num_current + 1);
        }
    },
    _lazy_value : function(val) {
        if (typeof(val) == "function") {
            return val();
        } else {
            return val;
        }
    }
});

function AjaxPaginator(src_prefix, render_info, callback, url) {
    SimplePaginator.apply(this, [src_prefix, render_info, callback]);
    this.url = url;
};
$.extend(AjaxPaginator.prototype, SimplePaginator.prototype, {
    go_page : function(p) {
        var params = {'page': p};
        var th = this;
        HTTPConnection.get(
            this.url,
            params,
            function(data){
                th.render(data);
                th.num_current = params.page;
            },
            function(data){
                $('#' + th.src_prefix + '_paginator_errors').text(data.msg);
            }
        );
    },
    go_next : function() {
        var th = this;
        var params = {'page': th.num_current};
        HTTPConnection.get(
            th.url,
            params,
            function(data) {
                if (data.paginator == null) {
                    th.show();
                } else if (th.num_current >= data.paginator.num_pages) {
                    th.go_page(data.paginator.num_pages);
                } else {
                    th.go_page(th.num_current + 1);
                }
            },
            function(data){
                $('#' + th.src_prefix + '_paginator_errors').text(data.msg);
            }
        );
    }
});

function AjaxCachePaginator(src_prefix, render_info, callback, url, num_cache) {
    AjaxPaginator.apply(this, [src_prefix, render_info, callback, url]);
    this.cache = [];
    this.from = 1;
    this.to = 1;
    this.num_pages = 1;
    this.num_cache = num_cache;
};
$.extend(AjaxCachePaginator.prototype, AjaxPaginator.prototype, {
    clear : function() {
        this.cache = [];
    },
    refresh : function() {
        var temp = [];
        for (var i = this.from ; i <= this.to ; i++) {
            temp[i] = this.get_cache(i);
        }
        this.cache = temp;
    },
    get_cache : function(p) {
        try {
            return this.cache[p];
        } catch(e) {
            return null;
        }
    },
    go_page : function(p) {
        var th = this;
        this.from = Math.max(p - this.num_cache, 1);
        this.to = p + this.num_cache;
        this.refresh();
        
        var page = this.get_cache(p);
        if (page) {
            this.render({'paginator': page});
            this.num_current = p;
            return true;
        }
        
        var params = {'page_from': this.from, 'page_to': this.to};
        HTTPConnection.get(
            this.url,
            params,
            function(data){
                if (data.paginators == null) {
                    th.render({'paginator': null});
                    return;
                }
                th.num_pages = data.paginators[0].num_pages;
                if (th.num_pages < th.to) {
                    th.to = th.num_pages;
                }
                th.render({'paginator': data.paginators[p - th.from]});
                th.num_current = p;
                for (var i = th.from ; i <= th.to ; i++) {
                    th.cache[i] = data.paginators[i - th.from];
                }
            },
            function(data){
                $('#' + th.src_prefix + '_paginator_errors').text(data.msg);
            }
        );
    },
    go_next : function() {
        if (this.num_pages > this.num_current) {
            this.go_page(this.num_current + 1);
        }
    }
});



/* AppDetailPage */
var AppDetailPage = {};
AppDetailPage.show_post_review_form = function(){
  if ($("#review_form_wrapper").css('display') == 'block'){
    $("#review_form_wrapper").slideUp();
    $("#wom_form_wrapper").hide();
  }else{
    $("#review_form_wrapper").slideDown();
    $("#wom_form_wrapper").hide();
  }
};
AppDetailPage.show_post_wom_form = function(is_wom){ 
  if ($("#wom_form_wrapper").css('display') == 'block'){
    $("#wom_form_wrapper").slideUp();
    $("#review_form_wrapper").hide();
//    $("#require_wom").hide();
//    $("#require_rating").hide();
  }else{
    $("#wom_form_wrapper").slideDown();
    $("#review_form_wrapper").hide();
//    if (is_wom == true){
//        $("#require_wom").show();
//        $("#require_rating").hide();
//    } else if(is_wom == false){
//        $("#require_wom").hide();
//        $("#require_rating").show();
//    }
  }
};
AppDetailPage.post_rating_to_app_wom= function(wom_id,rating, post_url){
  this.post_url = post_url || "/post_rating_to_app_wom";
  HTTPConnection.post(this.post_url, {'wom_id' : wom_id, 'rating': rating}
    ,function(data){
      var h =$("#wom_rating_num_"+wom_id);
      h.html( 1 + new Number(h.html()) );
      $("#post_rating_app_wom_btn_"+wom_id).fadeOut();
    }
    ,function(data){
    }
  );
};
AppDetailPage.post_rating_to_review_wom= function(wom_id,rating, post_url){
  this.post_url = post_url || "/post_rating_to_review_wom";
  HTTPConnection.post(this.post_url, {'wom_id' : wom_id, 'rating': rating}
    ,function(data){
      var h =$("#review_wom_rating_num_"+wom_id);
      h.html( 1 + new Number(h.html()) );
      $("#post_rating_review_wom_btn_"+wom_id).fadeOut();
    }
    ,function(data){
    }
  );
};
AppDetailPage.removew_app_wom = function(wom_id, post_url){
  this.post_url = post_url || "/remove_app_wom";
  HTTPConnection.post(this.post_url, {'wom_id' : wom_id}
    ,function(data){
      $("#wom_"+wom_id).fadeOut();
    }
    ,function(data){
      alert("削除に失敗しました");
    }
  );
};
AppDetailPage.remove_app_image = function(id, post_url){
  this.post_url = post_url || "/delete_app_image";
  if(!confirm('本当に削除してよろしいですか？')){
    return;
  }
  HTTPConnection.post(this.post_url, {'id': id}
    ,function(data){
      $("#app_image_item_"+id).fadeOut();
    }
    ,function(data){
    }
  );
}
AppDetailPage.remove_review_image = function(id, post_url){
  this.post_url = post_url || "/delete_review_image";
  if(!confirm('本当に削除してよろしいですか？')){
    return;
  }
  HTTPConnection.post(this.post_url, {'id': id}
    ,function(data){
      $("#review_image_item_"+id).fadeOut();
    }
    ,function(data){
    }
  );
}
AppDetailPage.edit_app_wom = function(wom_id, content_id, freetags, selecttags, rating1, rating2, rating3, rating4, rating5){
  var frm =  new WomToAppUpdateForm("buzz-edit"+wom_id, "wom_update_step", wom_id);
  frm.params.content = HTMLUtil.undisplay($("#"+content_id).html());
  frm.params.freetags = freetags;
  frm.params.selecttags= selecttags;
  frm.params.rating1 = rating1;
  frm.params.rating2 = rating2;
  frm.params.rating3 = rating3;
  frm.params.rating4 = rating4;
  frm.params.rating5 = rating5;
  frm.show_form();
  $('input.rating-star').rating(); 
};

/* ReviewDetailPage */
var ReviewDetailPage = {};
ReviewDetailPage.post_rating_to_review_wom= function(wom_id,rating, post_url){
  this.post_url = post_url || "/post_rating_to_review_wom";
  HTTPConnection.post(this.post_url, {'wom_id' : wom_id, 'rating': rating}
    ,function(data){
      var h =$("#review_wom_rating_num_"+wom_id);
      h.html( 1 + new Number(h.html()) );
      $("#post_rating_review_wom_btn_"+wom_id).fadeOut();
    }
    ,function(data){
      $("#post_rating_review_wom_btn_"+wom_id).fadeOut();
    }
  );
};
ReviewDetailPage.removew_review_wom = function(wom_id, post_url){
  this.post_url = post_url || "/remove_review_wom";
  HTTPConnection.post(this.post_url, {'wom_id' : wom_id}
    ,function(data){
      $("#review_wom_"+wom_id).fadeOut();
    }
    ,function(data){
      $("#review_wom_"+wom_id).fadeOut();
    }
  );
};
ReviewDetailPage.edit_review_wom = function(wom_id){
  var frm =  new WomToReviewUpdateForm("review-buzz-edit"+wom_id, "review_wom_update_step", wom_id);
  frm.params.content = HTMLUtil.undisplay($("#review_wom_content_"+wom_id).html());
  frm.params.is_public = $("#review_wom_public_"+wom_id).text();
  frm.show_form();
};


/* for ranking */
var myModifiers = {
  zeroPrefix : function(str, totalLength) {
    var zerostr = "000000000000000"+str;
    return (zerostr).substring(zerostr.length - totalLength, zerostr.length);
  }
};
function RankingBuilder(template_id, container_id, error_container_id) {
  this.template_id = template_id;
  this.container_id = container_id;
  this.error_container_id = error_container_id;
};
$.extend(RankingBuilder.prototype, {
  show : function(url, category) {
    if (category == null || category == undefined) {
      category = "";
    }
    this.get_data(url, category);
  },

  rnder : function(data) {
    var th = this;
    data._MODIFIERS = myModifiers;

    $(th.container_id).html(
      TrimPath.processDOMTemplate(th.template_id, data)
    );
  },

  get_data : function(url, category) {
    var th = this;
    var url = url;
    var params = {"category": category};
    HTTPConnection.get(
      url,
      params,
      function(data) {
        if (data.context == null) {
          th.rnder({"data": null});
          return;
        }
        th.rnder({"data": data.context});
      },
      function(data){
        $(th.error_container_id).text(data.msg);
      }
    );
  }
});
