$(document).ready(function()
{
	//hoogte collaboration rechtrekken
	var $topheight = $(".toprow").height();
	var $bottomheight = $(".bottomrow").height();
	var $newheight = $topheight+$bottomheight+20;
	$("#introwrapper").css({'height' : ''+$newheight+'px'});
	
	$("ul#view a").click(function()
	{
		$divstext = $("div.p_text");
		$divspics = $("div.p_pic");
		
		var $zindex = new Array();
		$zindex[0] = $divstext.eq(0).css("z-index");
		$zindex[1] = $divspics.eq(0).css("z-index");
		
		if(!$(this).parent().hasClass("title"))
		{
			if($zindex[0]<300)
			{
				$divspics.each(function()
				{
					$(this).css({'z-index' : '200'});
				});
				$divstext.each(function()
				{
					$(this).css({'z-index' : '300'});
				});
			}
		}
		else
		{
			if($zindex[1]<300)
			{
				$divstext.each(function()
				{
					$(this).css({'z-index' : '200'});
				});
				$divspics.each(function()
				{
					$(this).css({'z-index' : '300'});
				});
			}
		}
		//.addClass('title') .removeClass('title')
		var p = $(this).parent();
		if(!p.is(":first-child")) {
			p.insertBefore($("ul#view li:first"));
		}
		p.siblings().hide();
		return false;
	});
	
	//p op index
	$("#indexcontent p:nth(0)").attr({ id: "newsfirst" });
	
	//in en uitklap dropdown
	$("div.dropdown").children("ul").each(function()
	{
		var $i=1;
		$(this).children("li").hide().each(function()
		{
			var $li = $(this);
			if($i==1)
			{
				$li.show();
			}
			
			$li.click(function(event)
			{				
				if($(event.target).is('li'))
				{
					if($li.siblings(":visible").size()<1)
					{
						$li.siblings().show();
					}
					else
					{
						$li.siblings().hide();
					}
				}
			});
			$i++;
		});
	});
	
	$(".preloader img").hide();
	
	$(".p_wrapper div:not(:first-child)").hide();
	
	$(window).bind("load", function()
	{
		$(".preloader img").fadeIn(600);
		$(".p_wrapper, .newsnav li").each(function()
		{
			var $a = $(this).find("a");
			$($a).click(function()
			{
				window.location=""+$a.attr("href")+"";
			});
			$(this).click(function()
			{
				window.location=""+$a.attr("href")+"";
			});
			
			var $kids = $(this).children("div");
			$($kids).eq(1).fadeIn(600, function()
			{
				$($kids).eq(0).hide();
				$($kids).eq(2).show();
			});
		});
	});
	
	$(".p_wrapper").hover(function()
	{
		$(this).children("div:visible").each(function()
		{
			if($(this).css("z-index")=="300")
			{
				$(this).css({'z-index' : '200'});
			}
			else if($(this).css("z-index")=="200")
			{
				$(this).css({'z-index' : '300'});
			}
		});
	}, 
	function()
	{
		$(this).children("div:visible").each(function()
		{
			if($(this).css("z-index")=="300")
			{
				$(this).css({'z-index' : '200'});
			}
			else if($(this).css("z-index")=="200")
			{
				$(this).css({'z-index' : '300'});
			}
		});
	});
	
	//form text fields
	$("input, textarea").focus(function()
	{
		$val = $(this).val();
		$(this).val('');
	}).blur(function()
	{
		if($(this).val()=='')
		{
			$(this).val($val);
		}
	});
	
	//form submit click
	$("#linksrow a").eq(0).click(function()
	{
		$("form").submit();
		return false;
	});
	
	safariMobileCheck();
	function safariMobileCheck()
	{
		var $mobileredir = false;
		
		var $regexpattern = new Array();
		$regexpattern[0] = new RegExp("iPhone");
		$regexpattern[1] = new RegExp("iPod");
		$regexpattern[2] = new RegExp("BlackBerry");
		$regexpattern[3] = new RegExp("Nokia");
		$regexpattern[4] = new RegExp("Opera Mini");
		
		for(var $i=0; $i<$regexpattern.length; $i++)
		{
			if($regexpattern[$i].test(navigator.userAgent))
			{
				$mobileredir = true;
			}
		}
		
		if($mobileredir===true)
		{
			window.location = "http://www.anothercompany.org/site/mobile";
		}
	}
});