/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

var dir;
var loginUser = null;
var searchRequest;
var isMozilla;
var favourites;
var startWidgetPath = null;

function setup() {
  isMozilla = myBrowser.isMozilla;
  if (window.netscape) {
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  }
  sendServerTest();
  var search = location.search;
  var index = search.indexOf("path=");
  if (index >= 0) {
    startWidgetPath = decodeURIComponent(search.substring(index + 5));
  }
  var path = location.protocol + "//" + location.host + location.pathname;
  lastSlash = path.lastIndexOf("/");
  if (lastSlash >= 0) {
    path = path.substring(0, lastSlash) + "/widgets/";
  }
  widgetsPath = path;
  if (location.protocol == "file:") {
    dir = new FirefoxLocalDir(path);
  }
  else {
    dir = new ServerDir("commands", document.getElementById("GalleryLoading"));
  }
  dir.getCategoryList(fillCategoryList);
  dir.getWidgetsCount(fillWidgetsCount);
  document.getElementById("SearchButton").onclick = search;
  document.getElementById("SearchText").onchange = search;
  document.getElementById("WidgetAdd").onclick = addToFavourites;
  document.getElementById("WidgetTest").onclick = testOnDesktop;
  if (myBrowser.supported) {
    document.getElementById("WidgetAddSection").style.display = "block";    
  }
  favourites = new CookieFavourites(dir, null);
  checkDeveloper();
  document.getElementById("Gallery").style.display = "block";
  document.getElementById("StartupLoading").style.display = "none";  
}

function checkDeveloper() {
  params = new Object();
  doRequest(params, "checkDeveloper", userChecked, null);
}

function userChecked(request) {
  if (request.status == 200) {
    var status = eval(request.responseText);
    if (status == "success" && user != null) {
      loginUser = user;
      favourites = new ServerFavourites(dir, null);
    }
  }  
}

function addOptionElement(selector, value, text) {
  var option = document.createElement("option");
  option.setAttribute("value", value);
  option.appendChild(document.createTextNode(text));
  selector.appendChild(option);
}

function fillCategoryList(list) {
  var selector = document.getElementById("Categories");
  selector.onchange = categorySelected;
  clear(selector);
  if (list == null || list.length == 0) {
    if (selector != null) {
      selector.setAttribute("disabled", "true");
    }  
    if (location.protocol == "http:") {
      dir = new ServerDir("myWidgets.txt", document.getElementById("GalleryLoading"));
    }  
    try {
      dir.getPathList(fillWidgetsList, null);
    }
    catch (e) {
      fillWidgetsList(null);
    }  
  }
  else {  
    var group = document.createElement("optgroup");
    group.setAttribute("label", "Special");
    selector.appendChild(group);
    addOptionElement(group, "LastAdded", "Last Added"); 
    addOptionElement(group, "LastUpdated", "Last Updated"); 
    addOptionElement(group, "Featured", "Featured Widgets"); 
    addOptionElement(group, "Top50", "Top 50"); 
    if (list.length > 0) {
      group = document.createElement("optgroup");
      group.setAttribute("label", "Category");
      selector.appendChild(group);
      for (var i = 0; i < list.length; i++) {      
        addOptionElement(group, list[i].category, list[i].category + " (" + list[i].count + ")");
      }
    }  
    selector.selectedIndex = 0;
    categorySelected();      
  }
}

function fillWidgetsCount(count) {
  document.getElementById("WidgetCount").innerHTML = count;
}

function categorySelected() {
  var selector = document.getElementById("Categories");
  if (selector.value.length > 0) {
    dir.getPathList(fillWidgetsList, selector.value);  
  }  
}

function fillWidgetsList(dirs) {
  var selector = document.getElementById("WidgetsList");
  selector.onchange = widgetSelected;
  clear(selector);
  for (var i = 0; i < dirs.length; i++) {
    var element = document.createElement("option");
    if (window.netscape && isMozilla) {
      var icon = document.createElement("img");
      icon.className = "widgetEntryIcon";
      icon.src = widgetsPath + dirs[i].path + "/Icon.png";
      icon.width = 32;
      icon.height = 32;
      element.appendChild(icon);
      var title = document.createElement("div");
      title.className = "widgetEntryTitle";
      title.appendChild(document.createTextNode(dirs[i].title));
      if (dirs[i].featured) {
        title.appendChild(document.createTextNode(" "));
        var featured = document.createElement("img");
        featured.src = "images/splashStarFeaturedTiny.png";
        title.appendChild(featured);
      }
      element.appendChild(title);
      if (dirs[i].summary != null) {
        var summary = document.createElement("div");
        summary.className = "widgetEntrySummary";
      
        summary.appendChild(document.createTextNode(dirs[i].summary));
        element.appendChild(summary);
      }  
    }
    else {
      var title = dirs[i].title;
      if (dirs[i].featured) {
        title = "*" + title;
      }
      element.appendChild(document.createTextNode(title));
      if (dirs[i].summary != null) {
        element.title = dirs[i].summary;
      }
    }
    if (i % 2 == 0) {
      element.className = "widgetEntry evenEntry";
    }
    else {
      element.className = "widgetEntry oddEntry";      
    }
    element.value = dirs[i].path;
    selector.appendChild(element);
  }
  if (startWidgetPath != null) {
    dir.getWidgetInfo(setWidgetInfo, startWidgetPath);  
    startWidgetPath = null;    
  }
  else {
    if (dirs.length > 0) {
      selector.selectedIndex = 0;
      widgetSelected(dirs[0].path);
    }
    else {
      clearWidgetInfo();
    }
  }
}

function widgetSelected(path) {
  clearWidgetInfo();  
  var selector = document.getElementById("WidgetsList");
  dir.getWidgetInfo(setWidgetInfo, selector.value);  
}

function setWidgetInfo(info) {
  var widgetInfo = document.getElementById("WidgetsSection");
  widgetInfo.style.visibility = "visible";  
  if (widgetInfo) {
    var title = document.getElementById("WidgetTitle");
    title.innerHTML = info.title;
    var description = document.getElementById("WidgetDescription");
    if (info.description) {
      description.innerHTML = info.description;
    }
    else {
      description.innerHTML = "No description";
    }
    var whatsnew = document.getElementById("WidgetWhatsNew");
    if (info.whatsnew) {
      whatsnew.innerHTML = info.whatsnew;
    }
    else {
      whatsnew.innerHTML = "no changes";
    }
    var version = document.getElementById("WidgetVersion");
    version.innerHTML = info.version;
    var img = document.getElementById("WidgetImage");
    var image = new Image();
    image.path = widgetsPath + info.path;
    image.src = image.path + "/_hectorScreenshot.png";
    image.onerror = function() {
      widgetImageError(image);
    }      
    image.onload = function() {
      widgetImageLoaded(image);
    }
    img.src = image.src;      
    var date = document.getElementById("WidgetUpdated");
    if (info.updated) {
      var time = new Date();
      time.setTime(parseInt(info.updated));
      date.innerHTML = time.toLocaleString();
    }
    else {
      date.innerHTML = "unknown";
    }  
    var developer = document.getElementById("WidgetDeveloper");
    if (info.companyName) {
      developer.innerHTML = info.companyName;
      developer.href = info.companyUrl;
    }
    else {
      developer.innerHTML = "unknown";
      developer.setAttribute("disabled", "true");
    }
    document.getElementById("WidgetDesktopLink").innerHTML = "http://www.widgetop.com/desktop.html?add=" + info.path;
    document.getElementById("WidgetGalleryLink").innerHTML = "http://www.widgetop.com/gallery.html?path=" + info.path;
    if (info.featured) {
      document.getElementById("WidgetFeatured").style.display = "block";
    }
    else {
      document.getElementById("WidgetFeatured").style.display = "none";      
    }  
    widgetInfo.style.display = "block";
  }  
}

function clearWidgetInfo() {
  var widgetInfo = document.getElementById("WidgetsSection");
  widgetInfo.style.visibility = "hidden";  
}

function widgetImageLoaded(img) {
  var image = document.getElementById("WidgetImage");
  if (img.width > 0) {
    image.src =img.src;
  }
  else {
    widgetImageError(img);    
  }   
}

function widgetImageError(img) {
  if (img.src.indexOf("/Default.png") < 0) {
    img.src = img.path + "/Default.png";
  }  
}

function showSearch() {
  document.getElementById("CategoriesSection").style.display = "none";
  document.getElementById("SearchSection").style.display = "inline";
}

function showCategories() {
  document.getElementById("CategoriesSection").style.display = "inline";
  document.getElementById("SearchSection").style.display = "none";
  categorySelected();
}

function search() {
  var text = document.getElementById("SearchText").value;
  if (text != null && text.length > 2) {
    params = new Object();
    params.search = text;
    if (searchRequest != null) {
      searchRequest.abort();
    }
    searchRequest = doRequest(params, "search", searchDone, "SearchLoading");
  }
}

function searchDone(request) {
  if (request.status == 200) {
    var dirs = eval(request.responseText);
    fillWidgetsList(dirs);
  }
  else {
    alert("Search failed. Please try again.");
  }
  searchRequest = null;    
}

function addToFavourites() {
  var selector = document.getElementById("WidgetsList");
  favourites.add(selector.value, selector.value);  
}

function testOnDesktop() {
  var selector = document.getElementById("WidgetsList");
  window.open("desktop.html?add=" + selector.value, "_self");
}

function addError() {
  
}

    
