function log(m) {
	try {
		console.log(m);
	} catch (e) {}
}

jQuery(function() {
	var mv_wai_contrast = jQuery.cookie('mv_wai_contrast');
	var mv_wai_size = jQuery.cookie('mv_wai_size');
	
	setContrast(mv_wai_contrast);
	setFontSize(mv_wai_size);
	
	var targetHeight = 109;
	var images = jQuery('.containerTeaser img.teaserimage');
	
	for (var i = 0; i < images.size(); i++) {
		var img = images.eq(i);
		var height = img.attr('height');
		var offset = Math.floor((targetHeight - height) / 2.0);
		
		img.closest('a').css('position', 'relative').css('top', '' + offset + 'px');
	}
	
	jQuery('#languageSwitcher').change(function() {
		var langid = jQuery("#languageSwitcher option:selected").val();
		var path = location.href;
		var nodeId = jQuery(document).getUrlParam("a-Common_menu-n_Selected");
		var storyId = jQuery(document).getUrlParam("a-Common_menu-n_Selected");
		var searchNodeId = jQuery(document).getUrlParam("a-Common_storyOutput-n_SearchNodeId");
		
		if (langid >= 0) {
			if (path.indexOf('detail.html') >= 0) {
				var newUrl = 'detail.html?a-Common_global-n_NewLanguageId=' + langid + '&button-Common_global-change_language=';
				
				if (storyId != null && nodeId != null) {
					newUrl = 'detail.html?button-Common_storyContentDetail-find=&a-Common_storyContentDetail-id=' + storyId + '&a-Common_OpenTree-att_NodeID=' + nodeId + '&button-Common_OpenTree-open_tree=&a-Common_global-n_NewLanguageId=' + langid + '&button-Common_global-change_language=';
				}
				
				location = PBEURL(newUrl);
			} else {
				var newUrl = 'index.html?a-Common_global-n_NewLanguageId=' + langid + '&button-Common_global-change_language=';
				
				if (searchNodeId != null && nodeId != null) {
					newUrl = 'index.html?a-Common_menu-n_Selected=' + nodeId + '&button-CurrentMenuTree-setmenutree=&a-Common_storyOutput-n_SearchNodeId=' + searchNodeId + '&button-Common_storyOutput-find_story=&a-Common_global-n_NewLanguageId=' + langid + '&button-Common_global-change_language=';
				}
				
				location = PBEURL(newUrl);
			}
		}
	});
	
	if (jQuery('#formMiniContact')) {
		var miniFormRecp = jQuery('#formMiniContact div.recp').text();
		
		jQuery('#formMiniContact input.recp').val(miniFormRecp);
	}
	
	jQuery('#formSearchbox label').overlabel();
	
	// WAI
	jQuery('.wai a.normal').click(function () {
		setFontSize("0");
		return false;
	});
	
	jQuery('.wai a.big').click(function () {
		setFontSize("1");
		return false;
	});
	
	jQuery('.wai a.bigger').click(function () {
		setFontSize("2");
		return false;
	});
});

function setContrast(value) {
	if (value && value == "on") {
		jQuery('#doc4').addClass('wai-contrast');
		jQuery.cookie('mv_wai_contrast', 'on', { path: '/' });
	} else {
		jQuery('#doc4').removeClass('wai-contrast');
		jQuery.cookie('mv_wai_contrast', 'off', { path: '/' });
	}
}

function setFontSize(value) {
	if (value) {
		jQuery('#doc4').removeClass('wai-fontsize-1');
		jQuery('#doc4').removeClass('wai-fontsize-2');
		
		if (value == "1") {
			jQuery('#doc4').addClass('wai-fontsize-1');
			jQuery.cookie('mv_wai_size', '1', { path: '/' });
		} else if (value == "2") {
			jQuery('#doc4').addClass('wai-fontsize-2');
			jQuery.cookie('mv_wai_size', '2', { path: '/' });
		} else {
			jQuery.cookie('mv_wai_size', '0', { path: '/' });
		}
	}
}
