


(function() {
    if (window.PageCtrl) {
        return;
    }

    window.PageCtrl = {
        loadJs: function(url, callback) {
            var script = document.createElement("script");
            script.type = "text/javascript";
            if (script.readyState) { //IE
                script.onreadystatechange = function() {
                    if (script.readyState == "loaded" || script.readyState == "complete") {
                        script.onreadystatechange = null;
                        callback();
                    }
                };
            } else { //Others
                script.onload = function() {
                    callback();
                };
            }
            script.src = url;
            document.getElementsByTagName("head")[0].appendChild(script);
        },
		/*
		 * 
		 * @param {Object} conf
		 * {url:"请求页面url",dom:"填充元素可以是id或者dom对象,param:{url参数},callback:请求页面回来以后回调",refresh:是否刷新容器，默认刷新}
		 */
        load: function(conf,isBlock) {
            var $dom = null;
            var url = conf.url,
				dom = conf.dom,
				param=conf.param ? conf.param:{},
				type=conf.type ? conf.type : "Post",
				callback = conf.callback?conf.callback:null,
				callbackParam = conf.callbackParam ? conf.callbackParam : null,
				contentType = conf.contentType ? conf.contentType : "application/x-www-form-urlencoded";
				var refresh = conf.refresh===false ? false : true;	
            if (dom) {
                if (typeof conf.dom === "string") {
                    $dom = $("#" + conf.dom);
                } else {
                    $dom = $(conf.dom);
                }
            } else {
                new Error("load page is Error");
            }
            if(window.Loading){
            //	window.Loading.start();
            }
			window.PageCtrl.ajax({
                url: url,
                cache: false,
                dataType: "html",
                data: param,
                contentType : contentType,
                type: type,
                error:conf.error,
                success: function(data,textStatus,jqXHR) {
                    
                    if(refresh){
                    	$dom.find("*").unbind();
                    	$dom.html("");
                    }
					var rscript = /(<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>)/gim;
					var rsrc = /src="(\S+.js)"/ig;
					var rname = /namespace="(\S+)"/ig;
					var scripts = data.match(rscript);
					
                    data = trim(data.replace(rscript, ""));
                    var $content = $(data);
                    
				    $dom.append($content);	
				    if(window.FoldPanel){
				    	window.FoldPanel.init();
				    	
				    }
					if(window.LayoutManager){
						LayoutManager.doLayout($dom);
					}
		            if(window.Loading){
		            	//window.Loading.stop();
		            }					
                    if (callback) {
                        callback(callbackParam, $content);
                    }
					
					if(!scripts) return;
					
					var loadConfs = [];//用于阻塞加载js保存配置信息
					
                    for (var i = 0, len = scripts.length; i < len; i++) {					
                        var script = scripts[i];	
						var src = script.match(rsrc);
						if(src){
							src = src[0].replace('src="',"").replace('"',"");
						}else{
							$dom.append(trim(script));
							continue;
						}
						var matchScript = script.match(rname);
						var name = null;
						if(matchScript){
							name = matchScript[0].replace('namespace="',"").replace('"',"");
						}
						if(!window[name]){
							if(isBlock){
								loadConfs.push({src:src,name:name});
							}else{
								window.PageCtrl.loadJs(src,
														function() {
															var innername = name;
															return function(){
																if(innername){
																	window[innername].init();
																}
																
															}														
														}());							
							}
							

						}else{							
							window[name].init();
						}					
                    }
					
					function blockScript(loadConf){
							if(!loadConf) return;
						    window.PageCtrl.loadJs(loadConf.src,
							                        function() {
														var innername = loadConf.name;
															window[innername].init();
															blockScript(loadConfs[loadConfStep++]);
							                        });													
					}
					var loadConfStep = 0;
					blockScript(loadConfs[loadConfStep++]);
					
					
				
					
					
					

                }
            })
        },
		submit:function(conf){
           window.PageCtrl.ajax(conf);	
		},
		formSubmit : function(conf){
			var $form = $("#"+conf.formId);
			var $iframe = $("#"+$form.attr("target"));
			if(!$iframe[0]){
				$iframe = $('<iframe  style="display:none" id="'+$form.attr("target")+'" name="'+$form.attr("target")+'" ></iframe>');
				$(document.body).append($iframe);				
			}
			if(conf.success){
				$iframe.unbind().load(conf.success);
			}
			$form.submit();
		},
		ajax:function(conf){
			if(conf.formId){
				PageCtrl.formSubmit(conf);
				return;
			}
			
			var success = conf.success;
			var error = conf.error;
			
			conf.success = function(data,textStatus,jqXHR){
				if(conf.loading===true){
					window.Loading.stop();
				}
				if(success) success(data,textStatus,jqXHR)
			};
			conf.error = function(jqXHR, textStatus, errorThrown){
				if(window.Loading){
					window.Loading.stop();
				}
				if(jqXHR){
					var errorFrame = "";
					
					if(jqXHR.status == 404 || jqXHR.status==0){
						if(error){
							error(jqXHR, textStatus, errorThrown);
						}
						if(conf.error404){  //404错误自定义处理
							conf.error404(jqXHR, textStatus, errorThrown);
						}
					}else{
						if(jqXHR.status == 501){
							var errorJson = $.StringtoJson(jqXHR.responseText);
							if(error){
								error(jqXHR, textStatus, errorThrown);
							}
							if(errorJson.errLevel=="1"){//错误提示方式
								argusAlert(errorJson.errorMessage);
								return;
							}
							errorFrame = ExceptionAlert.createFrame(errorJson.errorMessage,errorJson.errorCode||"",errorJson.stackTrack,errorJson.hasErrDetail);
							$(document.body).append(errorFrame);
							ExceptionAlert.init();

						}else{
							if(error){
								error(jqXHR, textStatus, errorThrown);
							}
							errorFrame = jqXHR.responseText;
							$(document.body).append(errorFrame);
						}
						
					}
				}
			};

			if(conf.loading===true){
				window.Loading.start();
			}
			
			if(conf.url.indexOf(".html")==-1){
				
				var url = conf.url;
				
				if(url.indexOf("?")!=-1){
					url += "&";
				}else{
					url +="?"
				}
				
				
				conf.data = conf.data || {};
				conf.data.aaaabbbb = new Date().getTime();
				conf.url = url;
			}
			return $.ajax(conf).responseText;
		},
		/*
		 * 弹出普通窗口
		 * */
		winOpen:function(obj){
			var resizeable = obj.resizeable ===true ? "yes" : "no";
			var scrollable = obj.scrollable ===true ? "yes" : "no";
			var height = obj.height;
			var width = obj.width;
			
			var left =  obj.left || window.screen.width/2 - width/2;
			 var top = obj.top || window.screen.height/2 -height/2;	
			 
			 
				var windowLeft = window.screenLeft;
				var windowTop = window.screenTop;
				if(windowLeft<0 || windowLeft >= window.screen.width){
					 left = windowLeft + left;
				}
				
				if(windowTop<0 || windowTop >= window.screen.height){
					top = windowTop + top;
				}
			 
			 var name = obj.name ? obj.name : "newwin";
			 var url = obj.url;
			 //默认不设置参数，不缓存加参数，如果设置true，则不加参数缓存请求
			 if(!obj.noCache){
				 if(url.indexOf("?")!=-1){
					 url += "&";
				 }else{
					 url +="?"
				 }
				 url +="aaaa="+new Date().getTime();
			 }
			 
			 
			 var newWin = window.open(url,name,'height='+height+', width='+width+', top='+top+', left='+left+', toolbar=no, menubar=no, scrollbars='+scrollable+',resizable='+resizeable+',location=no, status=no');
			 newWin.focus();
			 return newWin;			
		},
		/*
		 * 弹出模态窗口
		 * */
		winModalOpen:function(obj){
			var height = obj.height;
			var width = obj.width;
			var left =  obj.left || window.screen.availWidth/2 - width/2;
			var top = obj.top || window.screen.availHeight/2 -height/2;
			var name = obj.name ? obj.name : "newwin";
			 var url = obj.url;
			 //默认不设置参数，不缓存加参数，如果设置true，则不加参数缓存请求
			 if(!obj.noCache){
				 if(url.indexOf("?")!=-1){
					 url += "&";
				 }else{
					 url +="?"
				 }
				 url +="aaaa="+new Date().getTime();
			 }
				var windowLeft = window.screenLeft;
				var windowTop = window.screenTop;
				if(windowLeft<0 || windowLeft >= window.screen.width){
					 left = windowLeft + left;
				}
				
				if(windowTop<0 || windowTop >= window.screen.height){
					top = windowTop + top;
				}
			//window.showModalDialog('Noname2.html',window,'dialogWidth:400px;dialogHeight:400px');
			var popstyle="dialogTop:"+top+"px;dialogLeft:"+left+"px;help:no;center:no;dialogHeight:"+height+"px;dialogWidth:"+width+"px;status:no;resizable:no;scroll:no";
			window.showModalDialog(url,window,popstyle);
		},
		/*
		 * 显示无数据标签
		 * @param applyId 将提示标签应用到元素位置
		 * @param mini 如果为true，则为小提示
		 * @param text 提示信息
		 * */
		loadNoData:function(applyId,mini,text){
			var $append = $("#"+applyId);
			if($.isString(mini)){
				text = mini;
			}else if(!text){
				text = window.S_DATA_EMPTY;
			}
			
			
			var $nodata = $('<div class="nodata'+(mini===true? "-min":"")+'">'+text+'</div>');
			$append.html($nodata);
			$nodata.css({marginTop:$append.height()/2-$nodata.height()/2,marginLeft:$append.width()/2-$nodata.width()/2});
			return $nodata;
		}
		
		
    }
})();


function trim(string){
        return string.replace( /^[\s\r\n]*/,"");
}
 function ltrim(s){
    return s.replace( /^\s*/,"");
} 
 


/* sets the class of the tr containing the checked checkbox to selected */
function set_tr_class(element, selected) {
    if (selected) {
        element.attr("class", "selected " + element.attr("class"))
    } else {
        var css = element.attr("class");
        var position = css.indexOf('selected');

        element.attr("class", css.substring(position + 9));
    }
}

$(document).ready(function () {
	
	$('table th input:checkbox').on('click' , function(){
		var that = this;
		$(this).closest('table').find('tr > td:first-child input:checkbox')
		.each(function(){
			this.checked = that.checked;
			$(this).closest('tr').toggleClass('selected');
		});
			
	});
	

	
	
/*     checks all the checkboxes within a table 
    $("table input[class=checkall]").bind("click", function (event) {
        var checked = $(this).attr("checked");

        $("table input[type=checkbox]").each(function () {
            this.checked = checked;

            if (checked) {
                set_tr_class($(this).parent().parent(), true);
            } else {
                set_tr_class($(this).parent().parent(), false);
            }
        });
    });

     sets the class of the table tr when a checkbox within the table is checked 
    $("table input[type=checkbox]").bind("click", function (event) {
        if ($(this).attr("checked")) {
            set_tr_class($(this).parent().parent(), true);
        } else {
            set_tr_class($(this).parent().parent(), false);
        }
    });*/
    handlePortletTools();
});

var handlePortletTools = function () {
    jQuery('body').on('click', '.portlet .tools a.remove', function (e) {
        e.preventDefault();
            var removable = jQuery(this).parents(".portlet");
            if (removable.next().hasClass('portlet') || removable.prev().hasClass('portlet')) {
                jQuery(this).parents(".portlet").remove();
            } else {
                jQuery(this).parents(".portlet").parent().remove();
            }
    });

    jQuery('body').on('click', '.portlet .tools a.reload', function (e) {
        e.preventDefault();
            var el = jQuery(this).parents(".portlet");
            App.blockUI(el);
            window.setTimeout(function () {
                    App.unblockUI(el);
                }, 1000);
    });

    jQuery('body').on('click', '.portlet .tools .collapse, .portlet .tools .expand', function (e) {
        e.preventDefault();
            var el = jQuery(this).closest(".portlet").children(".portlet-body");
            if (jQuery(this).hasClass("collapse")) {
                jQuery(this).removeClass("collapse").addClass("expand");
                el.slideUp(200);
            } else {
                jQuery(this).removeClass("expand").addClass("collapse");
                el.slideDown(200);
            }
    });
}

var Util = {
	jDom: function(dom) {
		if (Util.isString(dom)) {
			return $("#" + dom);
		} else {
			return $(dom);
		}
	},
	isString: function(o) {
		return Object.prototype.toString.call(o) === "[object String]";
	}
}	
var ZIndex = {
	index: 19891015,
	set: function(dom) {
		Util.jDom(dom).css("zIndex", ++this.index);
	},
	free: function() {
		this.index--;
	},
	get:function(){
		return ++this.index;
	}
}

Loading = {
	start: function(str) {
		MaskShaw.start('loading');
		if (!str)
			str = "Loading";
		var $loading = $('<div id="Loading" class="ui-ios-overlay ios-overlay-show"><span class="title">'+str+'</span><img src="'+webPath+'/resources/images/loading-90.gif"></div>');
		$(document.body).append($loading);
		ZIndex.set($loading);

	},
	stop: function() {
		MaskShaw.stop('loading');
		ZIndex.free();
		$("#Loading").fadeOut(600);
		setTimeout('$("#Loading").remove()', 700);
	}
}
MaskShaw = {
	start: function(id) {
		var $shadow = $('<div class="mask" id="mask' + (id || '') + '"></div>');
		$shadow.height("100%");
		//$shadow.css({"opacity":".3"});
		$(document.body).append($shadow);
		ZIndex.set($shadow);
	},
	stop: function(id) {
		$("#mask" + (id || '')).remove();
		ZIndex.free();
	}
}
