﻿var labelType, useGradients, nativeTextSupport, animate;

(function() {
  var ua = navigator.userAgent,
      iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
      typeOfCanvas = typeof HTMLCanvasElement,
      nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
      textSupport = nativeCanvasSupport 
        && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
  //I'm setting this based on the fact that ExCanvas provides text support for IE
  //and that as of today iPhone/iPad current text support is lame
  labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
  nativeTextSupport = labelType == 'Native';
  useGradients = nativeCanvasSupport;
  animate = !(iStuff || !nativeCanvasSupport);
})();

var Log = {
  elem: false,
  write: function(text){
    if (!this.elem) 
      this.elem = document.getElementById('log');
    this.elem.innerHTML = text;
    this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
  }
};


function init(){
  //init data
  // tumeroheline: #FFFF66, heleroheline: #CCCC99 
  var json = {
    "children": [
     {
       "children": [
         { "children": [], "data": { "EUR": "56", "$color": "#CCCC99", "$area": 56 }, 
           "id": "node1.1", "name": "Elukestev õpe" }, 
         { "children": [], "data": { "EUR": "103", "$color": "#CCCC99", "$area": 103 }, 
           "id": "node1.2", "name": "Teadus- ja arendustegevuse inimressursi arendamine" },
         { "children": [], "data": { "EUR": "128", "$color": "#CCCC99", "$area": 128 }, 
           "id": "node1.3", "name": "Pikk ja kvaliteetne tööelu" },
         { "children": [], "data": { "EUR": "75", "$color": "#CCCC99", "$area": 75 }, 
           "id": "node1.4", "name": "Teadmised ja oskused uuendusmeelseks ettevõtluseks" },
         { "children": [], "data": { "EUR": "22", "$color": "#CCCC99", "$area": 22 }, 
           "id": "node1.5", "name": "Suurem haldusvõimekus" }		   
       ], 
       "data": { "EUR": 400, "$color": "#999966", "$area": 400 }, 
       "id": "node1", 
       "name": "Inimressursi arendamise rakenduskava"
     },

     {
       "children": [
         { "children": [], "data": { "EUR": "626", "$color": "#CCCC99", "$area": 626 }, 
           "id": "node2.1", "name": "Veemajanduse ja jäätmekäitluse infrastruktuuri arendamine" }, 
         { "children": [], "data": { "EUR": "92", "$color": "#CCCC99", "$area": 92 }, 
           "id": "node2.2", "name": "Säästva keskkonnakasutuse infrastruktuuride ja tugisüsteemide arendamine" },
         { "children": [], "data": { "EUR": "87", "$color": "#CCCC99", "$area": 87 }, 
           "id": "node2.3", "name": "Energiamajanduse arendamine" },
         { "children": [
		 
			 { "children": [], "data": { "EUR": "144", "$color": "#CCCC99", "$area": 144 }, 
			   "id": "2.4.1", "name": "Kohalike avalike teenuste arendamine" }, 		 
			 { "children": [], "data": { "EUR": "56", "$color": "#CCCC99", "$area": 56 }, 
			   "id": "2.4.2", "name": "Linnaliste piirkondade arendamine" }, 		 
			 { "children": [
			 
				 { "children": [], "data": { "EUR": "87", "$color": "#CCCC99", "$area": 87 }, 
				   "id": "2.4.3.1", "name": "Piirkondade konkurentsivõime tugevdamine" }, 		 
				 { "children": [], "data": { "EUR": "62", "$color": "#CCCC99", "$area": 62 }, 
				   "id": "2.4.3.2", "name": "Üleriigilise tähtsusega kultuuri- ja turismiobjektide väljaarendamine" }, 		 
				 { "children": [], "data": { "EUR": "38", "$color": "#CCCC99", "$area": 38 }, 
				   "id": "2.4.3.3", "name": "Kompetentsikeskuste arendamine" }		 
				 
			 ], "data": { "EUR": "187", "$color": "#999966", "$area": 187 }, 
			   "id": "2.4.3", "name": "Piirkondade konkurentsivõime tugevdamine" }		 
		 
		 
		 ], "data": { "EUR": "389", "$color": "#999966", "$area": 389 }, 
           "id": "node2.4", "name": "Piirkondade terviklik ja tasakaalustatud areng" },
         { "children": [], "data": { "EUR": "213", "$color": "#CCCC99", "$area": 213 }, 
           "id": "node2.5", "name": " Hariduse infrastruktuuri arendamine" },
         { "children": [], "data": { "EUR": "169", "$color": "#CCCC99", "$area": 169 }, 
           "id": "node2.6", "name": "Tervishoiu ja hoolekande infrastruktuuri arendamine" }		   
       ], 
       "data": { "EUR": 1607, "$color": "#999966", "$area": 1607 }, 
       "id": "node2", "name": "Elukeskkonna arendamise rakenduskava"
     },

     {
       "children": [
         { "children": [
		 
			 { "children": [], "data": { "EUR": "3", "$color": "#CCCC99", "$area": 3 }, 
			   "id": "3.1.1", "name": "Ettevõtluslaenude riiklike tagatiste ja kapitalilaenu programm" }, 		 
			 { "children": [], "data": { "EUR": "28", "$color": "#CCCC99", "$area": 28 }, 
			   "id": "3.1.2", "name": "Ettevõtlusinkubatsiooni toetusprogramm" }, 
			 { "children": [], "data": { "EUR": "144", "$color": "#CCCC99", "$area": 144 }, 
			   "id": "3.1.3", "name": "Eesti kui reisisihi tuntuse suurendamise programm" }, 		   
			 { "children": [], "data": { "EUR": "3", "$color": "#CCCC99", "$area": 3 }, 
			   "id": "3.1.4", "name": "Turismiinfosüsteemi arendamise programm" }, 
			 { "children": [], "data": { "EUR": "2", "$color": "#CCCC99", "$area": 2 }, 
			   "id": "3.1.5", "name": "Riiklike turundusürituste programm" }, 
			 { "children": [], "data": { "EUR": "13", "$color": "#CCCC99", "$area": 13 }, 
			   "id": "3.1.6", "name": "Riikliku ekspordi krediidikindlustuse programm" }, 
			 { "children": [], "data": { "EUR": "128", "$color": "#CCCC99", "$area": 128 }, 
			   "id": "3.1.7", "name": "Ettevõtjate laenukapitali kättesaadavuse parandamise täiendav tugiprogramm" }		   
		 
		 ], "data": { "EUR": "375", "$color": "#999966", "$area": 375 }, 
           "id": "node3.1", "name": "Ettevõtluse uuendus- ja kasvuvõime" }, 
         { "children": [], "data": { "EUR": "310", "$color": "#CCCC99", "$area": 310 }, 
           "id": "node3.2", "name": "Eesti teadus- ja arendustegevuse tugevdamine teadusprogrammide ja kõrgkoolide ning teadusasutuste kaasajastamise kaudu" },
         { "children": [], "data": { "EUR": "525", "$color": "#CCCC99", "$area": 525 }, 
           "id": "node3.3", "name": "Strateegilise tähtsusega transpordiinvesteeringud" },
         { "children": [], "data": { "EUR": "101", "$color": "#CCCC99", "$area": 101 }, 
           "id": "node3.4", "name": "Regionaalse tähtsusega transpordi infrastruktuuri arendamine" },
         { "children": [
		 
			 { "children": [], "data": { "EUR": "40", "$color": "#CCFFFF", "$area": 40 }, 
			   "id": "3.5.1", "name": "Avatud taotlemine" }, 		 
			 { "children": [], "data": { "EUR": "19", "$color": "#CCFFFF", "$area": 19 }, 
			   "id": "3.5.2", "name": "Investeeringute kava" }, 
			 { "children": [], "data": { "EUR": "3", "$color": "#CCFFFF", "$area": 3 }, 
			   "id": "3.5.3", "name": "Infoühiskonna teadlikkuse tõstmise programm" }		 
		 
		 ], "data": { "EUR": "62", "$color": "#CCFFCC", "$area": 62 }, 
           "id": "node3.5", "name": "Infoühiskonna edendamine" }		   
       ], 
       "data": { "EUR": 1405, "$color": "#999966", "$area": 1405 }, 
       "id": "node3", 
       "name": "Majanduskeskkonna arendamise rakenduskava"
     }	 
	 
   ], 
   "data": { "EUR": 3398, "$color": "#CCCC99" }, 
   "id": "node0", 
   "name": "EUROOPA LIIDU STRUKTUURITOETUSED 2007-2013"
   };
  //end
  //init TreeMap
  var tm = new $jit.TM.Squarified({
    //where to inject the visualization
    injectInto: 'infovis',
    //parent box title heights
    titleHeight: 30,
    //enable animations
    animate: animate,
    //box offsets
    offset: 1,
    //Attach left and right click events
    Events: {
      enable: true,
      onClick: function(node) {
        if(node) tm.enter(node);
      },
      onRightClick: function() {
        tm.out();
      }
    },
    duration: 1000,
    //Enable tips
    Tips: {
      enable: true,
      //add positioning offsets
      offsetX: 20, offsetY: 20,
      //implement the onShow method to
      //add content to the tooltip when a node
      //is hovered
      onShow: function(tip, node, isLeaf, domElement) {
        var html = "<div class=\"tip-title\">" + node.name 
          + "</div><div class=\"tip-text\">";
        var data = node.data;
        if(data.EUR) {
          html += "EUR: " + data.EUR + " mln";
        }
        tip.innerHTML =  html; 
      }  
    },
    //Add the name of the node in the correponding label
    //This method is called once, on label creation.
    onCreateLabel: function(domElement, node){
        domElement.innerHTML = node.name;
        var style = domElement.style;
        style.display = '';
        style.border = '1px solid transparent';
        domElement.onmouseover = function() {
          style.border = '6px solid white';
        };
        domElement.onmouseout = function() {
          style.border = '1px solid transparent';
  
        };
    }
  });
  tm.loadJSON(json);
  tm.refresh();
  //end
  //add events to radio buttons
  var sq = $jit.id('r-sq'),
      st = $jit.id('r-st'),
      sd = $jit.id('r-sd');
  var util = $jit.util;
  util.addEvent(sq, 'change', function() {
    if(!sq.checked) return;
    util.extend(tm, new $jit.Layouts.TM.Squarified);
    tm.refresh();
  });
  util.addEvent(st, 'change', function() {
    if(!st.checked) return;
    util.extend(tm, new $jit.Layouts.TM.Strip);
    tm.layout.orientation = "v";
    tm.refresh();
  });
  util.addEvent(sd, 'change', function() {
    if(!sd.checked) return;
    util.extend(tm, new $jit.Layouts.TM.SliceAndDice);
    tm.layout.orientation = "v";
    tm.refresh();
  });
  //add event to the back button
  var back = $jit.id('back');
  $jit.util.addEvent(back, 'click', function() {
    tm.out();
  });
}

