/* Copyright 2007-2008 Maciej Marczewski, BlueMedia (Sopot, Poland), All rights reserved */ 
  var bcolor = "4A7DF1";
  var fcolor= "ffffff";
  var nwidth = 500;
  var nheight = 550;
  var bShowDetails = true;

   
   function changeColor(target,color){
    var id = target.attr("id");
    if (id === "bcolor"){
      jQuery("div.widget").css("background-color","#" + color);
      bcolor = color;    
    }else if (id === "fcolor"){
      jQuery("div.widget").css("color","#" + color);
      jQuery("div.widget div.powered_by a").css("color","#" + color);
      jQuery("div.widget_about table td").css("color","#"+color);
      jQuery("div.widget_tabs_links a").css("color","#"+color);
      jQuery("div.widget_about table a").css("color","#"+color);
      fcolor = color;          
    }    
    setWidgetCode();
   } 
   
   function setWidgetCode(){
     var srcUrl = widgetUrl +''+nwidth+'/'+bcolor+'/'+fcolor+'/';
     if (bShowDetails === false){
      srcUrl += "0/";
      nheightTemp = 420;
     } else {
      nheightTemp = nheight;
     
     }
     var code = '<div style="border:2px solid white;font-style:normal;padding:0px;padding-bottom:3px !important;float:left" >'+
          '<iframe src="'+srcUrl+'" border="0" frameborder="0" marginheight="0" marginwidth="0"  scrolling="no" width="'+ parseInt(nwidth + 10) +'" height="'+ nheightTemp +'">'+
          '</iframe>'+
          '</div>';    
     jQuery("input#widget_code").val(code); 
   }
   
   function setWidgetCodeInit(){
     var code = '<div style="border:2px solid white;font-style:normal;padding:0px;padding-bottom:3px !important;float:left">'+
          '<iframe src="'+widgetUrl+'" border="0" frameborder="0" marginheight="0" marginwidth="0"  scrolling="no" width="510" height="550">'+
          '</iframe>'+
          '</div>';    
     jQuery("input#widget_code").val(code);    
   }
   
    
    function initFlots(){
      
      
    }
   
   
   function createWidgetFlots(){
    flot_widget = new MMPlot();        
    flot_widget2 = new MMPlot();    
    
    var s1 = new MMPlotSeries();
    s1.initLineData(null,"speed","#3961A3",1.8,true,"#BCDFFF",5);
    flot_widget.addSeries(s1);
   
    var s2 = new MMPlotSeries();
    s2.initLineData(null,"altitude","#2B8B49",1.8,true,"#BCFFBF",5);
    flot_widget2.addSeries(s2);        
    
    flot_widget.setLegend("ne",5,"#DBEDFB",0.5,"white",3); 
    flot_widget2.setLegend("ne",5,"#DBEDFB",0.5,"white",3); 

    flot_widget.setGrid("#DBEDFB","#DBEDFB","#DBF8FF");        
    flot_widget2.setGrid("#DBEDFB","#DCFFDF","#EEFFF0");    
    
    flot_widget.setAxisNumberOfTicks(5,3);

    flot_widget.resetMovie = function(){
       flot_widget.series[0].data = [];
       //flot.setMaxAxisValues(390,undefined);
       flot_widget.resetXAxis();
       flot_widget.setAxisNumberOfTicks(new Array());
       flot_widget.removeClickEvent();
       flot_widget.removeSelectionEvent();
       flot_widget.removeDoubleClickEvent();
    }; 
     
    flot_widget.showFrame = function(n,params){
      if (n === 0){
        flot_widget.series[0].data = [];
      }else {
        flot_widget.series[0].data.push(new Array(n,params[2]));
      }        
      if (jQuery("#"+flot.id+":visible").length > 0) {
          //it is hidden      
         flot_widget.redraw();            
     }    
    };     
  
  
    flot_widget2.resetMovie = function(){
       flot_widget2.series[0].data = [];
       flot_widget2.resetXAxis();
       flot_widget2.setAxisNumberOfTicks(new Array());
       flot_widget2.removeClickEvent();
       flot_widget2.removeSelectionEvent();
       flot_widget2.removeDoubleClickEvent();
    }; 
     
    flot_widget2.showFrame = function(n,params){
       if (n === 0){
         flot_widget2.series[0].data = [];
       }else {
         flot_widget2.series[0].data.push(new Array(n,params[2]));
       }
         flot_widget2.redraw();
    };             

   
   } 
   function loadFlot1(){
      flot_widget.redraw();     
      flot_widget.showLoading();
      flot_widget.getData(1);
      
      flot_widget.addDoubleClickEvent(function(){
        flot_widget.reset();
      });    
   } 
   function loadFlot2(){
      flot_widget2.redraw();     
      flot_widget2.showLoading();
      flot_widget2.getData(1);
      
      flot_widget2.addDoubleClickEvent(function(){
        flot_widget2.reset();
      });    
   }    
    /**
     * on window load
     */         
   function loadWidgetFlots(){
      flot_widget.redraw();
      flot_widget2.redraw();
      
      flot_widget.showLoading();
      flot_widget2.showLoading();      
      flot_widget.getData(1);
      flot_widget2.getData(1);
      
      flot_widget.addDoubleClickEvent(function(){
        flot.reset();
      });  
      
      flot_widget2.addDoubleClickEvent(function(){
        flot_widget2.reset();
      });           
 
 
  }     
    
    function resizeIFrame(){
       try {
        sHrefMain = parent.document.location.href;
        sHrefHost = parent.document.location.host;
        var iframe = jQuery("iframe[src="+window.location.href+"]", parent.document.body);
        
        //var iframe = jQuery(parent).find("iframe[src="+document.baseURI+"]");
        //alert("uri:" + document.baseURI + "wind " + window.location.href);
        
        iframe.height(jQuery("div#widget_content > div.widget").height() + 30);
        iframe.width(jQuery("div#widget_content > div.widget").width() + 30);
        //alert(iframe);
        //alert(jQuery("div#widget_content > div.widget").height() + " h:" + jQuery("div#widget_content > div.widget").height());

        
        }catch (err){
         // alert(err);
        }    
    }


   
   jQuery(document).ready(function(){

         setWidgetCodeInit();
          
          jQuery('input.color').gccolor({
            useButton:false,
            onChange:changeColor
         });
    
         jQuery("input#b_changewidth").click(function(){
            var v = jQuery("input#frame_width").val();
            v = parseInt(v);
            if (v < 380){
              v = 380;
            }
            jQuery("div.widget").css("width",v +"px");
            nwidth = v;
            if (flotLoaded1 === true){          
              loadFlot1();
            }
            if (flotLoaded2 === true){
              loadFlot2();
            }            
            setWidgetCode();             
         });   
        
        jQuery("a#a_embed").click(function(){
            widgetMap.load("map_widget","/data_map.php","/data_info.php",widgetCode);        
          //alert("loadmap");
        });
        jQuery("a#a_embed_main").click(function(){
            widgetMap.load("map_widget","/data_map.php","/data_info.php",widgetCode);        
          //alert("loadmap");
        });

        jQuery(".widget_customize_link").toggle(function(){
          jQuery("div.div_widget_customize").show();
        },function(){
          jQuery("div.div_widget_customize").hide();
        });
        
        jQuery("input[name=r_show]").click(function(){
          if (this.value === "true"){
            jQuery("div.widget_about div").not(".powered_by").not(".widget_tabs").show();
            jQuery("div.widget_about div.widget_tabs:first").show();                    
            jQuery(".widget_tabs_links a").removeClass("active");
            jQuery("a.widget_tab_1").addClass("active");                    
            bShowDetails = true;
            setWidgetCode();
          }else if (this.value === "false"){
            jQuery("div.widget_about div").not(".powered_by").hide();                    
            bShowDetails = false;
            setWidgetCode();
          }
        });
    /*
        jQuery("input#chk_statistics").toggle(function(){
          jQuery("input[id=chk_statistics]").attr("checked",false);
        },function(){
          //jQuery("input#chk_statistics").attr("checked","checked");
          jQuery("input[id=chk_statistics]").attr("checked",true);
          alert("23");

        });
        */
        flotLoaded1 = false;
        flotLoaded2 = false;
        markPrev = null;
        jQuery(".widget_tabs_links a").click(function(){
         jQuery(".widget_tabs_links a").removeClass("active");
         
         var id = jQuery(this).attr("class");
         jQuery(".widget_tabs").hide();
         jQuery(".widget_tabs#"+id).show();
         if (id === "widget_tab_2" && flotLoaded1 === false){          
          loadFlot1();
          flotLoaded1 = true;
         }
         if (id === "widget_tab_3" && flotLoaded2 === false){
          loadFlot2();
          flotLoaded2 = true;
         }
          jQuery(this).addClass("active");
          resizeIFrame();
        });       
        resizeIFrame();    
    
    
   });