// ************************************************************************************************************************
// LOGO GADGET:
// ************************************************************************************************************************  
// DEFINITION:
function LogoGadget(data){
  window.SVGLogo = this;
  this.LOGOSRC = data.LOGOSRC;
  this.SERVICENAME = data.SERVICENAME;
  this.VERSION = data.VERSION; 
  this.SLOGAN = data.SLOGAN;
  
  // USER INTERFACE:
  this.paper = Raphael("pnl_logo", 275, 70);
  this.iLogo = this.paper.image(this.LOGOSRC, 10, 3, 40, 55).attr({ "scale": "1.2" }); 
  this.iLogo.node.oriSrc = this.LOGOSRC; 
  this.iLogo.node.setAttribute("tooltip", this.SERVICENAME);
  this.iLogoAlt = {};
  this.iLogoAlt["ktu"] = this.paper.image("./img/ktu_logo.png", 10, 34, 30, 42).attr({ "scale": "0.01" }); 
  this.iLogoAlt["ktu"].node.setAttribute("tooltip", "KTU");
  this.iLogoAlt["kaunas"] = this.paper.image("./img/kaunas_logo.png", 22, 26, 30, 42).attr({ "scale": "0.01" }); 
  this.iLogoAlt["kaunas"].node.setAttribute("tooltip", "KAUNAS");
  this.iLogoAlt["verslas"] = this.paper.image("./img/patogiau_logo.png", 32, 20, 30, 42).attr({ "scale": "0.01" }); 
  this.iLogoAlt["verslas"].node.setAttribute("tooltip", "VERSLAS");
  this.tTitle = this.paper.text(54, 27, "PATOGIAU.LT").attr({ "font": "bold 29px 'Arial'", "fill": "#000", "opacity": "0", "text-anchor": "start" });
  this.tVersion = this.paper.text(248, 10, this.VERSION).attr({ "font": "bold 11px 'Arial'", "fill": "#888", "opacity": "0", "rotation": "30", "text-anchor": "start" });
  this.tSlogan = this.paper.text(69, 46, this.SLOGAN).attr({ "font": "bold 11px 'Arial'", "fill": "#000", "opacity": "0", "text-anchor": "start" });
  this.tService = this.paper.text(-6, 34, "").attr({ "font": "bold 7px 'Verdana'", "fill": "#333", "text-anchor": "start", "opacity": "0"});

  // FUNCTIONALITY:
  this.service = {
    show: function(){
      clearTimeout(SVGLogo.iLogo.node.tHide);
      for (var c in SVGLogo.iLogoAlt) SVGLogo.iLogoAlt[c].animate({"scale": "0.5"}, 300);
      SVGLogo.iLogo.attr({ "src": SVGLogo.iLogo.node.oriSrc });
      SVGLogo.tService.attr({"text": SVGLogo.service.textToVertical(SVGLogo.iLogo.node.getAttribute("tooltip"))});;      
    },
    hide: function(){
      if(SVGLogo.iLogo.node.isHiding == false || SVGLogo.iLogo.node.isHiding == undefined){
        for (var c in SVGLogo.iLogoAlt) SVGLogo.iLogoAlt[c].animate({"scale": "0.01"}, 1000);
        SVGLogo.iLogo.attr({ "scale": "1.1" });
        SVGLogo.iLogo.animate({ "scale": "1.0" }, 1000);
        SVGLogo.iLogo.node.isHiding = true;
        setTimeout("SVGLogo.iLogo.node.isHiding = false", 1000);
      } 
    },    
    focus: function(d){
      clearTimeout(SVGLogo.iLogo.node.tHide);   
      if(SVGLogo.iLogo.node.isHiding == false || SVGLogo.iLogo.node.isHiding == undefined){
        SVGLogo.iLogo.attr({ "src": (d.src || d.getAttribute("href")) });
        SVGLogo.tService.attr({"text": SVGLogo.service.textToVertical(d.getAttribute("tooltip"))});
      }     
    },
    blur: function(d){
      SVGLogo.iLogo.attr({ "src": SVGLogo.iLogo.node.oriSrc });
      SVGLogo.tService.attr({"text": SVGLogo.service.textToVertical(SVGLogo.iLogo.node.getAttribute("tooltip"))});
    },    
    select: function(d){
      if(d.toLowerCase() == SVGLogo.SERVICENAME.toLowerCase()) return false;
      window.location = "http://" + d + ".patogiau.lt";
    },
    textToVertical: function(text){
      var tooltip = ""; for(var i=0; i<text.length; i++) tooltip += text.substr(i, 1) + "\n"; return tooltip;
    }
  }  
  // INITIALIZATION:
  this.tService.attr({"text": this.service.textToVertical(this.iLogo.node.getAttribute("tooltip"))});  
  this.iLogo.animate({ "scale": "1" }, 1500);
  this.tTitle.animate({ "opacity": "1" }, 3500);
  this.tSlogan.animate({ "opacity": "1" }, 3500);
  setTimeout("SVGLogo.tService.animate({ 'opacity': '1', 'x': '2' }, 500)", 1200);  
  setTimeout("SVGLogo.tVersion.animate({ 'opacity': '1', 'rotation': '720, 260, 10' }, 750)", 1500);  

  // LOGIC:
  this.iLogo.node.onmouseover = function() {
    SVGLogo.iLogo.animate({ "scale": "1.1" }, 300); 
    SVGLogo.iLogo.node.tShow = setTimeout(SVGLogo.service.show, 1);
  };
  this.iLogo.node.onmouseout = function() {
    clearTimeout(SVGLogo.iLogo.node.tShow);
    SVGLogo.iLogo.node.tHide = setTimeout(SVGLogo.service.hide, 500);
  };      
  for (var c in this.iLogoAlt) {
    if(c.toLowerCase() != this.SERVICENAME.toLowerCase()) SVGLogo.iLogoAlt[c].node.style.cursor = "pointer";
    SVGLogo.iLogoAlt[c].node.onmousedown = new Function("SVGLogo.service.select('" + c + "')");
    SVGLogo.iLogoAlt[c].node.onmouseover = function(){ SVGLogo.service.focus(this) };
    SVGLogo.iLogoAlt[c].node.onmouseout  = new Function("SVGLogo.iLogo.node.tHide = setTimeout(\"SVGLogo.service.blur(this); SVGLogo.service.hide(this)\", 500)");
  } 
}
// ************************************************************************************************************************
// SERVICE GADGET:
// ************************************************************************************************************************  
// DEFINITION:
function CategoryGadget(data){
  this.CATEGORIES = data.CATEGORIES;
  window.SVGCSelector = this;
  // DATA MODEL:
  this.category = {  
    focus: function(c, item){
      SVGCSelector.iCategoryIcon[item].animate({ 'y': '6',  'scale': '1.00' }, 200);
      SVGCSelector.tCategoryName[item].attr({ 'fill': '#000' });
    },
    blur: function(c, item){
      if(SVGCSelector.iCategoryIcon[item].node.selected != true){
        setTimeout("SVGCSelector.iCategoryIcon[" + item + "].animate({ 'y': '10',  'scale': '0.85' }, 300)", 100);
        setTimeout("SVGCSelector.tCategoryName[" + item + "].animate({ 'fill': '#aaa' }, 300)", 100); 
      }
    },    
    select: function(c, item){
      var elem = SVGCSelector.iCategoryIcon[item].node; 
      //if(elem.selected == true) return false;
      elem.selected = true;
      for(var i = 0; i < SVGCSelector.iCategoryIcon.length; i++){
        if(i != item){
          SVGCSelector.iCategoryIcon[i].node.selected = false;
          SVGCSelector.category.blur(SVGCSelector.tCategoryName.node, i);
        }
      }
      var cid = (typeof elem.getAttribute("cid") != "undefined" ? elem.getAttribute("cid") : null);
      var gid = (typeof elem.getAttribute("gid") != "undefined" ? elem.getAttribute("gid") : null);
      document.getElementById('cmb_sCategory').value = cid;      
      //document.getElementById('chb_sDoZoomToFit').checked = true;
      if(gid != null){
        dlg_searchMarker_onclick_ok({id: gid, ids: SVGCSelector.iCategoryIcon[item].node.getAttribute("text")});    
      }
      else{
        if(cid != null){
          dlg_searchMarker_onclick_ok();
        }
      }
    }
  } 

  // USER INTERFACE:
  this.paper = Raphael("pnl_service", 745, 70);
  this.iCategoryIcon = [];
  this.tCategoryName = [];
  for(var i=0; i<this.CATEGORIES.length; i++){
    var image = this.paper.image(this.CATEGORIES[i].img, 745 - 16 - 46 - (46 + 28) * (this.CATEGORIES.length - i - 1), 10, 46, 46).attr({ "y": "6" });
    image.node.style.cursor = "pointer";
    image.node.setAttribute("text", this.CATEGORIES[i].name);
    if(typeof this.CATEGORIES[i].cid  != "undefined") image.node.setAttribute("cid", this.CATEGORIES[i].cid);
    if(typeof this.CATEGORIES[i].gid  != "undefined") image.node.setAttribute("gid", this.CATEGORIES[i].gid);    
    this.iCategoryIcon[i] = image; 
    var text = this.paper.text(745 - 16 - 46 - (46 + 28) * (this.CATEGORIES.length - i - 1) + 23, 56, this.CATEGORIES[i].name).attr({ "font": "bold 10px 'Arial'", "fill": "#aaa", "opacity": "0" });
    this.tCategoryName[i] = text;
    // INITIALIZATION (ANIMATION - SCALE):
    this.iCategoryIcon[i].attr({ "scale": "0.01"}); 
    setTimeout("SVGCSelector.iCategoryIcon[" + i + "].animate({ 'scale': '0.85' }, 1000)", 2250 + 200 * i);     
    setTimeout("SVGCSelector.tCategoryName[" + i + "].animate({ 'opacity': '1' }, 1000)", 2750 + 200 * i);
    // LOGIC:
    this.iCategoryIcon[i].node.onmousedown = new Function("SVGCSelector.category.select(this, " + i + ")");
    this.iCategoryIcon[i].node.onmouseover = new Function("SVGCSelector.category.focus(this, " + i + ")");
    this.iCategoryIcon[i].node.onmouseout  = new Function("SVGCSelector.category.blur(this, " + i + ")");
  }
}
