$(document).ready(function () {
	$('div#nav ul#menu li a, .font').addClass('font');
	Cufon.replace('#header .wrapper .col1 a');

	/* Støt-box mousepress effect */
	$('.stot-box a.btn').mousedown(function () {
		$(this).addClass('press');
	}).mouseup(function () {
		$(this).removeClass('press');
	});

	/* Show submenu */
	var navigationTimer = null;
	var navigationTimeout = 1000;

	var hideSubmenu = function (menu) {
		clearTimeout(navigationTimer);
		navigationTimer = null;
		navigationTimer = setTimeout(function () {
			menu.hide();
			$('#nav ul#menu li a.active').removeClass('active');
		}, navigationTimeout);
	};

	$('#nav ul#menu li').live('mouseenter', function () {
		var i = $(this).attr('rel');
		$('#nav ul#menu li a.active').removeClass('active');
		$(this).find('a').addClass('active');
		$('#nav .submenu').each(function (el, index) {
			if ($(this).attr('rel') != i) {
				$(this).hide();
			}
		});

		clearTimeout(navigationTimer);
		navigationTimer = null;

		//var pos = $('#header').outerHeight();
		var menu = $('#nav .submenu[rel="' + i + '"]');

		if (menu.length > 0) {
			menu.show();
			if ($(this).offset().left <= ($('#header').width() / 2)) {
				menu.css({ left: 0 });
			} else {
				menu.css({ right: 0 });
			}
		}
	}).live('mouseleave', function () {
		var i = $(this).attr('rel');
		var menu = $('#nav .submenu[rel="' + i + '"]');
		menu.live('mouseenter', function () {
			clearTimeout(navigationTimer);
			navigationTimer = null;
		}).live('mouseleave', function () {
			hideSubmenu(menu);
		});

		hideSubmenu(menu);
	});

	/* News-box overlay */

	$('.news-box ul.list li').bind('click', function () {
		var overlay = $('.overlay:eq(' + $(this).index() + ')');
		/* Positionate*/
		var position = ($(this).offset().top - $(this).parents('ul:first').height()) + 29;

		/* IE 7  margin fix */
		if ($.browser.msie && $.browser.version == 7) {
			position += 10;
		}

		overlay.css({ top: position });
		if (overlay.is(':visible')) {
			$(this).find('.bg:first').removeClass('active');
			overlay.slideUp(200);
		} else {
			$('.overlay').hide();
			$(this).find('.bg:first').addClass('active');
			overlay.slideDown(200);
		}
	});

	$('.overlay .content a.js-close').bind('click', function (e) {
		e.preventDefault();
		$('.news-box ul.list li:eq(' + $(this).attr('rel') + ')').click();
	});

	/* Frontpage slider */
	$('.js-slider').each(function () {
		var slider = $(this);
		var active = 0;
		var max = 0;
		//var timeout = 0;
		//var timer = null;

		var stop = function () {
			/*clearTimeout(timer);
			timer = null;*/
		};

		slider.find('.js-subjects li a').live('mouseenter', function () {
			stop();
			slider.find('.js-subjects li a.active').removeClass('active');
			$(this).addClass('active');
			Cufon.refresh();
			showSlide($(this).parent().index());
		}).live('mouseleave', function () {
			/*timer = setTimeout(function () {
			nextSlide();
			}, timeout);*/
		}).live('click', function (e) {
			e.preventDefault();
			/* Do nothing */
		});

		slider.find('.content').live('mouseenter', function () {
			stop();
		}).live('mouseleave', function () {
			/*timer = setTimeout(function () {
			nextSlide();
			}, timeout);*/
		});

		var init = function () {
			active = 0;
			max = slider.find('.content').length;
			slider.find('.content').hide();
			slider.find('.content:eq(0)').show();
			slider.find('.js-subjects li a.active').removeClass('active');
			slider.find('.js-subjects li:eq(0) a').addClass('active');
			/*timer = setTimeout(function () {
			nextSlide();
			}, timeout);*/
			Cufon.refresh();
		};

		var moveCursor = function () {
			slider.find('.js-subjects li a.active').removeClass('active');
			slider.find('.js-subjects li:eq(' + active + ') a').addClass('active');
			Cufon.refresh();
		};

		var showSlide = function (index) {
			slider.find('.content').hide();
			active = index;
			slider.find('.content:eq(' + active + ')').show();
		};

		var nextSlide = function () {
			if ((active + 1) < max) {
				slider.find('.content:eq(' + active + ')').fadeOut(800, function () {
					active++;
					moveCursor();
					slider.find('.content:eq(' + active + ')').fadeIn(800);
					/*timer = setTimeout(function () {
					nextSlide();
					}, timeout);*/

				});
			} else {
				slider.find('.content:eq(' + active + ')').fadeOut(800, function () {
					active = 0;
					moveCursor();
					slider.find('.content:eq(' + active + ')').fadeIn(800);
					/*timer = setTimeout(function () {
					nextSlide();
					}, timeout);*/
				});
			}
		};
		init();
	});

});

/* Old stuff */

function EPG_Toggle(id) {
	if (document.all) {
		if (document.all[id].style.display == 'none') {
			document.all[id].style.display = '';
		} else {
			document.all[id].style.display = 'none';
		}
		return false;
	} else if (document.getElementById) {
		if (document.getElementById(id).style.display == 'none') {
			document.getElementById(id).style.display = 'block';
		} else {
			document.getElementById(id).style.display = 'none';
		}
		return false;
	}
}

function EPG_Close(id) {
	if (document.all) {
		if (document.all[id].style.display == '') {
			document.all[id].style.display = 'none';
		}
	} else if (document.getElementById) {
		if (document.getElementById(id).style.display == 'block') {
			document.getElementById(id).style.display = 'none';
		}
		if (document.getElementById(id).style.display == '') {
			document.getElementById(id).style.display = 'none';
		}
	}
}

function EPG_Open(id) {
	if (document.all) {
		if (document.all[id].style.display == 'none') {
			document.all[id].style.display = '';
		}
	} else if (document.getElementById) {
		if (document.getElementById(id).style.display == 'none') {
			document.getElementById(id).style.display = 'block';
		}
	}
}

function EPG_OpenAll(media) {
	var counter;
	if (media == 'TV') {
		counter = 6
	} else {
		counter = 4
	}
	var arrmenu = Array(6);
	arrmenu[0] = "Danish";
	arrmenu[1] = "Local";
	arrmenu[2] = "SvNo";
	arrmenu[3] = "German";
	arrmenu[4] = "EngAmr";
	arrmenu[5] = "French";

	var divId;
	for (i = 0; i < counter; i++) {
		divId = media + arrmenu[i];
		EPG_Open(divId);
	}
}

function EPG_ImageToggle(daImage, src1, src2) {
	//Check to make sure that images are supported in the DOM.
	myImage = document.images[daImage];
	if (document.images) {
		// Check to see whether you are using a name, number, or object
		if (myImage.src.indexOf(src1) > -1) {
			myImage.src = src2;
		} else {
			myImage.src = src1;
		}
	}
}

function EPG_ImageChange(daImage, src, netscape_container) {
	// Check to make sure that images are supported in the DOM.
	myImage = document.images[daImage];
	if (document.images) {
		myImage.src = src;
	}
}
