$(function()
{
  $("a[rel='colorbox']").colorbox();
  $("a[rel='video']").colorbox();
  $("a[rel='youtube']").colorbox({ initialWidth: 10, initialHeight: 10, innerWidth: 640, innerHeight: 483 });
  $("a[rel='_blank']").each(function() { $(this).attr("target", "_blank") });
                                 
  $("select[default] option:first").attr("text", function() { return $(this).parent().attr("default") }).parent().addClass("default");
  $("select[default]").focusin(function()
  {
    if ($("option:selected", this).attr("text") != $(this).attr("default"))
    {        
        return false;
    }
    else { $("option:selected", this).attr("text", ""); $(this).removeClass("default") }
  });
  $("select[default]").focusout(function()
  {
    if ($("option:selected", this).attr("text") != "")
    {        
        return false;
    }
    else { $("option:selected", this).attr("text", function() { return $(this).parent().attr("default"); }); $(this).addClass("default") }
  }); 
  $("select[default]").focusout(function()
  {
    var _next = $(this).next();
    if (_next.hasClass("form-required"))
    {
      if ($("option:selected", this).attr("text") != "" && $("option:selected", this).attr("text") != $(this).attr("default"))
      {
        //$(".form-required.title").hide();
        _next.hide();
      }
      else
      {
        //$(".form-required.title").show();
        _next.show();
      }
    }
  });
           
  $("input[type='text'][default]").attr("value", function() { return $(this).attr("default"); }).addClass("default"); 
  $("input[type='text'][default]").focusin(function()
  {
    if ($(this).attr("value") != $(this).attr("default"))
    {
        return false;
    }
    else { $(this).attr("value", ""); $(this).removeClass("default") }
  });
  $("input[type='text'][default]").focusout(function()
  {
    if ($(this).attr("value") != "")
    {
        var _next = $(this).next();
        if (_next.hasClass("form-required"))
        {
          if ($(this).attr("value") != $(this).attr("default"))
          {
            _next.hide();
          }
          else
          {
            _next.show();
          }
        }
      
        return false;
    }
    else { $(this).attr("value", function() { return $(this).attr("default"); }); $(this).addClass("default") }
  });
  $("input[type='text'][default]").live("keyup focusout", function()
  {
    var _next = $(this).next();
    if (_next.hasClass("form-required"))
    {
      if ($(this).attr("value") != "" && $(this).attr("value") != $(this).attr("default"))
      {
        _next.hide();
      }
      else
      {
        _next.show();
      }
    }
  });
           
  $("input[type='text'][title]").attr("value", function() { return $(this).attr("title"); }).addClass("default"); 
  $("input[type='text'][title]").focusin(function()
  {
    if ($(this).attr("value") != $(this).attr("title"))
    {
        return false;
    }
    else { $(this).attr("value", ""); $(this).removeClass("default") }
  });
  $("input[type='text'][title]").focusout(function()
  {
    if ($(this).attr("value") != "")
    {
        var _next = $(this).next();
        if (_next.hasClass("form-required"))
        {
          if ($(this).attr("value") != $(this).attr("title"))
          {
            _next.hide();
          }
          else
          {
            _next.show();
          }
        }
      
        return false;
    }
    else { $(this).attr("value", function() { return $(this).attr("title"); }); $(this).addClass("default") }
  });
  $("input[type='text'][title]").live("keyup focusout", function()
  {
    var _next = $(this).next();
    if (_next.hasClass("form-required"))
    {
      if ($(this).attr("value") != "" && $(this).attr("value") != $(this).attr("title"))
      {
        _next.hide();
      }
      else
      {
        _next.show();
      }
    }
  });
           
  $("input[type='text'][id='confirm'][link]").addClass("fail");
  $("input[type='text'][id='confirm'][link]").each(function()
  {
    var _self = $(this);
    var _link = $(this).attr("link");
    
    $("." + _link).keyup(function()
    {
      if ($(this).attr("value") != _self.attr("value"))
      {
        _self.removeClass("pass");
        _self.addClass("fail");
      }
      else
      {
        _self.removeClass("fail");
        _self.addClass("pass");
      }
    });
  });
  $("input[type='text'][id='confirm'][link]").bind("paste", function()
  {
    return false;
  });
  $("input[type='text'][id='confirm'][link]").keyup(function()
  {
    var _link = $(this).attr("link");
    if ($(this).attr("value") == $("." + _link).attr("value"))
    {
      $(this).removeClass("fail");
      $(this).addClass("pass");
    }
    else
    {
      $(this).removeClass("pass");
      $(this).addClass("fail");
    }
  });
           
  $("textarea[default]").attr("value", function() { return $(this).attr("default"); }).addClass("default"); 
  $("textarea[default]").focusin(function()
  {
    if ($(this).attr("value") != $(this).attr("default"))
    {
        return false;
    }
    else { $(this).attr("value", ""); $(this).removeClass("default") }
  });
  $("textarea[default]").focusout(function()
  {
    if ($(this).attr("value") != "")
    {
        return false;
    }
    else { $(this).attr("value", function() { return $(this).attr("default"); }); $(this).addClass("default") }
  });
  $("textarea[default]").live("keyup focusout", function()
  {
    var _next = $(this).next();
    if (_next.hasClass("form-required"))
    {
      if ($(this).attr("value") != "" && $(this).attr("value") != $(this).attr("default"))
      {
        _next.hide();
      }
      else
      {
        _next.show();
      }
    }
  });
           
  $("a[rel='gallery']").click(function()
  {
    $(".body-page-content-design-case-main > div").show();
    
    //var _src = "/ImageGen.ashx?Image=" + $(this).attr("href") + "&Compression=85&Height=595&Width=595&Pad=True&Format=JPG&Bgcolor=ECEFE8";
    var _src = "/ImageGen.ashx?Image=" + $(this).attr("href") + "&Compression=85&Height=595&Width=595&Crop=resize&Format=JPG&Align=Center&VAlign=Center";
    $(".body-page-content-design-case-main > img").attr("src", _src);
    $(".body-page-content-design-case-main > img").load(function()
    {
      $(".body-page-content-design-case-main > div").hide();
    });
    
    return false;
  });         

});

var UserName = 'CHESS11115';
var Key = 'XF58-TF49-ZA96-GE95';

function PostcodeAnywhere_Interactive_RetrieveByPostcodeAndBuilding_v1_10Begin(Postcode, Building)
{
    if (Building == $(".form-building").attr("default") || Building == "")
    {
       alert("Invalid House Name\\No.!")
       return false;
    }
  
    var scriptTag = document.getElementById("PCA38d38252878f434581f85b249661cd94");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //Build the url
    strUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.10/json.ws?";
    strUrl += "&Key=" + escape(Key);
    strUrl += "&Postcode=" + escape(Postcode);
    strUrl += "&Building=" + escape(Building);
    strUrl += "&UserName=" + escape(UserName);
    strUrl += "&CallbackFunction=PostcodeAnywhere_Interactive_RetrieveByPostcodeAndBuilding_v1_10End";

    //Make the request
    if (scriptTag) 
        {
        try
            {
                headTag.removeChild(scriptTag);
            }
        catch (e)
            {
                //Ignore
            }
        }
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl
    scriptTag.type = "text/javascript";
    scriptTag.id = "PCA38d38252878f434581f85b249661cd94";
    headTag.appendChild(scriptTag);
}

function PostcodeAnywhere_Interactive_RetrieveByPostcodeAndBuilding_v1_10End(response)
{
    //Test for an error
    if (response.length == 1 && typeof(response[0].Error) != 'undefined')
    {
        //Show the error message
        alert(response[0].Description);
    }
    else
    {
        //Check if there were any items found
      if (response.length == 0)
      {
        alert("Sorry, no matching items found");
      }
      else
      {
        $('.form-building').hide();
        $('.body-store-content-events-grotto-details .hidden').show();

        
        if (response[0].Line1 != "")
        {
          $('.form-address1').attr('value', response[0].Line1);
          $('.form-address1').removeClass("default");
        }
        if (response[0].Line2 != "")
        {
          $('.form-address2').attr('value', response[0].Line2);
          $('.form-address2').removeClass("default");
        }
        if (response[0].Line3 != "")
        {
          $('.form-address3').attr('value', response[0].Line3);
          $('.form-address3').removeClass("default");
        }
        if (response[0].PostTown!= "")
        {
          $('.form-posttown').attr('value', response[0].PostTown);
          $('.form-posttown').removeClass("default");
        }
        if (response[0].County!= "")
        {
          $('.form-county').attr('value', response[0].County);
          $('.form-county').removeClass("default");
        }

        $('.form-postcode').attr('value', response[0].Postcode);
        
        //PUT YOUR CODE HERE
                //FYI: The output is an array of key value pairs (e.g. response[0].Udprn), the keys being:
                //Udprn
                //Company
                //Department
                //Line1
                //Line2
                //Line3
                //Line4
                //Line5
                //PostTown
                //County
                //Postcode
                //Mailsort
                //Barcode
                //Type
                //DeliveryPointSuffix
                //SubBuilding
                //BuildingName
                //BuildingNumber
                //PrimaryStreet
                //SecondaryStreet
                //DoubleDependentLocality
                //DependentLocality
                //PoBox
                //CountryName
      }
   }
}
