// Version 1.0.0
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
// Alternatively, you may redistribute this library, use and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
// You may obtain a copy of the LGPL at http://www.gnu.org/copyleft/.
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
// specific language governing rights and limitations under the License.
//
// The original code is kml.js, released October 1, 2006.
//
// The initial developer of the original code is Luca Rocchi (Rome, Italy, www.googleearthairlines.com,www.musicdyne.com),
// (luca.rocchi@tin.it, lr1313@gmail.com).



var request = false;
var request2 = false;
var verbose = false;

var fullscreen=false;
var timerID=0;
var defLineHeight=18;
var defColWidth=19
var controls=new Array();
var panels=new Array();
var curPage='plug_x';
var sidebarWidth=200;
//-----------------------------------------------------------------
function fullscreenclick(){
   try{
       fullscreen=!fullscreen;
       doResize();
   }catch(e){
      alert(e.description);
   }
}
function getLeftArea(page) {
    var url = "http://www.googleearthairlines.com/"+page+'.html';
    try {
      request2 = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try {
        request2 = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try {
          request2 = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          request2 = false;
        }
      }
    }
   request2.open("GET", url, true);
   request2.onreadystatechange = updateLeftArea;
   request2.send(null);
}

function updateLeftArea() {
   if (request2.readyState == 4) {
      if (request2.status == 200) {
         var response = request2.responseText;
         var contentarea=document.getElementById('leftarea');
		   contentarea.innerHTML=response;
      }
   }
}
//-----------------------------------------------------------------------------
function getContent(page) {
    curPage=page;
    var url = "http://www.googleearthairlines.com/"+page+'.html';
    try {
      request = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try {
          request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          request = false;
        }
      }
    }
   request.open("GET", url, true);
   request.onreadystatechange = updateContent;
   request.send(null);
}

function updateContent() {
   //alert(request);
   if (request.readyState == 4) {
      if (request.status == 200) {
         var response = request.responseText;
         var contentarea=document.getElementById('content');
         contentarea.innerHTML=response;
         var userfunc=curPage+'OnLoad';
         resize();
         try{
            if (userfunc!=undefined){
               eval(userfunc+'()');
            }
            var ar =contentarea.getElementsByTagName("script")
            for (var i=0;i<ar.length();i++){
               var sc=ar[i]
               eval(sc.innerHTML)
            }
            ar =contentarea.getElementsByTagName("object")
            //alert(ar);
            for (var i=0;i<ar.length();i++){
               var sc=ar[i]
               eval(sc.innerHTML)
            }
         }catch(e){
           //_alert(e);
         }
      }
   }
}

function doUnload() {
   try{
   }catch(e){
   }
}

function doLoad() {
   doResize();
   getContent("welcome");
}




function doResize() {
   resize();
   resize();//scrollbar...
}

function resize() {
    //alert(document.documentElement.offsetHeight);
    try{
        var hBarHeight=36;
        var hAdvHeight=90;
        var w=document.body.clientWidth;
        var h=document.body.clientHeight;
        var co=document.getElementById('content');
        //var gd=document.getElementById('geapDiv');
        //var ge=document.getElementById('geapContainer');
        var ad=document.getElementById('adv');
        var sb=document.getElementById('sysbutton');
        var sbi=document.getElementById('sysbtn');
        var la=document.getElementById('leftarea');
        var ra=document.getElementById('rightarea');
        var ba=document.getElementById('bottomarea');
        ad.style.top=(parseInt(hBarHeight))+'px';
        co.style.top=(parseInt(hBarHeight)+parseInt(hAdvHeight)+parseInt(1))+'px';
        la.style.top=hBarHeight-25+'px';
        ra.style.top=hBarHeight-25+'px';
        try{
           //alert(sb);
           sb.style.left=(w-23)+'px';
        }catch(e){
        }
        //ra.style.top='0px';
        //co.style.top=hBarHeight+'px';
        var hh=h-(hBarHeight*2)-3-hAdvHeight;
        //co.style.height=(hh)+'px';
        co.style.height=(hh-1)+'px';
        ad.style.height=(hAdvHeight)+'px';

        var ww=w-2;
        if (!fullscreen){
            ww=w-(sidebarWidth*2)-2;
            la.style.width=(sidebarWidth)+'px';
            ra.style.width=(sidebarWidth)+'px';
            co.style.left=(sidebarWidth)+'px';
            ad.style.left=(sidebarWidth)+'px';
            co.style.height=(hh)+'px';
            la.style.height=(h-(hBarHeight)-1+parseInt(25))+'px';
            ra.style.height=(h-(hBarHeight)-1+parseInt(25))+'px';
            ba.style.height=(hBarHeight)+'px';
            ba.style.left=(sidebarWidth)+'px';
            ba.style.width=(w-sidebarWidth)+'px';
            sbi.src="../images/max.png";
        }else{
            sbi.src="../images/res.png";
            la.style.width=(0)+'px';
            ra.style.width=(0)+'px';
            co.style.left='0px';
            ad.style.left='0px';
            ad.style.height='0px';
            co.style.top=(parseInt(hBarHeight)+parseInt(1))+'px';
            co.style.height=(h-(hBarHeight)-3)+'px';
            la.style.height=(0)+'px';
            ra.style.height=(0)+'px';
            ba.style.height=(0)+'px';
            ba.style.width=(0)+'px';
        }
        co.style.width=(ww)+'px';
        ba.style.width=(ww)+'px';
        ad.style.width=(ww)+'px';
        //co.style.left=co.style.left;
        if (window.ActiveXObject!=undefined){
           co.style.width=(ww-1)+'px';//co.style.width;
        //}else{
           //co.style.width=co.style.width;
        }
        var h2=hh/2;
        /*
        if (curPage=='plug_x'){
           co.style.height='0px';
           co.style.top=(hBarHeight+h2)+'px';
        }else{
           co.style.height=0+'px';
           //co.style.top=hBarHeight+'px';
           co.style.top=-5+'px';
        }*/
        try{
            //ge.style.left='0px';
            //ge.style.top='0px';
            //ge.style.width='100%';
            //ge.style.height='100%';
        }catch(e){
        }
    }catch(e){
        alert(e);
    }
    return;
}

function format(id,value,precision){
   var field=document.getElementById(id);
   n=new Number(value);
   n=n.toFixed(precision);
   field.innerHTML=n;
}
function format2(value,precision){
   n=new Number(value);
   return n.toFixed(precision);
}
function setText(id,text){
   field=document.getElementById(id);
   field.innerHTML=text;
}

function createButtons(items,line,classname){
    var row=new ui_control(classname,'','');
    var left=45;
    row.top=2+line*defLineHeight;
    for (var j=0;j<items.length;j++){
       var classname=items[j][0];
       var id=items[j][1];
       var label=items[j][2];
       var value=items[j][3];

       var width=0;
       var child=new ui_control(classname,id,label,value);
       child.cols=items[j][4];
       if (id!=''){
           controls[child.id]=child;
       }
       row.childs.push(child);
       child.left=left;
       left+= child.cols*defColWidth;
       child.width=child.cols*defColWidth-3;
       child.top=0;
    }
    row.show2();
}

function createPanels(items ){
   for (var i=0;i<items.length;i++){
      var name=items[i][0];
      var caption=items[i][1];
      var rows=items[i][2];
      var rr=rows.length;
      var panel=new ui_control('panel',name,caption,true);
      controls[panel.id]=panel;
      var left=0;
      for (var r=0;r<rows.length;r++){
          var childs=rows[r];
          var row=new ui_control('row','','');
          left=0;
          panel.childs.push(row);
          row.top=3+r*defLineHeight;
          if (panel.label!='')
             row.top+=defLineHeight+3;
          for (j=0;j<childs.length;j++){
             var classname=childs[j][0];
             var id=childs[j][1];
             var label=childs[j][2];
             var value=childs[j][3];
             var child=null;
             var width=0;
             child=new ui_control(classname,id,label,value);
             child.cols=childs[j][4];
             if (id!=''){
                 controls[child.id]=child;
             }
             row.childs.push(child);
             child.left=left;
             left+= child.cols*defColWidth;
             child.width=child.cols*defColWidth-3;
             child.top=0;
          }
      }
      panel.show();
   }
}


//ui_control--------------------------------------------------------
function ui_control(classname,id,label,value) {
   this.classname=classname;
   this.id=id;
   this.label=label;
   this.value=value;
   this.cols=5;
   this.row=0;
   this.left=0;
   this.top=0;
   this.width=0;
   this.height=defLineHeight;
   this.childs=new Array();
}

ui_control.prototype.show=function(){
   var html='';
   if (this.classname=='panel' ){
       this.height=parseInt(defLineHeight*this.childs.length+8);
       if (this.label!='')
          this.height+=parseInt(defLineHeight);
       var style='height:'+this.height+'px;top:'+this.top+'px;';
       html+='<div class="panel" id="'+this.id+'" style="'+style+'">';
       if (this.label!=''){
           var jsevent='javascript:ui_caption_clicked("'+this.id+'")';
           html+='<div id="'+this.id+'" class="caption" onclick='+jsevent+'>'+this.label+'<div id=btn'+this.id+' class=capbtn></div>'+'</div>';
       }
       for (var i=0;i<this.childs.length;i++){
           var c=this.childs[i];
           html+=c.toString();
       }
       html+='</div>';
   }
   //alert(html);
   document.write(html);
}
ui_control.prototype.show2=function(){
   var html='';
       var style='height:'+this.height+'px;top:'+this.top+'px;';
       html+='<div class="'+this.classname+'" id="'+this.id+'" style="'+style+'">';
       for (var i=0;i<this.childs.length;i++){
           var c=this.childs[i];
           html+=c.toString();
       }
       html+='</div>';
   //alert(html);
   document.write(html);
}

ui_control.prototype.toString=function(){
   var html='';
   var style='left:'+this.left+'px;'+'top:'+this.top+'px;'+'width:'+this.width+'px;';
   if (this.classname=='row'){
       style='top:'+this.top+'px;';
       html+='<div class="row" style="'+style+'">';
       for (var i=0;i<this.childs.length;i++){
           var c=this.childs[i];
           html+=c.toString();
       }
       html+='</div>';
       return html;
   }

   if (this.classname=='progress'){

       var style=' style="'+'left:'+this.left+'px;'+'top:0px;'+'width:'+this.width+'px;"';//+'height:'+this.height+'px;" ';
       html+='<div id="'+this.id+'" class="progress" '+style+'>';
       for (var i=0;i<this.value;i++){
           var jsevent='javascript:ui_progress_clicked("'+this.id+'",'+(i+1)+')';
           var left=i*2+2;
           var style2=' style="'+'left:'+left+'px;'+'top:1px;'+'width:'+3+'px;"';//+height:'+(this.height)+'px;" ';
           html+='<div class="progressLed" '+style2+' + onclick='+jsevent+ '></div>';
           //alert(html);
           //break;
       }
       html+='</div>';
       return html;
   }

   var jsevent='javascript:ui_control_clicked("'+this.id+'")';
   var keyevent='""';
   var clickevent='';
   //if (this.id!=''){clickevent=' onclick='+'javascript:ui_control_clicked("'+this.id+'") ';   }
   html+='<div class="'+this.classname+'" id="'+this.id+'" style="'+style+'"'+clickevent+'>';
   var attr='';
   var itext=this.label;
   if (this.id!=''){
      var style=' style="'+'left:0px;'+'top:0px;'+'width:'+this.width+'px;height:'+this.height+'px;" ';
      attr=     ' id="a_'+this.id+'" ';
      itext='<a  '+style+attr+' class="'+this.classname+'" href='+jsevent+'>'+this.label+'</a>';
      if (this.classname=='hypertext'){
         itext='<a  '+style+attr+' class="'+this.classname+'" href='+this.value+'>'+this.label+'</a>';
      }
      if (this.classname=='edit'){
         var style=' style="'+'left:3px;'+'top:1px;'+'width:'+(this.width-6)+'px;height:'+(this.height-2)+'px;" ';
         itext='<input type=text value="'+this.label+'" class="'+this.classname+'" id="t_'+this.id+'" '+style+'></input>';
         //alert(itext);
      }
   }
   html+=itext;
   html+='</div>';

   return html;
}


ui_control.prototype.set=function(value){
   this.value=value;
   var text=this.value;
   if (this.classname=='check')
      text=this.label;
   var itext=text;
   var pdiv =document.getElementById(this.id);
   var adiv =document.getElementById('a_'+this.id);
   var tdiv =document.getElementById('t_'+this.id);
   if (this.id!=undefined){
      if (adiv!=undefined)
         adiv.innerHTML=itext;
      if (tdiv!=undefined){
         tdiv.value=itext;
      }
      if (this.classname=='check'){
          if (this.value){
              pdiv.style.border='1px solid white';
              if (adiv!=undefined)
                 adiv.style.color='orange';
          }else{
              pdiv.style.border='1px solid #ffffff';
              if (adiv!=undefined)
                  adiv.style.color='white';
          }
      }
      if (this.classname=='progress'){
         var children = pdiv.childNodes;
         for (var i=0;i<children.length;i++) {
            if (i<this.value) {
               children[i].className='progressLedOn';
            }else{
               children[i].className='progressLed';
            }
         }
      }
   }
}

function _alert(msg){
    if (verbose)
        alert(msg);
}

function ui_control_clicked(id){
   verbose=true;
   if (controls[id].classname=='check'){
        controls[id].set(! controls[id].value);
   }
   var userfunc=controls[id].id+'click';
   try{
      if (userfunc!=undefined){
         eval(userfunc+'()');
      }
   }catch(e){
     //_alert(e);
   }
   //if (doControlClick!=undefined){
   //   doControlClick();
   //}
   verbose=false;
}
function ui_control_key(event,id){
//alert(event);
   verbose=true;
   if (controls[id].classname=='check'){
        controls[id].set(! controls[id].value);
   }
   var userfunc=controls[id].id+'key';
   try{
      if (userfunc!=undefined){
         eval(userfunc+'(event)');
      }
   }catch(e){
     //_alert(e);
   }
   //if (doControlClick!=undefined){
   //   doControlClick();
   //}
   verbose=false;
}

function ui_caption_clicked(id){
   verbose=true;
   var divPanel=document.getElementById(id);
   var divBtn=document.getElementById('btn'+id);
   //alert(divPanel.style.height);
   if (divPanel.style.height=='18px'){
       divPanel.style.height=controls[id].height+'px';
       divBtn.style.backgroundColor='orange';
       controls[id].value=true;
   }else{
       divPanel.style.height='18px';
       divBtn.style.backgroundColor='#303030';
       controls[id].value=false;
   }
   verbose=false;
}

function ui_progress_clicked(id,value){
   verbose=true;
   //_alert(id+' '+value);
   controls[id].value=value;
   ui_control_clicked(id);
   verbose=false;
}

