/**
 * @author bastianw
 */

function setMatchDays(date) {
	for (i = 0; i < matchDays.length; i++) {
		if (date.getFullYear() == matchDays[i][0] && date.getMonth() == matchDays[i][1] - 1 && date.getDate() == matchDays[i][2]) {
			return [true, 'matchDay'];
		}
	}
	return [false, ''];
}

function matchDaySelected(date) {
	
   var a = $.manageAjax.create('clearQueue', {queue: 'clear', maxRequests: 2, cacheResponse: true});
   var url = fixturesBaseUrl + date;
    
   		$('#matchcenter-changeInnerContent').html('<div class=\"loader\">&nbsp;</div>');
        a.add({
        	url: url,
        	dataType: 'html', 
            success: function(html) {                           	 
                $('#matchcenter-changeInnerContent').html(html);
                $('.matches').MatchTableColorizer();
                a.add({
                	url: url,
                      dataType: 'html', 
                    success: function(html) {
                    }
                });
        	}
        });
}


var modules = ['topnews', 'matchcenter', 'background', 'presscenter', 'fanzone', 'mediacenter'];
jQuery.fn.DefaultValue = function(){

    // Scope
    var elements = this;
    var args = arguments;
    var c = 0;
    
    return (elements.each(function(){
    
        // Default values within scope
        var el = $(this);
        var def = args[c++];
        
        el.val(def).focus(function(){
            if (el.val() == def) {
                el.val("");
            }
            el.blur(function(){
                if (el.val() == "") {
                    el.val(def);
                }
            });
        });
        
    }));
};


jQuery.fn.MatchTableColorizer = function () {
	$('table.matches tr').each(function(i,obj){
		if(i%2) {
			$(this).css('backgroundColor', '#363636');
		}	
	
	});

	$('div.teamroster table').each(function(i,obj){
		$('tr', obj).each(function(j, objj){
			if(j%2) {
				$(this).css('backgroundColor', '#363636');
				
			}	
	
			
		});

	
	});
	
};

var BWTabs = {
	
	init: function() {
		
		var totalWidth = 0;
		var totalElements = 0;
		var elementWidth = 0;
		totalElements = $('#tab-navigation-container .tabs ul li').length;
		elementWidth = 786 / totalElements;
		$('#tab-navigation-container .tabs ul li').each(function(i,obj){
			if($(this).find('br').length === 1) {
				$(this).addClass('long');
			}
			if($(this).width() > elementWidth) {
				$(this).addClass('long');
			}
			$(this).width(elementWidth);

		});	
	
		
	}
	
}

var ListContentWidget =  {
	
	loadContent: function(elementID) {
		
		$(elementID+ ' ul.horizontalContentSlider li div.scrollerElement').each(function(i,obj){
		if($(this).hasClass('active')) {
			var image = $('div.data div.image', obj).html();
			var teasertext = $('div.data div.teasertext', obj).html();
			var headline = $('a', obj).html();
			//getting link
			var	linkurl = $('a', obj).attr('rel');
			//var imageTextHTMLPattern = image+'<div class=\"overlay\"><h3><a href="'+linkurl+'">'+headline+'</a></h3>'+teasertext+'</div>';
			var imageTextHTMLPattern = '<div class=\"media\"><a href="'+linkurl+'">'+image+'</a></div><div class=\"overlay\"><h3><a href="'+linkurl+'">'+headline+'</a></h3>'+teasertext+'</div>';
			$(elementID+' div.imageWithTextTeaser').html(imageTextHTMLPattern);
		}
		});
		
	},
	
	init: function(elementID) {
		// init carousel
		$(elementID +' ul.horizontalContentSlider').jcarousel({
			scroll: 1
		});
		// Aktiviere ersten Punkt
		$(elementID +' ul.horizontalContentSlider li div.scrollerElement:first').addClass('active');
		// Lade Content vom ersten Eintrag
		setTimeout(function() {
			ListContentWidget.loadContent(elementID);
		}, 1500);
		
		// clickfuntion für elemente
		$(elementID+' ul.horizontalContentSlider li div.scrollerElement').click(function() {
		
			if($(this).hasClass('active') === false){
				$(elementID+' ul.horizontalContentSlider li div.active').each(function(i,obj) {
					$(this).removeClass('active');
				});
				$(this).addClass('active');
				ListContentWidget.loadContent(elementID);		
			};

		});

	}

}

var bwTopnewsScroller = {
	
	init: function(elementID) {
		// init carousel
		$(elementID +' ul.topnewsSlider').jcarousel({
			scroll: 1
		});
		// Aktiviere ersten Punkt
		$(elementID +' ul.topnewsSlider li div.scrollerElement:first').addClass('active');
		setTimeout(function() {
			bwTopnewsScroller.loadContent(elementID);
		}, 1500);
		
		// click/mouseover/mouseout handling
		$(elementID+' ul.topnewsSlider li div.scrollerElement').click(function(){
			if ($(this).hasClass('active') === false) {
				$(elementID + ' ul.topnewsSlider li div.active').each(function(i, obj){
					$(this).removeClass('active');
				});
				$(this).addClass('active');
				$(this).removeClass('mouseover');
				bwTopnewsScroller.loadContent(elementID);
			};
		}).mouseover(function() {
			if($(this).hasClass('active') === false)
				$(this).addClass('mouseover');	
		}).mouseout(function(){
			$(this).removeClass('mouseover');
		});
		return false
		
	},
	
	loadContent: function(elementID) {
		
		$(elementID+ ' ul.topnewsSlider li div.scrollerElement').each(function(i,obj){
		if($(this).hasClass('active')) {
			var image;
			var flashurl;
			var flashconfig;
			var teasertext;
			var headline;
			var linkurl;
			var imageTextHTMLPattern;
			var overlay = "";
			var mediaCss = ""
			if ($('div.data div.image', obj).length === 1) {
				
				// checks if overlay is visible
				if($(elementID+ '.overlay').hide() === 1) {
					bwTopnewsScroller.showTeaserLayer();
				}
				// get image url
				image = $('div.data div.image', obj).html();
				// gets teaser text
				teasertext = $('div.data div.teasertext', obj).html();
			 	// gets headline
				headline = $('a', obj).html();
				//getting link
				linkurl = $('a', obj).attr('rel');
				
				if ($('div.data div.image div.hideOverlay', obj).length != 1){
						overlay = '<div class=\"overlay\"><h3><a href="'+linkurl+'">'+headline+'</a></h3><div class=\"content\">'+teasertext+'</div></div>';
				} 
				if ($('div.data div.image div.fullsize', obj).length == 1){
					mediaCss = " fullsize";
				}
				// setting xhtml code for overlay generation
				imageTextHTMLPattern = '<div class=\"media'+mediaCss+'\"><a href="'+linkurl+'">'+image+'</a></div>' + overlay;
				// writing xhtml
				$(elementID+' div.topnewsTeaser').html(imageTextHTMLPattern);
				
				
			}
			else 
				// if there is a flash 
				if ($('div.data div.flash', obj).length === 1) {
				// get flash url
				flashurl = $('div.data div.flash span.flashurl', obj).html();
				
				// TO-DO 
				// insert flash config variables
				//flashconfig = {}
				//flashconfig['wmode'] = 'transparent';
				
				
				// get teasertest
				teasertext = $('div.data div.teasertext', obj).html();
				// gets headline
			 	headline = $('a', obj).html();
				// creating xhtml overlay
				imageTextHTMLPattern = '<div class=\"media\" id=\"flashTopnews\"></div><div class=\"overlay "+\"><h3>'+headline+'</h3><div class=\"content\">'+teasertext+'</div></div>';
				// writing xhtml
				$(elementID+' div.topnewsTeaser').html(imageTextHTMLPattern);
				// launching flash
				flashembed("flashTopnews", {src: flashurl, wmode: 'transparent'});
				//bwTopnewsScroller.hideTeaserLayer();
				
				
			}
			//console.log($(elementID+' div.topnewsTeaser').html());
			return false
		}
		});
		
	},
	
	// this functions hides the overlay layer with animation
	hideTeaserLayer: function() {
		
		$('#module-topnews .topnewsTeaser .overlay').animate({
			opacity: 0
		}, 1500);
		return false
	},
	
	// this functions shows overlay layer with animation
	showTeaserLayer: function() {
		
		$('#module-topnews .topnewsTeaser .overlay').animate({
			opacity: 1
		}, 1500)
		return false
	}
	
}

var bwPager = {
		page: 1,
		init: function(url) {
			if($('.pager').length >= 1) {
				var a = $.manageAjax.create('clearQueue', {queue: 'clear', maxRequests: 2, cacheResponse: true});;
				
				$('.pager').jcarousel({
						scroll: 1
				});
				
				
				$('.pager li:first').addClass('active');
				
				$('.pager li').click(function(){
					var page = $(this).html();
					bwPager.page = page;
					 a.add({
		                      url: url+''+page,
		                      dataType: 'html', 
							  success: function(html) {
								 $('#searchResults').html(html);
								  $('.pager li.active').removeClass('active');
								  $(".pager li").each(function (i) {
									  if((i + 1) == bwPager.page){
										  $(this).addClass('active')
										  return;
									  }
								  });
		                      }
			             });
				});
			}
		}
	}


var bwTeamAjaxContentLoader = {
	
	init: function() {
		var a = $.manageAjax.create('clearQueue', {queue: 'clear', maxRequests: 2, cacheResponse: true});;
		$('#matchcenter-teamlinks a').each(function(i, obj){
		
			$(this).click(function(){
				$('#matchcenter-teamlinks a').removeClass('active');
				$(this).addClass('active');
				$('#matchcenter-changeInnerContent').html('<div class=\"loader\">&nbsp;</div>');
				 a.add({
                      url: $(this).attr('rel'),
                      dataType: 'html', 
					  success: function(html) {
 						  $('#matchcenter-changeInnerContent').html(html);
						  $('.matches').MatchTableColorizer();
						  
                      }
	             });
				 
			});
			
		});
		
		
	}
	
}


var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "244";

/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("div");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				span[a].onmousedown = Custom.pushed;
				span[a].onmouseup = Custom.check;
				document.onmouseup = Custom.clear;
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.choose;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
window.onload = Custom.init;
