﻿

var MAX_SPLASH_IDX = 4;

var splashIdx = 1;

$(document).ready(
             function()
             {
                 $("#divSplash").click(function(e) { goHome(); });
                 $("#btnMap").click(function(e) { goSiteMap(); });
                 $("#btnSearch").click(function(e) { displaySearch(true); e.stopPropagation(); });
                 $("#tbSearchText").keydown(function(e) { if (e.keyCode==13) { goSearch(); e.stopPropagation(); return false; } } );
                
                 splashIdx = Math.floor(Math.random()*(MAX_SPLASH_IDX+1));
                 swapSplash(true);
                 setInterval('swapSplash()', 7000);
                 
                 
                 
                 var blades = new Blades(51, 46, 0.15);
                 var bladeAngle = 0.0;
                 setInterval( function()
                          {
                            var cnv = ITA.id('cnvWindmill');
                            cnv.width = cnv.width;  //reset                       
                            var ctx = cnv.getContext('2d');
                                    
                            ctx.drawImage(ITA.id("imgMill"), 27, 24);
                            blades.Draw(ctx, bladeAngle);
                            bladeAngle = ITA.Geometry.wrapAngle(bladeAngle, - 3.14 / 180);
                          }
                         , 20); 
             }
        );

     $(document).keydown( function(e) {  if (e.keyCode==27) displaySearch(false);  } );
     $(document).click( function(e) {  displaySearch(false);  } );

     
     
     $(window).resize(function(){ displaySearch(false, true); });
     
     $(window).scroll(function(){ displaySearch(false, true); });
     
     
     
     function swapSplash(instant)
     {
       var cls ="divSplash"; 
       if (splashIdx>0) cls+= splashIdx.toString();
       
       
       if (instant)
          $('#divSplash').attr("class", cls);
       else
          $('#divSplash').fadeOut(270,
                                function() 
                                { 
                                  $('#divSplash').attr("class", cls); 
                                  $('#divSplash').fadeIn(400);
                                }
                              );
                             
       
         
       
       splashIdx++;
       if (splashIdx>MAX_SPLASH_IDX) splashIdx = 0;
       
     }
     
     
     
     var dslatch = false;
     function displaySearch(display, instant)
     {
       $("#tbSearchText").val("");
       
       var css = display ? { left: "218px", width: "277px"} : { left: '357px', width: '0px' };
       var csse = display ? { display: "block"} : { display: "none" };
       
       if (instant)
       {
         $("#divSearch").css(css);
         $("#divSearch").css(csse);
       }
       else
       if (!dslatch)
       {
         dslatch = true;
         $('#divSearch').animate(css, 350,
                             function() 
                             { 
                                $("#divSearch").css(csse);
                                 dslatch=false; 
                                 if (display) $("#tbSearchText").focus();
                             } );
       }
     }
