////////////////////////////////////////////////////////////////////////////////
//                            iSee
//                    Author: Hernan Amiune
//                          Copyright
////////////////////////////////////////////////////////////////////////////////
var xmlDoc = null;  //xml document
////////////////////////////////////////////////////////////////////////////////
/**
* Initialize the settings windows
*/
function init(){
  System.Gadget.onSettingsClosing = settingsClosing;
  loadFeedsDatabase();
  var channelType = document.getElementById("channelType");
  channelType.selectedIndex = 0;
  handleOnChange(channelType);
}


function handleOnChange(channelType)
{
  var idx = channelType.selectedIndex;
  var selection = channelType[idx].value;
  if(selection != "selectOption"){
      if (selection == "predefined")
      {
        document.getElementById("selectText").innerHTML = "Seleccionar: ";
        document.getElementById("feedUrl").innerHTML = '<select id="selectedFeed" onchange="handleOnChange(this)" style="WIDTH: 200px; font-family:arial; font-size:10px; border:0">'+
        '<option value="http://youtube.com/rss/global/recently_added.rss">YouTube Recently Added</option>'+
        '<option value="http://youtube.com/rss/global/recently_featured.rss">YouTube Recently Featured</option>'+
        '<option value="http://youtube.com/rss/global/top_favorites.rss">YouTube Top Favorites</option>'+
        '<option value="http://youtube.com/rss/global/top_rated.rss">YouTube Top Rated</option>'+
        '<option value="http://youtube.com/rss/global/top_viewed_today.rss">YouTube Most Viewed Today</option>'+
        '<option value="http://youtube.com/rss/global/top_viewed_week.rss">YouTube Most Viewed This Week</option>'+
        '<option value="http://youtube.com/rss/global/top_viewed_month.rss">YouTube Most Viewed This Month</option>'+
        '<option value="http://youtube.com/rss/global/top_viewed.rss">YouTube Most Viewed All Time</option>'+
        '<option value="http://youtube.com/rss/global/most_discussed_today.rss">YouTube Most Discussed Today</option>'+
        '<option value="http://youtube.com/rss/global/most_discussed_week.rss">YouTube Most Discussed This Week</option>'+
        '<option value="http://youtube.com/rss/global/most_discussed_month.rss">YouTube Most Discussed This Month</option>'+
        '<option value="http://video.google.com/videofeed?type=top100new&num=20&output=rss">Google Popular</option>'+
        '</select>';
      }
      else if(selection == "youTubeChannel"){
        document.getElementById("selectText").innerHTML = "Canal o usuario: ";
        document.getElementById("feedUrl").innerHTML = '<input class="txt" id="txtChannel" type="text" name="txtSearch" value="" />';
      }
      else if(selection == "youTubeSearch" || selection == "googleSearch"){
        document.getElementById("selectText").innerHTML = "Buscar:";
        document.getElementById("feedUrl").innerHTML = '<input class="txt" id="txtSearch" type="text" name="txtSearch" value="" />';
      }
  }
}

////////////////////////////////////////////////////////////////////////////////
/**
* Load the feeds database
*/
function loadFeedsDatabase(){
    try{
        if(window.ActiveXObject){
            var oFS = new ActiveXObject("Scripting.FileSystemObject");
            var path = System.Gadget.path;
            path = path.replace(/\\/g,"\\\\");
            path = path + "\\databases\\feedsDB.xml";
            if(oFS.FileExists(path) == true){
              var oFSFile = oFS.OpenTextFile(path,1)
              var strXML = oFSFile.readall();
              strXML = strXML.replace(/&/g,"&amp;");

              xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.loadXML(strXML);

              var node = xmlDoc.documentElement;
              var items = node.getElementsByTagName("item");

              for(var i=0; i<items.length; i++){
                  var feedTitle = items[i].getElementsByTagName("title").item(0).firstChild.data;
                  var feedUrl = items[i].getElementsByTagName("url").item(0).firstChild.data;
                  addItem(feedTitle, feedUrl);
              }
              
            }      
        }
    }catch(e){}
}
////////////////////////////////////////////////////////////////////////////////
/**
* On closing save the feeds list
*/
function settingsClosing(event)
{
    if(event.closeAction == event.Action.commit)
    {
        saveList();
    }
    else if (event.closeAction == event.Action.cancel)
    {
    }
    event.cancel = false;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Save the feeds to a file
*/
function saveList(){

    try{
        var objListBox = document.getElementById("rssList");
        var root = xmlDoc.documentElement;
        var items = root.getElementsByTagName("item");

        //delete all old items
        for(var i=0; i<items.length; i++){
          root.removeChild(root.childNodes.item(0));
        }

        //create all new items
        for(var i=0; i<objListBox.children.length; i++){
           var newItem = xmlDoc.createNode(1, "item", "");
           var newTitle = xmlDoc.createNode(1, "title", "");
           var titleText = xmlDoc.createNode(3, "", "");
           titleText.text = objListBox.children[i].value; 
           newTitle.appendChild(titleText);
           newItem.appendChild(newTitle);
           var newUrl = xmlDoc.createNode(1, "url", "");
           var urlText = xmlDoc.createNode(3, "", "");
           urlText.text = objListBox.children[i].id; 
           newUrl.appendChild(urlText);
           newItem.appendChild(newUrl);
           root.appendChild(newItem);
        }

        //save
        var oFS = new ActiveXObject("Scripting.FileSystemObject");
        var path = System.Gadget.path;
        path = path.replace(/\\/g,"\\\\");
        path = path + "\\databases\\feedsDB.xml";
        var oFSFile = oFS.OpenTextFile(path,2,true);
        oFSFile.write(xmlDoc.xml); 
        oFSFile.close();
        //alert("Configuration saved succesfully");
    }catch(e){
        //alert("An error ocurred when saving configuration");
    }
}
////////////////////////////////////////////////////////////////////////////////
/**
* Add Button function
*/
function doAdd(){
  var channelType = document.getElementById("channelType");
  var index = channelType.selectedIndex;
  switch(index){
      case 0:
        var selectedFeed = document.getElementById("selectedFeed");
        var sfindex = selectedFeed.selectedIndex;
        var strTitle = selectedFeed[sfindex].text;
        strTitle = strTitle.replace(/YouTube /g,"");
        strTitle = strTitle.replace(/Google /g,"");
        var strUrl = selectedFeed[sfindex].value;
        break;
      case 1:
        var channel = document.getElementById("txtChannel").value;
        channel = TrimAll(channel);
        channel = channel.replace(/'/g,"");
        channel = channel.replace(/"/g,"");
        channel = channel.replace(/ /g,"_");
        var strTitle = channel;
        var strUrl = "http://www.youtube.com/rss/user/"+channel+"/videos.rss";
        break;
      case 2:
        var search = document.getElementById("txtSearch").value;
        search = TrimAll(search);
        search = search.replace(/'/g,"");
        search = search.replace(/"/g,"");
        var strTitle = search;
        search = search.replace(/ /g,"+");
        var strUrl = "http://www.youtube.com/rss/tag/"+search+".rss";
        break;
      case 3:
        var search = document.getElementById("txtSearch").value;
        search = TrimAll(search);
        search = search.replace(/'/g,"");
        search = search.replace(/"/g,"");
        var strTitle = search;
        search = search.replace(/ /g,"+");
        var strUrl = "http://video.google.com/videofeed?type=search&q="+search+"&so=1&num=20&output=rss";
        break;
  }
  addItem(strTitle, strUrl);
}
////////////////////////////////////////////////////////////////////////////////
/**
* Add the item to the list
* @param {string} strTitle title of the feed
* @param {string} strUrl url of the feed
*/
function addItem(strTitle, strUrl)
{
  var objListBox = document.getElementById("rssList");
  var newOpt;
  newOpt = document.createElement("OPTION");
  var strDisplay = strTitle;
  newOpt = new Option(strDisplay,strTitle);
  newOpt.id = strUrl;
  objListBox.add(newOpt);
}
////////////////////////////////////////////////////////////////////////////////
/**
* Remove Button function
*/
function removeItem()
{
  var objListBox = document.getElementById("rssList");
  if(objListBox.selectedIndex<0)return;
  objListBox.remove(objListBox.selectedIndex);
}

////////////////////////////////////////////////////////////////////////////////
/**
* Up item position
*/
function itemUp()
{
  var objListBox = document.getElementById("rssList");
  var i = objListBox.selectedIndex;
  if(i<1)return;
  var tmpId = objListBox.children[i].id;
  var tmpValue = objListBox.children[i].value;
  objListBox.children[i].id = objListBox.children[i-1].id;
  objListBox.children[i].value = objListBox.children[i-1].value;
  objListBox.children[i-1].id = tmpId;
  objListBox.children[i-1].value = tmpValue;
  objListBox.children[i].label = objListBox.children[i].value;
  objListBox.children[i-1].label = objListBox.children[i-1].value;
  objListBox.selectedIndex = i-1;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Down item position
*/
function itemDown()
{
  var objListBox = document.getElementById("rssList");
  var i = objListBox.selectedIndex;
  if(i>objListBox.children.length-2)return;
  var tmpId = objListBox.children[i].id;
  var tmpValue = objListBox.children[i].value;
  objListBox.children[i].id = objListBox.children[i+1].id;
  objListBox.children[i].value = objListBox.children[i+1].value;
  objListBox.children[i+1].id = tmpId;
  objListBox.children[i+1].value = tmpValue;
  objListBox.children[i].label = objListBox.children[i].value;
  objListBox.children[i+1].label = objListBox.children[i+1].value;
  objListBox.selectedIndex = i+1;
}
////////////////////////////////////////////////////////////////////////////////
window.onload = init;
////////////////////////////////////////////////////////////////////////////////