var colorbandW = 620;
var colorbandX = 0;
var colorband2X = 0;


function moveColorband() {
	colorbandX = (colorbandX < -colorbandW)? 0 : colorbandX-8;
	colorband2X = (colorbandX < -colorbandW)? 0 : colorbandX-4;
	$("#colorband").css("backgroundPosition", colorbandX+"px 0px");
	$("#colorband2").css("backgroundPosition", -colorband2X+"px 0px");
	setTimeout("moveColorband()", 100);
}

function slideInNav(e){
  bgPosY = $(e).data('bgPosY');
  if(bgPosY > 1) {
    bgPosY = Math.ceil(bgPosY / 2);
    $(e).css('backgroundPosition', '0px '+bgPosY+'px');
    $(e).data('bgPosY', bgPosY);
    setTimeout(function(){ slideInNav(e, bgPosY); }, 50);
  } else {
    $(e).data('bgPosY', 0);
    $(e).css('backgroundPosition', '0px 0px');
  }
}

function bwebToolTip(selector){
  $(selector)
    .each(function(){
      $(this).data('tool',$(this).attr('title'));
      $(this).removeAttr('title');
    })
    .mouseover(function(e){
      $("#bweb-tooltip").remove();
      $("body").prepend("<div id='bweb-tooltip'>"+$(this).data('tool')+"</div>");
      
      offsetY = 50;
      offsetX = Math.round($("#bweb-tooltip").width() / 2)+10;
      $("#bweb-tooltip")
        .css({
          opacity: 0,
          top: (e.pageY-offsetY)+"px",
          left: (e.pageX-offsetX)+"px"
        })
        .animate({ opacity: 1 }, 400);
        
      $(this)
        .mousemove(function(e){
          offsetY = 50;
          offsetX = Math.round($("#bweb-tooltip").width() / 2)+10;
          $("#bweb-tooltip").css({
            top: (e.pageY-offsetY)+"px",
            left: (e.pageX-offsetX)+"px"
          });
        })
        .mouseout(function(e){
          $("#bweb-tooltip").stop().animate({ opacity: 0 }, 100, "linear", function(){ $(this).remove(); $(selector).unbind("mousemove, mouseout"); });	
        });
    })
}


$(document).ready(function(){

  bwebToolTip(".thumbs a, #header input");
  
  if(!ie6){
  
    //fade the page in
    /*
    $("#admin-bar, #fadeout").hide();
    $("#pagewrapper").animate({ opacity: 1, marginTop: 0, marginBottom: 0 }, 800, function(){
      $("#admin-bar, #fadeout").show();
    });
    //*/
    
    //start the trippy experience
    moveColorband();
  
    //nifty page transition
    $("#header ul a, .post a").click(function(){
      theHref = $(this).attr("href");
      clickedLink = $(this);
      
      //do not fade out if the clicked link is a common filetype
      if(theHref.match(/.xls|.doc|.pdf|.jpg|.gif|.zip/i)) return;
      
      //fade out and then go to the page
      $("#admin-bar, #fadeout").hide();
      $("#pagewrapper")
        .animate({ opacity: 0, marginTop: -200, marginBottom: 200 }, 400, function(){
          document.location.href = theHref;
        });
      return false;
    });
    
    //and so does clicking the search button
    $("#header ul button").click(function(){
      inputValue = $("#header ul input[name='s']").val();
      $("#admin-bar, #fadeout").hide();
      if(!$.trim(inputValue)) {
        $("#header ul input[name='s']").focus();
        return false;
      }
      $("#page")
        .animate({ opacity: 0, marginTop: -200, marginBottom: 200 }, 500, function(){
          $("#header ul form").submit();
        });
      return false;
    });
    
  }
  
  //I don't like the selction rectangle on buttons
  $("button").focus(function(){ $(this).trigger("blur"); });
  
  //add some markeup to h3's for the chopped effect
  $("h3:not(:has(span))").wrapInner("<span></span>");

  //animated menu rollovers
  $("#header ul li.page_item:not(.current_page_item, .current_page_ancestor) a")
    .each(function(){
      $(this).removeAttr('title'); //so there is no popup on mouseover
    })
    .mouseover(function(){
      $(this).css('backgroundPosition', '0px 40px');
      $(this).data('bgPosY', '40');
      slideInNav(this);
    })
    .mouseout(function(){ $(this).css('backgroundPosition', '0px 40px'); });
  
  //focus & blur events for search box
  $("#header li input")
    .focus(function(){ $(this).addClass("focus"); })
    .blur(function(){ $(this).removeClass("focus"); });
  
  //focus & blur events for input fields
  $(".wpcf7-form input[type='text'], .wpcf7-form input[type='password'], .wpcf7-form textarea")
    .focus(function(){
      $(this)
        .css({ color: '#000', opacity: 1 })
        .parents(".wpcf7-form-control-wrap").css({ opacity: 1 });
    })
    .blur(function(){
      $(this)
        .css({ color: '#777', opacity: .8 })
        .parents(".wpcf7-form-control-wrap").css({ opacity: .8 });
    })
    .css({ color: '#777', opacity: .8 })
    .parents(".wpcf7-form-control-wrap").css({ opacity: .8 });
  
});