// 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 verbose = false;

var timerID=0;
var defLineHeight=18;
var controls=new Array();
var panels=new Array();
var curPage='plug_x';

//-----------------------------------------------------------------------------
function getContent(page) {
    curPage=page;
    var url = "http://www.svtahlequah.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+'()');
            }
         }catch(e){
           //_alert(e);
         }
      }
   }
}

function doUnload() {
   try{
   }catch(e){
      geap.shut();
   }
}

function doLoad() {
   doResize();
}

function plug_xOnLoad(){
    //setTimeout('initPlugTimer()',1000);
}
function source_xOnLoad(){
}

function log_xOnLoad(){
   var lo=document.getElementById('logDiv');
   try{
      //alert(geap.log.toString());
      lo.innerHTML='<pre>'+geap.log.toString()+'</pre>';
      //geap=undefined;
   }catch(e){
   }
}

function initPlugTimer(){
   geap=new GEAPlugin();
   geap.initCallback=doInit;
   geap.show();
}


function doResize() {
   Resize();
   Resize();//scrollbar...
}

function Resize() {
    try{
//		debugger;
//        var w=document.body.clientWidth;
//        var h=document.body.clientHeight;
//        var w1=document.getElementById('iePlugX');
//		  var w=w1.clientWidth;
//        var h1=document.getElementById('iePlugX');
//		  var h=h1.clientHeight;
        var co=document.getElementById('content');
        var gd=document.getElementById('ieDiv');
        var ge=document.getElementById('ieContainer');
        var la=document.getElementById('leftarea');
        var ra=document.getElementById('rightarea');
        var ca=document.getElementById('centerarea');
        var ov=document.getElementById('overlay');
        var top0=0;
		  var leftWidth=200;
		  var rightWidth=200;
		  
//        co.style.top='36px';
//        gd.style.top='36px';
			// added EGM 3/20/07
			var h=625;
			var w=1200;
			
			if ((screen.width>=1600) && (screen.height>=1200)){
				h=625;
				w=1300;
			}
			else if ((screen.width>=1280) && (screen.height>=1024)){
				h=525;
				w=1150;
			}
			else if ((screen.width>=1024) && (screen.height>=768)){
				h=375;
				w=900;
			}
			else{
				h=250;
				w=700;
			   leftWidth=150;
			   rightWidth=150;
			}
//			debugger;
//			w=(parseInt(ca.style.width)+leftWidth+rightWidth);
			// End Added
//        var hh=h-96-2;
//		  h=(gd.style.pixelHeight+gd.style.pixelTop);
        var hh=parseInt(h*1.1);
        co.style.height=(hh)+'px';
        gd.style.height=(hh)+'px';
        co.style.height=(h)+'px';
        gd.style.height=(h)+'px';
//        var ww=w-2;
        var ww=parseInt(w-leftWidth-rightWidth);
		  //debugger;
        var h2=h+20;
        if (!fullscreen){
//            ww=w-(200*2)-2;
//            ww=w-(200*2)-2-gd.style.pixelLeft;
//            ww=w-(200*2)-2-gd.style.pixelLeft;
            la.style.width=parseInt(leftWidth)+'px';
            ra.style.width=parseInt(rightWidth)+'px';
//            co.style.left='200px';
            co.style.left='0px';
//            la.style.height=(h-(36)-1)+'px';
//            ra.style.height=(h-(36)-1)+'px';
            la.style.height=(h2)+'px';
            ra.style.height=(h2)+'px';
        }else{
            la.style.width=(0)+'px';
            ra.style.width=(0)+'px';
            co.style.left='0px';
            la.style.height=(0)+'px';
            ra.style.height=(0)+'px';
        }
        co.style.width=(w)+'px';
//        gd.style.left=co.style.left;
        gd.style.left='0px';
        gd.style.width=parseInt(ww)+'px';
//        var h2=h+ov.style.height;
        if (curPage=='plug_x'){
           co.style.height='0px';
           gd.style.height=h2+'px';
//           gd.style.top =co.style.top;
           gd.style.top='0px';
           co.style.top=(0)+'px';
        }else{
           co.style.height=h2+'px';
           gd.style.height=0+'px';
           co.style.top=top0+'px';
           gd.style.top=0+'px';
        }
//		  ov.style.top=(parseInt(gd.style.top)+parseInt(gd.style.height)-parseInt(ov.style.height))+"px"
        try{
            ge.style.left='0px';
            ge.style.top='0px';
				// firefox doesn't center properly as oper CSS so we use full-width and no gutter.
            ge.style.width=parseInt(ww-((document.all)?0:0))+'px';
            ge.style.height=((document.all)?'100%':'100%');
            if (geap!=undefined){
              // geap.resize();
            }
        }catch(e){
        }
    }catch(e){
//		alert(e);
//		debugger;
    }
	 try{
		map.setFocus();
	 }catch(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 createPanels(items ){
//alert(items.length);
   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=0+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*18;
             child.width=child.cols*18-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>';
   }
   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:0px;'+'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*3+15;
           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 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>';
   }
   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);
   if (this.id!=undefined){
      if (adiv!=undefined)
         adiv.innerHTML=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 #808080';
              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_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;
}

