/* Modified: 10/8/2008 by bperry
 * -Version 1.0 - incorporated keyboard support with additional ability to adapt show left/right dropdowns
 * -TopNav Dropdown was modified from the DickerFisch system and adapted for jquery version 1.2.6
 * -Incorporated updated Dimensioning from jquery Core libraries
 *
 * @author 	Alexander Farkas <a.farkas@pfirsichmelba.de> // http://pfirsichmelba.de 
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * @version 0.5
 * @param {String} jQueryselector CSS-selector of the wrapping element, id-selector = beeter performance
 * @param {options} [_settings] list of sveral options/settings
 * 
 * @option {Number|Array} [NavTimeout] delay to hide menu, if ypu use an array of two numbers, first is the delay for firstsubmenu, second for all other submenus, default is 250
 * @option {String} [ListType] listtype i.e. ol (default = ul)
 * @option {Number} [LimitLinks] 1 = (default) .active and .activepath submenus are available for keyboard users, Opera browser overrirdes this (because of spatial navigation),0 = all submenus are available for keyboard users. if you use this you should remove display:none for submenus -> better consistency for screenreader (the submenus stay hidden / are out of viewport), 2 = .active and .activepath submenus are available for keyboard users (Opera browser doesn´t override this)
*/
function DickerFisch(jQueryselector,_settings) {
	//config start
	//classnames: Styles
	var _DF_hoverclass = 'over', _DF_parent_class = 'parent',jQueryofocusstyle = 'ofocus';
	//Array 0 = ancestors of active li-category | 1 = active li category
	var _DF_ActiveCatnAncestorsofIT = new Array(".activepath", ".active");
	var _jsreadyclass = 'navfxenabled';
	var  _DF_hideclass = 'DF_hidelis', _waitingnavclass = 'DF_waitlis';
	// effect css-class: visible class value and hidden class value see/same in CleanCSSAnimation/unMakeInlineCSS, will be removed after animation
	var jQuerynodisClass = "displaynone",jQuerydisClass = "displayblock";
	//config end
	var _DF_obj = this;
	//optional parameter
	var _settings = _settings || this;
	this.NavTimeout = _settings.NavTimeout || 250;
	this.ListType = _settings.ListType || 'ul';
	this.Effect = (_settings.Effect) ? _settings.Effect : 0;
	this.ShowEffect = _settings.ShowEffect || 'show';
	this.HideEffect = _settings.HideEffect || 'hide';
	this.EffectSpeed = _settings.EffectSpeed || 300;
	this.FitIfViewportSmaller = _settings.FitIfViewportSmaller || 1111;
	this.Fit2ViewportClass = _settings.Fit2ViewportClass || false;
	this.LimitLinks = (typeof _settings.LimitLinks == "number") ? _settings.LimitLinks : 1;
	_DF_obj.LimitLinks = (jQuery.browser.opera && _DF_obj.LimitLinks == "1") ? 0 : _DF_obj.LimitLinks;
	this._DF_NavTimeID;
	//methods
	this._fastpullout = function(_eventobj){
		var jQueryfastpullreturn = false;
		if(typeof jQuery(jQueryselector+' li.'+_DF_hoverclass).get(0) == "undefined")
			jQueryfastpullreturn = true;
		else {
			jQueryfastpullreturn = true;
			jQuery(_eventobj).siblings("li").each(function(i){
				if(jQuery(this).is('.'+_DF_hoverclass)){
					jQueryfastpullreturn = false;
					return false; //=break
				}
			});
		}
		return jQueryfastpullreturn;
	};
	this.jQueryoldtimer = 0;
	this.jQueryfastpullin = function(_eventobj){
		var jQueryaktimer = (typeof _DF_obj.NavTimeout != "number") ? _DF_obj.NavTimeout[1] : _DF_obj.NavTimeout;
		if(typeof _DF_obj.NavTimeout != "number" && jQuery(_eventobj).parents('li').length == "0" && !jQuery(_eventobj).children(_DF_obj.ListType).children('li').is('.'+_DF_hoverclass))
			jQueryaktimer = _DF_obj.NavTimeout[0];
		if(jQuery(_eventobj).is('.'+_DF_hoverclass) || _DF_obj.jQueryoldtimer != jQueryaktimer){
			window.clearTimeout(_DF_obj._DF_NavTimeID);
			_DF_obj._DF_NavTimeID = window.setTimeout(_DF_obj._hide,jQueryaktimer);
		}
		_DF_obj.jQueryoldtimer = jQueryaktimer;
	};
	this.jQuerynavstatus = function(_eventobj,jQueryaction){
		if(jQueryaction == "show"){
			jQuery(_eventobj).addClass(_waitingnavclass).removeClass(_DF_hideclass);
			if(_DF_obj._fastpullout(_eventobj))
				_DF_obj.jQueryshow();
		}else if(jQueryaction == "hide")
			jQuery(_eventobj).removeClass(_waitingnavclass).addClass(_DF_hideclass);
		_DF_obj.jQueryfastpullin(_eventobj,'pullin');
	};	
	this.jQueryshow = function(){
		_showobj = jQueryselector+' li.'+_waitingnavclass;
		if(_DF_obj.Effect == 1 || _DF_obj.Effect == 2)
			jQuery(_showobj+':not(.'+_DF_hoverclass+')>'+_DF_obj.ListType).addClass(jQuerynodisClass).CleanCSSAnimation(_DF_obj.ShowEffect,_DF_obj.EffectSpeed,function(){
   				jQuery(this).removeClass(jQuerydisClass);
 			});
		jQuery(_showobj).removeClass(_waitingnavclass).addClass(_DF_hoverclass);
	};
	this._hide = function(){
		jQueryhideobj = jQueryselector+' li.'+_DF_hideclass;
		if(_DF_obj.Effect == 1 || _DF_obj.Effect == 3)
			jQuery(jQueryhideobj).removeClass(_DF_hideclass).children(_DF_obj.ListType).CleanCSSAnimation(_DF_obj.HideEffect,_DF_obj.EffectSpeed,function(){
   				jQuery(this).removeClass(jQuerynodisClass).parent('li').removeClass(_DF_hoverclass);
				_DF_obj.jQueryshow();
 			});
		else{
			jQuery(jQueryhideobj).removeClass(_DF_hoverclass+' '+_DF_hideclass);
			_DF_obj.jQueryshow();
		}
	};
	this.FitToScreen = function(jQueryliobj){
		var jQuerysubpos = jQuery(jQueryliobj).addClass(_DF_hoverclass).children(_DF_obj.ListType).css('visibility','hidden').offset({scroll:false})['left'] + jQuery(jQueryliobj).children(_DF_obj.ListType).outerWidth();
		if(_viewport < jQuerysubpos)
			jQuery(jQueryliobj).children(_DF_obj.ListType).addClass(_DF_obj.Fit2ViewportClass);
	};
	if(_DF_obj.Fit2ViewportClass){
		var _viewport = jQuery(document).width();
		_DF_obj.Fit2ViewportClass = (_viewport < _DF_obj.FitIfViewportSmaller) ? _DF_obj.Fit2ViewportClass : false;
	}
	//Init
	jQuery(jQueryselector+">"+_DF_obj.ListType).addClass(_jsreadyclass).find("li:has("+_DF_obj.ListType+")").each(function(){
		//Mouse Init
		jQuery(this).hoverIntent(function() { 
				_DF_obj.jQuerynavstatus(this,'show');
			}, function() { 
				_DF_obj.jQuerynavstatus(this,'hide');
			}
		).addClass(_DF_parent_class).find('a').focus(function() {
			jQuery(this).addClass(jQueryofocusstyle);
			if(_DF_obj.LimitLinks < 1)
				jQuery(this).parents('li').addClass(_DF_hoverclass);	
			else
				jQuery(this).parents('li').filter(_DF_ActiveCatnAncestorsofIT[0]+','+_DF_ActiveCatnAncestorsofIT[1]).addClass(_DF_hoverclass);
		} ).blur(function() { 
			jQuery(this).removeClass(jQueryofocusstyle).parents("li").removeClass(_DF_hoverclass);
		});	
		if(_DF_obj.Fit2ViewportClass)
			_DF_obj.FitToScreen(this);
	});
	if(_DF_obj.Fit2ViewportClass)
		jQuery(jQueryselector+" li:has("+_DF_obj.ListType+")").removeClass(_DF_hoverclass).children(_DF_obj.ListType).css('visibility','');
};

function checkPulldownDirection(object) {
  // Determine whether to show the menu to the left or to the right
  // Get width of Pulldown
  // Get distance from menu.left to offset + width (right edge) of container
  // If distance > width, switch from left edge to right edge  
  var pulldownWidth = 0;
  var containerWidth = 0;
  var myWidth = 0;
  var leftPosition = 0;
  var distance = 0;
  //alert(jQuery(object).html());
  pulldownWidth = jQuery(object).children("ul").outerWidth();
  var offset = jQuery(object).offset();
  myWidth = jQuery(object).outerWidth();
  leftPosition = offset.left;
  containerWidth = jQuery(object).parent().outerWidth();
  var parentOffset = jQuery(object).parent().offset();
  var distance = (parentOffset.left + containerWidth) - leftPosition;
  //console.log("pulldownWidth: " + pulldownWidth);  
  //console.log("leftPosition: " + leftPosition);
  if (distance < pulldownWidth) {
    var newOffset = 0;
    newOffset = leftPosition - (pulldownWidth - myWidth);
    jQuery(object).children("ul").css({ left: newOffset});
  } else {
    jQuery(object).children("ul").css({ left: leftPosition});
  }
}

// Client-side redirect function
function GtL(s){top.location.href=s;}

//This was added for web studio problem with
function GtL_S(s){location.href=s;}

