var cache = [];
var background_names = new Array("cat","default","dog","vet");
//---------------------------------------
// CONSTRUCTORS
//---------------------------------------
$(document).ready(function()
{
   // toggles the more_info box on clicking the More Info &darr; link 
   $('.toggle').click(function(){
     if ($(this).next('blockquote').length > 0) {
        $(this).next('blockquote').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     } else if ($(this).next('.toggle_content').length > 0) {
        $(this).next('.toggle_content').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     }
     return false;
   }).toggleClass("down");

   //hide the div's to begin with
   $('.toggle').next().hide().addClass("toggle_content");
   $('.toggle:first').toggleClass("up").toggleClass("down").next().show();
   
   //autopopulate for labels and forms
   ($("label.autopopulate").length > 0) ? autopopulateLabels() : "";
   
   $.each($("#contentSlot img"),function(n,el){
      var align = $(el).attr("align");
      var img_class = "";
      switch(align) {
         case "right":
            img_class = " img_right";
         break;
         case "left":
            img_class = " img_left";
         break;
      }
      if (!$(el).hasClass("authcode") && !$(el).hasClass("close-img") && !$(el + "[src*='spacer']")) $(el).wrap("<div class='img-shadow"+img_class+"'></div>").attr("style","");
   });
   //temp fix for old links
   $("#contentSlot a[href*='ID=']").attr("href","javascript:void(0);").attr("target","_self");
   
   /* SIFR ADJUSTMENTS (removed on 2010-03-11 by NHANNA since sIFR was removed)
   //make h2's a little smarter for store accordions and sIFR
   $.each($("h2"),function(n,el){ if($(el).parent().parent().parent().attr("id") != "product-widgets") $(el).addClass("can_replace"); });
   */
   
   //alignment issues with margin and top title of every content area
   $("#layout :header:not('h1.pg_title'):first-child").attr("style","margin-top: 0; position: static;");
   
   //wrap all hr's in a div for better style control
   $("#layout hr").wrap("<div class='hr'></div>");
   
   //preload all CSS images
   if ($("#combined #container").length > 0 && document.images)
   {
      //_debug("PRE-LOAD IMAGES");
      var pic1= new Image(897,415); 
      pic1.src="images/bg_body-default.jpg";
      
      var pic2= new Image(897,415); 
      pic2.src="images/bg_body-cat.jpg";
      
      var pic3= new Image(897,415); 
      pic3.src="images/bg_body-dog.jpg";
      
      var pic4= new Image(897,415); 
      pic4.src="images/bg_body-vet.jpg";
   }
   
   //swap the background image on load based on randomly picked picture
   if ($("#hdrImg img").length)
   {
      $.each(background_names,function(i,bg){
         if ($("#hdrImg img").attr("src").indexOf(bg) != -1) $("body").attr("class","default " + bg);
      });
   }
});
//---------------------------------------
$(window).load(function()
{
   if ($("#combined #container").length > 0)
   {
      $("#combined").addClass("jquery_scrollable_enabled");
      $("#navi_tip").removeClass("hidden");
      
      //init jquery tools scroller
      var api = $("#container").scrollable({
         // vertical:true,
         size:1,
         hoverClass: 'hover'
      }).circular().navigator("#main_navi").autoscroll({
         steps: 1,
         interval: 5000,
         autopause: true,
         autoplay: false,
         api: true
      });
      
      
      $("#main_navi li:eq(1)").addClass("active_after");
      
      // do some custom actions when scrolling occurs 
      api.onSeek(function()
      {
         var background_names = new Array("cat","default","dog","vet");
         var after_index = this.getIndex();
         var visible_item = this.getVisibleItems();
         var img = $(visible_item).find("img");
         var bg_class = "";
         
         //swap the bg image to match the foreground image
         $.each(background_names,function(i,bg){
            if (img.attr("src").indexOf(bg) != -1) {
               $("body").attr("class","default " + bg);
            }
         });
      });
   }
});
//---------------------------------------
// PUBLIC METHODS
//---------------------------------------
function _debug ($string) {
   try {
      console.log($string);
   } catch (err) { 
      //$("#footer").append($string + "<div class='hr'><hr /></div>"); 
   }
}
//---------------------------------------
function autopopulateLabels() {
   $.each($("label.autopopulate"), function(){
      //_debug("VAL: " + ($(this).next("input[type='text']").val()));
      if ($(this).next("input[type='text']").val() == "") {
         $(this).next("input[type='text']").attr("value", $(this).html());
      }
      $(this).next("input[type='text']").attr("title", $(this).html());
      $(this).hide();
      //REMOVE populated label on a focus
      $(this).next("input[type='text']").focus(function () {
         // If value and title are equal on focus, clear value
			if (this.value == this.title) {
				this.value = '';
				this.select(); // Make input caret visible in IE
			}
      });
      //ADDED it back if field is empty
      $(this).next("input[type='text']").blur(function () {
         if (!this.value.length) { this.value = this.title; }
      });
   });
}
//---------------------------------------
function preLoadImages () {
   var args_len = arguments.length;
   for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
   }
}
