var frontBackgrounds = null;
var currentBackground = 0;

$(document).ready(function() {
   $('#LegalIssueUrl').change(legalIssue);
   
   window.setTimeout(slideHeadline,6000);
   //BEGIN Search Field behavior
   $('#searchField').focus(function() {
      if($('#searchField').val() == 'Search') $('#searchField').val('');
   });

   $('#searchField').blur(function() {
      if($('#searchField').val() == '') $('#searchField').val('Search');
   });

   $("#searchField").suggest("/tags/suggest",{
      onSelect: function() {
         document.location.href = '/search/' + $('#searchField').val().toLowerCase();
      }
   });

   $('#searchField').keyup(function() {
      if((typeof(event) != 'undefined') && event.keyCode == 13) { //Enter
         document.location.href = '/search/' + $('#searchField').val().toLowerCase();
      }
   });

   $('#go').click(function() {
      document.location.href = '/search/' + $('#searchField').val().toLowerCase();
      return false;
   });
   //END Search Field behavior   
});

loadBackgrounds = function(bglist) {  
   frontBackgrounds = bglist;
};

swapBackgrounds = function() {
   if(currentBackground >= frontBackgrounds.length) currentBackground = 0;
   //console.log(currentBackground + ': ' + frontBackgrounds[currentBackground]);
      
   var next = 'url(' + frontBackgrounds[currentBackground] + ') no-repeat center 30px';
   var front = 'url(' + frontBackgrounds[currentBackground] + ') no-repeat center top';
   
   $('#homeBanner1').css('background',next);
   window.setTimeout(function() {
      $('#homeBanner').fadeTo(1200,0,function() { 
         $('#homeBanner').css('background',front);
         $('#homeBanner').fadeTo("fast",1,function() {
            currentBackground++;
            
            window.setTimeout(swapBackgrounds,3000);
         });
      });
   },2000);
};

slideHeadline = function() {
    if($('#headlinesInner dl').length <= 3) return false;
    $('#headlinesInner dl.col1').animate({marginLeft:'-165px'},1000,null,function() {

	    $(this).css('marginLeft',0);
	    $('#headlinesInner').append(this);
	    
	    var i = 1;
	    $('#headlines dl').each(function() {
	       $(this).attr('class','col' + i++);
	    });
	    
	    window.setTimeout(slideHeadline,6000);
	 });
};

legalIssue = function() {
  if($(this).val() != '') {
      document.location.href = $(this).val();
  } 
};

// Son-Of-Sucker-Fish IE Hack
// -----------------------------------------------------------------
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		};
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		};
	}
};
if (window.attachEvent) window.attachEvent("onload", sfHover);


