if('undefined' == typeof(GI)) {
  var GI = function(){};
}
GI.GiftFinder = function(finderId,buttonId,options){
  /**
   * Reference to GI.GiftFinder
   */
  var self = this;
  
  /**
   * The finder div element
   */
  var _finder = null;
  
  /**
   * The opener element
   */
  var _opener = null;
  
  /**
   * mutexes
   */
  var _blocks = {open: false, close: false, all: false};

  /**
   * General options
   */
  var _options = {
    buttonOn: null,
    buttonOff: null,
    buttonHoverOn: null,
    buttonHoverOff: null,
    placeElement: null
  };

  /**
   * Place the giftfinder
   */
  var _placeFinder = function(){
    if(!self._finder.style.position){
      self._finder.style.position = 'absolute';
    }
    var _placeElement = null;
    if(null != _options.placeElement){
      if('string' == typeof(_options.placeElement)){
        _placeElement = document.getElementById(_options.placeElement);
      }
      else if('object' == typeof(_options.placeElement)){
        _placeElement = _options.placeElement;
      }
      else {
        _placeElement = self._opener;
      }
    }
    else {
      _placeElement = self._opener;
    }
    self._finder.style.left = GI.Element().getPosX(_placeElement) + 1 + 'px';
    self._finder.style.top = GI.Element().getPosY(_placeElement) + self._opener.offsetHeight + 10 + 'px';
    
    return self;
  };

  /**
   * Open the giftfinder
   */
  var _openFinder = function(){
    if( _blocks.all){
      return self;
    }
    _blocks.all = true;
    _placeFinder();
    new GI.Element.SlideOpen(self._finder,{
      duration: 0.3, interval: 10,
      onComplete: function(){
        _blocks.all = false;
        /*
        if('string' == typeof(_options.buttonOff)){
          self._opener.firstChild.setAttribute('src',_options.buttonOff);
        }
        if('string' == typeof(_options.buttonHoverOff)){
          self._opener.onmouseover = function(){
             self._opener.firstChild.setAttribute('src',_options.buttonHoverOff);
          };
          self._opener.onmouseout = function(){
             self._opener.firstChild.setAttribute('src',_options.buttonOff)
          };
        }
        */
        self._opener.onclick = function(){
          _closeFinder();
        };
      }
    });
    return self;
  };

  /**
   * Close the giftfinder
   */
  var _closeFinder = function(){
    if(_blocks.all){
      return self;
    }
    _blocks.all = true;
    new GI.Element.SlideClose(self._finder,{
      duration: 0.3, interval: 10,
      onComplete: function(){
        _blocks.all = false;
        /*
        if('string' == typeof(_options.buttonOn)){
          self._opener.firstChild.setAttribute('src',_options.buttonOn);
        };
        if('string' == typeof(_options.buttonHoverOn)){
          self._opener.onmouseover = function(){
             self._opener.firstChild.setAttribute('src',_options.buttonHoverOn);
          };
          self._opener.onmouseout = function(){
             self._opener.firstChild.setAttribute('src',_options.buttonOn);
          };
        }
        */
        self._opener.onclick = function(){
          _openFinder();
        };
      }
    });
    return self;
  };
  
  /**
   * Opens the finder
   */
  this.show = function(){
    _openFinder();
    return self;
  };
  
  /**
   * Closes the finder
   */
  this.hide = function(){
    _closeFinder();
    return self;
  };

  /**
   * Constructor
   *
   * @param string|object  finderId    the (id of the) giftfinder element
   * @param string|object  buttonId    the (id of the) open/close (-button) element
   * @param object         options     (optional) options
   */
  var _init = function(finderId,buttonId,options){
    if('string' == typeof(finderId)){
      self._finder = document.getElementById(finderId);
    }
    else if( 'object' == typeof(finderId)){
      self._finder = finderId;
    }
    if( null == self._finder ){
      throw 'Need a valid giftfinder element';
    }
    if('string' == typeof(buttonId)){
      self._opener = document.getElementById(buttonId);
    }
    else if( 'object' == typeof(buttonId)){
      self._opener = buttonId;
    }
    if( null == self._opener){
      throw 'Need a valid opener element';
    }
    if('object' == typeof(options)){
      for(var property in options){
        _options[property] = options[property];
      }
    }
    if(null == _options.buttonOn || 'undefined' == typeof(_options.buttonOn)){
      _options.buttonOn = self._opener.getAttribute('src');
    }
    self._opener.onclick = function(){
      _openFinder();
    };

    return self;
  };

  _init(finderId,buttonId,options);
  return this;
};

function checkSubmitButton(elem,buttonId) {
  if( !elem || !elem.form ) return;
  var felem = elem.form;
  if( !felem.elements['event'] || !felem.elements['receiver'] || !felem.elements['price'] ) return;
  var _button = document.getElementById(buttonId);
  if( !_button ) return;
  var _chosen = 0;
  if( '' != felem.elements['event'].value ) _chosen += 1;
  if( '' != felem.elements['receiver'].value ) _chosen += 1;
  if( '' != felem.elements['price'].value ) _chosen += 1;
  if( 1 <= _chosen ) {
    _button.disabled = false;
  }
  else {
    _button.disabled = true;
  }
}

function checkSubmitButtons(formId,buttonId) {
  var _form = document.getElementById(formId);
  if(_form) {
    checkSubmitButton(_form.elements[0],buttonId);
  }
}

function switchStyle( elem )
{
  if( elem )
  {
    activeClass   = elem.getAttribute( 'class' );
    if( 'formfull' == activeClass )
    {
      elem.setAttribute( 'class', 'formactive' );
    }
    else
    {
      elem.setAttribute( 'class', 'formfull' );
    }
  }
}

function giftFinderIndexPage()
{
	document.getElementById('giftFinderFlipLink')
		.style.marginRight = '0px';
  GI.Loader().addOnLoadEvent(
  function(){
    if(GI().isIE()){
      new GI.GiftFinder('GiftFinderLayerDyn','giftFinderFlipLink',
            {
              buttonOff: '/de/images/giftfinder/up_normal.jpg',
              buttonHoverOff: '/de/images/giftfinder/up_over.jpg',
              buttonHoverOn: '/de/images/giftfinder/down_over.jpg'
            }).show();
    }
    else {
      GiftFinder.show();
    }
    checkSubmitButton();
  });
}
