var l = window.location.href;
swfobject.embedSWF("/images/" + (l.indexOf('qiushi') >= 0 ? 'qiushi' : '') + "flash.swf", "divFlashNumber", "100", "100", "9.0.0", "expressInstall.swf", {}, {wmode:'transparent'});

function setFlashPosition(obj){
    var pos = getObjectPosition(obj);
    var div = $("#divFlashNumber");
    div.css({left: pos.left - 50 + "px", top: pos.top - 50 + "px" });
}

function showAnimation(containerId, actionValue){
    var obj = document.getElementById(containerId);
    var flashx = document.getElementById("divFlashNumber");
    if(!flashx)return;

    setFlashPosition(obj);
    try{
        if (actionValue > 0){
            flashx.plus();
        }else{
            flashx.subtract();
        }
    }catch(e){}
}

function hideFlash(){
    $('#divFlashNumber').css({left: "-99px", top: "-99px"});
}

function testmovie_DoFSCommand(command, args) {if(command=="hideFlash"){alert("Here''s the Flash message: "+args);}}

function getObjectPosition(obj){
    var result = new Object();
    result.top = obj.offsetTop;
    result.left = obj.offsetLeft;

    if (obj.offsetParent != null){
        obj = obj.offsetParent;
        var pos = getObjectPosition(obj);

        result.top += pos.top;
        result.left += pos.left;
    }

    return result;
}

function SelfXY(){
    var yScrolltop;
    var xScrollleft;
    if (self.pageYOffset || self.pageXOffset) {
        yScrolltop = self.pageYOffset;
        xScrollleft = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){     // Explorer 6 Strict
        yScrolltop = document.documentElement.scrollTop;
        xScrollleft = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScrolltop = document.body.scrollTop;
        xScrollleft = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScrollleft + event.clientX ,yScrolltop + event.clientY) 
    return arrayPageScroll;
}


/**
 *  速度标记
 *
 */

function SpeedTester(){
    this.displayControlId = "divSpeed";

    this.goodColor = "#33CC33";
    this.averageColor = "#3300FF";
    this.badColor = "#FF0033";

    this.goodMessage = "仅用{0}秒就载入了整个页面，您的网络真棒";
    this.averageMessage = "载入整个页面用了{0}秒，您的网络马马虎虎";
    this.badMessage = "居然用了{0}秒才把整个页面载入进来，实在是抱歉";
    this.goodSpeed = 10000;
    this.averageSpeed = 50000;
    this.badSpeed = 100000;

    this.beginTest = function()    {
        this.startTime = new Date();

        window.onload = function(){
            var displayControl =document.getElementById(SpeedTester.displayControlId);

            SpeedTester.endTime = new Date();

            var spentTime = SpeedTester.endTime - SpeedTester.startTime;

            if (spentTime <= SpeedTester.goodSpeed){
                displayControl.style.backgroundColor = SpeedTester.goodColor;
                displayControl.title = SpeedTester.goodMessage.replace("{0}", spentTime / 1000);
            }else if (spentTime <= SpeedTester.averageSpeed){
                displayControl.style.backgroundColor = SpeedTester.averageColor;
                displayControl.title = SpeedTester.averageMessage.replace ("{0}", spentTime / 1000);
            }else{
                displayControl.style.backgroundColor = SpeedTester.badColor;
                displayControl.title = SpeedTester.badMessage.replace("{0}", spentTime / 1000);
            }
        }
    }
}

SpeedTester = new SpeedTester();
SpeedTester.beginTest();

function addBookmark(title,url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    } else if( document.all ) {
        window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
        return true;
    }
    return false;
}
//-->

function voteup(id, link){
    var posscore = parseInt($('#pos-score-'+id).text());
    showAnimation('score'+id, 1);
    $('#pos-score-'+id).text(posscore + 1);
    $('#score'+id+' .voteicon').hide();
    $.get(link.href);
    $(link).hide();
    return false;
}

function votedown(id, link){
    var negscore = parseInt($('#neg-score-'+id).text());
    showAnimation('neg-score-'+id, -1);
    $('#neg-score-'+id).text(negscore - 1)
    $('#score'+id+' .voteicon').hide();
    $.get(link.href);
    $(link).hide();
    return false;
}

$(function(){
    $('.favorite-button').click(function(){
        new $(this).load($(this).children('a').attr('href'));
        return false;
    });
    $('input.numeric').keydown(function(e){
        var k = e.keyCode;
        if(((k>47)&&(k<58)) ||
            (k == 8) ||
            (k == 46)||
            (k == 13)||
            (k>=96 && k<=105)){
            //            event.returnValue = true;
            return true;
        } else {
            e.returnValue = false;
            return false;
        }
    });
});

function open_form(id){
    $('#quickform-' + id).show();
    $('#reply-'+id).hide();
}

function close_form(id){
    $('#quickform-' + id).hide();
    $('#reply-'+id).show();
}

function reply(id){
    var f = $('#quickform-' + id);
    $.ajax({
        type: "POST",
        url: f.attr('action'),
        data: f.serialize(),
        success: function(){
            $('#comment-'+id)
        }
    })
    close_form(id);
    return false;
}

function vote2(id, v){
    var posscore = parseInt($('#pos-score-'+id).text());
    var negscore = parseInt($('#neg-score-'+id).text());
    showAnimation('score-'+id, v);
    $.get('/articles/vote'+(v>0?'up':'down')+'/'+id);
    if(v>0){posscore++}else{negscore--}
    $('#score-'+id).html('<strong>' + posscore + '</strong>人支持<span class="space" style="zoom:1">|</span><strong>' + negscore + '</strong>槑');
}

var COMMENT_WARNING='请不要发表与本内容无关的评论，您有了账号就是有身份的人了，我们可认识您。'
function clear_warning(e){
    var t = $(this);
    t.focus();
    if($.trim(t.val()) == COMMENT_WARNING){
        t.val('');
    }
    t.blur(function(){
        var t=$(this);
        if($.trim(t.val())==''){
            t.val(COMMENT_WARNING);
        }
    });

}
$(function(){
    $('#comment_submit').click(function(){
        if($.trim($('#comment_text').val()) == ''){
            return false;
        }
        $.ajax({
            complete: function(){
                $("#comment_post").hide();
                $("#comment_error").text('您的评论已成功提交，将在审核通过之后发表。');
                $("#comment_submit").val('发表评论');
                $("#comment_submit")[0].disabled =  false;
            }
            });

        $.post(this.form.action, $(this.form).serialize());
        this.value = ('正在发表...');
        this.disabled = true;
        return false;
    });
    $('input.comment_submit').live('click', function(e){
        e.preventDefault();
        var e =$(this),f = this.form, fe = $(f);
        var v = $.trim(fe.find('.comment_input').val());
        if(v == '' || v == COMMENT_WARNING){
            return false;
        }
        
        $.post(f.action, fe.serialize(), function(data){
            //$("#comment_post").hide();
            //$("#comment_error").text('您的评论已成功提交，将在审核通过之后发表。');
            e.val('发表评论');
            e.attr('disabled', false);
            fe.find(".comment_input").val('');
            fe.find(".comment_input").height('50px');
            fe.parent().children('ul').append(data)
        });
        this.value = ('正在发表');
        this.disabled = true;
        
        return false;
    });
    $('.comment_input').live('click', clear_warning).live('mouseover', clear_warning);
    $('a.comments').click(function(e){
		var l=$(this);
        var id = /\d+/.exec(l.attr('id'));
        if(!id)return;
        id=id[0];
        var comments_el = $('#qiushi_comments_'+id);
        if(comments_el.size() == 0){
			var xx = $('#c-'+id).html();
			$('#c-'+id).text('载入中...');
            $.get('/articles/'+id+'/comments.html', null, function(data){
                if($('#qiushi_comments_'+id).size() == 0 ){
                    $('#qiushi_counts_'+id).after(data).toggleClass('qiushi_counts_afterclick');
                    $('#qiushi_comments_'+id).show();
					$('#c-'+id).html(xx);
                }
            });            
        }else{
            comments_el.toggle();
            $('#qiushi_counts_'+id).toggleClass('qiushi_counts_afterclick');
        }

        e.preventDefault();
    });
});

String.prototype.rot13 = function(){
    return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
    });
};
