// ========================== IE Blows Goats ============================ //
if(/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)){  //I hate coding for Explorer so I made it a seperate page
  window.location="ieblowsgoats.php";
}

// ========================== Init & Rendering ========================== //
window.onload=init;
window.onresize=hSet;

function init(){						//run on window load
  var xhttp=new XMLHttpRequest();
  xhttp.open("GET","includes/ort.xml",false);
  xhttp.send("");
  window.blog=xhttp.responseXML;
  window.isPlaying=null;
  window.currentFrame=document.getElementsByClassName('frame')[0];
  var scrollContentList=document.getElementsByClassName('content');
  for(var i=0, c=scrollContentList.length;i<c;i++){
    scrollContentList[i].style.overflow='hidden';
  }
  scrUpList=document.getElementsByClassName('scrUp');
  for(var i=0, c=scrUpList.length; i<c; i++){
    scrUpList[i].addEventListener('mousedown',scrollUp,false);
    scrUpList[i].addEventListener('mouseup',clearScrollTimer,false);
    scrUpList[i].addEventListener('mouseout',clearScrollTimer,false);
  }
  scrDownList=document.getElementsByClassName('scrDown');
  for(var i=0, c=scrDownList.length; i<c; i++){
    scrDownList[i].addEventListener('mousedown',scrollDown,false);
    scrDownList[i].addEventListener('mouseup',clearScrollTimer,false);
    scrDownList[i].addEventListener('mouseout',clearScrollTimer,false);
  }
  hSet();							//sets frames to height of window
  document.getElementById('newsUl').getElementsByTagName('li')[0].style.listStyleType='disc';
  document.body.style.overflow='hidden';
  drawInit();
}


function hSet(){
  window.scrollTo(0,currentFrame.offsetTop);
  var boxList=document.getElementsByClassName('scrollBox');
  for(var i=0, c=boxList.length; i<c; i++){				//shows scroll arrows for divs with overflow
    hasScrollBar(boxList[i]);
  }
}


function hasScrollBar(box){					//shows scroll arrows for divs with overflow
  box.getElementsByClassName('scrUp')[0].style.opacity=.2;
  box.getElementsByClassName('scrDown')[0].style.opacity=1;
  var content=box.getElementsByClassName('content')[0];
  content.scrollTop=0;
  if(content.scrollHeight > content.offsetHeight){
    box.getElementsByClassName('scrUp')[0].style.display='block';
    box.getElementsByClassName('scrDown')[0].style.display='block';
  }else{
    box.getElementsByClassName('scrUp')[0].style.display='none';
    box.getElementsByClassName('scrDown')[0].style.display='none';
  }
}


// ========================= Navigation ==================== //
function navChange(el){
  try{if (navTimer){clearTimeout(navTimer);}}catch(e){}		//clear any current navTimer
  currentFrame=document.getElementById(el.href.split("#")[1]);
  var targ=currentFrame.offsetTop;  //get offset of frame pointed to by anchor
  var whereami=window.pageYOffset;
  var incabs=60;
  var inc;
  if(whereami>targ){inc=-incabs}else{inc=incabs}		//up or down the page?
  var navCounter=Math.floor(Math.abs(targ-whereami)/incabs);	//set a counter for navScroll function
  navScroll(inc, navCounter, targ);				//call scrolling loop
  return false;
}


function navScroll(inc, navCounter, targ){			//smoothly scroll to target frame
  if(navCounter>0){
    window.scrollBy(0,inc);
    navCounter--;
    navTimer=setTimeout("navScroll("+inc+","+navCounter+","+targ+")", 30)
  }else{
    window.scrollTo(0,targ);
  }
}


// ========================= Scrollboxes =================== //
function clearScrollTimer(){
  try{if (scrollTimer){clearTimeout(scrollTimer);}}catch(e){}
}


function scrollUp(){
  if(this.parentNode.getElementsByClassName('content')[0].scrollTop>0){
    this.parentNode.getElementsByClassName('scrDown')[0].style.opacity=1;
    Uscrolling(this.parentNode.id);
  }else{
    this.style.opacity=.2;
  }
}

function scrollDown(){
  if(this.parentNode.getElementsByClassName('content')[0].scrollTop<this.parentNode.getElementsByClassName('content')[0].scrollHeight-this.parentNode.getElementsByClassName('content')[0].clientHeight){
    this.parentNode.getElementsByClassName('scrUp')[0].style.opacity=1;
    Dscrolling(this.parentNode.id);
  }else{
    this.style.opacity=.2;
  }
}

function Uscrolling(elID){
  var targ=document.getElementById(elID).getElementsByClassName('content')[0];
  if(targ.scrollTop>0){
    targ.scrollTop-=20;
    scrollTimer=setTimeout("Uscrolling('"+elID+"')", 30);  
  }else{
    document.getElementById(elID).getElementsByClassName('scrUp')[0].style.opacity=.2;
  }
}

function Dscrolling(elID){
  var targ=document.getElementById(elID).getElementsByClassName('content')[0];
  if(targ.scrollTop<targ.scrollHeight-targ.clientHeight){
    targ.scrollTop+=20;
    scrollTimer=setTimeout("Dscrolling('"+elID+"')", 30);  
  }else{
    document.getElementById(elID).getElementsByClassName('scrDown')[0].style.opacity=.2;
  }
}
// ================================ News ============================= //

function newsSwap(index){
  var itemList=document.getElementById('newsUl').getElementsByTagName('li');
  for(var i=0, c=itemList.length; i<c; i++){
    itemList[i].style.listStyleType='none';
  }
  itemList[index].style.listStyleType='disc';
  var item=blog.getElementsByTagName('item')[index];
  var pList=item.getElementsByTagName('description')[0].firstChild.nodeValue.split("\n");
  var page1=document.getElementById('newsItem').getElementsByClassName('content')[0];
  while(page1.firstChild){page1.removeChild(page1.firstChild);}
  var newHeader=document.createElement('h3');
  newHeader.appendChild(document.createTextNode(item.getElementsByTagName('title')[0].firstChild.nodeValue));
  page1.appendChild(newHeader);
  var pic=item.getElementsByTagName('enclosure')[0];
  if(pic){
    var newImage=new Image();
    newImage.addEventListener('load',function(){hasScrollBar(document.getElementById('newsItem'))},false);
    newImage.src=pic.getAttribute('url');
    page1.appendChild(newImage);
  }
  for(var i=0, c=pList.length; i<c; i++){
    if(pList[i] != "\r"){
      var newP=document.createElement('p');
      newP.innerHTML=pList[i];
      page1.appendChild(newP);
    }
  }
  hasScrollBar(document.getElementById('newsItem'));
  return false;
}

// ================================ Music ============================ //
function albumSwap(album){
  if(album=='prsvbwshat'){
    document.getElementById('untitledTracks').style.display='none';
    document.getElementById('prsTracks').style.display='';
    document.getElementById('prsvbwshat').setAttribute('class','album choosed');
    document.getElementById('untitled').setAttribute('class','album unchoosed');
  }
  if(album=='untitled'){
    document.getElementById('untitledTracks').style.display='';
    document.getElementById('prsTracks').style.display='none';
    document.getElementById('prsvbwshat').setAttribute('class','album unchoosed');
    document.getElementById('untitled').setAttribute('class','album choosed');
  }
  hasScrollBar(document.getElementById('trackList'));
  return false;
}


function ortPlay(track){
  if(track == isPlaying){
    document.getElementById('jukeBox').innerHTML="";
    track.getElementsByTagName('img')[0].src='includes/default/play.png';
    isPlaying=null;
  }else{
    document.getElementById('jukeBox').innerHTML="<embed src='"+track.href+"' />";
    if(isPlaying != null){isPlaying.getElementsByTagName('img')[0].src='includes/default/play.png';}
    isPlaying=track;
    isPlaying.getElementsByTagName('img')[0].src='includes/default/pause.png';
  }
  return false;
}

// ================================ Photos =========================== //

function lightbox(pic){
  var LBox=document.createElement('div');
  LBox.setAttribute('id','lBox');
  var LBPic=new Image();
  LBPic.setAttribute('src',pic);
  LBPic.setAttribute('id','LBPic');
  LBox.addEventListener('click',killLBox,false);
  LBPic.style.opacity=0;
  LBPic.addEventListener('load',picFade,false);
  LBPic.addEventListener('click',picSwap,false);
  document.getElementById('photos').appendChild(LBox);
  document.getElementById('photos').appendChild(LBPic);
  return false;
}

function picFade(){
  if(document.getElementById('LBPic').style.opacity < 1){
    document.getElementById('LBPic').style.opacity=Number(document.getElementById('LBPic').style.opacity)+.1;
    setTimeout('picFade()',40);
  }
}

function picSwap(){
  var picList=document.getElementById('thumbs').getElementsByTagName('a');
  for(var i=0, c=picList.length; i<c; i++){
    if(picList[i].href==this.src){
      if(picList[i+1]){
        i++;
        break;
      }else{
        i=0;
        break;
      }
    }
  }
  var pho=document.getElementById('LBPic');
  pho.style.opacity=0;
  pho.src=picList[i].href;
}

function killLBox(){
  document.getElementById('photos').removeChild(document.getElementById('photos').lastChild);
  document.getElementById('photos').removeChild(document.getElementById('photos').lastChild);
}