var adjustAssets = function(){

   // stretch short pages
   /**
   var bh = 
      window.innerHeight ? window.innerHeight : document.documentElement.clientHeight 
      ? document.documentElement.clientHeight : document.body.clientHeight;

   if (Ext.get('wrapper').getHeight() < bh)
   {
      var hh = Ext.get('ctl00_header').getHeight();
      var fh = Ext.get('ctl00_footer').getHeight();
      
      Ext.get('ctl00_centerColumn').setHeight(bh - hh - fh - 20);
   }
   */

}
var loadSearch = function() { 
   var searchText = document.getElementById("ttHeaderSearchTextbox"); 
   var searchButton = document.getElementById("ttHeaderSearchButton");

   var searchButtonOff = '/_media/SearchFindIt.gif';
   var searchButtonOn  = '/_media/SearchFindIt_o.gif';

   searchText.onkeyup = function() { 
      if (event.keyCode=='13') { searchButton.click();} 
   }; 

   searchButton.onmouseover = function() { 
      this.src = searchButtonOn; 
   };

   searchButton.onmouseout = function() { 
      this.src = searchButtonOff; 
   }; 

   searchButton.onclick = function() { 
      if (searchText.value != 'FOOBAR') { 
         window.location = '/search.aspx?find=' + encodeURIComponent(searchText.value).replace(/%20/g, '+'); 
         return false; 
      } 
   }; 
} 

var showPreview = function(imagePath, imageIndex){
   var subImages = document.getElementById('ttImagePane').getElementsByTagName('IMG');
   var img;

   if (subImages.length == 0) {
      img = document.createElement('IMG');
      document.getElementById('ttImagePane').appendChild(img);
   } else {
      img = subImages[0];
   }
      
   img.src = imagePath;
      
   var thumb = document.getElementById('ttImageGalleryImages').getElementsByTagName('IMG');

   for(var no=0; no < thumb.length; no++){
      if (imageIndex == no) {
         thumb[no].style.border = '2px solid #000000';
      } else {
         thumb[no].style.border = '1px solid #CCCCCC';
      }
   }
}

var writeImage = function(displayImage, imageCount) {

   var galleryHolder = Ext.get("ttImageGalleryImages");
   var html = '';

   html += '<a href="#" onclick="showPreview(\'' + displayImage + '\' ,\'' + imageCount + '\');return false">';

   html += '<img '

   if (imageCount == 0) {
      html += 'class="first" ';
   }

   html += 'src="' + displayImage + '" />'
   html += '</a>';

   galleryHolder.insertHtml("beforeEnd", html, false);
}

function imageHolder(image, category) {
   this.img = image;
   this.cat = category;
}

var writeHomepageImage = function() {
   var imgCat = new Array();
   var i = 0;
   imgCat[i++] = new imageHolder('TT_AC_tools.jpg', 3596);
   imgCat[i++] = new imageHolder('TT_Air_Compressors.jpg', 3617);
   imgCat[i++] = new imageHolder('TT_Air_Tools.jpg', 3606);
   imgCat[i++] = new imageHolder('TT_Battery_Tools.jpg', 3629);
   imgCat[i++] = new imageHolder('TT_Diagnostic_Tools.jpg', 3626);
   imgCat[i++] = new imageHolder('TT_Electric_Tools.jpg', 3634);
   imgCat[i++] = new imageHolder('TT_Hand_Tools.jpg', 3593);
   imgCat[i++] = new imageHolder('TT_Lifting_Equipment.jpg', 3665);
   imgCat[i++] = new imageHolder('TT_Paint_Body_Tools.jpg', 3623);
   imgCat[i++] = new imageHolder('TT_Safety_Equipment.jpg', 3678);
   imgCat[i++] = new imageHolder('TT_Shop_Equipment.jpg', 3643);
   imgCat[i++] = new imageHolder('TT_Tool_Storage.jpg', 3647);

   var max = imgCat.length - 1;

   i = Math.round(Math.random()*(max));

   if (i < 0 || i > max) {
      i = 0;
   }

   document.write('<a href="http:\/\/www.tooltopia.com\/search.aspx?find=&category='+ imgCat[i].cat + '"><img src="\/_media\/banners\/'+ imgCat[i].img + '"></a>');

}


Ext.BLANK_IMAGE_URL = '/_media/js/ext/s.gif';

Ext.onReady(function(){
   // auto adjust everything on page load or window resize
   //window.onresize = adjustAssets;
   //adjustAssets();
   loadSearch();
   
   // hover images for the top menu bar
   var shopCategoriesOff = '/_media/ShopCatsOff.gif';
   var shopCategoriesOn = '/_media/ShopCatsOn.gif';
   var shopBrandsOff = '/_media/ShopBrandsOff.gif';
   var shopBrandsOn = '/_media/ShopBrandsOn.gif';
   var dailyDealsOff = '/_media/DailyDealsOff.gif';
   var dailyDealsOn = '/_media/DailyDealsOn.gif';
   
   var shopCategories = document.getElementById("shopCategoriesGif"); 
   shopCategories.onmouseover = function() { 
      this.src = shopCategoriesOn; 
   };
   shopCategories.onmouseout = function() { 
      this.src = shopCategoriesOff; 
   };
   var shopBrands = document.getElementById("shopBrandsGif"); 
   shopBrands.onmouseover = function() { 
      this.src = shopBrandsOn; 
   };
   shopBrands.onmouseout = function() { 
      this.src = shopBrandsOff; 
   };       
   var dailyDeals = document.getElementById("dailyDealsGif"); 
   dailyDeals.onmouseover = function() { 
      this.src = dailyDealsOn; 
   };
   dailyDeals.onmouseout = function() { 
      this.src = dailyDealsOff; 
   }; 
});