// JavaScript Document
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function addToMyList(str){
	if(str != undefined && str != ''){
		var myEpifanes = readCookie('myEpifanes');
		if( myEpifanes == null || myEpifanes == '' || myEpifanes == undefined ) { createCookie('myEpifanes',''); myEpifanes = ''; }
		else myEpifanes = myEpifanes + '[-]';
		regex = '(^|\\[\\-\\])' + str + '($|\\[\\-\\])';
		reg = new RegExp(regex);
		if(!reg.test(myEpifanes)){
			myEpifanes = myEpifanes + str;
			createCookie('myEpifanes',myEpifanes,5000);
		}else{
			alert(STRING_MOD_MYEPIFANES_EXISTS);
			return;
		}
	}else{
		alert('Wrong action');
		return;
	}
	$("#mylist span").html(parseInt($("#mylist span").html())+1);
	alert(STRING_MOD_MYEPIFANES_ADDED);
}

function removeFromMyList(str){
	if(str != undefined && str != ''){
		var myEpifanes = readCookie('myEpifanes');
		if( myEpifanes == null || myEpifanes == '' || myEpifanes == undefined ) { createCookie('myEpifanes',''); myEpifanes = ''; }
		myEpifanes = myEpifanes.replace(str+'[-]','');
		myEpifanes = myEpifanes.replace('[-]'+str,'');
		myEpifanes = myEpifanes.replace(str,'');
		createCookie('myEpifanes',myEpifanes,5000);
		window.location = window.location;
	}
}

function fixColls(){
	// COLUMNS: Make middle as high as the highest
	var maxColHeight = 0;
	$('#contentBlock .column:not(.ignore)').each(function(){
		maxColHeight = $(this).height() > maxColHeight ? $(this).height() : maxColHeight;
	});
	$('#contentBlock .column.middle:not(.ignore)').height(maxColHeight);

}
$(document).ready(function () {
	
	// FANCYBOX
	$("a.fancybox:has(img)").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	200, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
	
	// RADIO: activate the parent element
	$(".Jradio input").click(function(){					
		if($(this).is(':checked')) {
			$(this).parent().siblings('.active').removeClass('active');
			$(this).parent().addClass('active');
		}
	});
	
	// PRINT
	$('#print:not(.nonActive)').click(function(){
		window.print();
	});
	
	// SEARCH: make searchbutton
	$('#searchForm').submit(function(e){
		e.preventDefault();
		window.location = '/'+LANGUAGE+'/search/_q_'+$('#searchVal').val();
	});
	$('#startSearch').click(function(e){
		e.preventDefault();
		window.location = '/'+LANGUAGE+'/search/_q_'+$('#searchVal').val();
	});
	
	// FOCUS
	if($('.focus').length)
		$(document).scrollTop( $('.focus').offset().top-75 );
	
	// TARGET: all links to other sites target _blank
	$('a[href^="http://"]').attr('target','_blank');
	
	// FOCUS: focus on the first element on the page with class "focus"
	$('.jFocus:first').focus();

	// CONFIRM: asked to confirm before leaving to href url
	$(".jConfirm").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   
		var url = this.href; 
		var title = this.title;
		var yousure = confirm(title);
		
		if (yousure == true) {
			document.location = url;
		} else {
			// Do nothing
		}		
	
	});
	
	// INPUT: nice labels
	$(":text, textarea").each(function () {
		
		if ( $(this).attr("title") && $(this).val() == '' ) {
			$(this).val($(this).attr("title"));
		}else if($(this).val() != ''){
			$(this).addClass('inputFocus');
		}
		$(this).focus(function () {
			$(this).addClass('inputFocus');
			if ($(this).val() === $(this).attr("title")) {
				$(this).val("");
			}
		}).blur(function () {
			if ($(this).val() === "") {
				$(this).removeClass('inputFocus');
				$(this).val($(this).attr("title"));
			}
		});
	
	});

	
	// CLICKBOX: Make boxes clickable
	$('.clickBox a.clickEvent').click(function(e){e.preventDefault();}); //disable the clickevent link
	$(".clickBox").each(function () {
								  
		$(this).click(function(e){
			var href = $(this).find("a.clickEvent").attr("href");
			
			if (href){
				var target = $(this).find("a.clickEvent").attr("target");
				
				if (target == "_blank"){
					window.open(href);
				} else {
					document.location = href;
				}
			}else{
			}
		});
		$(this).hover(function() { 
			$(this).addClass('active');   
		  }, function() {   
			$(this).removeClass('active');
		});
	});
	
	//STORES
	$('.listToggle').next('ul').hide();
	$('.listToggle').unbind('click').click(function(e){
		e.preventDefault;
		$('.listToggle').removeClass('stayActive').next('ul').hide();
		$(this).addClass('stayActive').next('ul').toggle();
		fixColls();
	});
	fixColls();	
	
	// MY EPIFANES
	$('.addToMyList, .listAdd').click(function(e){
		e.preventDefault();
		if($(this).attr('id') && confirm('Toevoegen aan Mijn Epifanes?')){
			addToMyList($(this).attr('id'));
		}
	});
	// MY EPIFANES
	$('.removeFromMyList').click(function(e){
		e.preventDefault();
		if($(this).attr('id') && confirm('Verwijderen uit Mijn Epifanes?')){
			removeFromMyList($(this).attr('id'));
		}
	});
});
