/*!
* jQuery AppTree plugin
* Version 1.0.0   
* @requires jQuery v1.7.1 or later
* 
*Copyright (c) 2012 @cyril(王海龙)
*/

$(function() {

    $.common.forbidden();
    // $.http.getUserData();
  
    window.external.GetM161Cridential();
	//alert("before GetAppBoxData");
    window.external.GetAppBoxData();

    window.attachEvent("onload", $.common.autoSize);
    window.attachEvent("onresize", $.common.autoSize);

});


(function ($) {  

    // AppTree
    $.extend($.fn, {
		AppTree: function(settings) {
			var ps = $.extend({
				target: $(document.body),
				onAppItemClick: function() {},
                onAppItemRightClick: function() {},
				onBuddyItemMouseOver: function() {}
			},
			settings);
                 
			ps.target = $("#" + ps.target);

            var appTree = $("#divAllAppList"); 

			appTree.createAppList = function(appArr) {
				var appInfoStr ="";
                //var contentStr = '<ul id="userList" data-listidx="0">';				
                var contentStr = '';	
			    for(var i=0;i<appArr.length;i++){
                    var appItem = appTree.createAppItem(appArr[i],i+1);
                    
                    if(appArr[i].Type !="exe"){
                        FXC.Data._userAppMap.put(appArr[i].AppId,appArr[i]);
                        contentStr+= appItem.strContent;
                    }
                }
				//contentStr += '</ul><input name="list1SortOrder" type="hidden">';										
				appTree.find('ul').append(contentStr);	                
                
                $('#moreApps').bind('click',function(){   
                                   
                    var count = $('#moreApps em').text();
                    if(count!=""){
                        FXC.Data._tabType = 2;
                    }
                    else{
                        FXC.Data._tabType = 1;
                    }

                    $.API.clickMoreApp(FXC.Data._tabType);        
				    $.http.setCounter('501100005');
                });
                
                $('#userList li').hover(function(){
                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');
                },function(){
                    $(this).removeClass();
                });                

                function saveOrder() {
                    var data = $("#userList li").map(function () { return $(this).attr("app"); }).get();
                    $("input[name=list1SortOrder]").val(data.join("|"));

                    $.http.saveApp($("input[name=list1SortOrder]").val());
                };		
                
                if(appInfoStr.length>0){                   
                    var installStr = '{"QueryAppInfo":['+appInfoStr.substr(0,appInfoStr.length-1)+']}';
                    var installInfo = $.API.IsInstalled(installStr);
                    
                    var appArry = $.common.str2obj(installInfo).InstallAppStatus;
                    for(var jj=0;jj<appArry.length;jj++){
                         appTree.refreshAppItem(appArry[jj].AppId ,appArry[jj].IsInstall);
                    }
                }  

				
			};
			

			appTree.createAppItem = function(app,index) {
				var appItem = new Object();
				var itemId = "appitem_" + app.AppId;				
				appItem.groupObj = app;
                appItem.strContent = "<li id='" + itemId + "' app='"+ app.AppId + "' RegId='"+ app.RegId +"'><div><img src='"+ FXC.Url.icon + app.Icon +"'><span>" + app.Name + "</span></div></li>";

				return appItem;
			};


			appTree.bindEventToItem = function() {
                
				var items = appTree.find("li");               							
                
                items.bind('click',function(){                   
					//alert($(this).attr("app"));
                    var appItem = FXC.Data._userAppMap.get($(this).attr("app"));
                    if(appItem){						
                        $.API.openApp($.common.obj2str(appItem));
                        $.http.setCounter($(this).attr("RegId"));
                    }                                                  
                });                                           
                                     	
			};
			
            			
			appTree.getAppList = function() {
				var appList = appTree.find("#userList");
				return appList;
			};			


			appTree.insertAppItem = function(app) {
				
				var list = appTree.getAppList();
				if (list.size() == 0) {
					return;
				}

				var itemId = "appitem_" + app.AppId;					
				//var appItem = $("<li class='menuDiv' id='" + itemId + "' app='" + app.AppId + "'  RegId='"+ app.RegId +"'><div><img src='"+ FXC.Url.icon + app.Icon +"'><span>" + app.Name + "</span></div></li> ");
				var appItem = $("<li id='" + itemId + "' app='"+ app.AppId + "' RegId='"+ app.RegId +"'><div><img src='"+ FXC.Url.icon + app.Icon +"'><span>" + app.Name + "</span></div></li>");
				
				$(appItem).bind("click",function() {
                    var appItem = FXC.Data._userAppMap.get($(this).attr("app"));
                    if(appItem){
                        $.API.openApp($.common.obj2str(appItem));
                    };    

                    $.http.setCounter($(this).attr("RegId"));
					
				}).hover(function(){
                    $(this).siblings().removeClass('on');
                    $(this).addClass('on');
                },function(){
                    $(this).removeClass();
                });
				//添加至末尾 
                var items = list.find("li");
				var itemCount = items.size();
				items.eq(itemCount-1).after(appItem);
				
				/*右键菜单暂时去掉(2015-03-23)
                $.contextMenu({
                    selector: "#" + itemId ,
                    callback: function (key, options) {
                        var item = options.$trigger;     
                        var appId = item.attr('app');
                        if(key=="del"){                                                                   
                            $.http.removeApp(appId ,item);
                        }else{
                            var appItem = FXC.Data._userAppMap.get(appId);                  
                            if (appItem) {
                                $.API.openApp($.common.obj2str(appItem));
                            }
                        }
                    },
                    items: {
                        "open": {name:"打开", icon: "edit"},
                        "del": { name: "删除", icon: "edit"}  
                    }
                });
				*/
				/*暂时去掉(2015-03-23)
                $('.more_bk').hide();
                $('.all-applist').show();
				*/
			};

			
			appTree.deleteAppItem = function(appId) {
				//(2015-03-23)
				var list = appTree.getAppList();

				if (list.size() == 0) {
					return null;
				}

				var item = list.find("#appitem_" + appId);
				if (item.size() == 0) {
					return null;
				}

				item.remove();
				
				return item;
			};		

			
			appTree.getAppItem = function(appId) {
				var list = appTree.getAppList();
				return list.find("#appitem_" + appId);
			};

			
			appTree.updateAppItem = function(app) {
			    
				
			};


            appTree.refreshAppItem = function(appId,isInstall) {
			    
				var list = appTree.getAppList();
				var item = appTree.getAppItem(appId);

				if (item.size() == 0) return;					

                if(isInstall == "0"){
                    
                    item.contextMenu(false);                                
                    item.append($('<div class="mask"></div>'));                    
                }else{
                    item.find('.mask').remove();
                }                
			};

            appTree.refreshStatusAppItem = function(app) {
			    
			    var list = appTree.getAppList();
			    var item = appTree.getAppItem(app.AppId);

			    if (item.size() == 0) return;					                

                if(app.Status == "progress"||app.Status=="start"){
                    item.find('.mask-block').remove();
                    item.find('.mask').remove();
                    
                    // loading                    
                    item.append($('<div class="mask-block"><img src="'+ FXC.Url.localDir +'/img/tj.gif"></div>'));
                    // 禁止右键菜单
                    item.contextMenu(false);
                }else if(app.Type == "setup" && app.Status=="end"){
                    // remove loading       
                    item.find('.mask-block').remove();
                    item.find('.mask').remove();

                    item.contextMenu(true);
                }else if(app.Status == 'termination'){      
                    item.find('.mask-block').remove();
                    item.find('.mask').remove();/// <reference path="http://localhost:4049/script/" />

                                                  
                    item.append($('<div class="mask"></div>'));                    
                    item.contextMenu(true);
                }                
		    };

			return appTree;
		}
	});
})(jQuery);
