jQuery.preloadImages = function(arr) {
	for (var i = 0; i<arr.length; i++) {
		jQuery("<img>").attr("src", arr[i]);
	}
}

$(document).ready(function() {
	fczk.init();
});

var fczk = {
	init: function() {
		this.handleTabs();
		this.handleTopMenu();
		this.handleExternalLinks();
		this.handleBlockBRow();
		this.handleNextMatch();
		this.handleTextFields();
		this.countdown.init();
		this.filter.init();
		$.preloadImages(this.preloadArray);
	},
	preloadArray: ["/images/tabs-r-act.gif", "/images/tabs-r-inact.gif", "/images/tabs-l-act.gif", "/images/tabs-l-inact.gif", "/images/topmenu-r-act.gif", "/images/topmenu-l-act.gif"],
	handleNextMatch: function() {
		$('.nextmatch').click(function() {
			if ($('a.goto', this).attr("href")) { location.href = $('a.goto', this).attr("href"); }
		});
	},
	handleTextFields: function() {
		$('textarea, input[type=text]').focus(function() {
			$(this).addClass("text-field-focused");
		}).blur(function() {
			$(this).removeClass("text-field-focused");
		});
	},
	handleTabs: function() {
		$('.tabs').parent().children('.h-r').addClass("inact");

		$('.tabs td').click(function() {
			$(this).parent().children().removeClass("act");
			$('.tabs').parent().children('.h-l, .h-r').addClass("inact");
			$(this).addClass("act");
			if ($(this).hasClass("last")) { $('.tabs').parent().children('.h-r').removeClass("inact"); }
			if ($(this).hasClass("first")) { $('.tabs').parent().children('.h-l').removeClass("inact"); }

			id = $('a',this).attr("rel");

			$('#tab-'+id).parent().children().hide();
			$('#tab-'+id).show();
		});
	},
	handleTopMenu: function() {
		$('#topmenu- > ul > li').hover(
			function() {
				$('#topmenu- > ul > li, #topmenu .t-l, #topmenu .t-r').removeClass("act");
				$(this).addClass("act");
				if ($(this).hasClass("first")) { $('#topmenu .t-l').addClass("act"); }
				if ($(this).hasClass("last")) { $('#topmenu .t-r').addClass("act"); }
				$('.sub',this).show();
			},
			function() {
				$('#topmenu- > ul > li, #topmenu .t-l, #topmenu .t-r').removeClass("act");
				$('.sub',this).hide();
			}
		);

		$('#topmenu- a').parent().click(function() {
			location.href = $('a',this).attr("href");
		});
		$('#topmenu- a').click(function(e) {
			e.preventDefault();
		});

		$('#topmenu- li li').hover(
			function() {
				$('#topmenu- li li').removeClass("act");
				$(this).addClass("act");
			},
			function() {
				$('#topmenu li li').removeClass("act");
			}
		);
	},
	handleExternalLinks: function() {
		$('a.external').click(function(e) {
			e.preventDefault();
			window.open($(this).attr("href"));
		});
	},
	handleBlockBRow: function() {
		$('.block .brow.left, .block .brow.right').click(function() {
			location.href = $('a',this).attr("href");
		});
	},

	filter: {
		init: function() {
			this.handleBasic();
			this.handleStandings();
			this.handleSeasons();
			this.handleMatches();
		},
		champ: 0,
		defaultYear: 0,
		handleBasic: function() {
			$('.filters div[class!=off]').hover(
				function() {
					if (!$(this).hasClass("act")) { $(this).addClass("dact"); }
				},
				function() {
					$(this).removeClass("dact");
				}
			).click(function() {
				$(this).removeClass("dact");
			});

			$('.filters-tabs div[class!=off]').click(function() {
				$('#filters-tab-'+$('a',this).attr("rel")).parent().children('.filters-tab').hide();
				$('#filters-tab-'+$('a',this).attr("rel")).show();
				$(this).parent().children().removeClass("act");
				$(this).addClass("act");
			});
		},
		handleStandings: function () {
			$('#filters-standings div[class!=off]').click(function() {
				if (!$(this).hasClass("act")) {
					$(this).parent().children().removeClass("act");
					$('#filter-results').css("opacity","0.5");
					$(this).addClass("act");
					$.ajax({
						url: '/ajax/standings.php',
						type: 'POST',
						data: 'filter='+$('a',this).attr("rel")+'&champ='+fczk.filter.champ,
						success: function (r) { $('#filter-results').html(r); $('#filter-results').fadeTo(300,1); }
					});
				}
			});
		},
		handleSeasons: function() {
			$('#filters-seasons div[class!=off]').click(function() {
				if (!$(this).hasClass("act")) {
					$(this).parent().children().removeClass("act");
					$('#filter-results').css("opacity","0.5");
					$(this).addClass("act");
					$.ajax({
						url: '/ajax/seasons.php',
						type: 'POST',
						data: 'year='+$('a',this).attr("rel").replace("y",""),
						success: function (r) { $('#filter-results').html(r); $('#filter-results').fadeTo(300,1); }
					});
				}
			});

			if (fczk.filter.defaultYear) {
				$.ajax({
					url: '/ajax/seasons.php',
					type: 'POST',
					data: 'year='+fczk.filter.defaultYear,
					success: function (r) { $('#filter-results').html(r); $('#filter-results').fadeTo(300,1); }
				});
				$('#filters-seasons a[rel=y'+fczk.filter.defaultYear+']').parent().addClass("act");
			}
		},
		handleMatches: function() {
			$('#filters-match div[class!=off]').click(function() {
				if (!$(this).hasClass("act")) {
					$(this).parent().children().removeClass("act");
					$('#filter-results').css("opacity","0.5").children().hide();
					$(this).addClass("act");
					$('#match-'+$('a',this).attr("rel")).show();
					$('#filter-results').fadeTo(300,1);
				}
			});

			if ($('#filters-match a[rel=relations]').parent().hasClass("off")) {
				$('#filters-match a[rel=techdata]').parent().click();
			} else {
				$('#filters-match a[rel=relations]').parent().click();
			}
		}
	},

	countdown: {
		init: function() {
			this.startCountDown();
		},
		mdate: 0,
		cdate: 0,
		startCountDown: function() {
			diff = this.mdate-this.cdate;
			this.cdate++;

			if (diff>0) {
				days = Math.floor(diff/86400);
				diff = diff-(days*86400);

				hours = Math.floor(diff/3600);
				hours = (hours<10) ? "0"+hours : hours;
				diff = diff-(hours*3600);

				minutes = Math.floor(diff/60);
				minutes = (minutes<10) ? "0"+minutes : minutes;
		
				seconds = diff-(minutes*60);
				seconds = (seconds<10) ? "0"+seconds : seconds;

				$("#cdn-d").html(days); 
				$("#cdn-h").html(hours);
				$("#cdn-m").html(minutes);
				$("#cdn-s").html(seconds);
				setTimeout("fczk.countdown.startCountDown()",1000);
			} else {
				$("#cdn_d").html("0");
				$("#cdn_h").html("00");
				$("#cdn_m").html("00");
				$("#cdn_s").html("00");
			}
		}
	}
}
