var FrontendJs = {
	initialize: function()
	{
		$('table').each(function(index, el) {
			var table = $(el);
			table.find('tr').each(function(index, el) {
				$(el).children('td').each(function(index, el) {
					$(el).addClass('c' + (index + 1).toString());
				});
				$(el).children('th').each(function(index, el) {
					$(el).addClass('c' + (index + 1).toString());
				});
			});
		});

		// Spacer einbauen
		$('tbody[class!=nospacers]').each(function(index, el) {
			$(el).find('tr').each(function(index, el) {
				var tr = $(el);
				tr.addClass('content');
				var colspan = -1;
				if (index > 0) {
					if (colspan < 0) colspan = tr.children('td').length;
					tr.before('<tr class="spacer"><td colspan="' + colspan + '"></td></tr>');
				}
			});
		});

		$('div.bigtarife tbody tr td.c1').each(function(index, el) {
			var td = $(el);
			var result = td.html().match(/([0-9]+)-([0-9]+)&nbsp;Uhr/);
			if (result) {
				var hours = parseInt(result[2]) - parseInt(result[1]);
				if (hours === 0 || hours === 12 || hours === 24) {
					var img = '24';
				} else {
					var img = result[1] + '-' + result[2];
				}
				if (td.hasClass('top')) {
					td.removeClass('top'); // IE6 fix
					img = 'rot/' + img;
				}
				td.prepend('<img src="/tl_files/01070/img/uhren-klein/' + img + '.png" width="42" height="42" alt="' + img + ' Uhr" class="uhr" />');
			}
		});

		$('ul.countryLetters a').bind('click', function(ev) {
			$('ul.countryLetters a').removeClass('highlight');
			var a = $(ev.target);
			a.addClass('highlight');
			$('#laenderframe').attr('src', '/call-by-call/ausland/laender#' + a.attr('href').substr(1) );
			return false;
		});

		$('table.zones tr.zone').each(function(index, el) {
			var tr = $(el);
			FrontendJs.hideZoneCountries(tr);
			tr.next().after('<tr class="spacer"><td colspan="4"></td></tr>');
			tr.bind('click', function(ev) {
				var tr = $(ev.target).closest('tr');
				if (tr.hasClass('hideCountries')) {
					FrontendJs.showZoneCountries(tr);
				} else {
					FrontendJs.hideZoneCountries(tr);
				}
			});
		});

		$('a.popup').bind('click', function(ev) {
			FrontendJs.openLinkInPopup(ev.target);
			return false;
		});

		$('img').supersleight();
		$('div.visDetailAusland').supersleight();

		// IE6 does not support tr:hover
		$('table.laenderpreise tr').bind('mouseenter', function(ev) {
			$(ev.target).closest('tr').addClass('hover');
		});
		$('table.laenderpreise tr').bind('mouseleave', function(ev) {
			$(ev.target).closest('tr').removeClass('hover');
		});
	},
	hideZoneCountries: function(tr)
	{
		tr.children('td.c1:first').html('+');
		tr.children('td.c4:first').html('<span class="link highlight">Länder einblenden</span>');
		tr.addClass('hideCountries');
		tr.next().addClass('hiddenCountries');
	},
	showZoneCountries: function(tr)
	{
		tr.children('td.c1:first').html('-');
		tr.children('td.c4:first').html('<span class="link highlight">Länder ausblenden</span>');
		tr.removeClass('hideCountries');
		tr.next().removeClass('hiddenCountries');
	},
	openLinkInPopup: function(link, width) {
		var link = $(link);
		if (typeof width === 'undefined') width = 740;
		var pop = window.open(link.attr('href'), link.attr('id'), 'width=' + width + ',height=570,left=50,top=50,screenX=50,screenY=50,scrollbars=yes,location=no,status=no,personalbar=no,resizeable=yes,toolbar=no,menubar=no,dependent=no,directories=no');
		if ((typeof pop !== 'undefined') && (pop !== null)) {
			if (typeof pop.focus === 'function') pop.focus();
		}
	}
};
$(document).bind('ready', function() {
	FrontendJs.initialize();
});