var slideshow;
var navigation;
var navigationMobile;
var activityFacebook;
var activityTwitter;
var contentSlideshow;
var resolution;
var device;
var reloadTimeout;

initPage = function()
{
	slideshow			= new Slideshow($('.maxiteaser'), 'maxiteaser');
	navigation			= new Navigation($('.navigation'));
	activityFacebook	= new ActivityFeed($('.activity .entries.facebook'), 'facebook', 3);
	activityTwitter		= new ActivityFeed($('.activity .entries.twitter'), 'twitter', 2);

	if ($('.article .images').length > 0) {
		contentSlideshow = new Slideshow($('.article .images'), 'content');
	}
}

initSearch = function()
{
	$('.search input').each(function() {
		if ($(this).val()== '') {
			$(this).val($(this).attr('title'));
		}
		$(this).focus(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
		$(this).blur(function() {
			if ($(this).val()== '') {
				$(this).val($(this).attr('title'));
			}
		});
	});
	$('.search').submit(function() {
		if ($(this).find('input').val() == $(this).find('input').attr('title')) {
			return false;
		}
	})
}

getDevice = function()
{
	if ($('.device.mobile').is(':visible')) {
		device = 'mobile';
	} else if ($('.device.tablet').is(':visible')) {
		device = 'tablet';
	} else {
		device = 'desktop';
	}
	return;
}

changeResolution = function()
{
	// Reload page in IE7 for correct display
	if ($.browser.msie  && parseInt($.browser.version, 10) === 7) {
		clearTimeout(reloadTimeout);
		reloadTimeout = setTimeout(function() {
			document.location.reload();
		}, 100);
		return;
	}

	var lastDevice = device;
	getDevice();
	if (device != lastDevice) {
		if (device == 'mobile' && $('.content .teaser').length) {
			document.location.reload();
		} else {
			slideshow.refresh();
			navigation.refresh();
			activityFacebook.refresh();
			activityTwitter.refresh();
		}
	}
}

$(document).ready(function() {
	getDevice();
	$(window).resize(changeResolution);
	initPage();
	initSearch();
});

