/*
 * adwired.js
 *
 * Copyright (c) 2011 Adwired AG
 *
 */

/* -------------------------------------------------------------------------------------------------------------------------------------------- */

/*
 * variables
 */

/* -------------------------------------------------------------------------------------------------------------------------------------------- */

/*
 * initalize
 */
$(document).ready(function () {
	// variables
	//
	var isFading = false;

	// initialize the rollovers
	//
	if ($('#body #box-main').length > 0) {
		// attach the hover handler
		//
		$('#body .rollover-source').hover(
			function(ev) {
				// is still fading?
				//
				if (isFading) {
					return;
				}

				// toggle fading flag
				//
				isFading = true;

				// save "this"
				//
				var that = $(this);

				// hide the active box
				//
				$('#body DIV.box-active').fadeOut(0, function() {
					// toggle the box
					//
					$(this).removeClass('box-active');
					$('#body #' + that.attr('show')).fadeIn(0, function() {
						// toggle fading flag
						//
						isFading = false;
						$('#body #' + that.attr('show')).addClass('box-active')

						// attach the reset handler
						//
						$('#body #' + that.attr('show')).mouseover(function(ev) {
							// is still fading?
							//
							if (isFading) {
								return;
							}

							// restore the default content
							//
							isFading = true;
							$('#body DIV.box-active').fadeOut(0, function() {
								$('#body #box-main').fadeIn(0, function() {
									isFading = false;
									$('#body #box-main').addClass('box-active');
								});
							});

							// return
							//
							return false;
						});
					});
				});

				// add the hover to the first paragraph
				//
				$('P:first', this).addClass('hover');

				// return
				//
				return false;
			},

			function(ev) {
				// remove the hover from the first paragraph
				//
				$('P:first', this).removeClass('hover');

				// return
				//
				return false;
			}
		);
	}

	// resize white-box-1
	//
	var whiteBoxMaxHeight = 0;
	$('.white-box-1').each(function() {
		if ($(this).height() > whiteBoxMaxHeight) {
			whiteBoxMaxHeight = $(this).height();
		}
	});
	$('.white-box-1:not(.no-auto-height)').height(whiteBoxMaxHeight);

	// resize white-box-1-2
	//
	var whiteBoxMaxHeight = 0;
	$('.white-box-1-2').each(function() {
		if ($(this).height() > whiteBoxMaxHeight) {
			whiteBoxMaxHeight = $(this).height();
		}
	});
	$('.white-box-1-2').height(whiteBoxMaxHeight);

	// relocate news in the right column
	//
	if (($('.white-box').length > 0) && ($('.grid_right').length > 0)) {
		var posWB = $('.white-box').offset();
		var posGR = $('.grid_right').offset();
		$('.grid_right').offset({ top: posWB.top, left: posGR.left - 150 });
	}

	// replace mailme spans
	//
	$('SPAN.mailme').mailme();

	// add the input field watermarks
	//
	$('#input-username').watermark('Kundenlogin', {className: 'watermark' });
	$('#input-password').watermark('Passwort', {className: 'watermark' });

	// initialize the tabs
	//
	if ($('.jq-tabs').length > 0) {
		$('.jq-tabs').tabs();
	}

	// initialize link-boxes
	//
	if ($('DIV.link-box').length > 0) {
		$('DIV.link-box').click(function(ev) {
			// stop event propagation
			//
			ev.stopImmediatePropagation();

			// get the base & href from the link inside
			//
			var baseHref	= $('BASE').attr('href');
			var targetLink	= $('A', this).first().attr('href');

			// redirect the client
			//
			window.location = baseHref + targetLink;

			// return
			//
			return false;
		});
	}

	// initialize prettyphoto
	//
	if ($("a[rel^='prettyPhoto']").length > 0) {
		$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal',
			padding: 40,
			opacity: 0.5,
			showTitle: false,
			allowresize: false,
			theme: 'light_rounded'
		});
	}

	// handle login
	//
	$('FORM.login INPUT.button').click(function(ev) {
		// stop event propagation
		//
		ev.stopImmediatePropagation();

		// alert
		//
		alert('Login fehlgeschlagen! Bitte überprüfen Sie Benutzername und/oder Passwort.');

		// return
		//
		return false;
	});
});

/* -------------------------------------------------------------------------------------------------------------------------------------------- */

/*
 * generic methods
 */

/* -------------------------------------------------------------------------------------------------------------------------------------------- */
