document.domain = "daum.net";

UI.stModal=function(url,options){
	this.options={
		type:'image',
		alt:'',
		loading:false,
		opacity:30,
		width:0,height:0
	};
	Object.extend(this.options, options);
	var options=this.options;

	if(!UI.$('UIModalB')) UI.stModal.print();
	UI.setOpacity(UI.$('UIModalB'), options.opacity);
	
	UI.stModal.setB();
	if(options.loading) UI.stModal.dpPosition(UI.$('UIModalL'));

	if(options.type=='image')
	{
		UI.$('UIModalF').innerHTML = '<img id="UIModalImage" src="'+url+'" alt="'+options.alt+'" />';
		if(options.loading) UI.$('UIModalImage').style.display='none';
		UI.addEvent(UI.$('UIModalImage'), 'load', UI.stModal.onload);
	}
	else if(options.type=='iframe')
	{		
		UI.$('UIModalF').innerHTML = '<iframe name="UIModalIframe" id="UIModalIframe" src="'+url+'" '+options.status+'></iframe>';
		if(options.loading) 
		{
			UI.$('UIModalIframe').style.display='none';
			UI.addEvent(UI.$('UIModalIframe'), 'load', UI.stModal.onload);
		}
		UI.stModal.dpPosition(UI.$('UIModalF'));
	}
	UI.stModal.self=this;
	UI.addEvent(UI.$('UIModalB'), 'click', UI.stModal.reset);
	UI.addEvent(window,'resize', UI.stModal.onresize);
	UI.addEvent(window,'scroll', UI.stModal.onscroll);
};
UI.stModal.self={};
UI.stModal.onload=function(){
	UI.$('UIModalL').style.display='none';
	var pos=UI.getScroll();
	UI.stModal.dpPosition(UI.$('UIModalF'));	
	if(UI.stModal.self.options.type=='image')
	{
		UI.$('UIModalImage').style.display='block';
		UI.resizeImage(UI.$('UIModalImage'),pos.width,pos.height);	
	}
	else 
	{
		UI.$('UIModalIframe').style.display='block';
	}
	UI.stModal.dpPosition(UI.$('UIModalF'));
};
UI.stModal.print=function(){
	var d=document.createElement('div');
	var s='';
	s+='<div id="UIModalB" style="z-index:99998;width:100%;height:100%;position:absolute;display:none;background-color:#000;"></div>';
	s+='<div id="UIModalF" style="z-index:99999;position:absolute;display:none;"></div>';
	s+='<div id="UIModalL" style="z-index:99999;display:none;position:absolute;border:2px solid gray;">로딩중..</div>';
	d.innerHTML=s;
	document.getElementsByTagName('body')[0].appendChild(d);		
};
UI.stModal.setB=function(){
	var w=UI.$('UIModalB');
	var pos=UI.getScroll();
	w.style.top=pos.top+'px';
	w.style.left=pos.left+'px';
	if(document.all)
	{
		w.style.width=pos.width+'px';
		w.style.height=pos.height+'px';
	}
	w.style.display='block'	
};
UI.stModal.dpPosition=function(el){
	el.style.display='block';
	var pos=UI.getScroll();
  var browser = UI.getBrowser();
  h = (browser.ff)? 75 :66; 
  el.style.left=(pos.width/2-el.offsetWidth/2+pos.left)-h+'px';
	el.style.top= '137px';	
};
UI.stModal.reset=function(){
	UI.stModal.self=null;
	UI.$('UIModalF').innerHTML='';
	UI.$('UIModalB').style.display='none';
	UI.$('UIModalF').style.display='none';
	UI.$('UIModalL').style.display='none';
	UI.delEvent(window,'resize',UI.stModal.onresize);
	UI.delEvent(window,'scroll',UI.stModal.onscroll);
};
UI.stModal.onresize=function(){
	var pos=UI.getScroll();
	if(UI.stModal.self.options.type=='image') UI.resizeImage(UI.$('UIModalImage'),pos.width,pos.height);
	UI.stModal.dpPosition(UI.$('UIModalF'));
	UI.stModal.setB();
};
UI.stModal.onscroll=function(){
	UI.stModal.onresize();
};

pastHeadline = function(url) {
new UI.stModal(url, {type:'iframe',status:'width=606 height=700 frameborder=0 scrolling=no style=\'border:1px solid #555\'' } );
}

var stMyvalue="";
stMy_popnews = function(mytype) {
  if(mytype=="reset"){ //reset
    document.stMyfrm.reset(); 
    UI.setCookie('stMy_popnews','',-1 ,'/','media.daum.net');;
	stMyvalue="";
    alert("초기화 되었습니다.");
    new UI.Tab('stPopNews',3, {snum:1});
  }else if(mytype=="setup"){ //setup
    for (i=0;i<document.stMyfrm.stMy.length;i++) {
      if(document.stMyfrm.stMy[i].checked==true) {
        stMyvalue = document.stMyfrm.stMy[i].value;
		stMy_SelTextID = 'l_'+document.stMyfrm.stMy[i].id;
		stMyvalue += "/" + document.getElementById(stMy_SelTextID).childNodes[0].nodeValue;
	  }
    }
    if(stMyvalue == "")
      alert("원하시는 지역, 연령대 중 한가지를 선택한 후 설정 버튼을 눌러주세요");
    else {
      alert("설정되었습니다.");
      UI.setCookie('stMy_popnews',stMyvalue,365,'/','media.daum.net');
      stMy_popnews_view();
    }
  }
}

_type = "";
_dataid ="";

whoData = function(type,secid,code,agecode){
_type = type;
_secid = secid;
_code = code;
_agecode = agecode;
  action = "http://media.daum.net/netizen/distribution/category/"+_type+"/json/"+secid+"_ALL.json"; 
  new UI.Ajax( { url:action, param:'', asynchronous:true, onComplete:write } );
}

write = function(req){
    var reqText = req.responseText;
    if(reqText.indexOf("<!--Generated by Blues System-->") > 0)
      reqText = reqText.replace("<!--Generated by Blues System-->","");

  var result = eval('(' + reqText + ')');
  var obj=null;
  var writeHTML = '';
  var writediv = '';
  if(_type =="agesex") _type = "age";
  var json = (_type =="region")? eval('result.news.'+_type+'_'+_code) : eval('result.news.'+_type+'_'+_code+'.sex'+_agecode)  ;

  for(var i=0 ; i<10; i++){
    obj= json[i];
	title=UI.length(obj.title,40,"...");
	if(obj !=null)
		writeHTML += '<li class="no_s'+(i+1)+'"><a href="'+obj.url+'">'+UI.html2str(title,true)+'<\/a><\/li>';
  }
  UI.$("stPopNews_my").innerHTML = writeHTML;
}

stMy_popnews_setup = function() {  
  UI.$("div_stPopNews_3_setup").style.display="block";
  UI.$("div_stPopNews_3_view").style.display="none";
  getvalue = UI.getCookie('stMy_popnews');
  getvalueArr = getvalue.split("/");
  for (i=0;i<document.stMyfrm.stMy.length;i++) {
    if(document.stMyfrm.stMy[i].value == getvalueArr[0]) {
      document.stMyfrm.stMy[i].checked=true;	
    }
  } 
}

_secCode="";
stMy_popnews_view = function() {
  UI.$("div_stPopNews_3_setup").style.display="none";
  UI.$("div_stPopNews_3_view").style.display="block";
  ckvalue = UI.getCookie('stMy_popnews');
  ckvalueArr = ckvalue.split("/");
  type = (ckvalueArr[0].substring(0,1)=="r")? "region":"agesex";
  code = (ckvalueArr[0].substring(0,1)=="r")? ckvalueArr[0].substring(1,ckvalue.length) : ckvalueArr[0].substring(1,2);
  agecode =(ckvalueArr[0].substring(0,1)=="r")? null: ckvalueArr[0].substring(2,ckvalueArr[0].length);
  UI.$("stMySelname").innerHTML= ckvalueArr[1];
  whoData(type,_secCode, code, agecode);  
}