(function (window, undefined) {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
  var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
  if (ieversion<=7)
    var browserie = true;
  else
    var browserie = false;
}
window.tabify = function tabify ($tabs, $contents, remember) {
  if((browserie != true) || (browserie == true && $tabs.parents('div').parents('div').attr('id') == 'account_wrapper')) {
    var current_index = -1
    , old_state
    , select = 0;
    $tabs.click(function () {
      var $this = $(this)
      , index = $tabs.index($this);
      if (index !== current_index) {
        $contents.hideByPosition().eq(index).showByPosition();
        /*$contents.hide().eq(index).show();*/
        $tabs.removeClass('active');
        $this.addClass('active');
        current_index = index;
        if (remember) {
          $.cookie('tabify_'+remember, index);
        }
      }
    });
    if (remember) {
      old_state = parseInt($.cookie('tabify_'+remember), 10);
      if (old_state !== null && ! isNaN(old_state) ) {
        select = old_state;
      }
    }
    $tabs.eq(select).click();
  } else {
	$tabs.click(function () {
	  // Set all tabs to inactive
	  $(this).parents('.tabs').children('li').each( function () {
	    $(this).removeClass('active');
	  });
	  // Set the clicked tab to active
	  $(this).addClass('active');
	  var index = $tabs.index($(this));
	  // Get the current height of the tab
	  var the_height = $(this).parents('div').children('.tabContents').children('li').eq(index).find('.widget_wrapper').height();
	  // Get the heighest tabsize an use it as the size to set
	  if(the_height == 0) {
	    $(this).parents('div').children('.tabContents').children('li').each( function () {
	      if($(this).find('.widget_wrapper').height() > the_height) {
	        the_height = $(this).find('.widget_wrapper').height();
	      }
	    });
	  }
	  // Change the height of the shown tab in the height of the heighest height
	  $(this).parents('div').children('.tabContents').children('li').eq(index).find('.widget_wrapper').css('height', the_height+'px');
	  // Hide all tabs
	  $(this).parents('div').children('.tabContents').children('li').each( function () {
	    $(this).hide();
	  });
	  // Show the tab with the index of the clicked tab
	  $(this).parents('div').children('.tabContents').children('li').eq(index).show();
	});
	// This init the tabify. Activate the first tab, show the first tab and hide the second one
	$tabs.eq(0).addClass('active');
	$tabs.parents('div').children('.tabContents').children('li').each( function () {
	  $(this).hide();
	});
	$tabs.parents('div').children('.tabContents').children('li').eq(0).show();
  }
}

  window.popup_show = window.popup_hide = function ($el) {
    alert('old javascript code used!');
  }
  
  var pop = {
    current: null,
    dimmer: $('#dimmer'),
    
    show: function ($el) {
      if ( ! $el || typeof($el.length) === 'undefined' || $el.length === 0) {
        return false;
      }
      pop.hideCurrent();
      pop.dimmer.show();
      
      $el.show();
      pop.current = $el;
      $(document).scrollTop(0);
      return $el;
    },
    
    hideCurrent: function () {
      var cur = pop.current;
      if (cur !== null) {
        cur.hide();
        if (cur.parent().widget()) {
          cur.siblings('a.widget_closer').hide();
        }
        // need this for reloading the Login widget top right after closing the Login popup widget
        if(cur.hasClass('MyLoginWidget_login')){
          window.quick_login.widget().changeView('login');
        }
        pop.current = null;
      }
    },
    
    hide: function () {
      pop.hideCurrent();
      pop.dimmer.hide();
    }
  }
  
  window.popupManager = pop;
  
  //jquery plugin to get the offset of a hidden element
  $.fn.hiddenOffset = function() {
	  var visible = this.is(':visible')
	  , offsets;
	  
	  if ( ! visible)
		  this.show();
  
	  offsets = this.offset();
  
	  if ( ! visible)
		  this.hide();
  
	  return offsets;
  }; 
  
})(window);
