(function($) { $(function() {
  /**
    * Login, registration and lost password form logic
    */
  var $login            = $('#sidebar #login-form');
  var $lostPassword     = $('#sidebar #lost-password-form');

  if ($login && $lostPassword) {
    // If we're on the 'lost password' page, show the 'lost password' form.
    if (location.href.match(/action=lostpassword/gi)) {
      $login.hide();    
    } else if (location.href.match(/(wp\-login\.php)|(logg\-inn)/gi)) {
      $lostPassword.hide();
    }

    $('#sidebar a.lost-password').click(function() {
      $login.hide();
      $lostPassword.show();
      return false;
    });

    $('#sidebar a.login').click(function() {
      $lostPassword.hide();
      $login.show();
      return false;
    });
  } // if ($login && !$lostPassword)
  
  /**
    * Slides in a modal dialog.
    */  
  var slideDialogIn = function(dialog) {
    dialog.overlay.fadeIn('fast', function () {
  		dialog.container.slideDown('normal', function () {
  			dialog.data.fadeIn('fast');
  		});
  	});
  };
  

  /**
    * Slides out a modal dialog.
    */  
  var slideDialogOut = function(dialog) {
  	dialog.data.fadeOut('fast', function () {
  		dialog.container.slideUp('normal', function () {
  			dialog.overlay.fadeOut('fast', function () {
  				$.modal.close();
  			});
  		});
  	});
  };
  

  /**
    * Initializes the Google Map
    */
  var $map = $('#map');

  if ($map.length > 0) {
    var latitude  = 59.913287;
    var longitude = 10.743682;
    var $vcard    = $('.contact .vcard').clone();
    var map       = new GMap2($map[0]);
    var marker    = new GMarker(new GLatLng(latitude, longitude), {
      title: 'Galligani AS',
      clickable: true,
    });

    map.disableDragging();
    map.setCenter(new GLatLng(latitude + 0.0006, longitude), 16);
    map.addOverlay(marker);
    
    if ($vcard.length > 0) {
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindow($vcard[0]);
      });
    }
  } // Google Map


  /*
   * Product Set Picture logic
   */
  $('.product-set div.picture a').click(function() {
    var $$      = $(this);
    var src     = $$.attr('href');
    var classes = $$.attr('class');
    var width   = classes.match(/w([^\s]+)/i)[1];
    var height  = classes.match(/h([^\s]+)/i)[1];
  
    $.modal('<img src="' + src + '" width="' + width + '" height="' + height + '"/>', {
      onOpen        : slideDialogIn,
      onClose       : slideDialogOut,
      containerCss  : {
        height      : height + 'px',
        width       : width + 'px',
      },
    });

    return false;
  });
  

  /**
    * Wish List table editing logic
    */
  $('#content.wish-list table tr.new td.title input').change(function() {
    var $$ = $(this);
    if ($$.val() != '') {
      var $tbody     = $$.parents('tbody');
      var lastTitle  = $tbody.find('tr:last td.title input').val();
      
      // Only append a new row if the last title is empty
      if (lastTitle != '') {
        var $row      = $$.parents('tr').clone(true).find('td.title input').val('').end();
        $tbody.append($row);
      }
    }
  });
  

  /**
    * Wish List completion explanation visibility toggling
    */
  $('#content.wish-list .complete a.toggle').click(function() {
    $('#content.wish-list .complete .explanation').toggle();
    return false;
  });

  $('#content.wish-list .complete .explanation').hide();

  /**
    * Wish List URI selection logic
    */
  $('#wish-list-meta input.text').click(function() {
    this.select();
  });
});})(jQuery);