//(function(){
	function getHashParms(){
		//获取hash数据，处理为字典，方便拼接多个hash值
		var hash = location.hash.replace('#','');
		var res = {};
		if(hash){
			var hashArr = hash.split('&');
			for(var i=0;i<hashArr.length;i++){
				var kv = hashArr[i].split('=');
				res[kv[0]] = kv[1];
			}
		}
		return res;
	}
	
	function setHashParms(k,v,type){
		//设置一个hash值
		var hash = [];
		var kv = getHashParms();
		kv[k] = v;
		for(var k in kv){
			hash.push( k+'='+kv[k] );
		}
		location.hash = hash.join('&');
	}
	
	function setContentByUrl(url,id,fillTo){
		//通过url设置内容页，本质上就是一个ajax请求+填充
        if(!id){
            if(!fillTo)fillTo=$('#main-content');
        }else {
            if(!fillTo)fillTo=$('#'+id);
        }
        X.DATA['page'] = url;
        // console.log(url);
		$.get('pages/'+ url + '.html',function(content){
            // fillTo.empty();
			fillTo.html(content);
            X.event.emit('pageOnLoad',url);
            X.pageLogic[url] && X.pageLogic[url].init && X.pageLogic[url].init();
		});
	}
	
    X.gourl = function(url,id){
        if(url || id){
            setHashParms('page',url);
		    setContentByUrl(url,id);
        }
        X.DATA['eventid'] = "";
        X.DATA['ltvid'] = "";
        X.DATA['retentionid'] = "";
        X.DATA['user_propertyid'] = "";
        X.DATA['scatterid'] = "";
        X.DATA['traceid'] = "";
        if(url != "userinfolist" && url != "routeanalysis"){
            X.DATA['trace_query_cond'] = ""
        }
        layer.closeAll();
    };

	
	$(function(){
            //拦截所有class里有ajax-content的链接，由直接跳转修改为ajax跳转
            $('body').on("click",".ajax-content",function(){
                var href = $(this).attr('href');
                var id = $(this).attr('data-id');
                X.gourl(href,id);
                return false;
            });
            //设置主页，如果hash里有值的话，设置为hash的值，方便复制url传播
            var hash = getHashParms();
            setContentByUrl(hash.page || 'login');

            var n = window.event.screenX - window.screenLeft;
            var b = n > document.documentElement.scrollWidth-20;
            if(b && window.event.clientY < 0 || window.event.altKey){
                //页面关闭
                // console.log('页面关闭');
            }else{
                //页面刷新
                // console.log('页面刷新111');
                var query = window.location.href;
                var querys = query.split("=");
                if(querys[1] != undefined && querys[1] != 'login'){
                    $("#conetnt").html("");
                    X.gourl('dashboard');
                }
            }

	});	

    window.onhashchange = function(){
        // console.log( getHashParms() );
        var page = X.DATA['page'];
        var urlinfo = getHashParms();
        if(urlinfo.page == 'dashboard'){
            for(let i in X.DATA['ProjectData']){
                for(let j in X.DATA['ProjectData'][i]['children']){
                    if(X.DATA['ProjectData'][i]['children'][j]['_id'] == urlinfo.menu){
                        $(".tab-tit-box .tab-tit").html(X.DATA['ProjectData'][i]['children'][j]['name']);
                        X.DATA['ProjectData'][i]['children'][j]['onclick'] = 1;
                    }else{
                        X.DATA['ProjectData'][i]['children'][j]['onclick'] = 0;
                    }
                } 
            }

            X.laytpldata('#kongjian-dot',X.DATA['ProjectData'],'#kongjian',function(){
                // X.pageLogic.dashboard.sidebarslide();
                var sidebar = document.getElementById('sidebar-boxhuadong');
                new Sortable(sidebar, {
                    handle: '.tuodong', // handle class
                    animation: 150,
                    onUpdate:function(evt){
                        // console.log(evt);
                        var newIndex = evt.newIndex;
                        var oldIndex = evt.oldIndex;
                        var kbid = evt.item.attributes[3]['nodeValue'];
                        var index = 0;
                        for(let i in X.DATA['ProjectData']){
                            if(kbid == X.DATA['ProjectData'][i]['_id']){
                                index = i;
                                for(let j in X.DATA['ProjectData'][i]['children']){
                                    if(j > newIndex){
                                        X.DATA['ProjectData'][i]['children'][j]['sort'] = Number(X.DATA['ProjectData'][i]['children'][j]['sort'])  + Number(1);
                                    }else if(j == newIndex){
                                        X.DATA['ProjectData'][i]['children'][j]['sort'] = Number(X.DATA['ProjectData'][i]['children'][j]['sort'])  - Number(1);
                                    }
                                }
                            }
                        }
                        X.DATA['ProjectData'][index]['children'][oldIndex]['sort'] = newIndex;
                        var sortarr = [];
                        for(let q in X.DATA['ProjectData'][index]['children']){
                            var arr = {
                                name: X.DATA['ProjectData'][index]['children'][q]['name'],
                                dashboard_id: X.DATA['ProjectData'][index]['children'][q]['_id'],
                                sort: X.DATA['ProjectData'][index]['children'][q]['sort']
                            }
                            sortarr.push(arr);
                        }
                        X.api('dashboard/sort','post',{sort:sortarr},function(d){
                            if(d == 1){
                                X.pageLogic.dashboard.freshMenu(X.DATA['projectid']);
                            }
                        })
                    }
                });

            });
            X.DATA['page'] = urlinfo.page;
            // console.log(urlinfo.page);
            // console.log(urlinfo.menu);
            if(page != urlinfo.page){
                X.gourl(urlinfo.page,'conetnt');
            }
            X.DATA['dashboard_id'] = urlinfo.menu;
            X.pageLogic['dashboard'].gettabledata();
        }
    };

//})();