(function ($) {
  Drupal.behaviors.ghcRedirect = {    
    attach: function(context) {
//      $.ajax({
//        url: '/ghc/language_set',
//        dataType: 'json',
//        success: function(data) {
//          if (data.lang=='en'){              
//            window.location="http://www.grandhotel.se/en";
//          }
//        }
//      });

//      if (navigator.appName == 'Netscape') {
//        var language = navigator.language;
//      }
//      else {
//        var language = navigator.browserLanguage;
//      }
//      // Check if the browser uses another language than Swedish
////      if ((language.indexOf('sv') == -1) && (Drupal.settings.pathPrefix != "en/")) { 
//      if (language.indexOf('sv') == -1) { 
//        sv_url = "http://www.grandhotel.se";
//        en_url = "http://www.grandhotel.se/en";
//        chosen_language = $.cookie("grandhotel_language");
//        referrer = $(document)[0].referrer;
//        // If a cookie with language information doesn't exist
//        if (((!chosen_language) || (chosen_language == "en")) && (Drupal.settings.pathPrefix != "en/")) {
//          $.cookie("grandhotel_language", "en");
//          window.location = en_url;
//        }
//        // If referrer is the english site and current window.location is the swedish site
//        else if ((referrer.indexOf(en_url) > -1) && (window.location.href.indexOf(sv_url) > -1)) {
//          $.cookie("grandhotel_language", "sv");
//        }
//        else if ((referrer.indexOf(sv_url) > -1) && (window.location.href.indexOf(en_url) > -1)) {
//          $.cookie("grandhotel_language", "en");
//        }
//      } 


      /*
       *  ============================================================
       *  Code for redirect depending on brownser language
       *  ============================================================
       */

      // URLs to the different languages
      var lang_sv = "http://www.grandhotel.se/";
      var lang_en = "http://www.grandhotel.se/en";

      // Link: english
      $('#top-links .menu-551 a').click(function() {
        $.cookie("ghc_language_set", "en");
      });
      
      // Link: swedish
      $('#top-links .menu-553 a').click(function() {
        $.cookie("ghc_language_set", "sv");
      });
      
      // Get language (if set) and redirect the user to the english site,
      // if english is set as default language
      var language = $.cookie("ghc_language_set");

      // Get brownser language
      if (!language) {
        $.ajax({
          url: '/ghc/language_set',
          dataType: 'json',
          success: function(data) {
            language = data.lang;
            redirect_page();
          }
        });
      }
      else {
        redirect_page();
      }

      // Check if the current URL is different then the URL in 
      function redirect_page() {
        current_location = window.location.href;
        if ((language == "en") && (current_location.indexOf(lang_en) == -1)) {              
          window.location = lang_en;
        }
        // Add other languages here
      }

      /*
       *  ============================================================
       */

    }
  }
})(jQuery);


;

(function($) {

/**
 * Drupal FieldGroup object.
 */
Drupal.FieldGroup = Drupal.FieldGroup || {};
Drupal.FieldGroup.Effects = Drupal.FieldGroup.Effects || {};
Drupal.FieldGroup.groupWithfocus = null;

Drupal.FieldGroup.setGroupWithfocus = function(element) {
  element.css({display: 'block'});
  Drupal.FieldGroup.groupWithfocus = element;
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processFieldset = {
  execute: function (context, settings, type) {
    if (type == 'form') {
      // Add required fields mark to any fieldsets containing required fields
      $('fieldset.fieldset').each(function(i){
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
          $('legend span.fieldset-legend', $(this)).eq(0).append('&nbsp;').append($('.form-required').eq(0).clone());
        }
        if ($('.error', $(this)).length) {
          $('legend span.fieldset-legend', $(this)).eq(0).addClass('error');
          Drupal.FieldGroup.setGroupWithfocus($(this));
        }
      });
    }
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processAccordion = {
  execute: function (context, settings, type) {
    var accordions = $('div.field-group-accordion-wrapper', context).accordion({
      autoHeight: false,
      active: 0,
      collapsible: true
    });
    if (type == 'form') {
      // Add required fields mark to any element containing required fields
      $('div.accordion-item').each(function(i){
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
          $('h3.ui-accordion-header').eq(i).append('&nbsp;').append($('.form-required').eq(0).clone());
        }
        if ($('.error', $(this)).length) {
          $('h3.ui-accordion-header').eq(i).addClass('error');
          var activeOne = $(this).parent().accordion("activate" , i);
          $('.ui-accordion-content-active', activeOne).css({height: 'auto', width: 'auto', display: 'block'});
        }
      });
    }
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processHtabs = {
  execute: function (context, settings, type) {
    if (type == 'form') {
      // Add required fields mark to any element containing required fields
      $('fieldset.horizontal-tabs-pane').each(function(i){
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
          $(this).data('horizontalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after('&nbsp;');
        }
        if ($('.error', $(this)).length) {
          $(this).data('horizontalTab').link.parent().addClass('error');
          Drupal.FieldGroup.setGroupWithfocus($(this));
          $(this).data('horizontalTab').focus();
        }
      });
    }
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 */
Drupal.FieldGroup.Effects.processTabs = {
  execute: function (context, settings, type) {
    if (type == 'form') {
      // Add required fields mark to any fieldsets containing required fields
      $('fieldset.vertical-tabs-pane').each(function(i){
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
          $(this).data('verticalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after('&nbsp;');
        }
        if ($('.error', $(this)).length) {
          $(this).data('verticalTab').link.parent().addClass('error');
          Drupal.FieldGroup.setGroupWithfocus($(this));
          $(this).data('verticalTab').focus();
        }
      });
    }
  }
}

/**
 * Implements Drupal.FieldGroup.processHook().
 * 
 * TODO clean this up meaning check if this is really 
 *      necessary.
 */
Drupal.FieldGroup.Effects.processDiv = {
  execute: function (context, settings, type) {

    $('div.collapsible', context).each(function() {
      var $wrapper = $(this);

      // Turn the legend into a clickable link, but retain span.field-group-format-toggler
      // for CSS positioning.
      var $toggler = $('span.field-group-format-toggler:first', $wrapper);
      var $link = $('<a class="field-group-format-title" href="#"></a>');
      $link.prepend($toggler.contents()).appendTo($toggler);
      
      // .wrapInner() does not retain bound events.
      $link.click(function () {
        var wrapper = $wrapper.get(0);
        // Don't animate multiple times.
        if (!wrapper.animating) {
          wrapper.animating = true;
          var speed = $wrapper.hasClass('speed-fast') ? 300 : 1000;
          if ($wrapper.hasClass('effect-none') && $wrapper.hasClass('speed-none')) {
            $('> .field-group-format-wrapper', wrapper).toggle();
          }
          else if ($wrapper.hasClass('effect-blind')) {
            $('> .field-group-format-wrapper', wrapper).toggle('blind', {}, speed);
          }
          else {
            $('> .field-group-format-wrapper', wrapper).toggle(speed);
          }
          wrapper.animating = false;
        }
        return false;
      });
      
    });
  }
};

/**
 * Behaviors.
 */
Drupal.behaviors.fieldGroup = {
  attach: function (context, settings) {
    if (settings.field_group == undefined) {
      return;
    }
    $('body', context).once('fieldgroup-effects', function () {
      // Execute all of them.
      $.each(Drupal.FieldGroup.Effects, function (func) {
        // We check for a wrapper function in Drupal.field_group as 
        // alternative for dynamic string function calls.
        var type = func.toLowerCase().replace("process", "");
        if (settings.field_group[type] != undefined && $.isFunction(this.execute)) {
          this.execute(context, settings, settings.field_group[type]);
        }
      });
    });
  }
};

})(jQuery);;

