﻿
var ItemItem = Class.create();
ItemItem.prototype = 
{
    initialize:function(id,title,keyword,content)
    {
        this.Id = id;
        this.Title = title;
        this.Keyword = keyword;
        this.Content = getCateMenu(id,content);
    }
};


function fillItems()
{
    var groupItem = getGroupById(activeGroupId);
    if(groupItem.ItemStrs==null)
        attachItemsString(activeGroupId);
    
    fillItemsContent();
}

function fillItemsContent()
{        
    var groupItem = getGroupById(activeGroupId);
    if(groupItem.ItemStrs==null)
    {
        setTimeout(fillItemsContent,400);
        return ;
    }
    
    var categories = groupItem.Categories;
    var itemsStr = groupItem.ItemStrs;
    var items = itemsStr.split(';');
        
    for(i=0;i<categories.length;i++)
    {
        var cateId = categories[i].Id;
        var itemsContainer = categories[i].ItemContainer;
        var container = itemsContainer.getElementsByTagName("UL")[0];
        if(container.childNodes.length>0)
            continue;
        
        
        for(j=0;j<items.length;j++)
        {
            if(items[j]=='' || items[j]==null)
                continue;
                
            var iitem = items[j].split(',');
            var icateid = iitem[0];
            var iitemid = iitem[1];
            var title = iitem[2];
            
            if(icateid == cateId)
            {
                var itemobj = document.createElement("LI");
                itemobj.innerHTML = title;
                itemobj.id = iitemid;
                itemobj.onclick = function(e)
                {
                    var px;var py;
                    if(navigator.userAgent.toLowerCase().indexOf("msie")>-1)
                    {
                        px = event.clientX;
                        py = event.clientY;
                    }
                    else
                    {
                        px = e.clientX;
                        py = e.clientY;
                    }
                    showItemContentDetail(px,py,this.id,this.innerHTML);
                };
                container.appendChild(itemobj);
            }
        }
    }
}

function attachItemsString(groupId)
{
    var url = window.location.href;// '/webservice.aspx';
    var pars = 'o=ItemStrs&groupId='+groupId+"&ProvinceId="+globalVar.gProvinceId+"&CityId="+globalVar.gCityId ;
    if(globalVar.gAreaId!=0)
        pars += "&AreaId="+globalVar.gAreaId ;
	
    var myAjax = new Ajax.Request(
	    url, 
	    {
		    method: 'get', 
		    parameters: pars, 
		    onComplete: function(response)
		    {
		        attachItemsStringToGroup(response.responseText,groupId);
		    }
	    });
}

function attachItemsStringToGroup(responseStr,groupId)
{
    var groupItem = getGroupById(groupId);
    groupItem.ItemStrs = responseStr;
}


function showItemContentDetail(px,py,itemid,title)
{
    var tempobj = $("itemDetail");
    tempobj.style.left = px + "px";
    tempobj.style.top = py + "px";
    tempobj.style.display = "block";
    while(tempobj.childNodes.length>0)
        tempobj.removeChild(tempobj.childNodes[0]);
    
    var closeobj = document.createElement("DIV");
    closeobj.innerHTML = "[关闭]";
    closeobj.className = "close";
    closeobj.onclick = function(){hideParent(this);};
    tempobj.appendChild(closeobj);
    
    var tleobj = document.createElement("DIV");
    tleobj.innerHTML = title + "<br /><br />";
    tempobj.appendChild(tleobj);
    
    var content = document.createElement("DIV");
    content.innerHTML = "正在加载，请稍后...";
    tempobj.appendChild(content);
    
    var url = window.location.href;// '/webservice.aspx';
    var pars = 'o=ItemDetail&itemid='+itemid+"&ProvinceId="+globalVar.gProvinceId+"&CityId="+globalVar.gCityId ;
	
    var myAjax = new Ajax.Request(
	    url, 
	    {
		    method: 'get', 
		    parameters: pars, 
		    onComplete: function(response)
		    {
		        content.innerHTML = response.responseText ;
		    }
	    });
}