$(document).ready(function(){
    var lockFlag = false;
    var imgLastId = imgCnt-1; //总的最大的图片编号
    var apiUrl = urlRoot+"photo/view_ajax.php";

    if (thisImgId < firstImgId) {
        thisImgId = firstImgId;
    }
    if (firstImgId+$("#small_img_ls div").size()-1 > imgLastId) {
        firstImgId = imgLastId-$("#small_img_ls div").size()+1;
    }
    showLoading();
    setMouseEvent();

    var curFlag = "";
    $("#img_big").mousemove(function (e) {
        if (typeof(event)!='undefined') { //IE
            var imgX = event.offsetX;
        }else{ //FF
            var mouseLeft = (typeof(event)!='undefined') ? event.clientX : e.pageX;
            var imgLeft = $(this).offset().left;
            var imgX = mouseLeft-imgLeft;
        }
        var thisTitle = $("#spn_title").html();
        if (imgX < $(this).width()/2) {
            if (curFlag!="right") {
                $(this).attr("title", "[上一张] "+thisTitle)
                       .css("cursor", "url(photo/images/cur_left.cur),auto")
                       .unbind("click")
                       .click(function () {
                           $("#btn_prev").click();
                       });
            }
            curFlag = "right";
        }else{
            if (curFlag!="left") {
                $(this).attr("title", thisTitle+" [下一张]")
                       .css("cursor", "url(photo/images/cur_right.cur),auto")
                       .unbind("click")
                       .click(function () {
                           $("#btn_next").click();
                       });
            }
            curFlag = "left";
        }
    });

    $("#btn_prev, .btn_prev").click(function () {
        if (lockFlag==true) {
            return false;
        }
        if (thisImgId==0) {
            var url = urlFormat;
            url = url.replace("#ALBUMID#", prevAlbumId);
            url = url.replace("#PAGE#", 0);
            window.location = url;
            return;
        }
        lockFlag = true;
        showLoading();
        var imgId = thisImgId-1;
        $.get(apiUrl,
            { a:albumId, i:imgId },
            function(data){
                eval("var resultObj = "+data+";");
                setImgShow(resultObj);
                if (thisImgId > firstImgId) {
                    thisImgId--;
                    setThisImgStyle();
                    lockFlag = false;
                }else if (thisImgId == firstImgId){
                    var img = "<img src='"+resultObj.small_img_src+"' onload='formatSize(this, \"small\")'>";
                    $("#small_img_ls").prepend('<div style="display:none;">'+img+'</div>');
                    $("#small_img_ls div:first").slideDown("fast", function () {
                        $("#small_img_ls div:last").slideUp("fast", function () {
                            $(this).remove();
                            firstImgId--;
                            thisImgId--;
                            setMouseEvent();
                            lockFlag = false;
                        });
                    });
                }
            }
        );
        return false;
    });

    $("#btn_next, .btn_next").click(function () {
        if (lockFlag==true) {
            return false;
        }
        if (thisImgId==imgLastId) {
            var url = urlFormat;
            url = url.replace("#ALBUMID#", nextAlbumId);
            url = url.replace("#PAGE#", 0);
            window.location = url;
            return false;
        }
        lockFlag = true;
        showLoading();
        var imgId = thisImgId+1;
        if (false){
            var url = urlFormat;
            url = url.replace("#ALBUMID#", albumId);
            url = url.replace("#PAGE#", imgId);
            window.location = url;
            return false;
        }
        $.get(apiUrl,
            { a:albumId, i:imgId },
            function(data){
                eval("var resultObj = "+data+";");
                setImgShow(resultObj);
                if (thisImgId < firstImgId+$("#small_img_ls div").size()-1) {
                    thisImgId++;
                    setThisImgStyle();
                    lockFlag = false;
                }else if (thisImgId == firstImgId+$("#small_img_ls div").size()-1) {
                    var img = "<img src='"+resultObj.small_img_src+"' onload='formatSize(this, \"small\")'>";
                    $("#small_img_ls").append('<div style="display:none;">'+img+'</div>');
                    $("#small_img_ls div:last").slideDown("fast", function () {
                        $("#small_img_ls div:first").slideUp("fast", function () {
                            $(this).remove();
                            firstImgId++;
                            thisImgId++;
                            setMouseEvent();
                            lockFlag = false;
                        });
                    });
                }
            }
        );
        return false;
    });

    $(".btn_prev_album").click(function () {
        windo_location(prevAlbumId, 0);
    });

    $(".btn_next_album").click(function () {
        windo_location(nextAlbumId, 0);
    });

    $(".btn_first").click(function () {
        windo_location(albumId, 0);
    });

    $(".btn_last").click(function () {
        windo_location(albumId, imgCnt-1);
    });

    //设置小图片上的鼠标事件
    function setMouseEvent() {
        setThisImgStyle();
        $("#small_img_ls div").unbind("click");
        $("#small_img_ls div").mouseover(function () {
            $(this).addClass("view");
        }).mouseout(function () {
            if (thisImgId-firstImgId != $("#small_img_ls div").index(this)) {
                $(this).removeClass("view");
            }
        }).click(function () {
            lockFlag = true;
            showLoading();
            var imgId = firstImgId+$("#small_img_ls div").index(this);
            $.get(apiUrl,
                { a:albumId, i:imgId },
                function(data){
                    eval("var resultObj = "+data+";");
                    setImgShow(resultObj);
                    thisImgId = imgId;
                    setThisImgStyle();
                    lockFlag = false;
                }
            );
            return false;
        });
    }

    //设置当前小图片样式
    function setThisImgStyle() {
        $("#small_img_ls div").removeClass("view");
        $("#small_img_ls div:nth-child("+(thisImgId-firstImgId+1)+")").addClass("view");
    }

    //显示大图,评论
    function setImgShow(imgInfoObj) {
        $(".thickbox").attr("show", imgInfoObj.big_img_src);
        $("#img_big").removeAttr("width");
        $("#img_big").removeAttr("height");
        $("#img_big").attr("src", imgInfoObj.big_img_src);
        $("#img_big").css({width:"", height:""});
        $("#div_comment").html(imgInfoObj.comment);
        $("#img_loading").html("");
        if (typeof imgInfoObj.small_img_prev_src == "string") {
            $("#img_loading").append('<img src="'+imgInfoObj.small_img_prev_src+'">');
        }
        if (typeof imgInfoObj.big_img_prev_src == "string") {
            $("#img_loading").append('<img src="'+imgInfoObj.big_img_prev_src+'">');
        }
        if (typeof imgInfoObj.small_img_next_src == "string") {
            $("#img_loading").append('<img src="'+imgInfoObj.small_img_next_src+'">');
        }
        if (typeof imgInfoObj.big_img_next_src == "string") {
            $("#img_loading").append('<img src="'+imgInfoObj.big_img_next_src+'">');
        }
        $('#copy_div').slideUp();
        $('#fav_div').slideUp();
    }

    function showLoading() {
        var width = 130;
        var height = 25;
        var top = $(document).scrollTop();
        var left = $(document).scrollLeft()+$(window).width()-width-9;
        $("body").append("<div class='div_loading' style='padding:3px 0 0 9px; color:#FFFFFF; background-color:#6699FF; position:absolute; z-index:99;'>载入中..请稍候...</DIV>");
        $(".div_loading").css({"width":width, "height":height, "top":top, "left":left}); //aaaaaaa
    }

    function windo_location(aid, pid) {
        var url = urlFormat;
        url = url.replace("#ALBUMID#", aid);
        url = url.replace("#PAGE#", pid);
        window.location = url;
        return false;
    }
});

$(document).ready(function(){
    $(".btn_show_fav_div").click(function () {
        $("#fav_code_div").html(getFavHtmlCode());
        $('#fav_div').slideToggle("fast");
    });

    $(".btn_show_copy_div").click(function () {
        var pageUrl = urlFormat;
        pageUrl = pageUrl.replace("#ALBUMID#", albumId);
        pageUrl = pageUrl.replace("#PAGE#", thisImgId);
        var imgSrc = $("#img_big").attr("src");
        var pageTitle = $("#spn_title").html();
        $('#cp_url').val(pageTitle+" "+pageUrl);
        $('#cp_ubb').val('[url='+pageUrl+'][img]'+imgSrc+'[/img][/url]');
        $('#cp_html').val('<a href="'+pageUrl+'"><img src="'+imgSrc+'" alt="'+pageTitle+'" border="0"></a>');
        $('#cp_src').val(imgSrc);
        $('#copy_div').slideToggle();
    });

    $('#cp_url, #cp_ubb, #cp_html, #cp_src').click(function () {
        this.select();
    });

    $('.link_cp_url, .link_cp_ubb, .link_cp_html, .link_cp_src').click(function () {
        var ua = navigator.userAgent;
        var msieOffset = ua.indexOf("MSIE");
        if(msieOffset > 0){
            var clipBoardContent;
            var t = $(this).attr("class");
            var l = t.length;
            var copyType = t.substr(5, l-0-5);
            clipBoardContent = $("#"+copyType).val();
            window.clipboardData.setData("Text", clipBoardContent);
            switch (copyType){
                case 'cp_url': alert("复制成功,请粘贴到您的QQ/MSN上推荐给你的好友."); break;
                case 'cp_src': alert("复制成功,您可以单独引用这张图片."); break;
                case 'cp_ubb': alert("复制成功,请粘贴到您的论坛帖子中."); break;
                case 'cp_html': alert("复制成功,请粘贴到您的网页代码中."); break;
            }
        }else{
            alert("您的浏览器不支持此功能!\n请手动复制文本框中的代码!");
        }
    });
});

function hiddenLoading() {
    $(".div_loading").fadeOut("slow", function () {
        $(this).remove();
    });
}

function formatSize(t, type) {
    var width = t.width;
    var height = t.height;
    var maxWidth, maxHeight;
    if (type=='big') {
        hiddenLoading();
        maxWidth = 580;
        maxHeight = 700;
    }else if (type=='small') {
        maxWidth = 110;
        maxHeight = 110;
    }
    var widthPer = maxWidth/width;
    var heightPer = maxHeight/height;
    if(widthPer<1 || heightPer<1) {
        if(widthPer<heightPer) {
            t.width = width*widthPer;
            t.height = height*widthPer;
        }else {
            t.width = width*heightPer;
            t.height = height*heightPer;
        }
    }
}

function getFavHtmlCode() {
    return "<a href=\"javascript:window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent(document.title.substring(0,76))+'&iu='+encodeURIComponent(location.href)+'&fr=ien#nw=1','_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes'); void 0\">☆添加到百度搜藏</a>　<a href=\"javascript:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)\">☆收藏到QQ书签</a>　<a href=\"javascript:window.open('http://myweb.cn.yahoo.com/popadd.html?url='+encodeURIComponent(document.location.href)+'&title='+encodeURIComponent(document.title), 'Yahoo','scrollbars=yes,width=440,height=440,left=80,top=80,status=yes,resizable=yes');void(0);\">☆添加到雅虎收藏<sup>+</sup></a>　<a href=\"javascript:void(0);\" onclick=\"if(window.sidebar)window.sidebar.addPanel(document.title, document.location.href, ''); else if( document.all )window.external.AddFavorite(document.location.href, document.title); else if(window.opera && window.print)return true;\">☆添加到收藏夹</a>";
}

window.onload = function () {
    hiddenLoading();
}
