/*----------------------------------------------------------------------------*
 |                                                                            |
 |  Javascript for http://www.thecromartyhall.com/                            |
 |                                                                            |
 |  Copyright (c) 2007-2009 Tom Levens.                                       |
 |                                                                            |
 *----------------------------------------------------------------------------*/

var url = 'http://www.thecromartyhall.com';

function hideError(){
  $(".error").hide('slow', function () {
    $(this).css({display: 'none'});
  });
}

$(document).ready(function () {

  var int = self.setTimeout('hideError()', 10000);

  /*--------------------------------------------------------------------------*
   |  Fancy login link.                                                       |
   *--------------------------------------------------------------------------*/
  $("#login").css({opacity: '0'}).addClass('fancy').mouseover(function () {
    $(this).stop().animate({opacity: '1'}, 'normal');
  }).mouseout(function () {
    $(this).stop().animate({opacity: '0'}, 'slow');
  });

  /*--------------------------------------------------------------------------*
   |  Disappearing default text in e-mail box.                                |
   *--------------------------------------------------------------------------*/
  var text = $("#mailinglist #email").attr('value');

  $("#mailinglist #email").focus(function () {
    if ($(this).attr('value') == text) {
      $(this).attr('value', '');
    }
  });

  $("#mailinglist #email").blur(function () {
    if ($(this).attr('value') == '') {
      $(this).attr('value', text);
    }
  });

  /*--------------------------------------------------------------------------*
   |  Lightbox.                                                               |
   *--------------------------------------------------------------------------*/
  $("a[rel='lightbox']").each(function () {
    var href = $(this).attr('href');
    var desc = $(this).attr('title');

    $(this).click(function () {
      $("body").append('<div id="lightbox-container"><div id="lightbox-blind"></div><div id="lightbox-outer"><div id="lightbox-middle"><div id="lightbox-inner"><img src="' + url + '/images/lightbox-load.gif" id="lightbox-load" alt="" /><div id="lightbox"><div id="lightbox-close"></div><div id="lightbox-close-text">Click to close&hellip;</div><div id="lightbox-img"></div></div></div></div></div></div>');

      $("#login").hide();

      $("#lightbox-container").fadeIn('normal');
      $("#lightbox-close-text").css({opacity: '0'});

      $("#lightbox-close").mouseover(function () {
        $("#lightbox-close-text").stop().animate({opacity: '1'}, 'normal');
      }).mouseout(function () {
        $("#lightbox-close-text").stop().animate({opacity: '0'}, 'slow');
      });

      $("#lightbox-outer,#lightbox-close").click(function () {
        $("#lightbox-container").fadeOut('normal', function () {
          $(this).remove();
          $("#login").show();
        });
      });

      $(window).keydown(function (event)  {
        if (event.keyCode == 27) {
          $("#lightbox-container").fadeOut('normal', function () {
            $(this).remove();
            $("#login").show();
            $(window).keydown(function () {});
          });
        }
      });

      $("#lightbox").click(function () {
        return false;
      });

      var img = new Image();

      $(img).load(function () {
        $("#lightbox-img").html(this);
        $("#lightbox-load").hide();
        $("#lightbox").fadeIn('fast', function () {
          if (desc) {
            $("#lightbox").append('<div id="lightbox-caption" style="display: none;"><div id="lightbox-previous"><a href="#">&lsaquo;</a></div><div id="lightbox-next"><a href="#">&rsaquo;</a></div>' + desc + '</div>');
            $("#lightbox-caption").slideDown('fast');
          }
        });
      }).attr({src: href, alt: desc});

      return false;
    });
  });
  /*--------------------------------------------------------------------------*
   |  Google maps.                                                            |
   *--------------------------------------------------------------------------*/
  $(window).load(function () {
    if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
      var zoom = 12;

      var icon = new GIcon();
      icon.image = url + '/images/map-marker.png';
      icon.shadow = url + '/images/map-marker-shadow.png';
      icon.iconSize = new GSize(12, 20);
      icon.shadowSize = new GSize(22, 20);
      icon.iconAnchor = new GPoint(6, 20);
      icon.infoWindowAnchor = new GPoint(5, 1);

      map.addControl(new GSmallMapControl());
      map.setCenter(new GLatLng(58.82562, -2.96078), 12);
      map.addOverlay(new GMarker(new GLatLng(58.8255953946222, -2.96087980270386), icon));

      GEvent.addListener(map, 'click', function (marker, point) {
        if (marker) {
          if(map.getZoom() != 16) {
            zoom = map.getZoom();
            map.setCenter(new GLatLng(58.8255953946222, -2.96087980270386), 16);
          }
          else {
            map.setCenter(new GLatLng(58.8255953946222, -2.96087980270386), zoom);
          }
        }
      });
    }
    else {
      $("#map").replaceWith('<p class="center">Google Maps is not compatible with your browser.</p>');
    }
  }).unload(function () {
    GUnload();
  });

});

/*----------------------------------------------------------------------------*
 |  Uploads window.                                                           |
 *----------------------------------------------------------------------------*/
function launchUploads(mode) {
  var uploads = window.open(url + '/uploads/' + mode + '/', 'uploads', 'width=700,height=500,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,screenX=50,screenY=50,left=50,top=50');
  uploads.focus();
}

/*----------------------------------------------------------------------------*
 |  Functions for inserting text at caret position.                           |
 *----------------------------------------------------------------------------*/
function htmlEntities(text) {
   return text.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
   //.replace(/'/g,'\\'+'\'')
};

function imgInsert(img, form_name, text_name) {
  insert_text('<img src="' + url + '/image/' + img + '" alt="" />', form_name, text_name);
}

function imgEmailInsert(img, form_name, text_name) {
  insert_text('<image:' + img + '>', form_name, text_name);
}

function imgThumb(img, form_name, text_name) {
  var size = prompt('Please enter the maximum dimension of the thumbnail in pixels:', '640');
  insert_text('<img src="' + url + '/thumb/' + size + '/' + img + '" alt="" />', form_name, text_name);
}

function imgLightbox(img, form_name, text_name) {
  var size = prompt('Please enter the maximum dimension of the image in pixels:', '320');
  var alt = prompt('Please enter a caption for this image:');
  insert_text('<a href="' + url + '/thumb/640/' + img + '" title="' + htmlEntities(alt) + '" rel="lightbox"><img src="' + url + '/thumb/' + size + '/' + img + '" style="border: 1px solid #000;" alt="' + htmlEntities(alt) + '" /></a>', form_name, text_name);
}


// The following functions are adapted from phpBB 3:
// http://www.phpbb.com/
function insert_text(text, form_name, text_name)
{
  var textarea;

  textarea = opener.document.forms[form_name].elements[text_name];

  if (!isNaN(textarea.selectionStart))
  {
    var sel_start = textarea.selectionStart;
    var sel_end = textarea.selectionEnd;

    mozWrap(textarea, text, '')
    textarea.selectionStart = sel_start + text.length;
    textarea.selectionEnd = sel_end + text.length;
  }
  else if (textarea.createTextRange && textarea.caretPos)
  {
    if (baseHeight != textarea.caretPos.boundingHeight)
    {
      textarea.focus();
      storeCaret(textarea);
    }

    var caret_pos = textarea.caretPos;
    caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
  }
  else
  {
    textarea.value = textarea.value + text;
  }
  if (!popup)
  {
    textarea.focus();
  }
}

// http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
  var selLength = txtarea.textLength;
  var selStart = txtarea.selectionStart;
  var selEnd = txtarea.selectionEnd;
  var scrollTop = txtarea.scrollTop;

  if (selEnd == 1 || selEnd == 2)
  {
    selEnd = selLength;
  }

  var s1 = (txtarea.value).substring(0,selStart);
  var s2 = (txtarea.value).substring(selStart, selEnd)
  var s3 = (txtarea.value).substring(selEnd, selLength);

  txtarea.value = s1 + open + s2 + close + s3;
  txtarea.selectionStart = selEnd + open.length + close.length;
  txtarea.selectionEnd = txtarea.selectionStart;
  txtarea.focus();
  txtarea.scrollTop = scrollTop;

  return;
}

// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl)
{
  if (textEl.createTextRange)
  {
    textEl.caretPos = document.selection.createRange().duplicate();
  }
}

/*EOF*/
