var Engine = {
  detect: function() {
    var UA = navigator.userAgent;
    this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
    this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
    this.isOpera = /Opera/.test(UA);
    this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
    this.isMSIE6 = this.isMSIE && (/MSIE 6\./.test(UA) && !this.isOpera);
    this.isMSIE7 = this.isMSIE && (/MSIE 7\./.test(UA) && !this.isOpera);
  }
}
Engine.detect();


ScrollManager = Class.create();
ScrollManager.prototype =
{
	timer : null,
	effect : null,
	initialize: function()
	{
		if (Engine.isMSIE6 || Engine.isMSIE7)
		{
			Position.prepare();
		  if (Position.deltaY > 0)
		  {
		  	this.scrollHandler();
		  }
			Event.observe(window, 'scroll', this.scrollHandler.bind(this), false);
		}
	},
		
	scrollHandler : function(arg)
	{	
		if (this.effect != null)
		{
			this.effect.cancel();
		}
		$('switchToBar').setStyle({top: 0 });
		
		if (this.timer != null)
		{
			clearInterval(this.timer);
		}
		this.timer = setInterval(this.scrollEnd.bind(this), 500);
	},
	
	scrollEnd : function()
	{
		clearInterval(this.timer);
		
		Position.prepare();
		
	  if (Position.deltaY > 0)
	  {
			$('switchToBar').setStyle({top: Position.deltaY - 29 });
			this.effect = new Effect.Move('switchToBar', {y: Position.deltaY, mode: 'absolute', duration: 0.3});
		}
	}
}

function addEvent( obj, type, fn )
{
   if (obj.addEventListener) {
      obj.addEventListener( type, fn, false );
   } else if (obj.attachEvent) {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
      obj.attachEvent( "on"+type, obj[type+fn] );
   }
}

function removeEvent( obj, type, fn )
{
   if (obj.removeEventListener) {
      obj.removeEventListener( type, fn, false );
   } else if (obj.detachEvent) {
      obj.detachEvent( "on"+type, obj[type+fn] );
      obj[type+fn] = null;
      obj["e"+type+fn] = null;
   }
}

HandleTiny = Class.create();
HandleTiny.prototype =
{
	initialize: function(e)
	{
	},
	
	changeTinyBgColor: function()
	{
		if($('userBoxColor')) // nur wenn das Select vorhanden = Userportal Settings Seite
		{
			var id = document.UserportalDaten.userBoxColor.options[document.UserportalDaten.userBoxColor.selectedIndex].value;
			
			var pars = 'action=changeTinyBgColor&colorId='+id;
			new Ajax.Request(
					'/ajax/userportal.php',
					{ 
						method: 'post',
						parameters: pars,
						asynchronous:true, 
						onSuccess: this.doChangeTinyBgColor.bind(this)
					});
		}
	},
	
	doChangeTinyBgColor: function(response)
	{
		tinyMCE.getInstanceById('editor').getWin().document.body.style.backgroundColor=response.responseText;
	},

	handleFailure: function(response)
	{
		
	}
}
var objHandleTiny = new HandleTiny();

/* Hide Infos
*  Used for Starthilfe and public Infos
*/
HandleInfos = Class.create();
HandleInfos.prototype =
{
	initialize: function(e)
	{
	},
	
	hideInfo: function(infoId)
	{
		var pars = 'infoId='+infoId;
		new Ajax.Request(
				'/ajax/hideInfo.php',
				{ 
					method: 'post',
					parameters: pars,
					asynchronous:true, 
					onSuccess: this.doHideInfo.bind(this),
					onFailure: this.handleFailure.bind(this)
				});
	},
	
	hideStartBox: function()
	{
		var pars = 'startBox=1';
		new Ajax.Request(
				'/ajax/hideInfo.php',
				{ 
					method: 'post',
					parameters: pars,
					asynchronous:true, 
					onSuccess: this.doHideInfo.bind(this),
					onFailure: this.handleFailure.bind(this)
				});
	},
	
	doHideInfo: function(response)
	{
		if(response.responseText == '-1')
		{
			new Effect.Fade('startBox');
		}
		else if(response.responseText != '0')
		{
			new Effect.Fade('info_'+response.responseText);
		}
	},

	handleFailure: function(response)
	{
		
	}
}
var objHandleInfos = new HandleInfos();


/*
*	Hide & Show CSS Items 
*	f.e. via Mouseover & Mouseout
*	Usage: showItem(itemId1,itemId2,itemId3)
*/
function showItem(cssId)
{
	cssId = cssId.split(",");
	if($(cssId[0]) == null)
	{
		return false;
	}
	
	if(cssId.length > 1) 
	{
       for (var i = 0; i < cssId.length; i++) 
	   {
          $(cssId[i]).removeClassName('hide');
		  $(cssId[i]).addClassName('show');
       }
    }
	else
	{
		$(cssId[0]).removeClassName('hide');
		$(cssId[0]).addClassName('show');
	}
	
}

function hideItem(cssId)
{
	cssId = cssId.split(",");
	if($(cssId[0]) == null)
	{
		return false;
	}
	
	if(cssId.length > 1) 
	{
       for (var i = 0; i < cssId.length; i++) 
	   {
          $(cssId[i]).removeClassName('show');
		  $(cssId[i]).addClassName = 'hide';
       }
    }
	else
	{
		$(cssId[0]).removeClassName('show');
		$(cssId[0]).addClassName('hide');
	}
}
	