(function ($) {
	$(function () {
		//var loadingDivs = $('div', '#dindex');
		var loadingDivs = $("div[@id*=dindex]");
		var date = new Date();
		var str = "" + date.getYear()
				+ date.getMonth()
				+ date.getDate()
				+ date.getHours();
		loadingDivs.each(function () {
			if($(this).find('div[@id*=dindex]').length == 0){
				var id = $(this).attr('id');
				if ($(this).attr('url')) {
					var obj = $(this);
					var url = obj.attr('url');
					jQuery.ajax({
						url: url,
						type: "GET",
						dataType: "html",
						complete: function(res, status){
							if ( status == "success" || status == "notmodified" ) {
								//解决ie6浏览器下not found问题
								var resText = res.responseText.toLowerCase().indexOf("404 not found");
								obj.html( resText >= 0 ? "": res.responseText );
								//回调
								obj.each( function () {
									loadComplete(id, obj);
									fn_disposePicPNG(obj);
								}, [res.responseText, status, res] );
							}
						}
					});
					/*
					$(this).load($(this).attr('url'), function () {
						loadComplete(id, obj);
						fn_disposePicPNG(obj);
					});
					*/
				}
			}
		});

		// 解决ie6图片显示问题
		function fn_disposePicPNG(obj) {
			$("img", obj).each(function () {
				var imgName = $(this).attr("src").toUpperCase();
				if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
					var imgID = (this.id)? "id='" + this.id + "' ": "";
					var imgClass = (this.className) ? "class='" + this.className + "' " : "";
					var imgTitle = (this.title) ? "title='" + this.title + "' " : "title='" + this.alt + "' ";
					var imgStyle = "display:inline-block;" + this.style.cssText;
					if (this.align == "left") imgStyle = "float:left;" + imgStyle;
					if (this.align == "right") imgStyle = "float:right;" + imgStyle;
					if (this.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
					var strNewHTML = "<span " + imgID + imgClass + imgTitle
						+ " style=\"" + "width:" + this.width + "px; height:" + this.height + "px;" + imgStyle + ";"  
						+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"  
						+ "(src=\'" + this.src + "\', sizingMethod='image');\"></span>" 
					this.outerHTML = strNewHTML;
				}
			});
		}
		//计数器
		if (typeof(siteId) != 'undefined' && typeof(webSiteUrl) != 'undefined')
		{
//			var counterUrl = "http://" + document.domain +'/bg/article/count.bc?method=addCatalogArticleClickCount';
			var counterUrl = "http://" + document.domain + "/bg/stat/count.bc?method=count";

			if (typeof(siteId) != 'undefined') {
				counterUrl += "&columnId=" + siteId;
			}
			if (typeof(counterType) != 'undefined' && typeof(articleId) != 'undefined') {
				counterUrl += "&articleId=" + articleId;
			}

			$.get(counterUrl, {time: +new Date()});
		}
	});
	window.loadComplete = function () {
	};
	

})(jQuery);


function writeStrToHtmlField(inputStr,parentId,strLen){
	inputStr = inputStr.replace(/<[^>]+>/ig, '');
	//统计当前字符串长度的标志位
	var flagLen = 0;
	//结果字符串
	var resStr;
	//是否要加省略号
	var slFlag = false;
	//最终要截取的位置
	var i;
	if(inputStr.length <= strLen){
		resStr = inputStr;
	} else {
		for(i = 0; i < inputStr.length; i++){
			if(inputStr.charCodeAt(i)<=256){
				flagLen++;
			}else{
				flagLen += 2;
			}
			if(flagLen > (strLen * 2)){
				slFlag = true;
				break;
			}
		}
		resStr = inputStr.substr(0,i);
		if(slFlag){
			resStr += "...";
		}
	}
	$('#' + parentId).text(resStr);
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}


function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return "";
}



// store cookie value with optional details as needed

function setCookie(name, value, expires, path, domain, secure) {

document.cookie = name + "=" + escape (value) +((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}



// remove the cookie by setting ancient expiration date

function deleteCookie(name,path,domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +(path) ? "; path=" + path : ""+((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

window.onunload = function(){
	deleteCookie("firstCome");
}

/**
日期比较，计算指定日期和当前日期相差的天数
参数格式：2010-01-01
**/
function getDateDiff(startDate)   
{  
  var arr = startDate.split("-");
  var asStartDate = arr[1] + "/" + arr[2] + "/" + arr[0];
  var todayDate = new Date();
  var asEndDate =  (todayDate.getMonth()+1) + '/' + todayDate.getDate() + '/' + todayDate.getFullYear();
  var miStart=Date.parse(asStartDate);
  var miEnd=Date.parse(asEndDate);   
  var days = (miEnd-miStart)/(1000*24*3600);
  return days;
}


function writeStrToHtmlField_new(inputStr,strLen){
	inputStr = inputStr.replace(/<[^>]+>/ig, '');
	//inputStr = inputStr.replace(/&nbsp;/ig, '');
	//统计当前字符串长度的标志位
	var flagLen = 0;
	//结果字符串
	var resStr;
	//是否要加省略号
	var slFlag = false;
	//最终要截取的位置
	var i;
	if(inputStr.length <= strLen){
		resStr = inputStr;
	} else {
		for(i = 0; i < inputStr.length; i++){
			if(inputStr.charCodeAt(i)<=256){
				flagLen++;
			}else{
				flagLen += 2;
			}
			if(flagLen > (strLen * 2)){
				slFlag = true;
				break;
			}
		}
		resStr = inputStr.substr(0,i);
		if(slFlag){
			resStr += "...";
		}
	}
	document.write(resStr);
}
