/********************************************************************************************************************
*          函数库
*********************************************************************************************************************/
//缩放图片
var flag=false; 
function imageReSize(Img, width,height )
{
	 var image=new Image();  
	 image.src=Img.src;   
	 if(image.width>width||image.height>height){
	  		w=image.width/width; 
	   		h=image.height/height; 
	   	if(w>h){
	    	Img.width=width; 
	    	Img.height=image.height/w; 
	   	}else{
	    	Img.height=height; 
	    	Img.width=image.width/h; 
	   	} 
	 } 
}
/**** 对话框 ****/
function showDialog(  url )
{
        $("body").append( "<a href=\"javascript:void(0);\" id=\"ajaxLink\"></a>" );
		$.fn.createDialog.defaults = {
			progress: false,
			center: true,
			opacity: 0.9,
			bg: '#000'
		}
		$( "#ajaxLink" ).createDialog({
			 addr:  url
		});
		$( "#ajaxLink" ).click(  );
		$( "#ajaxLink" ).remove();
}
function closeDialog()
{
	   $("#jDialogOverlay").remove();
	   $("#jDialogContainer").remove();
}


function Dialog_show( id, data )
{
     // document.write( url +"<br/>" );
	 $( "#jDialogContainer").show();
     $("#jDialogOverlay").show();
	   $.fn.createDialog.defaults = {
			progress: false,
			center: true,
			opacity: 0.7,
			bg: '#ddd' ,
			index :3000000
		}
		$("#" +id ).createDialog({
			addr: '/lib/dialog/' + Math.random() ,  
			opacity: 0.9 ,
			method: 'POST' ,
			data: data
		});
}

function Dialog_close()
{
    $.closeDialog();
}

function openWindow( url , width , height )
{
	var setting = "height=" + height +
						", width=" + width + 
						", toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no";
	var winId = window.open( url , null , setting  );
	return winId;
}
/* boxy dialog */
/*
$(function() {
            Boxy.DEFAULTS.title = 'Title';
});
function OpenBoxy( url , title )
{
		Boxy.load( url , {cache:true, title:title , closeText:"[X]"  } );
}
function MessageBox( msg , title )
{
	  Boxy.DEFAULTS.title =( title == ""? '提示' : title ) ;
	  Boxy.alert("<p style=\"width:400px;height:150px;overflow-y:auto;\">"+msg + "</p>",  function(){}  );
}
*/

/*------------------------------------------------------------------
|  validate															       |
-------------------------------------------------------------------*/
function String_Trim( str )
{
	 return str.replace( /(^\s*)|(\s*$)/g , "" ); 
}

function Form_ShowMsg( spanId , msg , color )
{
	$( "#"+spanId ).html( "<span style=\"color:" + color + "\">" + msg  + "</span>" );
}
function Form_Username( username_id, spanId, msgOk, msgError )
{
	var  value_reg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){3,20}$/  ;  
	return Form_Reg( username_id, spanId, msgOk, msgError ,value_reg );
}

function Form_Reg( textfield_id, spanId, msgOk, msgError ,value_reg )
{
	var value  = $( "#" + textfield_id  ).val(  );
	if( typeof value == "string" )
	{
		if( ! value_reg.test( value ) )
		{
			Form_ShowMsg( spanId , msgError , "red" );
			return false;
		}
		else 
		{
				Form_ShowMsg( spanId , msgOk , "green" );
				return true;
		}
	}
	else
	{
		Form_ShowMsg( spanId , msgError , "red" );
		return false;
	}
}

function Form_Mail( email_id  , spanId , msgOk, msgError ){
	var email_reg = /(\w)+@(\w)+\.(com|net|cn|org|cn|com.cn|yqr|biz)/;
	return Form_Reg( email_id , spanId, msgOk, msgError , email_reg  );
}

function  Form_Required(  inputId , spanId, msgOk, msgError )
{
	var inputVal = String_Trim( $( "#"+inputId ).val() );
    if( String_Trim( inputVal ) == "" )
	{
		 Form_ShowMsg( spanId , msgError , "red" );
		 return false;
	}else
	{
		Form_ShowMsg( spanId , msgOk , "green" );
		return true;
	}
}

function Form_Number(  inputId , spanId, msgOk, msgNotNumber,msgNumLen , arrLength  )
{
	 var inputVal = $( "#"+inputId ).val() ;
     var numReg = /([0-9])+/;
	 if( ! numReg.test( inputVal ) )
	 {
		   Form_ShowMsg( spanId , msgNotNumber , "red" );
		   return false;
	 }
	 else
	 {
		 var inLen = false;
		  for( var i=0; i<arrLength.length; i++ )
		 {
				if( arrLength[i] == inputVal.length )
			   {
					inLen = true;
			   }
		 }
		 if( inLen )
		 {
				Form_ShowMsg( spanId , msgOk , "green" );
				return true;
		 } else
		 {
				Form_ShowMsg( spanId , msgNumLen , "red" );
				return false;
		 }
	 }
}
function  CheckDateTime(str){                            
       var  reg  =  /^(\d+)-(\d{1,2})-(\d{1,2})  (\d{1,2}):(\d{1,2}):(\d{1,2})$/;    
       var  r  =  str.match(reg);    
       if(r==null)return  false;    
       r[2]=r[2]-1;    
       var  d=  new  Date(r[1],  r[2],r[3],  r[4],r[5],  r[6]);    
       if(d.getFullYear()!=r[1])return  false;    
       if(d.getMonth()!=r[2])return  false;    
       if(d.getDate()!=r[3])return  false;    
       if(d.getHours()!=r[4])return  false;    
       if(d.getMinutes()!=r[5])return  false;    
       if(d.getSeconds()!=r[6])return  false;    
       return  true;  
}  


function checkall( formid , checkboxid )
{
	if($(checkboxid).attr("checked")){
        $("#"+formid+" input[type='checkbox']").attr("checked",true);
	}else{
	    $("#"+formid+" input[type='checkbox']").attr("checked",false);
	}
}


function chkAll( chkAllName , subChkName )
{
	 $("." + chkAllName ).click(
	         function()
	         {
	             if($(this).attr("checked")==true)
	             {
	                $("." + chkAllName ).attr("checked",'true');
	                $("[name='" + subChkName + "']").attr("checked",'true');//全选
	             }
	             else
	             {
	                $("." + chkAllName ).removeAttr("checked");
	                $( "[name='" + subChkName + "']" ).removeAttr("checked");//取消全选
	             }
	         }
	 );
}

function checkAll( className , checkBoxName )
{
	chkAll(  className , checkBoxName );
}
function scrollToBottom( divId )
{
	try{
		var objDiv = document.getElementById( divId );
		objDiv.scrollTop = objDiv.scrollHeight;
	}catch( e){}
}
/**
******************************************************************************************************************
*          应用
*********************************************************************************************************************/
/* initMenu 初始化菜单 */

/**
 * 创建首页的静态html
 */
function __createHomepageHtml( outputDivId  )
{
	$.get( "/Welcome/adminHtmlExecute/" + Math.random() , {} , function(){
		$("#" + outputDivId ).append( "<span style='color:green;'> 首页生成成功。</span> <br />" );
	});
	scrollToBottom( "msg_error" );
}

/**
 * 按照id字符串生成静态html
 * 
 * @param idString  id字符串 "1,3,5"
 * @param outputDivId   "updateHtml"
 * @param recallFunctionName  "gotoList()"
 * @return
 */
function __createCtgHtmlByIdString( idString , article_id , outputDivId , recallFunctionName )
{
	var doneCounter = 1;
	var idArr = idString.split(",");
	for( var i=0; i< idArr.length ; i++ )
	{
		if( idArr[i] != "" )
		{
			$.get( "/Article/adminDirHtml/" + idArr[i] + "/" + article_id , {} , function( txt ){
				$("#" + outputDivId ).append( "<span style='color:green;'> " + txt + "</span> <br />" );
				doneCounter ++;
                if( doneCounter == idArr.length )
                {
                	$("#" + outputDivId ).append( "生成完毕！<br />" );
        			setTimeout( recallFunctionName ,  5000 );
        			scrollToBottom( "msg_error" );
                }
            });
		}
	}
}

/**
 * 创建某分类静态html
 * 
 * @param int ctgId 分类的id
 * @param String outputDivId  显示结果的div的id
 * @param String recallFunctionName  回调函数的名称
 */
function __createCtgHtml( ctgId , article_id , outputDivId , recallFunctionName  )
{
	__createHomepageHtml( outputDivId  );
	$.get( "/articlectg/getThisAndParentIds/" + ctgId , {} , function( idString ) {
		__createCtgHtmlByIdString( idString , article_id ,  outputDivId , recallFunctionName );
	});
}

/**
 * 设置某个div的文字大小
 * @param size
 * @return null
 */
function setContentFontSize( divId , size )
{
 	$("#" + divId ).css( "font-size" , size + "px" );
}

/**
 * 设置字体，关闭窗口，打印的事件。
 * 
 * @param divId eg: "msgcnt"
 * @return
 */
function initFontSizeButtons( divId )
{
	$("#font_big").click( function(){
		setContentFontSize( divId , 18 );
	});

	$("#font_mid").click( function(){
		setContentFontSize( divId , 15 );
	});

	$("#font_small").click( function(){
		setContentFontSize( divId , 12 );
	});

	$("#print").click( function(){
		 print();
	});

	$("#closeWin").click( function(){
		window.opener=null;  
		  window.close();
	});
}

/**
 *初始化下拉分面select控件
 * 
 * @param pageCount
 * @param selectId
 * @return
 */
function initHtmlSelectPagination( pageCount , selectId )
{
	for( var i=1; i<= pageCount; i++ )
	{
		$("#" + selectId ).append( "<option value=\"" + i + "\">" + i + "</option>" );
	}

	$("#" + selectId ).change( function(){
		var href = location.href;
		if( href.indexOf( "index.html" ) != -1 )
		{
			var harr = href.split( ".html" );
			var newHref = harr[0] + "_" + $( this ).val() +".html";
			location.href = newHref;
		}
		else if( href.indexOf( "_" ) != -1 )
		{
			var harr = href.split( "_" );
			var newHref = harr[0] + "_" + $( this ).val() +".html";
			location.href = newHref;
		}else
		{
			var newHref = href + "index_" + $( this ).val() +".html";
			location.href = newHref;
		}
	});
}

