var pop_view_path_to_images = "http://xnimg.cn/xnapp/movie/img/";
var pop_view_current = 0;
var pop_view_path = 'http://movie.' + XN.env.domain + '/';
var shield;
function classifieds_pop_view(id, star) {
    shield = document.createElement("div");
    shield.className = "shield";
    if (isIE) shield.style.height = Element.getHeight(document.body) * 2 + "px";
    else  shield.style.height =Element.getHeight('moviepage') + "px";
    document.body.appendChild(shield);
    _score = star;
    var show_message = '';
    if (name)
        show_message = '&moviename=' + name;
    popup = new pop_dialog('classifieds_pop_view_dialog');
    popup.show_ajax_dialog_custom_loader('<div class="inner_loader">' + '<img src="' + pop_view_path_to_images + 'loading.gif">' + '</div>', pop_view_path + 'star.html?v=10&star=' + star + '&id=' + id + show_message);
	//popup.show_ajax_dialog_custom_loader('<div class="inner_loader">'+'<img src="'+pop_view_path_to_images+'loading.gif">'+'</div>','star.html');
}


function classifieds_listpop_view(id, score, name, annotation) {
    shield = document.createElement("div");
    shield.className = "shield";
    if (isIE) shield.style.height = Element.getHeight(document.body) * 2 + "px";
    else  shield.style.height = Element.getHeight('moviepage') + "px";
    document.body.appendChild(shield);
    _score = score;
    _name = name;
    _annotation = annotation;
    _mid = id;
    popup = new pop_dialog('classifieds_pop_view_dialog');
    popup.show_ajax_dialog_custom_loader('<div class="inner_loader">' + '<img src="' + pop_view_path_to_images + 'loading.gif">' + '</div>', pop_view_path + 'pop.html');
	//popup.show_ajax_dialog_custom_loader('<div class="inner_loader">'+'<img src="'+pop_view_path_to_images+'loading.gif">'+'</div>','star.html');
}

function close_dialog(dialog_to_close) {
    Element.remove(shield);
    pop_view_current = 0;
    generic_dialog.get_dialog(dialog_to_close).fade_out(100);
}
var __close_dialog = function(dialog_to_close) {
    return function() {
        close_dialog(dialog_to_close);
    }
}

//edit by joshua
function watchedOrWanted(action) {
    _pageStarCount = 0;
    new Ajax.Request("http://movie." + XN.env.domain + "/movie.do", {
        method:"post",
        parameters:"d=flushScore&checksum=" + token + "&mid=" + __mid + "&action=" + action + "&score=0",
        onSuccess:function(o) {
            getMovieScore(__mid);
            eval(o.responseText);
            $('movie_watched').innerHTML = look_info.watched_num;
            $('movie_want').innerHTML = look_info.want_num;
            $("wantedbut").style.display = "none";
            if (action == 1) {
                $('watchedText').style.display = "none";
                $('wantedText').style.display = "";
            }
            else {
                $('watchedText').style.display = "";
                $('wantedText').style.display = "none";
            }

            $('wantedOrWatchedDiv').style.display = "";
        },
        onFailure:function(o) {
            alert("操作失败")
        }
    });
}


function viewMovie(action) {
    //edit by joshua now we don't have to rate in order to clike the "want see" button
    if (_score == 0 && action == 2) {
        alert("请评分");
        return;
    }
	var temp = "";
	if(window._annotation!=undefined) 
		temp = _annotation;
    var annotation_value = $('movieReviewCont').value;
    annotation_value = annotation_value.replace(/ +$/, "");
    annotation_value = annotation_value.replace(/\n/, "");
    if (annotation_value == "请用一句话来评论这部电影吧。") {
        annotation_value = "";
    }
    if (annotation_value.length > 150) {
        if (confirm("字数超出了，把你的评论作为影评发布吗？")) {
            $('fh_score').value = '' + _score;
            $('fh_content').value = annotation_value;
            $('fh_mid').value = '' + __mid;
            $('hidden_comment_form').action = "/posttopic.do?d=toadd";
            $('hidden_comment_form').submit();
        }
        return;
    }

    _annotation = annotation_value;
    if (action == 1)
        _score = 0;
     //_initscore = _score;
    _pageStarCount = _score;
    $('watchedbtn').disabled = true;
    $('wantbtn').disabled = true;
    new Ajax.Request("http://movie." + XN.env.domain + "/movie.do", {
        method:"post",
        parameters:"d=flushScore&checksum=" + token + "&mid=" + __mid + "&action=" + action + "&score=" + _score + "&ma=" + annotation_value,
        onSuccess:function(o) {
			if(o.responseText.indexOf('alert')>=0){
				eval(o.responseText);
			    $('watchedbtn').disabled = false;
			    $('wantbtn').disabled = false;
				_annotation = temp;
				return;
			}
            getMovieScore(__mid);
            eval(o.responseText);
            $('movie_watched').innerHTML = look_info.watched_num;
            $('movie_want').innerHTML = look_info.want_num;
            hide($("pop_movie_content"));
            hide($("actionButtons"));
            if (action == 2)
                show($("msg_box"));
            else
                show($("ope_box"));
            //doMark(_initscore);
            $("wantedbut").style.display = "none";
            if (action == 1) {
                $('watchedText').style.display = "none";
                $('wantedText').style.display = "";
            }
            else {
                $('watchedText').style.display = "";
                $('wantedText').style.display = "none";
                flushMovieAnnotation();
            }
            $('wantedOrWatchedDiv').style.display = "";
            window.setTimeout(function() {
                close_dialog($('a_close'));
            }, 1000);
        },
        onFailure:function(o) {
            hide($("pop_movie_content"));
            hide($("actionButtons"));
            show($("err_box"));
            window.setTimeout(function() {
                close_dialog($('a_close'));
            }, 1000);
        }
    });
}


function rateMovie(action) {
    //edit by joshua now we don't have to rate in order to clike the "want see" button
    if (_score == 0 && action == 2) {
        alert("请评分");
        return;
    }
    var annotation_value = $('movieReviewCont').value;
    annotation_value = annotation_value.replace(/ +$/, "");
    annotation_value = annotation_value.replace(/^ +/, "");
    annotation_value = annotation_value.replace(/\n/g, "");
    if (annotation_value == "请用一句话来评论这部电影吧。") {
        annotation_value = "";
    }
    if (annotation_value.length > 150) {
        if (confirm("字数超出了，把你的评论作为影评发布吗？")) {
            $('fh_score').value = '' + _score;
            $('fh_content').value = annotation_value;
            $('fh_mid').value = '' +_mid;
            $('hidden_comment_form').action = "/posttopic.do?d=toadd";
            $('hidden_comment_form').submit();
        }
        return;
    }
    _annotation = annotation_value;
    if (action == 1)
        _score = 0;
    _pageStarCount = _score;
    new Ajax.Request("http://movie." + XN.env.domain + "/movie.do", {
        method:"post",
        parameters:"d=flushScore&checksum=" + token + "&mid=" + _mid + "&action=" + action + "&score=" + _score + "&ma=" + _annotation,
        onSuccess:function(o) {
			if(o.responseText.indexOf('alert')>=0){
				eval(o.responseText);
				return;
			}
            hide($("pop_movie_content"));
            hide($("actionButtons"));
            show($("msg_box"));
            chang_view(action, _mid, _score, _annotation);
            window.setTimeout(function() {
                close_dialog($('a_close'));
            }, 1000);
        },
        onFailure:function(o) {
            hide($("pop_movie_content"));
            hide($("actionButtons"));
            show($("err_box"));
            window.setTimeout(function() {
                close_dialog($('a_close'));
            }, 1000);
        }
    });
}


function flushMovieAnnotation(mid) {
    new Ajax.Request("http://movie." + XN.env.domain + "/movie.do", {
        method:"post",
        parameters:"d=anno&checksum=" + token + "&mid=" + __mid,
        onSuccess:function(o) {
            if (o.responseText && o.responseText != '') {
                $('annotation_area').innerHTML = o.responseText;
                $('oneSentenceMovie').style.display = '';
            }
        },
        onFailure:function(o) {
        }
    });
}

function getMovieScore(mid) {
    new Ajax.Request("http://movie." + XN.env.domain + "/movie.do", {
        method:"post",
        parameters:"d=getScore&checksum=" + token + "&mid=" + __mid,
        onSuccess:function(o) {
            eval(o.responseText);
            displayScore();
        },
        onFailure:function(o) {
        }
    });
}

function displayScore() {
    var _all = 0;
    for (var i = 1; i < 6; i++) {
        _all += allscore[i];
        $('score_value_' + i).innerHTML = allscore[i];
    }
    if (_all == 0) _all = 1;
    for (var i = 1; i < 6; i++) {
        $('score_item_' + i).style.width = 80.0 * allscore[i] / _all + "px";
    }
    updateAverageScore();
}
function updateAverageScore() {
    var txt = Array('', '很差', '较差', '还行', '推荐', '力荐');
    var sum = 0, _all = 0;
    for (var i = 1; i < 6; i++) {
        sum += i * allscore[i];
        _all += allscore[i]
    }
    if (!_all || _all == 0 || sum == 0) {
        $('scorethisDiv').style.display = "none";
        _scoreAverage = 0;
    }
    else {
        $('scorethis').style.display = "";
        var _scoreAverage = (sum / _all).toPrecision(3);
        var _S = _scoreAverage.substr(0, 1);
        var _Core = _scoreAverage.substr(1);
        $('mark_txt').innerHTML = txt[_S];
        $('scorethis').innerHTML = '<strong>' + _S + '</strong>' + _Core;
    }
    var starRating = document.getElementById('StarRating');
    var rating = starRating.getElementsByTagName('li');
    rating[0].className = 'current-rating';
    rating[0].style.width = (_scoreAverage / 5) * 100 + '%';
    return _scoreAverage;
    //$('scorethis').innerHTML = _scoreAverage;

    //alert(rating[0].style.width);
}
