/*
 * init javascript by CCI IT GROEP
 * http://www.cci-itgroep.nl/
 * Copyright (c) 2009 CCI IT GROEP
 */

/*== Search functions (New CMS) ==*/
function validateSearchForm()
{
	var searchTerm = encodeURIComponent($('#txtContentSearch')[0].value);
	if (searchTerm.length > 1 )
	{
		var searchUrl = $('#zoekUrl')[0].value;
		window.location.href = searchUrl + searchTerm;
	}
	else
	{
		alert('Voer een geldige zoekterm van minimaal 2 tekens in!');
	}
}

function setSearchBoxEventHandlers(txtId,btnId)
{
	$('#'+txtId).focus(function()
	{
		if ($(this).attr("value") == "zoeken")
		{
			$(this).attr("value","");
		}
	});
	$('#'+txtId).blur(function()
	{
		if ($(this).attr("value") == "")
		{
			$(this).attr("value","zoeken");
		}
	});
	$('#'+txtId).keydown(
		function(e)
		{
			if (e.keyCode == 13)
			{
				validateSearchForm();
				return false;
			}
		}
	);
	$('#'+btnId).click(
		function()
		{
			validateSearchForm();
			return false;
		}
	);
}
/*== ==*/

/*== setFont ==*/

// Wanneer er nog geen font aanwezig is in de cookie wordt deze automatisch toegewesen aan alle divs met de classname font_proportional
var initial_font = "font_small"; //font_small,  normal_font, font_big zorg dat deze tekst niet overruled wordt door andere classes

function setFont(font,fontSwitcherContainerId)
{
	if (!font || font == "")
	{
		font = readCookie("fontType");
	}
	else
	{
		font = font;
	}
	
	if (!font || font == "")
	{
		font = initial_font;
		createCookie("fontType",initial_font,366);
	}
	else
	{
		createCookie("fontType",font,366);
	}
	
	$("#"+fontSwitcherContainerId+" a").each(function(index)
	{
		$(this).removeClass("active");
		
		if (font.match(/font_small/))
		{
			if (index == 0)
			{
				$(this).addClass("active");
			}
		}
		else if (font.match(/font_normal/))
		{
			if (index == 1)
			{
				$(this).addClass("active");
			}
		}
		else if (font.match(/font_big/))
		{
			if (index == 2)
			{
				$(this).addClass("active");
			}
		}
	});
	
	var debugCollection = "";
	var currentClassName = false;
	
	$(".font_proportional").each(function()
	{
		$(this).removeClass("font_small");
		$(this).removeClass("font_normal");
		$(this).removeClass("font_big");
		$(this).addClass(font);
	});
	
	//javascript:alert(createCookie('fontType','',-1)); <-- Put this in URL to clear the cookie
}
/*== ==*/

/*== Cookie scripts ==*/
//createCookie("_cookieName","cookieValue",365,"/","","");
function createCookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function readCookie( check_name )
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
/*== ==*/

/*== checkActiveLvl3 ==*/
function checkActiveLvl3()
{
	$("#header_navigation_level3:not(:has(li))").remove();
}
/*== ==*/

/*== hideEmptyElements ==*/
function hideEmptyElements(expression)
{
	$(expression+":empty").css("display","none");
}
/*== ==*/

/*== hideParentFromEmptyElements ==*/
function hideParentFromEmptyElements(expression,parentExpression)
{
	$(expression+":empty").parents(parentExpression).css("display","none");
}
/*== ==*/

/*== setLoginFormEvents ==*/
function setLoginFormEvents()
{
	var username = $("#lbl_username").html();
	var password = $("#lbl_password").html();
	
	if ($("#input_container_username input").attr("value") == "" || $("#input_container_username input").attr("value") == username)
	{
		$("#input_container_username input").attr("value",username);
		$("#input_container_password input").attr("value",password);
		
		$("#input_container_username input").focus(function()
		{
			if ($(this).attr("value") == username)
			{
				$(this).attr("value","");
			}
		});
		$("#input_container_username input").blur(function()
		{
			if ($(this).attr("value") == "")
			{
				$(this).attr("value",username);
			}
		});
		$("#input_container_password input").focus(function()
		{
			if ($(this).attr("value") == password)
			{
				$(this).attr("value","");
			}
		});
		$("#input_container_password input").blur(function()
		{
			if ($(this).attr("value") == "")
			{
				$(this).attr("value",password);
			}
		});
	}
	
	$("#btn_login input").attr("value","");
}
/*== ==*/

/*== setTitleBackgroundIfActiveBlockLeft ==*/
function setTitleBackgroundIfActiveBlockLeft()
{
	if ($(".block_rubriek li.active").length)
	{
		var activeBlock = $(".block_rubriek li.active:eq(0)").parents(".block_rubriek:eq(0)");
		
		if ($(activeBlock).hasClass("block_rubriek_brown"))
		{
			$("#CCIPlaceHolder_Midden h2:eq(0)").addClass("block_rubriek_brown_active");
		}
		else if ($(activeBlock).hasClass("block_rubriek_lightblue"))
		{
			$("#CCIPlaceHolder_Midden h2:eq(0)").addClass("block_rubriek_lightblue_active");
		}
		else if ($(activeBlock).hasClass("block_rubriek_yellow"))
		{
			$("#CCIPlaceHolder_Midden h2:eq(0)").addClass("block_rubriek_yellow_active");
		}
		else if ($(activeBlock).hasClass("block_rubriek_darkblue"))
		{
			$("#CCIPlaceHolder_Midden h2:eq(0)").addClass("block_rubriek_darkblue_active");
		}
	}
}
/*== ==*/

/*== setFormbuilderClasses ==*/
function setFormbuilderClasses()
{
	$(".formbuilder_formpanel input").each(function(index)
	{
		$(this).addClass($(this).attr("type"));
	});
	
	$(".formbuilder_formpanel .formbuilder_element").each(function(index)
	{
		if ($(".formbuilder_formpanel .formbuilder_element:eq("+index+") tr").length > 1)
		{
			$(this).addClass("formbuilder_element_vertical");
		}
		
		$(this).children("input").each(function(index)
		{
			$(this).addClass("element_"+$(this).attr("type")+"_"+(index+1));
		});
	});
}
/*== ==*/

/*== setRubriekBlocks ==*/
function setRubriekBlocks()
{
	$(".block_rubriek .navigation ul.level1").each(function()
	{
		if (!$(this).children("li:eq(0)").hasClass("active"))
		{
			$(this).parents(".navigation:eq(0)").remove();
		}
	});
}
/*== ==*/

/*== setHeaderNavigation ==*/
function setHeaderNavigation()
{
	$('#header_navigation ul.level1').superfish({
		pathClass: 'active',
		delay: 400
	});
}
/*== ==*/

/*== initImgRoulet ==*/
function initImgRoulet()
{
	$('#img_roulet_pics').cycle({ 
		fx:     'fade',
		speed:  'fast',
		timeout: 3000, 
		pager:  '#img_roulet_nav'
	});
}
/*== ==*/

/*== setTabs ==*/
function setTabs(containerExpression)
{
	$(containerExpression + " > ul").tabs({ fx: { opacity: 'toggle' } });
}
/*== ==*/

/*== initTemplateFunctions ==*/
function initTemplateFunctions()
{
	if (typeof(templateFunctions) == "function")
	{
		templateFunctions();
	}
}
/*== ==*/

/*== showGotoMyProfile ==*/
function showGotoMyProfile(id)
{
	$("#"+id).html('<a id="text_loggedin_vervanging" href="\/cms\/showpage.aspx?id=27">Naar het ledennet</a>');
}
/*== ==*/

/*== Body onload ==*/
$(document).ready(function()
{
	setSearchBoxEventHandlers("txtContentSearch","btnContentSearch");
	setFont(false,"font_changer");
	hideEmptyElements(".hideIfEmpty");
	hideParentFromEmptyElements("#midden_yellow_block h2","#midden_yellow_block");
	hideParentFromEmptyElements(".block_rubriek h3",".block_rubriek");
	hideParentFromEmptyElements(".block_general h4",".block_general");
	hideParentFromEmptyElements("#CCIPlaceHolder_Midden .text span.hideParentIfEmpty",".text");
	setFormbuilderClasses();
	setRubriekBlocks();
	checkActiveLvl3();
	setLoginFormEvents();
	setHeaderNavigation();
	setTitleBackgroundIfActiveBlockLeft();
	showGotoMyProfile("text_loggedin");
	initImgRoulet();
	initTemplateFunctions();
	
		
	$('#info').click(function(){
		var displayInfo = $('#infopopup').css('display');
		if(displayInfo == 'none')
		{
		    $('#infopopup').css('display', 'block');
		}
		else
		{
		    $('#infopopup').css('display', 'none');
		}
	});
});
/*== ==*/

