var popbox = {
	reload:0,
	width_:0,
	height_:0,
	iframe_:0,
	_title:'',
	padingH:50,
	padingW:50,
	type:0,
	boxOpen:0,
	boxID:'box',
	titleBinded:0,	
	
	box:function(href,r,w,h,iframe,title,type,o){	
	
		popbox.closebox();
		popbox.boxOpen = 1;
			
		if(!w){w = 0;}
		if(!h){h = 0;}
		if(!r){r = 0;}
		if(!iframe){iframe = 1;}
		if(!title){title = '';}
		if(!type){type = 0;}
		if(!o){o = 1;}

		if(title.length!=0){
			popbox.padingH = 50 + 23*2;
		}
		
		popbox._title = title;
		popbox.width_ = parseInt(w);
		popbox.height_ = parseInt(h);
		popbox.iframe_ = parseInt(iframe);
		popbox.reload = parseInt(r);
		popbox.type = parseInt(type);
		
		var content = '';
		if(iframe==1){
			content = $('<iframe/>').attr({'id':'iframebox','name':'popboxy','class':'iframebox radius10','frameborder':'0','border':'0'});
		}else{
			content = $('<div/>').attr({'id':'iframebox','class':'iframebox radius10'}).html('');
		}

		if(type==0){
			popbox.boxID = 'box';
			$('body').append(
				$('<div/>').attr({'id':'box'}).html(			
						$('<div/>').attr({'id':'boxActive'}).html('')	
				).append(
					$('<table/>').attr({'id':'boxTable'}).html(
						$('<tr/>').html(
							$('<td/>').attr({'id':'boxTableTd'}).html('')					
						)				
					)
				)
			);			
		}else{
			popbox.boxID = 'flyTable';
			$('body').append(
				$('<table/>').attr({'id':'flyTable'}).html(
					$('<tr/>').html(
						$('<td/>').attr({'id':'boxTableTd'}).html('')					
					)				
				)
			);	
		}
			
		$('#boxTableTd').append(	
				$('<table/>').attr({'id':'boxin'}).html(
					$('<tr/>').html(
						$('<td/>').html(
							$('<div/>').attr({'class':'corner'}).html('')
						)				
					).append(
						$('<td/>').html('')
					).append(
						$('<td/>').html(
							$('<div/>').attr({'class':'corner'}).html($('<div/>').attr({'id':'popboxClose','class':'close'}).html(''))
						)
					)				
				).append(
					$('<tr/>').html(
						$('<td/>').html('')				
					).append(
						$('<td/>').html(
							$('<div/>').attr({'id':'boxinframe','class':'radius10'}).html(
							$('<div/>').attr({'id':'boxinframeIN','class':'radius10'}).html(
								$('<div/>').attr({'id':'pbloading'}).width(200).height(200).html('')
							).append(
								content
							)
							)
						)
					).append(
						$('<td/>').html('')	
					)
				).append(
					$('<tr/>').html(
						$('<td/>').html(
							$('<div/>').attr({'class':'corner'}).html('')
						)				
					).append(
						$('<td/>').html('')
					).append(
						$('<td/>').html(
							$('<div/>').attr({'class':'corner'}).html('')
						)
					)
				)
		);
			
		if(type==0){
			$('#boxActive').css({'opacity':0.7});
			$('#boxTable').bind('click',{'out':1},popbox.closebox_test);
			$('#boxin').bind('click',{'out':-1},popbox.closebox_test);
		}else{
			popbox.moveToCenter();
		}
		
		$('#popboxClose').bind('click',popbox.closebox);
		
		if(iframe!=1){
			$.get(href,function(data){popbox.loaded(1);$('#iframebox').html(data);popbox.boxOpen = 1;});
		}else{
			$('#iframebox').attr({'src':href}).width(w).height(h);
			if(o==1){
				$('#iframebox').bind('load',function(){popbox.loaded(0);});
			}
		}		

		return false;
	},

	moveToCenter:function(){	
		var t = ($(window).height()-$('#'+popbox.boxID).height())/2;
		var l = ($(window).width()-$('#'+popbox.boxID).width())/2;		
		$('#'+popbox.boxID).css({'left':l+'px','top':t+'px'});
	},
	
	
	last_place:0,
	
	closebox_test:function(e){	
		popbox.last_place += parseInt(e.data.out);
		if(popbox.last_place>0){
			popbox.last_place = 0;
			popbox.closebox();
		}
	},
	closebox:function(){
		if(popbox.boxOpen==1){
			popbox.boxOpen = 0;
			$('#popboxClose').unbind('click',popbox.closebox);
			
			if(popbox.type==0){
				$('#boxTable').unbind('click',popbox.closebox_test);
				$('#boxin').unbind('click',popbox.closebox_test);
			}else if(popbox.titleBinded==1){
				$('#popboxTitle').unbind('mousedown',popbox.downSelector);
			}
			
			$('#'+popbox.boxID).remove();
			
			if(popbox.reload==1){
				main.url_reload(1);
			}

			if(popbox.iframe_==0){
				popbox.clear_interval();
			}
			
			popbox.WT_save = 0;
			popbox.HT_save = 0;			
		}
	},
	
	moveSelector:function(e){	
		var t = parseInt(e.data.t + e.pageY - e.data.top); 
		var l = parseInt(e.data.l + e.pageX - e.data.left); 		
		$('#'+popbox.boxID).css({'left':l+'px','top':t+'px'});
		return false;
	},
	downSelector:function(e){
		var t = parseInt($('#'+popbox.boxID).css('top'));
		var l = parseInt($('#'+popbox.boxID).css('left'));
		var cur = {top:e.pageY,left:e.pageX,t:t,l:l};
		$(document).bind('mouseup', cur, popbox.upSelector);
		$(document).bind('mousemove', cur, popbox.moveSelector);
		return false;		
	},
	upSelector:function(){	
		$(document).unbind('mouseup', popbox.upSelector);
		$(document).unbind('mousemove', popbox.moveSelector);
		return false;
	},
	
	loaded:function(c){
		$('#pbloading').hide();
		$('#iframebox').show();		
		if(c==1){
			popbox.res(0);
		}		
		if(popbox._title.length!=0){
			$('#boxinframeIN').before($('<div/>').attr({'class':'popboxTitle','id':'popboxTitle'}).html(popbox._title));
			popbox._title = '';
			if(popbox.type==1){		
				$('#popboxTitle').css({'cursor':'move'})
				.bind('mousedown',popbox.downSelector);
				popbox.titleBinded = 1;
			}	
		}
		if(popbox.type==1){		
			popbox.moveToCenter();
		}
	},
	
	interval_time:500,
	interval_id:0,
	
	set_interval:function(){

		popbox.interval_id = setInterval(popbox.interval_function,popbox.interval_time);
	
	},
	clear_interval:function(){
	
		clearInterval(popbox.interval_id);
	
	},	
	
	interval_function:function(){
		popbox.res(1);
	},

		WT_save:0,
		HT_save:0,
		
	res:function(p){
		if(!p){p = 0;}
		
		var wo = window.parent.document.getElementById('iframebox');
		var wos = window.parent.document.body;
		
		if(wos && wo && window.parent !== window && window.parent.popbox.iframe_ == 1){

			if(p!=1){
				window.parent.popbox.loaded(0);
				//$('html').css({'overflow':'auto'});
				popbox.set_interval();
				wo.style.width = '120px';
				wo.style.height = '100px';				
			}			
		
			var hs =  window.parent.popbox.padingH;
			var ws =  window.parent.popbox.padingW;	
			var W = 0;
			var WT = $(document).width();			
			var WR = window.parent.popbox.width_;
			var HR = window.parent.popbox.height_;

			if(WR>0){
				window.parent.popbox.width_ = 0;
				W = WR;
			}else{
				W = WT;
			}

			if(W > wos.clientWidth-ws){
				W = wos.clientWidth-ws;
			}
				
			if(WT < W){
				WT = W;
			}
				
			var H = $(document).height();
			
			if(HR>0){
				window.parent.popbox.height_ = 0;
				H = HR;
			}
			
			if(WT > W){
				H = H + 17;
			}			
		
			if(H > wos.clientHeight-hs){
				H = wos.clientHeight-hs;
				W = W + 17;
				if(W > wos.clientWidth-ws){
					W = wos.clientWidth-ws;
				}
			}
			

			
			wo.style.width = W + 'px';
			wo.style.height = H + 'px';
			
		}else if(wos && wo){
			if(p!=1){
				popbox.set_interval();				
			}
			var hs =  popbox.padingH;
			var ws =  popbox.padingW;	
					
			var WT = wo.scrollWidth;
			var HT = wo.scrollHeight;
			
			var W = WT;
			var H = HT;
					
			if(W > wos.clientWidth-ws){
				W = wos.clientWidth-ws;
			}				
					
			if(WT > W){
				H = H + 17;
			}
			
			if( H > wos.clientHeight-hs){
				H = wos.clientHeight-hs;
				W = W + 17;
				if(W > wos.clientWidth-ws){
					W = wos.clientWidth-ws;
				}
			}

			$('#iframebox').width(W).height(H);

		}

	}

}
