﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />
/******************************
预加载图片，先显示等待图标
Justin 090531
******************************/
//jQuery(document).ready(function(jQuery) {
//    alert(jQuery("#cartbar").html);
//});

function loadImage(url, callback, obj)
{
    var imgTmp = new Image();
    imgTmp.src = "/images/spinner.gif";
    
    var div = document.getElementById(obj);
    if (div != null)
    {  
        imgTmp.width = 16;
        imgTmp.height = 16;
        div.appendChild(imgTmp);
    }

    var img = new Image(); //创建一个Image对象，实现图片的预下载
    img.src = url;
   
    if (img.complete) { // 如果图片已经存在于浏览器缓存，直接调用回调函数
        callback.call(img,obj);
        return; // 直接返回，不用再处理onload事件
    }

    img.onload = function () { //图片下载完毕时异步调用callback函数。
        callback.call(img,obj);//将回调函数的this替换为Image对象
    };
};

function imgLoaded(obj)
{
    var div = document.getElementById(obj);
    if (div != null)
    {  
        while (div.childNodes.length > 0)
        {
            div.removeChild(div.childNodes[0]);
        }
        div.appendChild(this);
    }
}

/******************************
前台JS先选中属性，再Post到后台
Justin 090602
******************************/
function SetSelecStyle(val, obj)
{
    var tag, link;
    var liobj = obj.parentNode.parentNode.childNodes;
//    for (var i=0;i<liobj.length;i++)
//    {
//        tag = liobj[i].tagName;
//        if (tag == "LI")
//        {
//            link = liobj[i].childNodes[0].innerHTML;
//            if (link == val)
//            {
//                liobj[i].className = "select";
//                
//                //增加"√"符号   
//                var elei = document.createElement("i");
//                var dg = new Image();
//                dg.src = "/images/i.png";
//                elei.appendChild(dg);
//                liobj[i].appendChild(elei);
//            }
//        }
//    }
//    
//    for (var i=0;i<liobj.length;i++)
//    {
//        tag = liobj[i].tagName;
//        if (tag == "LI")
//        {
//            link = liobj[i].childNodes[0].innerHTML;
//            if (link != val)
//            {
//                liobj[i].className = "";
//                
//                if (liobj[i].childNodes.length > 1)
//                {
//                    //移除"√"符号
//                    liobj[i].removeChild(liobj[i].childNodes[1]);
//                }
//            }
//        }
//    }
}
/*迷你购物车Justin090608*/
jQuery(document).ready(
    function(jQuery)
    {
//        jQuery('#cartbar').hover(function() { openDiv(); });
//        jQuery('#cartbar').bind("click", function() { closeDiv(); });
//        jQuery("#cartbar").bind("mouseleave", function() { closeDiv(); });
//        jQuery("#panel").bind("mouseleave", function() { closeDiv(); });

        AttachEvent();
        function openDiv()
        {
            if (document.URL.toLowerCase().indexOf("cart.aspx") <= 0)
            {
                jQuery("#panel").slideDown("normal");
            }
        }
        function closeDiv()
        {
            jQuery("#panel").slideUp("normal");
        }
        function removeProduct(id)
        {
            jQuery("#panel").load("/ashx/minicart.ashx?action=removeproduct&id=" + id, function() { endRemove(); });
        }
        function removeSuit(id)
        {
            jQuery("#panel").load("/ashx/minicart.ashx?action=removeproduct&issuit=1&id=" + id, function() { endRemove(); });
        }
        function endRemove()
        {
            //删除后回调函数，再次绑定删除事件
            AttachEvent();

            //更新购物车数量
            jQuery("#SGoodsCount").load("/ashx/minicart.ashx?action=getcartcount");
        }

        function AttachEvent()
        {
            jQuery("a[name='RemoveProduct']").each(function()
            {
                jQuery(this).click(function(event)
                {
                    removeProduct(this.id);
                });
            });

            jQuery("a[name='RemoveSuit']").each(function()
            {
                jQuery(this).click(function(event)
                {
                    removeSuit(this.id);
                });
            });
        }

    }
);

function UpdateVote(votevalue, votetext, commentid)
{
    jQuery.get("/ashx/CommonService.ashx?ram=" + Math.random() + "&action=isauthority", function(data) {
        if (data == "true") {
            jQuery.post("/ashx/GoodsComments.ashx?ram=" + Math.random() + "&action=updatevote&commentid=" + commentid + "&votevalue=" + votevalue
                + "&votetext=" + votetext, function(data) {
                    var result = data.split(":");
                    if (result.length > 0 && result[0] == "ok") {
                        var str;
                        if (votevalue == "1")
                            str = "_1";
                        else
                            str = "_2";
                        str = commentid + str;
                        jQuery("#" + str)[0].innerHTML = "(" + result[1] + ")";

                        alert("投票成功!");
                    }
                    else {
                        alert(data);
                    }
                });
        }
        else {
            login(location.href);
        }

    });

        return false;
}

function login(url)
{
    jQuery.post("/ashx/CommonService.ashx?ram=" + Math.random() + "&action=setreturnurl&returnurl=" + encodeURIComponent(url), function(data)
    {
        location.href = "/login.aspx";
        return false;
    });
}

function SubmitComment(url, isnew)
{
    jQuery.get("/ashx/CommonService.ashx?ram=" + Math.random() + "&action=isauthority", function(data)
    {
        if (data == "true")
        {
            if (isnew)
                window.open(url);
            else
                location.href = url;
        }
        else
        {
            login(url);
        }

    });
}

function CommentLogin(url)
{
    var resu = jQuery("#isauth")[0].innerHTML;
    if (resu != "true")
    {
        if (confirm("发表评价需要登录，是否登录？"))
        {
            login(url);
        }
    }
}
