/**
* behaviours.js: Ein Modul um das Verhalten verschiedener 
* Seitenelemente zu verändern
*
**/
var Behaviours = {
	isIE6: false,
	init: function(isIE6) {
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
  		this.isIE6 = true;
		};
	}
};

/**
* Behaviours.getisIE6: getter für isIE6
* 
**/
Behaviours.getisIE6 = function() {
	return this.isIE6
};

/**
* Behaviours.searchFocus: eine Funktion um das Aussehen des
* Sucheingabefeldes zu verändern
*
* bei onfocus wird der vordefinierte text gelöscht und der
* Cursor springt an die Anfangsposition des Feldes
* verliert das Feld den Focus wird der Rahmen wieder schwächer
* und der vordefinierte Text wird wieder eingefügt
**/
Behaviours.searchFocus = function() {
	$('input.betSearchQuery').bind("focusin focusout", function(event) {
		if (event.type == 'focusin') {
			$(this).parent().addClass('hasFocus');
			// $(this).val('');
		}
		if (event.type == 'focusout') {
			$(this).parent().removeClass('hasFocus').val('Suche Sportwetten');
			// $(this).val('Suche Sportwetten');
		}
	});
};

/**
* Behaviours.searchFocus: eine Funktion um das Aussehen des
* Texteingabefeldes zu verändern
*
**/
Behaviours.textInputFocus = function() {
	$('table input.inputtext, table input.address').bind("focusin focusout", function(event) {
		$parentRow = $(this).closest('.subTable').closest('tr');			
		$row = $(this).closest('tr');																																									
		if (event.type == 'focusin') {
			$row.addClass('hasFocus');
			$parentRow.addClass('hasFocus');
			/* $row.find('div.validationBoxContainer').css('z-index', '1000'); */
			$row.find('div.textInputContainer').css('z-index', '1000');
			/* $(this).closest('tr').addClass('hasFocus');
			$(this).closest('div').css('z-index', '1000'); */
		}
		if (event.type == 'focusout') {
			$row.removeClass('hasFocus');
			$parentRow.removeClass('hasFocus');
			/* $(this).closest('tr').removeClass('hasFocus'); */
			$row.find('div.textInputContainer').css('z-index', '1');
		}
	});
	
	$('table select').bind("focusin focusout", function(event) {
		if (event.type == 'focusin') {
			$(this).closest('tr').addClass('hasFocus');
		}
		if (event.type == 'focusout') {
			$(this).closest('tr').removeClass('hasFocus');
		}
	});
};


/**
* Behaviours.searchFocus: eine Funktion um das Aussehen des
* Sucheingabefeldes zu verändern
*
* bei onfocus wird der vordefinierte text gelöscht und der
* Cursor springt an die Anfangsposition des Feldes
* verliert das Feld den Focus wird der Rahmen wieder schwächer
* und der vordefinierte Text wird wieder eingefügt
**/
Behaviours.inputFocus = function() {
	$('#betSearchQuery').bind("focusin focusout", function(event) {
		if (event.type == 'focusin') {
			$(this).parent().addClass('hasFocus');
			$(this).val('');
		}
		if (event.type == 'focusout') {
			$(this).parent().removeClass('hasFocus').val('Suche Sportwetten');
			$(this).val('Suche Sportwetten');
		}
	});
}


/**
* Behaviours.tootlip.js: Eine Funktion zur Anzeige des tooltips
*
* bei mouseover über ein Element mit der Klasse tooltipAnchor
* wird das dazugehörige tooltip Element angezeigt und an den
* Mauszeiger angedockt
**/
Behaviours.tooltip = function() {
	// if no tooltip element exists -> return
	if (!$('.tooltipAnchor').length) {
		return;
	}
	 
	// body width and height without margin
	var $bW = 0;
	var $bH = 0;
	 
	// calculate the new body size on resize and load
	$bW = $('body').outerWidth();  // the width of the body
	$bH = $('body').outerHeight(); // the height of the body
		
	$('.tooltipAnchor').bind('mouseover mouseout mousemove click', function(event) {
		var $ttO = 'right'; 					// orientation of the tooltip (left | right | top | bottom)
		
		var $link = $(event.target).closest('a');
		// if link exists reset the title attribute
		if ($link.length) {
		 var link = $link[0];
		 link.title = '';
		}
		
		if (event.type == 'mouseover' || event.type == 'mousemove') {      
			// get the actual tooltip
			$tooltip = $(event.target).find('span.tooltipWrap');
			var $ttW = $tooltip.outerWidth();
			var $ttH = $tooltip.outerHeight();
			 
			// get the mouse coordinates
			var $mX = event.pageX; // mouse x position
			var $mY = event.pageY; // mouse y position
			 
			// tooltip width and height without margin
			// var $ttAW = $(this).outerWidth();  // the width of the tooltip anchor 
			// var $ttAH = $(this).outerHeight();  // the height of the tooltip anchor
			 
			// get the position of the tooltip anchor relative to the document
			var $ttAX = $(this).offset().left; // the x position of the tooltip
			var $ttAY = $(this).offset().top;  // the y position of the tooltip
			 
			// switch the style to left if there is not enough space on the right
			/* if ( ($mX + $ttW) > $bW && ($mY + $ttH) < $bH ) {
				$ttO = 'left';
			}
			 
			/* if ( ($mX + $ttW) < $bW && ($mY + $ttH) < $bH ) {
				$ttO = 'left';
			}
			 
			 /* if ( ($mY + $ttH) > $bH ) {
				$ttO = 'bottom';
			 } */
			 
			 //alert($ttO);
			 
			switch($ttO) {
				case 'left':
					$tooltip.removeClass();
					$tooltip.addClass('tooltipWrap tooltipWrapLeft');
					$tooltip.css({
						'top': $mY - $ttAY - 20,
						'right': -($mX - $ttAX) + 32
					});
					break;
				case 'right':
					$tooltip.removeClass();
					$tooltip.addClass('tooltipWrap tooltipWrapRight');
					$tooltip.css({
						'top': $mY - $ttAY - 20,
						'left': $mX - $ttAX + 22
					});
					break;
				case 'top':
					$tooltip.removeClass();
					$tooltip.addClass('tooltipWrap tooltipWrapTop');
					$tooltip.css({
						'bottom': -$mY + $ttAY + 25,
						'left': $mX - $ttAX - 22
					});
					break;
				case 'bottom':
					$tooltip.removeClass();
					$tooltip.addClass('tooltipWrap tooltipWrapBottom');
					$tooltip.css({
						'top': $mY - $ttAY + 25,
						'left': $mX - $ttAX - 22
					});
					break;
			}
			/* alert('MouseX: ' + $mX + '|' + 'MouseY: ' + $mY + '|' + 'TooltipW: ' + $ttW + '|' + 'TooltipH: ' + $ttH + '|' + 'BodyX: ' + $bW + '|' + 'BodyY: ' + $bH); */
		}
		
		if (event.type == 'click') {
			/* $ttO = 'left';
			alert('clicked'); */
		}
			
		if (event.type == 'mouseover') {
			$tooltip.show();
		}
			
		if (event.type == 'mouseout') {
			$tooltip.hide();
		}
	});
};


/**
* Behaviours.showLanguages: blendet bei Mausclick 
* das Auswahlmenü für die Flaggen ein
*
**/
Behaviours.showLanguages = function() {
	if (this.isIE6) {
  	Behaviours.ie6.showLanguages();
	}
	
	else {
		/* fix the hover in the footer section */
		$('#footer div.languagesLinkList').bind("click mouseleave", function(event) {
			var $subColWrapper = $(this).find('div.subColWrapper');																											 
			if (event.type == 'click') {
				$subColWrapper.toggle();
				$(this).find('img.active').toggleClass('menuOpen');
			}
			if (event.type == 'mouseleave') {
				$subColWrapper.hide();
				$(this).find('img.active').removeClass('menuOpen');
			}
		});
	}
};


/**
* Behaviours.tableSelectYear: changes visible rows in alternTable 
*
**/
Behaviours.tableSelectYear = function() {
	$('#yearSwitch').change(function() {
		$('#alternMessagesTable').removeClass("year2005");
		$('#alternMessagesTable').removeClass("year2006");
		$('#alternMessagesTable').removeClass("year2007");
		$('#alternMessagesTable').removeClass("year2008");
		$('#alternMessagesTable').removeClass("year2009");
		$('#alternMessagesTable').removeClass("year2010");
		$('#alternMessagesTable').removeClass("year2011");
	
	    var str = "";
        $("select option:selected").each(function () {
              str += $(this).val() + " ";
            });
	    $('#alternMessagesTable').addClass(str);
	});
};


/**
* Behaviours.nowLiveAnimation: animiert bei einem
* laufenden Event den jeweiligen Status
*
**/
Behaviours.nowLiveAnimation = function(led) {
	if (led) {
		$(".quotesRow.started .entry.state").textAnimation();
	}
	else {
		$toAnimate = $(".quotesRow.started .entry.state");
		function runIt() {
			$toAnimate
				.animate({opacity:0.1}, 600, function() {$toAnimate.css("color", "#88ac4f");})
				.animate({opacity:1.0}, 600)
				.animate({opacity:0.1}, 600, function() {$toAnimate.css("color", "#223139");})
				.animate({opacity:1.0}, 600, runIt);
		}
		runIt();
	}
};


/**
* Behaviours.nowLiveAnimation: Animation für
* gamesList li Element
*
**/
Behaviours.mostFrequentlyGames = function() {
	$(".gamesList li").hover(
  function () {
    //alert("hi");
		//$(this).find('img.gamePreviewImageTiny').fadeOut(250);
		//$(this).find('img.gamePreviewImageBig').hide().fadeIn(550);
  }, 
  function () {
    //$(this).find('img.gamePreviewImageBig').fadeOut(400).hide();
  });
};

/**
* Behaviours.teaserCufon: ersetzt das h1
* Element von teaserMessageBox mittels Cufon
*
**/
Behaviours.teaserCufon = function() {
	Cufon.replace('.teaserMessageBox h1');
};

/**
* Behaviours.myNotificationsCufon: ersetzt .type
* Element von mittels Cufon
*
**/
Behaviours.myNotificationsCufon = function() {
	Cufon.replace('.myNotifications table tr td.type');
};

/**
* Behaviours.myPromotionsCufon: ersetzt .myPromotions .promoHeader .contentContainer h2
* Element von mittels Cufon
*
**/
Behaviours.myPromotionsCufon = function() {
	Cufon.replace('.myPromotions .promoHeader .contentContainer h2');
};

/**
* Behaviours.myRegistration: ersetzt .myRegistration h1
* Element von mittels Cufon
*
**/
Behaviours.myRegistrationCufon = function() {
	Cufon.replace('div.myRegistration h1');
};


/**
* Behaviours.pokerHeader: replaces the font in #pokerHeader with Cufon
*
**/
Behaviours.pokerHeaderCufon = function() {
	Cufon.replace('#pokerHeader h1');
	Cufon.replace('#pokerHeader ul');
};

/**
* Behaviours.pokerHeader: replaces the font in #pokerHeader with Cufon
*
**/
Behaviours.promotionBoxCufon = function() {
	Cufon.replace('.promotionBox h1');
};

/**
* Behaviours.headlineCufon: replaces the font in #navbarLogo h1 with Cufon
*
**/
Behaviours.headlineCufon = function() {
	Cufon.replace('#navbarLogo h1');
	Cufon.replace('.contentBox h1');
};

/**
* Behaviours.equalHeightColumns: ersetzt .myRegistration h1
* Element von mittels Cufon
*
**/
Behaviours.equalHeightColumns = function() {
	var maxHeight = 0;
	$('div.equalHeightColumns').each(function() {
    //alert(index + ': ' + $(this).text());
		var tempHeight = 0
		tempHeight = $(this).height();
		
		if (tempHeight > maxHeight) 
			maxHeight = tempHeight;
  });
	
	$('div.equalHeightColumns').height(maxHeight);
	$('div.equalHeightColumns').height(maxHeight);
};


/**
* Behaviours.ie6 : ein Untermodul speziell für das Verhalten des ie6
*
* Funktionen die zur Behebung verschiedener ie6 Bugs dienen
**/
Behaviours.ie6 = {};


/**
* Behaviours.ie6.showLanguages() : eine Funktion zur Anzeige des 
* Sprachenauswahlmenüs
*
* da der ie6 nur bei <a> tags den hover Effekt unterstützt muss
* dieser hier mittels javascript simuliert werden
**/
Behaviours.ie6.showLanguages = function() {
	/* fix the hover in the footer section */
	$('#footer div.languagesLinkList').bind("mouseenter click mouseleave", function(event) {
		var $subColWrapper = $(this).find('div.subColWrapper');
		var $item = $(this).find('.item');
		var $itemText = $(this).find('.itemwrapper .itemText');
		var $activeImage = $(this).find('.item img.active');
		var $subItemLink = $subColWrapper.find('.subColWrapper a.subItemlink');
		if (event.type == 'click') {
			$subColWrapper.css({
				'visibility': 'visible'
			});
			$activeImage.css({
				'backgroundPosition': '-36px -362px'
			});
		} 
		if (event.type == 'mouseleave') {
			$subColWrapper.css({
				'visibility': 'hidden'
			});
			$activeImage.css({
				'backgroundPosition': '0 -155px'
			});
		}		
		$(this).hover(
			function() {
				$itemText.css({
					'color': '#88ac4f',
					'cursor': 'pointer'
				});
				$item.css({
					'color': '#88ac4f',
					'cursor': 'pointer'
				});
				$activeImage.css({
					'backgroundPosition': '-36px -155px'
				});
				/* $subItemLink.hover(
					function() {
						alert('subitemlink');
						$itemText.css({
							'color': '#88ac4f',
							'cursor': 'pointer'
						}); 
					},
					function() {
						$itemText.css({
							'color': '#04517f'
						});
					}
				); */				
			},
			function() {
				$itemText.css({
					'color': '#04517f'
				});  
				$item.css({
					'color': '#04517f'
				});  
			}
		);		
	});
};


/**
* Behaviours.ie6.quotesMutedMarginBug: verhindert die Addition 
* von margins bei mehreren <table>
*
**/
Behaviours.ie6.quotesMutedMarginBug = function() {
	$('div.quotes table.quotesMuted:first-child').css('margin-bottom','-2px');
};

/**
* Behaviours.ie6.subsetBug: subsets über jquery
*
**/
Behaviours.ie6.subsetBug = function() {
	
};


/**
* JQUERY PLUGINS
*
**/
(function($) {
	/**
	* textAnimation: löst den Text innerhalb des Elements in spans
	* auf und animiert diesen zwischen zwei vorgegebenen Farben
	* 
	**/
  $.fn.textAnimation = function(options) {
		// debug(this);
		// build main options before element iteration
		var opts = $.extend({}, $.fn.textAnimation.defaults, options);
		
		// iterate and reformat each matched element
		return this.each(function() {
			var $toAnimate = $(this);
			var $toAnimateText = $toAnimate.text();
			var $toAnimateTextLength = $toAnimateText.length;
			$toAnimate.text("");
			
			var show = false;
			var counter = 0;
			var char = [];
			var newColor;
			var $animation = null;
			
			// build element specific options
   		var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			for (i=0; i<$toAnimateText.length; i++) {
				char[i] = $toAnimateText.charAt(i);
				$toAnimate.append('<span class="' + i +  '">' + char[i] + '</span');
			}
			
			function runIt(show) {
				$animation = $toAnimate.children('span.' + counter);
				
				newColor = (show == true) ? o.fromColor : o.toColor;
				
				$animation
					.css("color", newColor)
					//.delay(120, countUp) // this seems to be a jquery bug - after the first loop countUp is not called
					.animate({left:'0'}, 120, countUp); // took the left because it makes no problem in all browsers
				
				function countUp() {
					counter++;
					
					if (counter >= $toAnimateTextLength) {
						counter = 0;
						show = (show == false) ? true : false;
						//$animation.clearQueue(); 
					}
					
					runIt(show);
				}
			}
		
			runIt(show);
		});
	};
		
	//
	// private function for debugging
	//
	function debug($obj) {
		if (window.console && window.console.log)
			window.console.log('textAnimation selection count: ' + $obj.size());
	};	
		
	//
	// plugin defaults
	//
	$.fn.textAnimation.defaults = {
		fromColor: '#223139',
		toColor: '#88ac4f'
	};
//
// end of closure
//
})(jQuery);
