﻿var firstNew = 0;

function initPage() {
  //document.getElementById("hfGenre").value = 'PRODUCT DESIGN';
  showRelatedNewLoading();
  showRecentlyAddedLoading();
  showMostPopularLoading();
  showSingleNewLoading();
  
  getRecentlyAdded(1);
  getMostPopular(1);
  
  if (location.hash != "" && location.hash.substr(0, 5) == "#new_") {
    var newid = parseInt(location.hash.substr(5),10);
    getSingleNew(newid);
  }else{
  
    getRelatedNews(1);
  }
}

//INETERACTIVES 
function reload(){
    getMostPopular(1);
    getRecentlyAdded(1);

}
//AJAX
function showRelatedNews() {
  
  document.getElementById("divRelatedNews").style.display = "";
  document.getElementById("divRecentlyAdded").style.display = "none";
  document.getElementById("divMostPopular").style.display = "none";
  document.getElementById("aRelatedNews").className = "secTabSel";
  document.getElementById("aRecentlyAdded").className = "secTab";
  document.getElementById("aMostPopular").className = "secTab";
  document.getElementById("genreCheckBox").style.display="none";  
}

function showRecentlyAdded() {
  document.getElementById("divRelatedNews").style.display = "none";
  document.getElementById("divRecentlyAdded").style.display = "";
  document.getElementById("divMostPopular").style.display = "none";
  document.getElementById("aRelatedNews").className = "secTab";
  document.getElementById("aRecentlyAdded").className = "secTabSel";
  document.getElementById("aMostPopular").className = "secTab";
  document.getElementById("genreCheckBox").style.display="";  
}

function showMostPopular() {
  document.getElementById("divRelatedNews").style.display = "none";
  document.getElementById("divRecentlyAdded").style.display = "none";
  document.getElementById("divMostPopular").style.display = "";
  document.getElementById("aRelatedNews").className = "secTab";
  document.getElementById("aRecentlyAdded").className = "secTab";
  document.getElementById("aMostPopular").className = "secTabSel";
  document.getElementById("genreCheckBox").style.display="";  
}


function showRelatedNewLoading() {
  document.getElementById("divRelatedNews").innerHTML = getLoadingBar();
}

function showRecentlyAddedLoading() {
  document.getElementById("divRecentlyAdded").innerHTML = getLoadingBar();
}

function showMostPopularLoading() {
  document.getElementById("divMostPopular").innerHTML = getLoadingBar();
}

function showSingleNewLoading(){
  document.getElementById("divEventDetail").innerHTML = getLoadingBar();
}

//AJAX
var httpRelatedNews;
var httpRecentlyAdded;
var httpMostPopular;
var httpSingleEvent;
var httpBookMark;

function getRelatedNews(page) {
  if (httpRelatedNews == null) {
    httpRelatedNews = GetXmlHttpObject();
  }
  
  if (httpRelatedNews != null)  {
    if (httpRelatedNews.readyState == 4 || httpRelatedNews.readyState == 0) {
      var genre = document.getElementById("hfGenre").value;
      
      var url="process.aspx?type=RELATED_NEW&page="+page+"&genre="+genre+"";
      httpRelatedNews.onreadystatechange=drawRelatedNews;
      httpRelatedNews.open("GET",url,true);
      httpRelatedNews.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      httpRelatedNews.send(null);
    }
  }
  else  {
    alert ("Your browser does not support AJAX!");
  }
}

function drawRelatedNews() {

  if (httpRelatedNews != null)  {
    if (httpRelatedNews.readyState==4 && httpRelatedNews.status == 200)  {
      var xmlDoc = httpRelatedNews.responseXML.documentElement; 
      var totalpage=-1;
      var currentpage=-1;
      var arrBookmark = new Array();
      var buff = "";
      
      for (n=0; n<xmlDoc.childNodes.length; n++) {
        
        if (xmlDoc.childNodes[n].nodeName == "new") {
          var newNode = xmlDoc.childNodes[n];
          var strNew = "";
          
          structNew = nodeFieldsToArray(newNode);
          
          if(structNew["icon"]!=""){
            buff += "<div class='divNIcon'><a class='listLink' onclick='javascript:getSingleNew(" + structNew["id"] + ")' href='#new_"+structNew["id"]+"'><img src='" + structNew["icon"] + "' alt='' style='vertical-align:bottom;border:0px' /></a></div>" ;
          }else{
            buff += "\n<div class='divNIcon' style='background-image:none'></div>\n";
          }
          buff += "<div class='divNDetail'>" ;
          buff += "<div><a class='linktitle' style='text-decoration:none' onclick='javascript:getSingleNew(" + structNew["id"] + ")' href='#new_"+structNew["id"]+"'>" + structNew["title"] + "</a></div>" ;
          buff += "</div>\n" ;
          if(arrBookmark[structNew["id"]]!=undefined&&arrBookmark[structNew["id"]]!="")
            buff += drawbookmarkBtn(structNew["id"],true,"related");
          else
            buff += drawbookmarkBtn(structNew["id"],false,"related");
          buff += "<div class='sepHor' style='clear:both'></div>";
          
        }else if(xmlDoc.childNodes[n].nodeName == "totalpage"){
           totalpage = xmlDoc.childNodes[n].childNodes[0].nodeValue;
        }else if(xmlDoc.childNodes[n].nodeName == "currentpage"){
           currentpage = xmlDoc.childNodes[n].childNodes[0].nodeValue
           httpRelatedNews = null;
        }else if(xmlDoc.childNodes[n].nodeName == "noresult"){
           buff += "<span class='noresult'>There are no news under this category.</span>";
        }else if(xmlDoc.childNodes[n].nodeName == "bookmark"){
            var node = xmlDoc.childNodes[n]; 
            for (j=0; j<node.childNodes.length; j++) {
                if (node.childNodes[j].childNodes[0] != undefined) {
                  arrBookmark[(node.childNodes[j].childNodes[0].nodeValue)] = node.childNodes[j].childNodes[0].nodeValue;
                }
            }
        }
      }
      
      buff += drawPaging(currentpage,totalpage,"getRelatedNews");
      document.getElementById("divRelatedNews").innerHTML = buff;
    }
  }
}


function getRecentlyAdded(page) {
  if (httpRecentlyAdded == null) {
    httpRecentlyAdded = GetXmlHttpObject();
  }
  
  if (httpRecentlyAdded != null)  {
    if (httpRecentlyAdded.readyState == 4 || httpRecentlyAdded.readyState == 0) {
        
      var url="process.aspx?type=RECENT_NEW&page="+page+"&genre="+getGenre();
      httpRecentlyAdded.onreadystatechange=drawRecentlyAdded;
      httpRecentlyAdded.open("GET",url,true);
      httpRecentlyAdded.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      httpRecentlyAdded.send(null);
    }
  }
  else  {
    alert ("Your browser does not support AJAX!");
  }
}

function drawRecentlyAdded() {
  
  if (httpRecentlyAdded != null)  {
    if (httpRecentlyAdded.readyState==4 && httpRecentlyAdded.status == 200)  {
      var xmlDoc = httpRecentlyAdded.responseXML.documentElement; 
      var totalpage=-1;
      var currentpage=-1;
      var arrBookmark = new Array();
      var buff = "";
      for (n=0; n<xmlDoc.childNodes.length; n++) {
        if (xmlDoc.childNodes[n].nodeName == "new") {
          
          var newNode = xmlDoc.childNodes[n];
          var strNew = "";
          
          structNew = nodeFieldsToArray(newNode);
          
          if(structNew["icon"]!=""){ 
            buff += "\n<div class='divNIcon'>\n<a class='listLink' onclick='javascript:getSingleNew(" + structNew["id"] + ")' href='#new_"+structNew["id"]+"'>\n<img src='" + structNew["icon"] + "' alt='' style='vertical-align:bottom;border:0px' />\n</a>\n</div>\n" ;
          }else{
            buff += "\n<div class='divNIcon' style='background-image:none'></div>\n";
          }
          buff += "<div class='divNDetail'>" ;
          buff += "<div><a class='linktitle' style='text-decoration:none' onclick='javascript:getSingleNew(" + structNew["id"] + ")' href='#new_"+structNew["id"]+"'>" + structNew["title"] + "</a></div>" ;
          buff += "</div>\n" ;
          if(arrBookmark[structNew["id"]]!=undefined&&arrBookmark[structNew["id"]]!="")
            buff += drawbookmarkBtn(structNew["id"],true,"recent");
          else
            buff += drawbookmarkBtn(structNew["id"],false,"recent");
          buff += "<div class='sepHor' style='clear:both'></div>";
            
            if(firstNew==0 && location.hash == ""){
                firstNew=1;
                getSingleNew(structNew["id"]);
            }
            
       }else if(xmlDoc.childNodes[n].nodeName == "totalpage"){
           totalpage = xmlDoc.childNodes[n].childNodes[0].nodeValue;
        }else if(xmlDoc.childNodes[n].nodeName == "currentpage"){
           currentpage = xmlDoc.childNodes[n].childNodes[0].nodeValue
        }else if(xmlDoc.childNodes[n].nodeName == "noresult"){
           buff += "<span class='noresult'>There are no news under this category.</span>";
        }else if(xmlDoc.childNodes[n].nodeName == "bookmark"){
            var node = xmlDoc.childNodes[n]; 
            for (j=0; j<node.childNodes.length; j++) {
                if (node.childNodes[j].childNodes[0] != undefined) {
                  arrBookmark[(node.childNodes[j].childNodes[0].nodeValue)] = node.childNodes[j].childNodes[0].nodeValue;
                }
            }
        }
      }
      
      buff += drawPaging(currentpage,totalpage,"getRecentlyAdded");
      document.getElementById("divRecentlyAdded").innerHTML = buff;
      httpRecentlyAdded = null;
    }
  }
}


function getMostPopular(page) {
  if (httpMostPopular == null) {
    httpMostPopular = GetXmlHttpObject();
  }
  
  if (httpMostPopular != null)  {
    if (httpMostPopular.readyState == 4 || httpMostPopular.readyState == 0) {
      var url="process.aspx?type=POPULAR_NEW&page="+page+"&genre="+getGenre();
      httpMostPopular.onreadystatechange=drawMostPopular;
      httpMostPopular.open("GET",url,true);
      httpMostPopular.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      httpMostPopular.send(null);
    }
  }
  else  {
    alert ("Your browser does not support AJAX!");
  }
}

function drawMostPopular() {

  if (httpMostPopular != null)  {
    if (httpMostPopular.readyState==4 && httpMostPopular.status == 200)  {
      var xmlDoc = httpMostPopular.responseXML.documentElement; 
      var totalpage=-1;
      var currentpage=-1;
      var arrBookmark = new Array();
      var buff = "";
      for (n=0; n<xmlDoc.childNodes.length; n++) {
        if (xmlDoc.childNodes[n].nodeName == "new") {
          var newNode = xmlDoc.childNodes[n];
          var strNew = "";
          
          structNew = nodeFieldsToArray(newNode);
          
          if(structNew["icon"]!=""){
            buff += "\n<a class='listLink' onclick='javascript:getSingleNew(" + structNew["id"] + ")' href='#new_"+structNew["id"]+"'><div class='divNIcon'><img src='" + structNew["icon"] + "' alt='' style='vertical-align:bottom;border:0px' /></a></div>" ;
          }else{
            buff += "\n<div class='divNIcon' style='background-image:none'></div>\n";
          }
          buff += "<div class='divNDetail'>" ;
          buff += "<div><a class='linktitle' style='text-decoration:none' onclick='javascript:getSingleNew(" + structNew["id"] + ")' href='#new_"+structNew["id"]+"'>" + structNew["title"] + "</a></div>" ;
          buff += "</div>\n" ;
          if(arrBookmark[structNew["id"]]!=undefined&&arrBookmark[structNew["id"]]!="")
            buff += drawbookmarkBtn(structNew["id"],true,"popular");
          else
            buff += drawbookmarkBtn(structNew["id"],false,"popular");
          buff += "<div class='sepHor' style='clear:both'></div>";
          buff += "<div id='divMpPaging'></div>";
        }else if(xmlDoc.childNodes[n].nodeName == "totalpage"){
           totalpage = xmlDoc.childNodes[n].childNodes[0].nodeValue;
        }else if(xmlDoc.childNodes[n].nodeName == "currentpage"){
           currentpage = xmlDoc.childNodes[n].childNodes[0].nodeValue
        }else if(xmlDoc.childNodes[n].nodeName == "noresult"){
           buff += "<span class='noresult'>There are no news under this category.</span>";
        }else if(xmlDoc.childNodes[n].nodeName == "bookmark"){
            var node = xmlDoc.childNodes[n]; 
            for (j=0; j<node.childNodes.length; j++) {
                if (node.childNodes[j].childNodes[0] != undefined) {
                  arrBookmark[(node.childNodes[j].childNodes[0].nodeValue)] = node.childNodes[j].childNodes[0].nodeValue;
                }
            }
        }
      }
      
      buff += drawPaging(currentpage,totalpage,"getMostPopular");
      document.getElementById("divMostPopular").innerHTML = buff;
      httpMostPopular = null;
    }
  }
}


function getSingleNew(eid) {
    
  if (httpSingleEvent == null) {
    httpSingleEvent = GetXmlHttpObject();
  }
  
  if (httpSingleEvent != null)  {
    if (httpSingleEvent.readyState == 4 || httpSingleEvent.readyState == 0) {
      var url="process.aspx?type=GET_NEW&new_id=" + eid;
      httpSingleEvent.onreadystatechange=drawSingleNew;
      httpSingleEvent.open("GET",url,true);
      httpSingleEvent.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      httpSingleEvent.send(null);

    }
  }
  else  {
    alert ("Your browser does not support AJAX!");
  }
}

function drawSingleNew() {

  if (httpSingleEvent != null)  {

    if (httpSingleEvent.readyState==4 && httpSingleEvent.status == 200)  {
        
      var xmlDoc = httpSingleEvent.responseXML.documentElement; 
      var buff = "";
      for (n=0; n<xmlDoc.childNodes.length; n++) {
        if (xmlDoc.childNodes[n].nodeName == "new") {
          var newNode = xmlDoc.childNodes[n];
          var strNew = "";
          
          structNew = nodeFieldsToArray(newNode);
          document.getElementById("hfGenre").value = structNew["genre"];
          
          buff += "<div class='divNTitle' style='font-size:20px;'>" + structNew["title"] + "</div>" ;
          buff += "<div class='divNDate' style='font-size:14px;'>" + structNew["date"] + "</div>" ;
          buff += "<div class='divNSubtitle' style='font-size:14px;'>" + structNew["subtitle"] + "</div>" ;
          buff += "<div>&nbsp;</div>";
          if(structNew["filename"]!=""){
						buff +=   "<table border='0' cellpadding='0' cellspacing='0' > " ;
						buff +=   "<tr>";
						buff +=   "<td class='NImage'><img src='" + structNew["filename"] + "' alt='"+structNew["title"]+"' style='vertical-align:bottom'></td>" ;
						buff +=   "<td class='rightSadow'>&nbsp;</td>" ;
						buff +=   "</tr>" ;
						buff +=   "<tr><td class='bottomShadow'>&nbsp;</td><td class='bottomRShadow'>&nbsp;</td></tr>" ;
						buff +=   "</table>" ;
						buff +=   "<div></div>" ;
						buff +=   "<div class='divNCaption' style='font-size:10px;'>" + structNew["file_desc"] + "</div>";
          }
          
          if(structNew["author"]!=""){
						buff += "<span>By</span> <span class='NAuthor'>" + structNew["author"] + "</span>" ;
          }
            
          buff += "<div>&nbsp;</div>" +
                  "<div>&nbsp;</div>" +
                  "<div class='divNDesc'>" + structNew["description"] + "</div>";
        }
      }
      
      document.getElementById("divEventDetail").innerHTML = buff;
      httpSingleEvent = null;
      getRelatedNews(1);
    }
    
    
  }
}

function drawPaging(cp,tp,func_name){
      var currentpage = parseInt(cp);
      var totalpage = parseInt(tp);
      var buff = "";
      var page_range = 3;
      var i;
      
      if(tp<=0){
        return buff;
      }
      
      /*if(currentpage==1&&totalpage!=1){
        buff += "<a href='javascript:"+func_name+"("+ (currentpage+1) +")' class='gonext'></a>";
      }
      else if(currentpage<totalpage)
      {
        buff += "<a href='javascript:"+func_name+"("+ (currentpage+1) +")' class='gonext'></a>";
        buff += "<a href='javascript:"+func_name+"("+ (currentpage-1) +")' class='goprev'></a>";
      }
      else if(currentpage==totalpage&&totalpage>1){
        buff += "<a href='javascript:"+func_name+"("+ (currentpage-1) +")' class='goprev'></a>";
      }
      buff +="<div style='clear:both'></div>";
      
      //document.getElementById("divMpPaging").innerHTML = buff;*/
      
      var curWin = Math.ceil((currentpage) / page_range); //this is the current "Window"
      var totalWin = Math.ceil(totalpage / page_range);
      var pStart = ((curWin -1) * page_range) + 1;
            
      if (curWin < totalWin) {
        buff += "<a href='javascript:"+func_name+"("+ ((curWin * page_range) + 1) +")' class='gonext'></a>"; 
      }
      
      buff += "<div  class='paging'>";
      for (p=pStart; p<(pStart+page_range)&&p<=totalpage; p++) {
        if (p == currentpage) {
          buff += "&nbsp;<b>" + p + "</b>&nbsp;";
        }
        else {
          buff += "&nbsp;<a href='javascript:"+func_name+"("+ p +")'>" + p + "</a>&nbsp;";
        }
      }
      buff += "</div>";
      
      if (currentpage > page_range) {
        buff += "<a href='javascript:"+func_name+"("+ (((curWin-2) * page_range) + 1) +")' class='goprev'></a>";
      }
      
      buff +="<div style='clear:both'></div>";
      
      return buff;
     
}


function drawbookmarkBtn(id,isAdded,prefix){
    
    if(isAdded){
        //return "<div style='bookmark'><a id='"+prefix+"bookmark_"+id+"' href='javascript:addBookmark("+id+")' class='bookmarkAddedlink'  title='Add Bookmark'></a></div>";
        return "<div style='bookmark'><a id='"+prefix+"bookmark_"+id+"' href='javascript:;' class='bookmarkAddedlink'  title='Bookmarked'></a></div>";
    }else{
        return "<div style='bookmark'><a id='"+prefix+"bookmark_"+id+"' href='javascript:addBookmark("+id+")' class='bookmarklink'  title='Add Bookmark'></a></div>";
    }
}


function addBookmark(new_id) {
    
  
    httpBookMark = GetXmlHttpObject();
  
  
  if (httpBookMark != null)  {
    if (httpBookMark.readyState == 4 || httpBookMark.readyState == 0) {
      var url="process.aspx?type=ADD_BOOKMARK_NEW&id=" + new_id;
      httpBookMark.onreadystatechange=bookmarkstatus;
      httpBookMark.open("GET",url,true);
      httpBookMark.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      httpBookMark.send(null);

    }
  }
  else  {
    alert ("Your browser does not support AJAX!");
  }
}

function bookmarkstatus() {

  if (httpBookMark != null)  {
       
    if (httpBookMark.readyState==4 && httpBookMark.status == 200)  {
       
      var xmlDoc = httpBookMark.responseXML.documentElement; 
      var buff = "";
      for (n=0; n<xmlDoc.childNodes.length; n++) {
      
        if (xmlDoc.childNodes[n].nodeName == "error") {
          var newNode = xmlDoc.childNodes[n];
          structNew = nodeFieldsToArray(newNode);
           
          if(structNew["responseUrl"]!=""){
            location.href = structNew["responseUrl"];
            break;
          }
          break;
          
        }else if(xmlDoc.childNodes[n].nodeName == "Update") {
         
          var newNode = xmlDoc.childNodes[n];
          structNew = nodeFieldsToArray(newNode);
          
          
          if(structNew["description"]=="BOOKMARKADDED"&&structNew["bookmark_id"]!=""){
            
            if(document.getElementById("bookmark_"+structNew["bookmark_id"])!=undefined)
                document.getElementById("bookmark_"+structNew["bookmark_id"]).className = "bookmarkAddedlink";
          }
          break;
        }
      }
      httpBookMark = null;
    }
    
  }
}

function uncheckAll(){
    uncheckAllGenreList(document.frmNews,'chkGenreList');
    getRecentlyAdded(1);
    getMostPopular(1);
}

function checkAll(){
    checkAllGenreList(document.frmNews,'chkGenreList');
    getRecentlyAdded(1);
    getMostPopular(1);
}

function uncheckAllGenreList(obj, prefix){
  var len = prefix.length;
  
  for (i=0; i<obj.elements.length; i++) {
    if (obj.elements[i].name.substring(0, len) == prefix) {    
      obj.elements[i].checked = false;
    }
  }     
}

function checkAllGenreList(obj, prefix){
  var len = prefix.length;
  
  for (i=0; i<obj.elements.length; i++) {
    if (obj.elements[i].name.substring(0, len) == prefix) {    
      obj.elements[i].checked = true;
    }
  }     
}
