﻿
function setSameheight(id_1, id_2)
{
    var obj1 = document.getElementById(id_1);
    var obj2 = document.getElementById(id_2);
    var maxHeight = obj1.clientHeight > obj2.clientHeight?obj1.clientHeight:obj2.clientHeight;
    obj1.style.height = maxHeight + "px";
    obj2.style.height = maxHeight + "px";
}
///用于首页slid -----------------------------
function initSlidShow()
{
    var ulEl = document.getElementById("slidItems");
    var aTagList = ulEl.getElementsByTagName("a");
    document.getElementById("slidPicture").parentNode.href = aTagList[0].href;
    
    for(var i=0;i<aTagList.length; i++)
    {
        aTagList[i].onmouseover = function()
        {
            
            var ulEl = document.getElementById("slidItems");
            var aTagList = ulEl.getElementsByTagName("a");
            for(var i = 0 ; i < aTagList.length; i++)
            {
                aTagList[i].className = "";
            }
            this.className = "selected";
            setSlidInfo(this);
            window.clearInterval(window.slidInterval);
            window.slidInterval = window.setInterval(intervalSetSlidNextSelected,5000);
        }
    }
    
    window.slidInterval = window.setInterval(intervalSetSlidNextSelected,5000);
    document.getElementById("slidItems").getElementsByTagName("a")[0].onmouseover();
}

function intervalSetSlidNextSelected()
{
    var ulEl = document.getElementById("slidItems");
    var aTagList = ulEl.getElementsByTagName("a");
    var isHighlight = false;
    var highlighted = false;
    for(var i=0;i<aTagList.length; i++)
    {
        if(isHighlight)
        {
            highlighted = true;
            aTagList[i].onmouseover();
            break;
        }
        
        if(aTagList[i].className == "selected")
        {
            isHighlight = true;
        }
    }
    if(!highlighted)
    {
        aTagList[0].onmouseover();
    }
}

function setSlidInfo(aTag)
{
    //slid图片
    var picUrl = aTag.firstChild.src;
    //slid标题
    var title = aTag.nextSibling.innerHTML;
    //slid介绍
    var content = aTag.nextSibling.nextSibling.innerHTML;
    
    document.getElementById("slidPicture").src = picUrl;
    document.getElementById("slidPicture").parentNode.href = aTag.href;
    document.getElementById("slidTitle").innerHTML = title;
    document.getElementById("slidTitle").href = aTag.href;
    document.getElementById("slidContent").innerHTML = content;
    
    
}


////////用ajax读取最新餐馆点评信息，并显示在最新点评列表 ------------------------

//当前最新的点评id
window.currentNewCommentId = 0;
//当前需要插入到显示列表中的点评列队
window.commentInsertQueue = new Array();
function initNewCommentGetter()
{
    window.setInterval
    (
        function()
        {
            var listParnet = document.getElementById("dlNewComment");
            var ddlist = listParnet.getElementsByTagName("dd");
            if(ddlist != null && ddlist.length > 0)
            {
                var nid = parseInt(ddlist[0].id.match(/\d+/)[0]);
                window.currentNewCommentId = nid;
                
                var ajax1 = new DTAjax()
                ajax1.onGet = function(str)
                {
                    if(str == "")
                    {
                        return;
                    }
                    str = str.replace("\r\n","");
                    var listObj = eval("["+str+"]");
                    if(!listObj[0].ds)
                    {
                        return;
                    }
                    var commentList = listObj[0].ds;
                    for(var i = 0; i< commentList.length; i++)
                    {
                        window.commentInsertQueue.push(commentList[i]);
                        window.currentNewCommentId = parseInt(commentList[i].Comment_ID);
                    }
                }
                //ajax1.getStringFromUrl("get","ajax/IndexGetNewCommentAjax.aspx?lastid=10161",true);
                ajax1.getStringFromUrl("get","ajax/IndexGetNewCommentAjax.aspx?lastid=" + nid,true);
            }
        },30000
    );
    
    window.setInterval(function()
    {
        insertAnimationMgr.render();
    },40);
    
}



var insertAnimationMgr = new Object();
insertAnimationMgr.transStatus = 0;
insertAnimationMgr.currentInsertEl = null;
insertAnimationMgr.currentInsertElHeight = 0;
insertAnimationMgr.animationOpacity = 10;
insertAnimationMgr.render = function()
{
    var listParnet = document.getElementById("dlNewComment");
    if(this.transStatus == 0 && window.commentInsertQueue.length == 0)
    {
        return;
    }
    if(this.transStatus == 0)
    {
        this.currentInsertEl = window.commentInsertQueue[0];
        window.commentInsertQueue = window.commentInsertQueue.slice(1);
        
        var scoreInfo = "";
        if(this.currentInsertEl.Comm_Type == "0")
        {
            scoreInfo = "<span class=\"stars\" id=\"RepeaterComments_ctl02_pingfen_panle\">口味: "+ get5Stars(this.currentInsertEl.Comm_TasteScore) +" | 环境: "+ get5Stars(this.currentInsertEl.Comm_EnvScore) +" | 服务: "+ get5Stars(this.currentInsertEl.Comm_ServScore) +"</span>";
        }
        
        var displayContent = this.currentInsertEl.Comm_Text.replace(/\<[^\>]*?\>/g,"");
        if(displayContent.length > 57)
        {
            displayContent = displayContent.substr(0,55) + "....";
        }
        
        
        var userFace = this.currentInsertEl.User_Face.replace("\\","/");
        if(userFace.length < 4)
        {
            userFace = "../images/cruser.gif";
        }
        
        var newEl = document.createElement("dd");
        newEl.id = "commId_" + this.currentInsertEl.Comment_ID;
        
        newEl.style.top = "0px";
        newEl.style.position = "absolute";
        newEl.innerHTML = "<a class=\"userHead\" href=\"personal.aspx?id=" + this.currentInsertEl.Comm_UID + "\"><img src=\"http://www.chenre.cn/bbs/"+userFace+"\"></a>\
                                    <div class=\"commentInfo\">\
                                        <a href=\"personal.aspx?id=" + this.currentInsertEl.Comm_UID + "\">"+this.currentInsertEl.User_name+"</a> "+this.currentInsertEl.Comm_Date+" 于<a href=\"CookRoom_Index.aspx?cook_id="+this.currentInsertEl.Comm_CookID+"\">"+this.currentInsertEl.cookname+"</a>\
                                        <br>\
                                        <span class=\"content\">"+displayContent+"</span>\
                                        <br>\
                                        <input type=\"hidden\" value=\"0\" id=\"\" name=\"\"> "+ scoreInfo +"\
                                    </div>";
        //listParnet.appendChild(newEl);
        var ddList = listParnet.getElementsByTagName("dd");
        listParnet.insertBefore(newEl,ddList[0]);
        
        this.currentInsertElHeight = newEl.offsetHeight;
        newEl.style.display = "none";
        this.transStatus = 1;
    }
    else if(this.transStatus == 1)
    {
        var liList = listParnet.getElementsByTagName("dd");
        
        if(liList.length < 7)
        {
            this.transStatus = 2;
            return;
        }
        
        this.animationOpacity = this.animationOpacity - 1;
        liList[liList.length - 1].style.filter = "Alpha(Opacity=" + this.animationOpacity + "0)";
        liList[liList.length - 1].style.opacity = "0." + this.animationOpacity;
        
        if(this.animationOpacity == 0)
        {
            liList[liList.length - 1].parentNode.removeChild(liList[liList.length - 1]);
            this.transStatus = 2;
        }
    }
    else if(this.transStatus == 2)
    {
        var ddList = listParnet.getElementsByTagName("dd");
        //this.currentInsertElHeight
        
        
        var newFirstMarginTop = parseInt(ddList[1].style.marginTop);
        if(isNaN(newFirstMarginTop)) newFirstMarginTop = 6;
        newFirstMarginTop += 5; 
        if(newFirstMarginTop > this.currentInsertElHeight + 12)
        {
            //newFirstMarginTop = this.currentInsertElHeight + 18;
            ddList[0].style.display = "block";
            ddList[0].style.position = "static";
            ddList[0].style.opacity = "0";
            ddList[0].style.filter = "Alpha(opacity=0)";
            ddList[1].style.marginTop = "6px";
            this.transStatus = 3;
            return;
        }
        else
        {
            ddList[1].style.marginTop = newFirstMarginTop + "px";
        }
    }
    else if(this.transStatus == 3)
    {
        var ddList = listParnet.getElementsByTagName("dd");
        this.animationOpacity = this.animationOpacity + 1;
        if(this.animationOpacity > 9)
        {
            ddList[0].style.filter = "";
            ddList[0].style.opacity = "1";
            this.transStatus = 0;
        }
        else
        {
            ddList[0].style.filter = "Alpha(opacity=" + this.animationOpacity + "0)";
            ddList[0].style.opacity = "0." + this.animationOpacity;
        }
    }
    else
    {
        alert("else");
    }
}

function get5Stars(num)
{
    var intNum = parseInt(num);
    var outStr = "";
    for(var i=0; i<5 ; i++)
    {
        if(intNum > 0)
        {
            outStr += "★";
        }
        else
        {
            outStr += "☆";
        }
        intNum = intNum - 1;
    }
    return outStr;
}


window.onload = function()
{
    initNewCommentGetter();
}
