function setText(formField)
{
  var label = $(formField).prev("label");
  var labelContent = label.html();
  label.next().val(labelContent);
  label.hide();

  $(formField).bind("focus", function(e){
    $(formField).val("");
  });

  $(formField).blur(function(e){
    if($(formField).val() == "")
    {
      $(formField).val(labelContent);
    }
  });
}

function selectHomeCat(homeCat)
{
  var homeImageArrow = $("#HomePageRotator #HomeImageArrow img");
  
  $("#HomePageRotator li").removeClass("selected");
  $(homeCat).addClass("selected");
    homeImageHolder.fadeTo(200,0, function(){
    homeImageHolder.html($(homeCat).find(".image").html());
    homeImageHolder.append("<div class=\"text\">" + $(homeCat).find(".text").html() + "</div>");
    homeImageHolder.fadeTo(200,1);
    homeImageHolder.find("p").show();
      
    var alignHeight = ($(homeCat).position().top + $(homeCat).outerHeight() / 2) - homeImageArrow.outerHeight() / 2;
    homeImageArrow.stop(true, false).animate({
      top: alignHeight
    }, 200);
  });
}
  
//Gets an internal link and makes the whole item clickable
function MakeElementClickable(item, hideLink)
{
  var link = item.find("a").attr("href");
  item.bind("click", function(){window.location = link;});
  
  if(hideLink)
  {
      item.find("a").hide();
  }
}

//Gets an internal link and makes the whole item clickable
function MakeElementHoverable(item, mouseOverClass)
{
  item.bind("mouseenter", function(){$(this).addClass(mouseOverClass)});
  item.bind("mouseleave", function(){$(this).removeClass(mouseOverClass)});
}

//make a set of elements all the same height
$.fn.EqualHeight = function () {

  var group = $(this);
  var thisHeight;

  tallest = 0;

  group.each(function () {
      thisHeight = $(this).height();

      if (thisHeight > tallest) {
          tallest = thisHeight;
      }
  });
  group.height(tallest);
}

$.fn.Scale = function() {
  var self = $(this);
  var scaleWidth = 600;
  var scaleHeight = 10;
  var scaleSlider = $("#ScaleSlider");
  var scaleRule = $("#ScaleRule");
  var scale = $("#Scale");
  var tweets = self.find(".tweet");
  var highestValue = 25000;
  var lowestValue = 0;
  var scaleSegments = 0;
  var tweetsDisplay;
  
  self.prepend("<div id=\"ScaleSlider\"></div>");
  self.append("<div id=\"TweetsDisplay\"><div id=\"TweetsDisplayInner\"><h3>Some of the people we helped...</h3></div></div>");
  tweets.wrapAll("<div id=\"ScaleRule\"></div>");
  scaleSlider = $("#ScaleSlider");
  scaleRule = $("#ScaleRule");
  tweetsDisplay = $("#TweetsDisplay");
  tweetsDisplayInner = $("#TweetsDisplayInner");
  tweetsDisplay.hide();
  
  scaleRule.css("width", scaleWidth + "px");
  scaleRule.css("height", scaleHeight + "px");
  
  tweets.EqualHeight();
  
  //get the largest / lowest figure
  tweets.each(function(){
    var tweetContent = $(this).find("p.content");
    tweetContent.text(tweetContent.text().replace("<a href=\"http://www.debtaid.ltd.uk\">http://www.debtaid.ltd.uk</a>",""));
    var prices = tweetContent.text().match(/( )*£(\d*.\d*)/gi);
    
    if(prices.length == 2)
    {
      var highPrice = jQuery.trim(prices[1].replace("£",""));
      highPrice = highPrice-0;
      
      var lowPrice= jQuery.trim(prices[0].replace("£",""));
      
      if(highPrice > highestValue)
      {
        highestValue = highPrice;
      }
      
      if(lowPrice < lowestValue)
      {
        lowestValue = lowPrice;
      }
    }
  });

  scaleSegments = scaleWidth / highestValue;  
  
  //add each tweet to a scale point
  var i = 0;
  tweets.each(function(){
  
    var tweetContent = $(this).find("p.content");
    tweetContent.html(tweetContent.html().replace(/, Call.*/gi,""));
    var prices = tweetContent.html().match(/( )*£(\d*.\d*)/gi);    
   
    if(prices.length == 2)
    {
      var tweet = $(this);
      var highPrice = jQuery.trim(prices[1].replace("£",""));
      var lowPrice= jQuery.trim(prices[0].replace("£",""));
      var sortedPrice = Math.round(highPrice/1000)*1000;
      var scalePoint = $("#ScalePoint" + sortedPrice);
      var tweetsContainer = scalePoint.find(".tweetsContainer");
      
      //wrap prices in span
      tweetContent.html(tweetContent.html().replace(prices[0], "<span class=\"payment\">" + prices[0] + "</span>"));
      tweetContent.html(tweetContent.html().replace(prices[1], "<span class=\"debt\">" + prices[1] + "</span>"));
      
      //alert(scaleSegments + " * " + highPrice + " = " + scaleSegments * highPrice);
      if(scalePoint.length == 0)
      {
        scaleRule.append("<div id=\"ScalePoint" + sortedPrice + "\" class=\"scalepoint\"></div>");
        scalePoint = $("#ScalePoint" + sortedPrice);
        scalePoint.prepend("<a id=\"Marker" + sortedPrice + "\" class=\"marker\">&nbsp;</a>");
        scalePoint.append("<div class=\"tweetsContainer\"></div>");
        tweetsContainer = scalePoint.find(".tweetsContainer");
        
        $("#Marker" + sortedPrice).bind("mouseenter", function(){
          tweetsDisplayInner.html("<h3>Some of the people we helped with debt around £" + (sortedPrice+1000) + "</h3>" + tweetsContainer.html())
          tweetsDisplay.fadeTo(0,0);
          tweetsDisplay.show();
          tweetsDisplay.find(".tweet").EqualHeight();
          tweetsDisplay.stop().animate({
            "opacity" : 1,
            "top" : "180px"
          }, 200)
        }); 

        $("#Marker" + sortedPrice).bind("mouseleave", function(){
          tweetsDisplay.stop().animate({
            "opacity" : 0,
            "top" : "200px"
          }, 200, function(){tweetsDisplay.hide(); tweetsDisplayInner.html("");})                  
        });
          
        //position the item        
        scalePoint.css({
          "left" : (scaleSegments * sortedPrice ) + "px"        
        });
      }
                                        
      if(tweetsContainer.find(".tweet").length < 12)
      {
          tweetsContainer.append(tweet);
      }
      else
      {
          tweet.remove();
      }
      tweetsContainer.hide();      
    }
    else
    {
      $(this).remove();
    }
    i++;
    
  });
}

$(document).ready(function(){
    
  $(".hoverable").each(function(){
    MakeElementHoverable($(this), "hover")
  });
  
  $(".body1051 .subPage").EqualHeight();
  $("#SingleCol .subPages .subPage").EqualHeight();
  $("#FooterTweets .tweets .tweet").EqualHeight();
  $("#FooterText .footerCol").EqualHeight();
  $("#Scale").Scale();
    
  //Add scroller content class to all tweets in footer and init scroller
  $("#FooterTweets .tweet").addClass("scrollerItem");
  $("#FooterTweets .tweets").ContentScroller();
    
  //Across the board add altTableRow to odd table rows
  $("tr:odd").addClass("altTableRow");
    
  //add image clipper for subPages
  if($(".subPages").length > 0)
  {
    var caseStudies = $(".subPages .subPage");
    
    caseStudies.each(function(){
      $($(this).find("img")).wrap("<div class=\"imageClip\"></div>");
    });
  }
    
  if($(".step").length > 0)
  {
    var steps = $("#Steps .step");
    
    steps.each(function(){
      var startingTop = $(this).position().top;
      
      $(this).bind("mouseenter", function(){
        $(this).addClass("hover");
        $(this).animate({
          "top": startingTop-20 + "px"
        }, 100);
      });
      
      $(this).bind("mouseleave", function(){
        $(this).removeClass("hover");
        $(this).animate({
          "top": startingTop + "px"
        }, 100);
      });
    });
  }
  
  //make input fields contain label text
  $(".inputField").bind("focus",function(){
    $(".inputField").removeClass("focus");    
    $(this).addClass("focus");
  });
    
  $(".inputField").each(function(i){
    setText(this);
  });
    
});


