/********************************************************/
/* magDOM-UB version 1.0                                */
/*                                                      */
/* writen by Magnum                                     */
/* date 2008/10/5                                       */
/********************************************************/

// for IE

var doc = document;
/*eval('var document = doc');*/

// /for IE

// 初期化

var UB = new Object();
UB.Misc = new Object();
UB.Core = new Object();
UB.Event = new Object();
UB.Ajax = new Object();

// /初期化

UB.Core.childNodes = function(node){
 var i,inner,str;
 var array = new Array();
 if(!node){
  return array;
 }
 if(!node.hasChildNodes()){
  return array;
 };
 var children = node.childNodes;
 for(i = 0;i < children.length;i++){
  inner = children.item(i);
  if(inner.nodeType == 3){
   str = inner.nodeValue;
   if(!str.match('/[^\s\t\n\r]/')){
    continue;
   }
  }
  array.push(inner);
 }
 array.item = function(num){
  if(array[num]){
   return array[num];
  }else{
   return null;
  }
 }
 return array;
}

UB.Core.previousNode = function(node){
 var obj,pvs,str;
 if(!node){
  return null;
 }
 obj = node;
 while(pvs = obj.previousSibling){
  if(pvs.nodeType == 3){
   str = pvs.nodeValue;
   if(!str.match('/[^\s\t\n\r]/')){
    obj = pvs;
    continue;
   }
  }
  return pvs;
 }
 return null;
}

UB.Core.nextNode = function(node){
 var obj,nxt,str;
 if(!node){
  return null;
 }
 obj = node;
 while(nxt = obj.nextSibling){
  if(nxt.nodeType == 3){
   str = nxt.nodeValue;
   if(!str.match('/[^\s\t\n\r]/')){
    obj = nxt;
    continue;
   }
  }
  return nxt;
 }
 return null;
}

UB.Core.TextContent = function(node){
 var i,obj;
 var str = '';
 if(!node){
  return str;
 }
 if(typeof node.textContent != 'undefined'){
  str = node.textContent;
 }else if(typeof node.innerText != 'undefined'){
  str = node.innerText;
 }else if(node.hasChildNodes()){
  for(i = node.childNodes.length;i--;){
   obj = node.childNodes.item(i);
   if(obj.nodeType == 3){
    str = obj.nodeValue+str;
   }else{
    str = UB.core.getTextContent(obj)+str;
   }
  }
 }
 return str;
}

UB.Core.removeChilds = function(node){
 while(node.hasChildNodes()){
  node.removeChild(node.lastChild);
 }
}

UB.Core.removeChild = function(elm){
 if(elm != null){
  elm.parentNode.removeChild(elm);
 }
}

UB.Misc.empty = function(){
 return false;
}

UB.Misc.windowSize = function(){
 var win = new Object();
 if(document.uniqueID){
  win.width = document.documentElement.clientWidth;
  win.height = document.documentElement.clientHeight;
 }else{
  win.width = window.innerWidth;
  win.height = window.innerHeight;
 }
 return win;
}

UB.Misc.windowX = function(){
 return UB.Misc.windowSize().width;
}

UB.Misc.windowY = function(){
 return UB.Misc.windowSize().height;
}

UB.Misc.docSize = function(){
 var doc = new Object();
 doc.width = document.documentElement.scrollWidth || document.body.scrollWidth;
 doc.height = document.documentElement.scrollHeight || document.body.scrollHeight;
 return doc;
}

UB.Misc.docX = function(){
 return UB.Mise.docSize().width;
}

UB.Misc.docY = function(){
 return UB.Mise.docSize().height;
}

UB.Misc.getElmPosition = function(elm){
 var Position = new Object();
 Position.x = elm.offsetLeft;
 Position.y = elm.offsetTop;
 while(elm.offsetParent){
  elm = elm.offsetParent;
  Position.x += elm.offsetLeft;
  Position.y += elm.offsetTop;
 }
 return Position;
}

UB.Misc.getPosLeft = function(elm){
 return UB.Misc.getElmPosition(elm).x;
}

UB.Misc.getPosTop = function(elm){
 return UB.Misc.getElmPosition(elm).y;
}

UB.Misc.getScroll = function(){
 var scroll = new Object();
 scroll.left = document.body.scrollLeft || document.documentElement.scrollLeft;
 scroll.top  = document.body.scrollTop  || document.documentElement.scrollTop;
 return scroll;
}

UB.Misc.getScrollY = function(){
 return UB.Misc.getScroll().top;
}

UB.Misc.getScrollX = function(){
 return UB.Misc.getScroll().left;
}

UB.Misc.createElm = function(elm){
 return document.createElement(elm);
}

UB.Misc.createTxt = function(txt){
 return document.createTextNode(txt);
}

UB.Misc.getElm = function(elm,parent){
 if(parent != null){
  return parent.getElementById(elm);
 }else{
  return document.getElementById(elm);
 }
}

UB.Misc.getName = function(elm,parent){
 if(parent != null){
  return parent.getElementsByName(elm);
 }else{
  return document.getElementsByName(elm);
 }
}

UB.Misc.getTag = function(elm,parent){
 if(parent != null){
  return parent.getElementsByTagName(elm);
 }else{
  return document.getElementsByTagName(elm);
 }
}

UB.Misc.getStyle = function(elm){
 return elm.style;
}

UB.Misc.getValue = function(elm){
 var val;
 if(elm.value != 'undefined'){
  val = elm.value;
 }else{
  val = elm.selectedIndex;
  val = elm.options[val].value;
 }
 return val;
}


UB.Misc.openWindow = function(winName,url,x,y){
 var positionX = screen.availWidth-x-20;
 var positionY = 20;
 winName = window.open(url,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,titlebar=no,resizable=no,fullscreen=no,width='+x+',height='+y+',top='+positionY+',left='+positionX);
 return winName;
}

var openWindow = function(url,x,y,position){
 if(position){
  switch(position){
   case 1:
    var positionX = 20;
    var positionY = 20;
   break;
   case 2:
    var positionX = screen.availWidth-x-20;
    var positionY = 20;
   break;
   case 2:
    var positionX = 20;
    var positionY = screen.availWidth-y-20;
   break;
   case 2:
    var positionX = screen.availWidth-x-20;
    var positionY = screen.availWidth-y-20;
   break;
  }
 }else{
  var positionX = 20;
  var positionY = 20;
 }
 var winName = window.open(url,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,titlebar=no,resizable=no,fullscreen=no,width='+x+',height='+y+',top='+positionY+',left='+positionX);
 return winName;
}

// window event
UB.Event.Trg = function(evt){
 if(evt && evt.target){
  if(evt.target.nodeType == 3){
   return evt.target.parentNode;
  }else{
   return evt.target;
  }
 }else if(window.event && window.event.srcElement){
  return window.event.srcElement;
 }else{
  return null;
 }
}

UB.Event.addEventListener = function(elm,type,func){
 if(!elm){
  return false;
 }
 if(elm.addEventListener){
  elm.addEventListener(type,func,false);
 }else if(elm.attachEvent){
  elm.attachEvent('on'+type,func);
 }else{
  return false;
 }
 return true;
};

UB.Event.removeEventListener = function(elm,type,func){
 if(!elm){
  return false;
 }
 if(elm.removeEventListener){
  elm.removeEventListener(type,func,false);
 }else if(elm.detachEvent){
  elm.detachEvent('on'+type,func);
 }else{
  return false;
 }
 return true;
};

UB.Event.preventDefault = function(evt){
 if(evt && evt.preventDefault){
  evt.preventDefault();
  evt.currentTarget['on'+evt.type] = function(){
   return false;
  	};
 }else if(window.event){
  window.event.returnValue = false;
 }
};

UB.Event.stopPropagation = function(evt){
 if(evt && evt.stopPropagation){
  evt.stopPropagation();
 }else if(window.event){
  window.event.cancelBubble = true;
 }
}

// /window event

// mouse section
UB.Event.getMousePos =function(evt){
 var Pos = new Object();
 if(window.opera){
  Pos.x = event.clientX;
  Pos.y = event.clientY;
 }else if(document.all){
  Pos.x = document.body.scrollLeft+event.clientX;
  Pos.y = document.body.scrollTop+event.clientY;
 }else if(navigator.userAgent.indexOf('Safari') != -1){
  Pos.x = event.pageX;
  Pos.y = event.pageY;
 }else{
  Pos.x = evt.pageX;
  Pos.y = evt.pageY;
 }
 return Pos;
}

UB.Event.MouseX = function(evt){
 return UB.Event.getMousePos(evt).x;
}

UB.Event.MouseY = function(evt){
 return UB.Event.getMousePos(evt).y;
}

UB.Event.OffsetPos = function(evt){
 var Pos = new Object();
 if(!evt){
  evt = window.event;
 }
 if(document.all){
  Pos.x = (evt.clientX+document.body.scrollLeft)-((evt.clientX+document.body.scrollLeft)-evt.srcElement.offsetLeft);
  Pos.y = (evt.clientY+document.body.scrollTop)-((evt.clientY+document.body.scrollTop)-evt.srcElement.offsetTop);
 }else{
  Pos.x = (evt.clientX+document.body.scrollLeft)-((evt.clientX+document.body.scrollLeft)-evt.target.offsetLeft);
  Pos.y = (evt.clientY+document.body.scrollTop)-((evt.clientY+document.body.scrollTop)-evt.target.offsetTop);
 }
 return Pos;
}

UB.Event.OffsetX = function(evt){
 return UB.Event.OffsetPos(evt).x;
}

UB.Event.OffsetY = function(evt){
 return UB.Event.OffsetPos(evt).y;
}

UB.Event.BoxPos = function(evt){
 var Pos = new Object();
 if(!evt){
  evt = window.event;
 }
 if(document.all){
  Pos.x = evt.offsetX;
  Pos.y = evt.offsetY;
 }else if(navigator.userAgent.indexOf('Safari') != -1){
  Pos.x = evt.offsetX;
  Pos.y = evt.offsetY;
 }else{
  Pos.x = evt.layerX;
  Pos.y = evt.layerY;
 }
 return Pos;
}

UB.Event.BoxX = function(evt){
 return UB.Event.BoxPos(evt).x;
}

UB.Event.BoxY = function(evt){
 return UB.Event.BoxPos(evt).y;
}

UB.Event.getSelection = function(){
 var selenct;
 if(document.all){
  select = document.selection.createRange().text;
 }else if(navigator.userAgent.indexOf('Safari') != -1){
   select = window.getSelection();
 }else{
  select = document.getSelection();
 }
 return select;
}

// /mouse section

// keyboard section

UB.Event.noEnter = function(evt){
  evt = (evt) ? evt : event; 
  var charCode=(evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
  if(Number(charCode) == 13 || Number(charCode) == 3){
   return false;
  }else{
   return true;
  }
 }

// /keyboard section

UB.Ajax.Request = function(url,$key,method,func,res){
 var req;
 var response;
 try{
  req = new ActiveXObject("Msxml2.XMLHTTP");
 }catch(e){
  try{
   req = new ActiveXObject("Microsoft.XMLHTTP");
  }catch(e){
   req = false;
  }
 }
 if(!req && typeof XMLHttpRequest != "undefined"){
  req = new XMLHttpRequest();
 }
 if(req){
  req.onreadystatechange = function(){
   if(req.readyState == 4 && req.status == 200){
    if(res == 'txt'){
     response = req.responseText;
    }else if(res == 'xml'){
     response = req.responseXML;
    }
    func(response);
   }
  }
  req.open(method,url);
  req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
  req.send($key);
 }
}

var telopIntervalMS = 25;
var telopIntervalPX = 6;
var telopLines = new Array();
var telopTimer;
var telopIndex = 0;
var telopStop = true;

var telopInit = function(){
 var telopSpace = UB.Misc.getElm('telop');
 var telopContents = UB.Misc.getElm('telopContents');
 var telopContentsStyle = UB.Misc.getStyle(telopContents);
 var Lines = telopContents.getElementsByTagName('DIV');
 for(var i = 0;i < Lines.length;i++){
  var Line = Lines.item(i).innerHTML;
  telopLines.push(Line);
 }
 
 var telopContents = UB.Misc.getElm('telopContents');
 UB.Core.removeChilds(telopContents);
 telopContentsStyle.display = 'block';
 
 telopStart();
}

var telopStart = function(){
 var telopSpace = UB.Misc.getElm('telop');
 var telopSpaceStyle = UB.Misc.getStyle(telopSpace);
 var telopContents = UB.Misc.getElm('telopContents');
 var telopContentsStyle = UB.Misc.getStyle(telopContents);
 
 var News = telopLines[telopIndex];
 UB.Core.removeChilds(telopContents);
 telopContents.innerHTML = News;
 
 telopContentsStyle.left = telopSpace.offsetWidth+'px';
 telopTimer = setInterval(telopMove,telopIntervalMS);
}

var telopMove = function(){
 var telopSpace = UB.Misc.getElm('telop');
 var telopContents = UB.Misc.getElm('telopContents');
 var telopContentsStyle = UB.Misc.getStyle(telopContents);
 
 var positionLeft = parseInt(telopContentsStyle.left);
 var newLeft = positionLeft-telopIntervalPX;
 var flg = Math.round((newLeft/10))+10;
 
 if(newLeft+telopContents.offsetWidth > 0){
  telopContentsStyle.left = newLeft+'px';
 }else{
  clearInterval(telopTimer);
  telopIndex++;
  if(telopIndex >= telopLines.length){
   telopIndex = 0;
   telopStop = true;
  }
  telopStart();
 }
}
