jQuery(function(){
    var floatBox = jQuery("._floatBox:first");
    var fields = jQuery("input[type=text],textarea",floatBox);
                
    fields.each(function(){
        var node  = jQuery(this);
        node.css("color","#444");
        node.attr("data",node.val());
    });
    fields.focus(function(){
        var node  = jQuery(this);
        node.css("color","#000000");
        var val = node.val();
        if (val == node.attr("data")){
            node.val("");
        }
    });
    fields.blur(function(){
        var node  = jQuery(this);
        var val = node.val();
        if(val == null || val.length == 0){
            node.val(node.attr("data"));
            node.css("color","#444");
        }
    });
                
    var floatForm = jQuery("#floatForm");
    var valiCode = jQuery("input[name=guestbookValiCode]",floatForm);
    var valiCodeImg = jQuery("#valiCodeImg",floatForm);
    var valiCodeChange = jQuery("#valiCodeChange",floatForm);
    valiCode.focus(function(){
        var position = valiCode.position();
        valiCodeImg.css("position","absolute");
        valiCodeImg.css("top",position.top - valiCodeImg.css("height"));
        valiCodeImg.show();
    });
    valiCode.blur(function(){
        valiCodeImg.hide();
    });
    valiCodeChange.click(function(){
        valiCodeImg.find("img").attr("src","/valiCode.php?rand="+(new Date()).getMilliseconds());
        valiCodeImg.show();
    });
    floatForm.submit(function(data){
        fields.each(function(){
            if (fields.val() == fields.attr("data")){
                fields.val("");
            }
        });
        jQuery("input[name=guestbookTitle]",floatForm)
        .val(jQuery("input[name=guestbookName]",floatForm).val()+" \u7684\u7559\u8a00");
        jQuery.post(floatForm.attr("action"),floatForm.serializeArray(),function(data){
            if (data.indexOf("\u6210\u529f") > 0){
                alert("\u7559\u8a00\u6210\u529f");
                jQuery("._close",floatBox).click();
                fields.val("");
                fields.blur();
            }else{
                alert(jQuery(".stateMessageBox",data).text());
            }
        });
        return false;
    });
    floatBox.css("overflow", "hidden");
    jQuery("._close",floatBox).toggle(function(){
        var _btn = jQuery(this);
        _btn.removeClass("gb_bt01");
        _btn.addClass("gb_bt02");
        floatBox.animate({
            height: "-=229"
        }, {
            duration: 500 ,
            easing: "linear"
        });
    },function(){
        var _btn = jQuery(this);
        _btn.removeClass("gb_bt02");
        _btn.addClass("gb_bt01");
        floatBox.animate({
            height: "+=229"
        }, {
            duration: 500 ,
            easing: "linear"
        });
    });
    jQuery("._close",floatBox).click();
    floatBox.css("position", "absolute");
    var inter = window.setInterval(function(){
        var win = jQuery(window);
        var shift = 25;
        floatBox.css("top", win.scrollTop()  + win.height() - floatBox.height() - shift + "px");
        floatBox.css("left",win.scrollLeft() + win.width()  - floatBox.width()  - shift + "px");
    }, 5);
});
