
if(jQuery.browser.msie)
		{
			if(detectBrowserVersion() == 7)
			{
			
			
			
			
			
			}
		}
			
			


function menu_handler(id)
{

	if(current_submenu != 0)
	{
		current_submenu.css("display","none");
	}
	current_submenu = $('#submenu_'+id);

	current_submenu.css("display","inline");
	
	refresh_menu_layout();


}



	
	
function set_menu_handler()
{



	jQuery('.menu-item').click(function()
		{
			jQuery('.menu-children').css('display','none');
			if(jQuery(this).children().size() == 2)
			{
			
				
				jQuery(this).children().eq(1).css('display','block');	
			
				refresh_menu_layout();
			}
		
		});
}		

	
	
function set_title_image()
{



	if(page_cat_id == 0)
	{
		var title_id = page_id;
	}
	else
	{
		var title_id = page_cat_id;
	}

	if(jQuery('#title_'+title_id).size() == 0)
	{
		var title_id = 0;
	}
	
	
		var title = jQuery('#title_'+title_id);
		
		

		if(title.css('display') == 'none' || title.css('display') == 'NONE')
		{
		
			jQuery('.titles').css('display','none');
			title.css('display','block');
		
			
		}
	
	

	
}	
	
	
	function popup_launch(content,time)
	{
	
	
			center_div_horizontal("popup","screen");
			//center_div_vertical("popup","screen");
		
			content = content.replace('[miumau]','@');
		
			$("#popup_content").html(content);
			$("#popup").fadeIn(300);
		
		if(time>0)
		{	
			setInterval('popup_close()',time);
		}
	}
	
	function popup_close()
	{
		
		$("#popup").fadeOut(300);
	
	}
	
	
	
	
	function element_height(divi)
	{
		if(jQuery.isPlainObject(divi))
		{
			obj = divi;
		}
		else
		{
			obj = $(divi);
		}
	
		
		
		var div_height;
		
		if(obj.height() == 0 || obj.height() == 'auto')
		{
			var div_height = obj.css("height").replace("px","");
		}
		else
		{
			var div_height = obj.height();
		}
		
		if(div_height == 0 || div_height == 'auto')
		{
			obj_id = obj.attr('id');
			div_height = document.getElementById(obj_id).offsetHeight;
			//div_height = div_height.replace("px","");

			
		}
		
		div_height = parseFloat(div_height);
			
		if(div_height > -1)
		{
			return div_height;
		}
		else
		{
			return false;
		}
	}
	
	
	function element_width(divi)
	{
	
		if(jQuery.isPlainObject(divi))
		{
			obj = divi;
		}
		else
		{
			obj = $(divi);
		}
		obj = $(divi);
		
		var div_width;
		
		if(obj.width() == 0 || obj.width() == 'auto')
		{
			var div_width = obj.css("width").replace("px","");
		}
		else
		{
			var div_width = obj.width();
		}
		
		if(div_width == 0 || div_width == 'auto')
		{
			//obj_id = obj.attr('id');
			div_width = document.getElementById(obj_id).offsetWidth;
			//alert(obj_id + ' - ' + div_width + ' - ' + obj.css("width").replace("px","") + obj.width());
			//.replace("px","");
		}
		
		div_width = parseFloat(div_width);
		
		if(div_width > -1)
		{
			return div_width
		}
		else
		{
			return false;
		}
	}
	
	
	
	
	
	
	
	function center_div_horizontal(divi,by)
	{
 
		var divi = "#" + divi;	
		div_width = element_width(divi);
		var obj = $(divi);

		
		var obj_position = obj.css('position');
		
		if(obj_position != 'absolute' && obj_position != 'fixed')
		{
			obj.css('position','absolute');
		}
 
 		if(by == "screen")
		{
			var parent_width = screen.width;	
		}
		else if(by == "parent")
		{

			var parent_width = obj.parent().width();
			var parent = obj.parent();
		
			
			if(parent_width == 0 || parent_width == 'auto')
			{
				parent_width = parent.width();
			}
			
			if(parent_width == 0 || parent_width == 'auto')
			{
				parent_width = document.getElementById(parent.attr('id')).offsetWidth.replace("px","");
			}
			
			
		}
		
		if(div_width >= parent_width)
		{
			parent_width = div_width;
		}
		
		
		if(parent_width>0 && div_width > 0 && parent_width != 'auto' && div_width != 'auto')
		{
			div_width = parseFloat(div_width);
		
			$(divi).css("left",(parent_width/2)-(div_width/2));
		}
		else
		{
	
		}
	}
 
 	function center_div_vertical(divi,by)
	{
		var divi = "#" + divi;

		obj = $(divi);
		var div_height;
		
		if(obj.height() == 0)
		{
			var div_height = obj.css("height").replace("px","");

		}
		else
		{
			var div_height = obj.height();
		}
		
		if(div_height == 0)
		{
		
		obj_id = obj.attr('id');
		
		div_height = document.getElementById(obj_id).offsetHeight;

		
		}
		
		
		
		
		
		if(by == "screen")
		{
			var parent_height = screen.height;	
		}
		else if(by == "parent")
		{
			var parent_height = $(divi).parent().css("height").replace("px","");	
			var parent = obj.parent();
		
			
			if(parent_height == 0 || parent_height == 'auto')
			{
				parent_height = parent.height();
			}
			
			if(parent_height == 0 || parent_height == 'auto')
			{
				parent_height = document.getElementById(parent.attr('id')).offsetHeight;
		
			}
		
		
		
		
		
		
		}
		
			div_height = parseFloat(div_height);
			parent_height = parseFloat(parent_height);
		
		if(parent_height > 0 && div_height > 0)
		{
			$(divi).css("top",(parent_height/2)-(div_height/2)); 
		}
		else
		{
	
		}
		
	}
	

	function center_obj_horizontal(obj,by)
	{
		
		var div_width = 0;
		var parent_width = 0;
		
		if(obj.width() == 0)
		{
			var div_width = obj.css("width").replace("px","");
		}
		else
		{
			var div_width = obj.width();
		}
		
		if(div_width == 0)
		{
		
		obj_id = obj.attr('id');
		
		div_width = document.getElementById(obj_id).offsetWidth;
	
		
		}
		
		
		
 
 		if(by == "screen")
		{
			parent_width = screen.width;	
		}
		else if(by == "parent")
		{
			parent_width = obj.parent().css("width").replace("px","");	
			var parent = obj.parent();
		
			
			if(parent_width == 0 || parent_width == 'auto')
			{
				parent_width = parent.width();
			}
			
			if(parent_width == 0 || parent_width == 'auto')
			{
			if(parent.attr('id') != 0)
			{
				parent_width = document.getElementById(parent.attr('id')).offsetWidth;
}			
		
			}
		}
		

		div_width = parseFloat(div_width);
		parent_width = parseFloat(parent_width);
		
		if(div_width > 0 && parent_width > 0)
		{
		
		if(div_width >= parent_width)
		{
			parent_width = div_width;
		}
		
		
			obj.css("left",(parent_width/2)-(div_width/2));
		}

	}
	
	
	
function set_menus()
{



	for(i=0;i<$('.menu').size();i++)
	{

		html_content = $('.menu').eq(i).html();
		menu_name = $('.menu').eq(i).attr("name");
		$('.menu').eq(i).html('<div id="menu_kruunu" style="width:60px;height:38px;"><img src="wp-content/themes/kanavankunkkuteema/images/graphics/menu_kruunu.png" style="width:60px;height:38px;" /></div><div id="menu_top"></div><div id="menu_middle"></div><div id="menu_bottom"></div><div id="menu_content">'+html_content+'</div><div id="menu_name">'+menu_name+'</div>');

		var menu_bg = $('.menu').eq(i).children().eq(0);
		var menu_top = $('.menu').eq(i).children().eq(1);
		var menu_middle = $('.menu').eq(i).children().eq(2);
		var menu_bottom = $('.menu').eq(i).children().eq(3);
		var menu_content = $('.menu').eq(i).children().eq(4);
		var menu_title = $('.menu').eq(i).children().eq(5);
	
		
		menu_content.html(menu_content.html().replace('[miumau]','@'));

		content_height = element_height(menu_content);
		

		
		if(content_height+80>$('.menu').eq(i).height())
		{
			$('.menu').eq(i).css('height',content_height + 150);
			menu_middle.height($('.menu').eq(i).height()-menu_top.height()-menu_bottom.height());
			
		}

	
	
			center_obj_horizontal(menu_bg,"parent");
			center_obj_horizontal(menu_title,"parent");

	
	
	
	}
	
	set_menu_handler();

}


function set_images()
{


var i = 0;
$("#JN_ajax_content img").each(function(){

img_src = $(this).attr('src');
img_style = $(this).attr('style');
img_width = element_width(this);
img_height = element_height(this);

img_width = $(this).width();
img_height = $(this).height();


$(this).attr('id','wp-image-'+i);


if(img_style == null)
{
	var img_style='';
}

var img_src_2 = img_src;



img_style = img_style.replace("display: none;","");
img_style = img_style.replace("DISPLAY: none;","");
img_style = img_style.replace("display: none","");
img_style = img_style.replace("display:none","");


	//var image_html = "<table id='wp-image-"+i+"-table' style='display:inline;' class=''><tr><td><img src='wp-content/themes/kanavankunkkuteema/images/graphics/mini_logo.png' align='center'></td><tr><td>"
/*
		var image_html = "<div style='position:relative;display:inline;height:100px;width:100px;border:1px solid black;'>dddd";
		 image_html += "<div style='position:absolute;display:block;height:100px;width:100px;border:1px solid red;'>";
	if($(this).parent().get(0).nodeName == "A")
	{
		var a_href = $(this).parent().attr('href');
		var a_rel = $(this).parent().attr('rel');
		var a_target = $(this).parent().attr('target');
		
	//	image_html += "<a href='"+a_href+"' rel='"+a_rel+"'>";
	//	image_html += "<img id='wp-image-"+i+"' src='"+$(this).attr('src')+"' />";
	//	image_html += "</a>";
			image_html += "</div>";
			image_html += "</div>";
	
		//image_html += "</td></tr></table>";
		$(this).parent().replaceWith(image_html);
		
		
		
	}
	else
	{
		image_html += "<img id='wp-image-"+i+"' src='"+$(this).attr('src')+"' />";
		image_html += "</td></tr></table>";
		$(this).replaceWith(image_html);
	}
	*/
	/*
if($(this).parent().get(0).nodeName == "A")
	{
$(this).parent().replaceWith('<div class="img_frame '+$(this).attr('class')+'" style="display:inline;height:300px;"><div style="background:url(\'wp-content/themes/kanavankunkkuteema/images/graphics/mini_logo.png\');position:absolute;top:-10px;left:'+(30+(img_width/2)-(80/2))+'px;width:80px;height:78px;"></div><img class="img" id="wp-image-'+i+'" style="position:relative;z-index:10;height:'+img_height+'px;width:'+img_width+'px;" src="'+img_src_2+'"/></div>');
}
*/

$("#wp-image-"+i+"-table").height(img_height+40).css('border:1px solid red;');
$("#wp-image-"+i+"-table").width(img_width+40);




i++;
});




}


function set_layot()
{

	//lasketaan mitat
	main_width = $("#main").width();
	text_height = ($("#main_text").height());
	text_width = ($("#main_text").width());
	
	title_height = element_height('#main_title');
	title_width = element_width('#main_title');
	

	
	text_height = text_height + title_height;
	title_pos_top = $('#main_title').css('top').replace('px','');
	
	//konvertoidaan floateiksi, jotta voidaan summata myöhemmin
	title_pos_top = parseFloat(title_pos_top);
	text_height = (parseFloat(text_height)+60);
	
	if(text_height < 200)
	{
		text_height = 600;
	}
	

	text_width = (main_width-($('#graph_main_left').width()+$('#graph_main_right').width()));


	
	main_new_pos_top = (title_height+title_pos_top);
	
	$("#main_text").css('top',main_new_pos_top);

	//määritellään korkeus
	$('#graph_main_left').css("height",text_height-200);
	$('#graph_main_bg').css("height",text_height-200);
	$('#graph_main_right').css("height",text_height-200);

	//Määritellään leveys
	$('#graph_main_top').css("width",text_width);
	$('#graph_main_bg').css("width",text_width);
	$('#graph_main_bottom').css("width",text_width);

	//määritelään myös parent elementille
	$('#graph_main').css("height",300+text_height).css("width",main_width);

}

function refresh_menu_layout()
{


	
	for(i=0;i<$('.menu').size();i++)
	{
	

	
		var menu_bg = $('.menu').eq(i).children().eq(0);
		var menu_top = $('.menu').eq(i).children().eq(1);
		var menu_middle = $('.menu').eq(i).children().eq(2);
		var menu_bottom = $('.menu').eq(i).children().eq(3);
		var menu_content = $('.menu').eq(i).children().eq(4);
		var menu_title = $('.menu').eq(i).children().eq(5);
		
		


	var content_height = menu_content.height();
	
	if(i == 0)
	{

	}
		if(content_height>( 320-menu_top.height()-menu_bottom.height() ) )
		{
			/*
			if((content_height + 110)<300)
			{
				content_height = 250;
			}
			*/
			
			$('.menu').eq(i).height(content_height + 110);
			
			
			menu_middle.height($('.menu').eq(i).height()-menu_top.height()-menu_bottom.height());
			
			if(($('.menu').eq(i).height()-menu_top.height()-menu_bottom.height())<0)
			{
			 	menu_middle.height(0);
			}
			else
			{
				menu_middle.height($('.menu').eq(i).height()-menu_top.height()-menu_bottom.height());
			}

				
		}
		else
		{
		
		}
		
		
		
		if(i>0 && ($('.menu').eq(i-1).position().top < $('.menu').eq(i).position().top))
		{
			$('.menu').eq(i).css('top',($('.menu').eq(i-1).position().top + $('.menu').eq(i-1).height()) + 50 +'px');
		
		
		
	
		
		}
	
	}


}





function start_bg_animation(photos)
{


		if(jQuery.browser.msie)
		{
			if(detectBrowserVersion() < 9)
			{
			
			
			
			//tyhjennetään bg taulu, jätetään alkio nolla
			var i;
			for(i=1;i<$('#background').children().size();i++)
			{
				$('#background').children().eq(i).remove();			
			}
			
			//keskitetään oletus bg
			center_obj_horizontal($('#background').children().eq(0));
		  var i = 0;

		  
				//haetaan taustat taulukosta latausta varten
				var i;
				for(i=0;i<photos.length;i++)
				{
				
					//alert(i +' / '+ photos.length);
					var bg_src = photos[i][0];
					var bg_width = photos[i][1];
					var bg_height = photos[i][2];
					
					if(bg_width>screen.width)
					{					
						bg_height = Math.round((screen.width/bg_width)*bg_height);
						bg_width = screen.width;
					}					
					
					  $('#background').append('<img width="'+bg_width+'" height="'+bg_height+'" style="display:none;position:absolute;height:'+bg_height+'px;width:"'+bg_width+'"px;" src="'+bg_src+'">');
				
				}
				
		  				  
		  		bg_size = $('#background').children().size();
				
								//kun kaikki taustat ovat ladattu
								$('#background').children().eq((bg_size-1)).load(function(){  
								var i2=1;
								var timer = 0;
								var counter = 0;
								for(i2=1;i2<10;i2++)
								{
									for(i=1;i<=bg_size;i++)
									{
										counter = counter + 1;
										timer = timer + 30000;	
										
										if(i == 1)
										{
											timeouts[counter] = setTimeout("fadeInBackground("+(i-1)+",8000)",timer);
										}
										else
										{
											timeouts[counter] = setTimeout("fadeInBackground("+(i-1)+",8000)",timer);
										}
									
									
									}
								
								}
								
						});
		  

		  
}}
			
	}	
		   
		  
		  
		  	function fadeInBackground(obj,time)
			{
			
			if((browser != 'IE' || (browser == 'IE' && browser_version > 11)) || screen.width < 810)
			{
			
				current_bg = obj;
			
				if(obj>0)
				{
					//center_obj_horizontal($('#background').children().eq(obj));
					
					$('#background').children().eq(obj).fadeIn(time,function(){

					
				
					if(obj > 0)
							{
	
							}
					});
					
					$('#background').children().eq(obj-1).fadeOut(time);
				}
				else
				{
					$('#background').children().last().fadeOut(time);
					$('#background').children().eq(obj).fadeIn(time);
				}
					

						
			}	
			}
			
			
			
			
	function clearTimeOuts(photos)
	{

		var i=0;
		for(i=0;i<timeouts.length;i++)
		{
	
		clearTimeout(timeouts[i]);
		
		}
		$('#background').children().eq(0).fadeIn(3000,function(){
		
			if(photos != 0)
			{
				start_bg_animation(photos);
			}
		});
		
			for(i=1;i<$('#background').children().size();i++)
			{
				
					$('#background').children().eq(i).fadeOut(3000,function(){
					
					
					});
	
			
			}

		
	}
	
	
	
	
var JNWPL_delay_time = 13000;
var JNWPL_fade_time = 3000;

function JNWPL_runBanners(i)
{

var delay_time = JNWPL_delay_time;

	banners = jQuery('#background').children();
	
	if(i == 0)
	{
		delay_time = 0;
		banners.eq(i).css('display','block');
	}
	

	
	if(banners.size()>1)
	{

		banners.eq(i).delay(delay_time).fadeIn(JNWPL_fade_time,function()
		{
			if(i>1)
			{
				banners.eq(i-1).css('display','none');
			}
		
			if((i+1)<banners.size())
			{
				JNWPL_runBanners(i+1);
			}
			else
			{
				banners.first().css('display','block');
				banners.eq(i).delay(JNWPL_delay_time).fadeOut(JNWPL_fade_time,function()
				{
					JNWPL_runBanners(1);
				});
			
			}
		});
	
	}
	else
	{
		banners.first().css('display','block');
	}



}

	
	
	
	
	
	
	
	
