var Popup,ClosePopup
YUI({base:"/public/javascript/yui3/", timeout: 10000}).use('node','io', function(Y) {
	//create the popup
	var default_scrollHeight = 0 ;
	var default_o_scrollHeight = 0 ;
	var now_top = 0 ;
	Popup = function(object){
		//get widow  setting
		Lock('hidden');
		var uWidth = (object.width) ? object.width:320 ;
		var uHeight = (object.height) ? object.height:300 ;
		var include_type = (object.includeType) ? object.includeType:'string' ;
		var content = object.content ? object.content:'undefined' ;
		var ajax_method = object.ajax_method ? object.ajax_method:'GET';
		var form_id = object.form_id ? object.form_id:'form1';
		
		var AllScreen = Y.get(document.body);
		
		var sWidth = AllScreen.get('winWidth');
		var sHeight = AllScreen.get('winHeight');
		var middleWidth = 0 ;
		middleWidth = (sWidth - uWidth)/2 ;
		
		if(uHeight>sHeight){
			uHeight = sHeight - 30 ;
		}
		
		var middleHeight = 0 ;
		middleHeight = (sHeight - uHeight)/2 ;
		
		if(middleHeight<=0){middleHeight = 30 ;sHeight = uHeight + 30 ;}
		//------------------------create the backgroung  gray div-------------------------------- 
		AllScreen.appendChild(Y.Node.create('<div id="PopupArea" ></div>'));
		
		if(window.screen.availHeight > sHeight){
			sHeight = window.screen.availHeight ;
		}
		
		var popup_area = Y.get('#PopupArea') ;
		popup_area.setStyle('width',sWidth+'px');
		popup_area.setStyle('height',(parseInt(AllScreen.get('winHeight'))+50)+'px');
		popup_area.setStyle('z-index','2');
		popup_area.setStyle('position','absolute');
		popup_area.setStyle('top','-50px');
		popup_area.setStyle('left','0px');
		popup_area.setStyle('backgroundColor', '#3c3d3f');
		popup_area.setStyle('opacity', 0.7);
		popup_area.on('click',ClosePopup);
		//----------------------careat content div---------------------------------------------------------
		AllScreen.appendChild(Y.Node.create('<div id="PopupContent" ><div id="PopupTop"></div></div>'));
		var popup_content = Y.get('#PopupContent') ;
		
		popup_content.setStyle('width',uWidth+'px');
		popup_content.setStyle('height',uHeight+'px');
		popup_content.setStyle('z-index','99');
		popup_content.setStyle('position','absolute');
		
		popup_content.setStyle('top',middleHeight+'px');
		popup_content.setStyle('left',middleWidth+'px');
		popup_content.setStyle('backgroundColor', '#eeeeee');
		popup_content.setStyle('borderRight', '2px solid #666666');
		popup_content.setStyle('borderBottom', '2px solid #666666');
		
		//--------------------setting show content div -----------------------------------------------------
		var popup_top = Y.get('#PopupTop') ;
		popup_top.setStyle('width',uWidth+'px');
		popup_top.setStyle('height',uHeight+'px');
		popup_top.setStyle('z-index','99');
		popup_top.setStyle('backgroundColor', '#eeeeee');
		popup_top.setStyle('border', '0px solid #cc0000');
		popup_top.setStyle('overflow', 'hidden');
		
		//-----------------create the show content------------------------------------------
		var innerSTR = '';
		switch ( include_type ) {
			case 'dom' :
				popup_top.appendChild(content);
				break ;
			case 'string' :
				innerSTR = content;
				popup_top.set('innerHTML',innerSTR);
				break ;
			case 'ajax' :
				var content = content ;
				Xcontent = content.split("?");
				if(ajax_method == 'GET'){
					var cfg = {
						method:ajax_method,
						headers: { 'X-Transaction': 'GET Example;charset:utf-8'},
						data: Xcontent[1]
					};	
				}
				else{
					var cfg = {
						method:'POST',
						headers: { 'X-Transaction': 'POST Example;charset:utf-8'},
						data: Xcontent[1],
						form: {id:form_id,useDisabled: true} 
					};
				}
				
				var request = Y.io(Xcontent[0] , cfg) ;
				var handleSuccess = function(ioId, o)
				{   
					if(o.responseText !== undefined){
						popup_top.set('innerHTML',o.responseText);
					}
				}
				
				var handleFailure = function(ioId, o){  
					if(o.responseText !== undefined){  
						var s = "<li>Transaction id: " + ioId + "</li>";   
						s += "<li>HTTP status: " + o.status + "</li>";   
						s += "<li>Status code message: " + o.statusText + "</li>";   
						getContent = s ;
						popup_top.set('innerHTML',s);
					}
				}
				Y.on('io:success', handleSuccess);
				Y.on('io:failure', handleFailure);
				break ; 
			case 'iframe' :
				innerSTR = '<iframe frameborder="0" scrolling="no" src="'+content+'" width="100%" height="'+uHeight+'" name="out"></iframe>' ;
				popup_top.set('innerHTML',innerSTR);
				break ;
		}
		
		//---------------------start listen the scroll event---------------------------------------------------------
		Y.on('scroll',MoveLayer,window);
		default_scrollHeight = document.documentElement.scrollHeight ;
		default_o_scrollHeight = default_scrollHeight ;
		MoveLayer();
	}
	
	//moving content div
	function MoveLayer() {
		
		var AllScreen = Y.get(document.body);
		var popup_area = Y.get('#PopupArea') ;
		var popup_content = Y.get('#PopupContent') ;
		var sHeight = AllScreen.get('winHeight');
		if(popup_content){
			
			var uHeight = parseInt(popup_content.getStyle('height').replace('px','')) ;
			
			middleHeight = (sHeight - uHeight)/2 ;
			if(middleHeight<=0){middleHeight = 50 ;sHeight = uHeight + middleHeight + 50 ;}
			var tempScrollTop = Y.get(document.body).get('docScrollY')
			sTop = Y.get(document.body).get('docScrollY') + parseInt(middleHeight);
			
			//if((document.documentElement.scrollHeight)+29 > ((Y.get(document.body).get('docScrollY'))+parseInt(AllScreen.get('winHeight'))) ){
			popup_content.setStyle('top',sTop+'px');
			
			if((document.documentElement.scrollHeight) > ((tempScrollTop)+parseInt(AllScreen.get('winHeight'))) ){
				popup_area.setStyle('top',tempScrollTop+'px');
			}
			else{
				popup_area.setStyle('top',(tempScrollTop-50)+'px');
			}
			//}
		}
	}
	
	//close the popup 
	ClosePopup = function(){
		var crrDiv1 = Y.get('#PopupArea');
		crrDiv1.get('parentNode').removeChild(crrDiv1);
		var crrDiv2 = Y.get('#PopupContent');
		if(Y.get('#PopupTop iframe') != null){
			Y.get('#PopupTop iframe').set('src','');
		}
		crrDiv2.get('parentNode').removeChild(crrDiv2);
		Y.detach('scroll', MoveLayer, window) ;
		Lock('visible') ;
	}
	
	function Lock(visibility){
		
		var selects = Y.Node.get(window.document).queryAll('select');
		if(selects){
			Y.each(selects, function(v1, k1, items1) {
				if (v1 != null) {	
					v1.setStyle('visibility',visibility) ;
				}
			});
		}
		var _embeds = Y.Node.get(window.document).queryAll('embed');
		if(_embeds){
			Y.each(_embeds, function(v2, k2, items2) {
				if (v2 != null) {	
					v2.setStyle('visibility',visibility) ;
				}
			});
		}
		var _objects = Y.Node.get(window.document).queryAll('object');
		if(_objects){
			Y.each(_objects, function(v3, k3, items3) {
				if (v3 != null) {	
					v3.setStyle('visibility',visibility) ;
				}
			});
		}
	}
})