/*
例子1：
ready(function(){
		 jPIC("je").reSize({
		   width:200,
		   height:200,
		   scaleType:"auto"
		   }); 
})
例子2： 结合jquery
$().ready(function(){
         jPIC($("#je img")).reSize({
		   width:200,
		   height:200
		   });
})
by pmaojie 2012-11-12		    
*/

var  flag=false;  
function  DrawImage(ImgD,width,height){  
     var  image=new Image();  
     image.src=ImgD.src;  
     if(image.width>0  &&  image.height>0){  
       flag=true;  
       if(image.width/image.height>=  width/height){  
         if(image.width>width){      
         ImgD.width=width;  
         ImgD.height=(image.height*width)/image.width;  
         }else{  
         ImgD.width=image.width;      
         ImgD.height=image.height;  
         }  
         //ImgD.alt=image.width+"×"+image.height;  
         }  
       else{  
         if(image.height>height){      
         ImgD.height=height;  
         ImgD.width=(image.width*height)/image.height;            
         }else{  
         ImgD.width=image.width;      
         ImgD.height=image.height;  
         }  
         //ImgD.alt=image.width+"×"+image.height;  
         }  
       }  
}    

  var jDOM = (function () {
            var isReady = false; //判断onDOMReady方法是否已经被执行过  
            var readyList = []; //把需要执行的方法先暂存在这个数组里  
            var timer; //定时器句柄  
            ready = function (fn) {
                if (isReady)
                    fn.call(document);
                else
                    readyList.push(function () { return fn.call(this); });
                return this;
            }
            var onDOMReady = function () {
                for (var i = 0; i < readyList.length; i++) {
                    readyList[i].apply(document);
                }
                readyList = null;
            }
            var bindReady = function (evt) {
                if (isReady) return;
                isReady = true;
                onDOMReady.call(window);
                if (document.removeEventListener) {
                    document.removeEventListener("DOMContentLoaded", bindReady, false);
                } else if (document.attachEvent) {
                    document.detachEvent("onreadystatechange", bindReady);
                    if (window == window.top) {
                        clearInterval(timer);
                        timer = null;
                    }
                }
            };
            if (document.addEventListener) {
                document.addEventListener("DOMContentLoaded", bindReady, false);
            } else if (document.attachEvent) {
                document.attachEvent("onreadystatechange", function () {
                    if ((/loaded|complete/).test(document.readyState))
                        bindReady();
                });
                if (window == window.top) {
                    timer = setInterval(function () {
                        try {
                            isReady || document.documentElement.doScroll('left'); //在IE下用能否执行doScroll判断dom是否加载完毕  
                        } catch (e) {
                            return;
                        }
                        bindReady();
                    }, 5);
                }
            }
        })();
(function (n){	
   jPIC  = (function (n){
	           var newImg = function(){}
				var imgLoad = function (url, callback) {
					var img = new Image();
					img.src = url.src;
					if (img.complete) {
						callback(url,img);
						return ;
					} else {
						img.onload = function () {
							img.onload = null;
							callback(url,img);
						};
					};
				};
                //o:原始图片对象，img:实际尺寸原始图片，newImg：指写对象宽，高对齐方式等
				_reSize=function(o,img,newImg){
						var zoom_type = img.width/img.height-newImg.width/newImg.height;
						var tempW,tempH;
					         tempW   =  img.width;
							 tempH   =  img.height;
						if(typeof(newImg.scaleType)=='undefined'){newImg.scaleType="";}
						//1
						if (zoom_type>0){ 
						   if(newImg.scaleType=="")
						   {
							 //
							 tempW    = img.width*(newImg.height/img.height); 
							 tempH    = newImg.height;
							 //选择左右居中位置
							 if(newImg.align=="center"){
								o.style.marginLeft=-(tempW-newImg.width)/2+"px";
							 } else if(newImg.align=="right"){//选择居右位置				
								o.style.marginLeft=-(tempW-newImg.width)+"px";
							 }
						   }else{
							 //等比缩放
							 if(img.width>newImg.width)
							 {
							   tempW    = newImg.width;
							   tempH    = (img.height*newImg.width)/img.width;
							 }
						   }
						   o.style.width  = tempW+"px";
						   o.style.height = tempH+"px";
						}
						//2
						else if (zoom_type<0){
					       if(newImg.scaleType=="")
						   {
							  tempW    = newImg.width; 
							  tempH    = img.height*(newImg.width/img.width);
							   //选择上下居中位置
							  if(newImg.valign=="center"){
								 o.style.marginTop=-(tempH-newImg.height)/2+"px";
							  }else if(newImg.valign=="bottom"){
								 o.style.marginTop=-(tempH-newImg.height)+"px";
							  }
						   }else{
							 //等比缩放
							 if(img.height>newImg.height)
							 {
							    tempW    = (img.width*newImg.height)/img.height
							    tempH    =  newImg.height; 
							 }
					       }
							 o.style.width  = tempW+"px";
							 o.style.height = tempH+"px";
						}
						//3
						else if (zoom_type==0){
						   o.style.width  = newImg.width +"px";  
						   o.style.height = newImg.height+"px";
						}
						//
						if(newImg.scaleType==""){
							if(o.parentNode.style.overflow!="hidden"){
							 o.parentNode.style.overflow="hidden";
							 o.parentNode.style.width =newImg.width+"px";  
							 o.parentNode.style.height=newImg.height+"px";
							 o.parentNode.style.display="block";
							}
					    }
			   }
			 return{ 
			    scaleType:"",
				width : 200,
				height: 200,
				align:"center",
				valign:"center",
				reSize : function (fn) {
				  if(typeof(fn)!="undefined"){
				   if(typeof(fn.width)!="undefined")  this.width  = fn.width;
				   if(typeof(fn.height)!="undefined") this.height = fn.height;
				   if(typeof(fn.align)!="undefined")  this.align  = fn.align;
				   if(typeof(fn.valign)!="undefined") this.valign  = fn.valign;
				   if(typeof(fn.scaleType)!="undefined") this.scaleType  = fn.scaleType;
				  } 
				  	 newImg.width     = this.width ;
					 newImg.height    = this.height;
					 newImg.align     = this.align;
					 newImg.valign    = this.valign;
					 newImg.scaleType = this.scaleType;
				  //-----------------------------------------
				      var obj_img = getId(n);
					  if(obj_img==null) return false;
					  for(var i=0;i<obj_img.length;i++){
	                    img = new Image();
						imgLoad(obj_img[i],function(oo,img){
							_reSize(oo,img,newImg);
							});
					  }
					  //-----------------------------------------
				},
				drawImage : function(o,w,h){
					 newImg.width     = w;
					 newImg.height    = h;
					 newImg.align     = this.align;
					 newImg.valign    = this.valign;
			         if(o.getAttribute("scaleType")!=null){
						 newImg.scaleType = o.getAttribute("scaleType");
						 }
					 img = new Image();
					 img.src = o.src;
					 _reSize(o,img,newImg);	
				},
	            DrawImage : function(o,w,h){
                 this.drawImage(o,w,h);   
			 }
           }			
        })
	getId  = function(id){ 
	if(typeof(id)!="object"){
		if(document.getElementById(id)==null){return null;}
		var gid = document.getElementById(id).getElementsByTagName("img");
        return gid;
	   }
	   else{return id;}
	}
})();
