﻿var pop ={
    mask : 'pop_mask',
    wrapper : 'pop_wrapper',
    container : 'pop_container',
    contentArea : 'pop_content',
    call_iframe : 'call_iframe',
    call_ajax : 'call_ajax',
    call_content : 'call_content',
    call_img : 'call_img',
    defaultWidth : 500,
    defaultHeight : 500,
	creatElementsDone:null,
    loadImg : 'images/loadImg.gif',
    isIE6 : function (){ return !window.XMLHttpRequest },
    //初始化
    init : function (){
        if(!document.getElementById || !document.createTextNode){return;}
        var all = document.getElementsByTagName('a');
        if(!all) {return;}
         for (var i=0,item;item=all[i];i++ ) {
            if ( all[i].rel.indexOf(pop.call_content)!=-1 || all[i].rel.indexOf(pop.call_iframe)!=-1 || all[i].rel.indexOf(pop.call_ajax)!=-1 || all[i].rel.indexOf(pop.call_img)!=-1 )
               { pop.addEvent(all[i], 'click', pop.initPop)}
          }
      },
     //初始化弹出窗口
     initPop : function (e){
          pop.c = arguments[0];
          pop.obj = ( arguments.length > 2) ? arguments[1] : this;
          if( pop.obj == this ){
             var arr = this.rel.split('&'),
                 arr01 = (this.rel.indexOf('&') != -1) ? arr[1].match(/\=(.*)/)[1] : null,
                 arr02 = (this.rel.indexOf('&') != -1) ? arr[2].match(/\=(.*)/)[1] : null;
             pop.stopDefault(e);
          }
          pop.width = arguments[2] || arr01 || pop.defaultWidth;
          pop.height = arguments[3] || arr02 || pop.defaultHeight;
          pop.title = arguments[4] || this.title || false;  
          pop.creatElement();
          pop.removePop(); 
      },
      // 判断窗口类型
      typePop : function (){
          var link = (typeof(pop.obj) == 'string') ? pop.obj : pop.obj.href;
          function type(arg){
            return ((!!pop.obj.rel ) ? pop.obj.rel.indexOf(arg)!=-1 : false) || ( pop.c == arg);
          }
          if ( type(pop.call_ajax) ){
             pop.content.style.backgroundImage = 'url(' + pop.loadImg + ')';
             function setHeader(req) {
                  pop.content.style.backgroundImage = 'none';
                  pop.content.innerHTML = req.responseText;
             };
             pop.ajax.sendRequest( link + '?' + Math.random(),setHeader );
           }
           else if ( type(pop.call_iframe) ){
               pop.content.style.backgroundImage = 'url(' + pop.loadImg + ')';
               pop.content.innerHTML = "<iframe frameborder=0 id='ifr' allowtransparency='true' src=" + link + "?"+Math.round(Math.random()*1000)+" height=" +parseInt(pop.height)+" name=frname"+Math.round(Math.random()*1000)+"></iframe>"; 
               var o = document.getElementById("ifr");
               o.style.display = 'none';
               o.onload = o.onreadystatechange = function (){
                 this.style.display = 'block';
               }
           }
           else if ( type(pop.call_content) ){
               var section = link.match(/#(.*)/)[1];
               section = document.getElementById(section);
               pop.content.innerHTML = section.innerHTML;
            }
           else if (type(pop.call_img)){
               var img = document.createElement("img");
               pop.content.style.backgroundImage = 'url(' + pop.loadImg + ')';
               img.onload = function (){
                  pop.content.style.backgroundImage = 'none';
                  pop.content.appendChild(img);
               }
               img.src = link;
            }
      },
      //创建窗口
      creatElement : function (){
          //建立元素
          if(!pop.creatElementsDone){
			  pop.pM = document.createElement('div');
			  pop.pM.id = pop.mask;
			  pop.pId = document.createElement('div');
			  pop.pId.id = pop.wrapper; 
			  pop.pW = document.createElement('div');
			  pop.pW.id = pop.container ; 
			  pop.close = document.createElement('span');
			  pop.close.title = '关闭弹出窗口';
			  pop.close.innerHTML = pop.closetxt;
			  pop.content = document.createElement('div');
			  pop.content.id = pop.contentArea;
			  if(pop.title){
				pop.h3 = document.createElement('h3');
				pop.h3.innerHTML = pop.title;
				pop.pW.appendChild( pop.h3 );
			  }
			  //添加元素
			  pop.pId.appendChild( pop.pW );
			  pop.pW.appendChild( pop.close );
			  pop.pW.appendChild( pop.content );
			  document.body.appendChild(pop.pId);
			  document.body.appendChild(pop.pM);
			  //设置style
			  if(pop.isIE6())
			  {
				var sh = document.body.offsetHeight;
				pop.Ifr = document.createElement('iframe');
				document.body.appendChild(pop.Ifr);
				pop.resetCSS( pop.Ifr, { position :'absolute', zIndex :'1', width :'100%', height : sh+'px', left:'0', top: '0',
				filter :'alpha(opacity=0)',border:'none' })
			  }
		  }
		  if(pop.isIE6()){pop.Ifr.style.display = 'block';}
          pop.creatElementsDone = true;
		  pop.pM.style.display = '';
          pop.content.style.height = pop.height + 'px';
          pop.height = parseInt(pop.height) + (pop.title ? parseInt(pop.h3.offsetHeight) : 0);
          pop.Fix();
          pop.show();
         
       },
       show : function (w,h){
          pop.resetCSS( pop.pId, { overflow :'hidden', width :'0', height :'0', display :'none' });
		  pop.pId.style.display = 'block';
          pop.pW.style.display = 'none';
          var b = m = 0, d = j = 20, t = h = 0,
              c = pop.width,
              n = pop.height;
		   function popShow(){
			  (function (){
				pop.pId.style.height = Math.ceil(pop.easeOut(h,m,n,j)) + 'px';
				if(!pop.isIE6()){ pop.pId.style.marginTop = '-' + parseInt((parseInt(pop.pId.style.height) / 2),10) + 'px' };
				if (h<j){h++; setTimeout(arguments.callee, 26)}
				else {return};
			  })();
			  (function (){
				 pop.pId.style.width = Math.ceil(pop.easeOut(t,b,c,d)) + 'px';
				 pop.pId.style.marginLeft =  '-' + parseInt((parseInt(pop.pId.style.width) / 2),10) + 'px';
				 if (t<d){t++; setTimeout(arguments.callee, 26)}
				 else {pop.pW.style.display = "block"; pop.typePop(); return}                
			  })();		    
		   }
		   setTimeout(popShow,500)

       },
	   setSize : function (W,H){
          var b = parseInt(pop.width), m = parseInt(pop.height), d = j = 20, t = h = 0,
              c = parseInt(W)-parseInt(pop.width),
              n = parseInt(H)-parseInt(pop.height);
		  (function (){
			pop.pId.style.height = Math.ceil(pop.easeOut(h,m,n,j)) + 'px';
			if(!pop.isIE6()){ pop.pId.style.marginTop = '-' + parseInt((parseInt(pop.pId.style.height) / 2),10) + 'px' };
			if (h<j){h++; setTimeout(arguments.callee, 26)}
			else {return};
		  })();
		  (function (){
			 pop.pId.style.width = Math.ceil(pop.easeOut(t,b,c,d)) + 'px';
			 pop.pId.style.marginLeft =  '-' + parseInt((parseInt(pop.pId.style.width) / 2),10) + 'px';
			 if (t<d){t++; setTimeout(arguments.callee, 26)}
			 else {return};
		  })();
          pop.content.style.height = H + 'px';
	   },
       // 移除窗口
       removePop : function (){
           pop.close.onclick = pop.hide;
       },
	   hide : function (){
		   if(pop.isIE6()){pop.Ifr.style.display = 'none'};
		   pop.pM.style.display = 'none';
		   pop.pId.style.display = 'none';
       },
       addEvent : function ( obj, type, fn ) {
           if (obj.addEventListener)
               obj.addEventListener(type, fn, false);
           else if (obj.attachEvent) 
               obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
       },
       stopDefault : function ( e ) {
           if ( e && e.preventDefault )
               e.preventDefault();
           else
               window.event.returnValue = false;
           return false;
       },
       setOpacity : function( elem, level ) {
             if ( elem.filters )
                 elem.style.filter = 'alpha(opacity=' + level + ')';
             else
                 elem.style.opacity = level / 100;
       },
       easeOut : function(t,b,c,d){
            if ((t/=d/2) < 1) return c/2*t*t*t + b;
            return c/2*((t-=2)*t*t + 2) + b;
       },
       Fix : function (){
          var app = navigator.appName;
          var verStr = navigator.appVersion;
          if (app.indexOf('Microsoft') != -1) {
             var dd = (document.compatMode && document.compatMode=='CSS1Compat') ? document.documentElement : document.body ;
             pop.pM.style.height = dd.scrollHeight > dd.offsetHeight ? dd.scrollHeight : dd.offsetHeight + 'px';   
           }
       },
       resetCSS : function ( elem, prop ) {
           for ( var i in prop ) { elem.style[ i ] = prop[i]; }
       },
       ajax : {
           sendRequest : function ( url,callback,postData){
              var req = pop.ajax.createXMLHTTPObject();
              if (!req) return;
              var method = (postData) ? "POST" : "GET";
              req.open(method,url,true);
              req.setRequestHeader('User-Agent','XMLHTTP/1.0');
              if (postData)
               req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
              req.onreadystatechange = function () {
               if (req.readyState != 4) return;
                 callback(req);
              }
              if (req.readyState == 4) return;
              req.send(postData);           
           },
           XMLHttpFactories : function (){
             return [
              function () {return new XMLHttpRequest()},
              function () {return new ActiveXObject("Msxml2.XMLHTTP")},
              function () {return new ActiveXObject("Msxml3.XMLHTTP")},
              function () {return new ActiveXObject("Microsoft.XMLHTTP")}
             ];
           },
           createXMLHTTPObject : function (){
               var xmlhttp = false;
               var factories = pop.ajax.XMLHttpFactories();
               for (var i=0;i<factories.length;i++) {
                try { xmlhttp = factories[i](); }
                catch (e) { continue;}
                break;
               }
               return xmlhttp;           
           }
       } 
}
pop.addEvent(window, 'load', pop.init);
