
function sendafriend(s) {
	
	alert(s);
	
}

/**
 * Advies op maat 
 */

function _adviceInit() {
	
	if ($('#formRadio').length == 1) {
		
		var combination = "";
		
		var items = $('#formRadio div');
		
		for (var i=0; i<items.length; i++) {
			
			combination += $(items[i]).find('input:checked').val();
		
		};
		
		$.post('/', { action: 'processAdvice', combination: combination }, function (response) {
			
			if (response) {
				
				$('#qQuestions').html(response);
				
				//window.location = response;
				
			} else {
				
				alert("Helaas, er kon geen advies worden gevonden");
				
			}
		
		});
		
	}
	
}

/**
 * Animation popups
 */

function _popupInit() {
	
	if ($('.popup').length > 0) {
		
		$('a.popup').each(function () {
			
			$(this).addClass('iframe');
			
			$(this).fancybox( { 'hideOnContentClick': false } );
			
		});
		
	}
	
}

/**
 * QUESTIONS
 */


function _questionProcessEducator() {
	
	var elem = $('#formEducator input:checked');
	
	if (elem.length == 4) {
	
		var answers = "";
		
		for (i=0; i<elem.length; i++) {
			
			answers += $(elem[i]).val()+",";
			
		}
		
		$.post('/', { action: 'processEducator', answers: answers }, function (response) {
			
			$('div#qEducator').html(response);
			
		});
		
	} else if (elem.length > 4) {
		
		alert("U heeft te veel stellingen aangevinkt. Uw moet uit 4 stellingen kiezen.");
		
	} else if (elem.length < 4) {
		
		alert("U heeft te weinig stellingen aangevinkt. Uw moet uit 4 stellingen kiezen.")
		
	}
	
}

function _createSigaretteCase(txt) {
	
	$.post('/', { action: 'createCasing', txt: txt }, function (response) {
		
		$('#casing').html(response);
	
	});
	
}

function _questionProcessCosts() {
	
	var year1 = $('[name=year1]').val();
	var year2 = $('[name=year2]').val();
	var packages = $('[name=number]').val();
	
	$.post('/', { action: 'processCosts', year1: year1, year2: year2, packages: packages }, function (response) {
		
		$('p#answer').html(response);
	
	});
	
}


function _questionProcessSelftest() {
		
	var countQuestions = $('#selftest div.qItem').length;
	
	if ($('#selftest input:checked').length == countQuestions) {
		
		var items = $('#selftest div.qItem');
		
		var error = new Array();
		
		for (i=0; i<items.length; i++) {
			
			if ( $(items[i]).find('input:checked').length == 1)  {
				
				if ($(items[i]).find('input:checked').parent('li').attr('ok') == '0') {
				
					error.push($(items[i]).find('input:checked').parent('li').parent('ul').attr('num'));
					
				} 
				
			}
			
		}
		
		var r = "";

		
		for (i=0; i<error.length; i++) {
			
			r += error[i]+",";
			
		}
		
		$.post('/', { action: 'processSelftest', r: r }, function (response) {
			
			$('#selftest').html('<p>'+response+'</p>');
			
			_popupInit();
			
			$('#selftest').fadeIn(200);
		
		});
		
		
	} else {
		
		alert("U heeft nog niet alle vragen beantwoord.");
		
	}
		
}

function _questionProcessPercentage(q) {
				
	if ($('[name=val]').val() != "") {
		
		if (isNaN($('[name=val]').val())) {
			
			alert("U kunt alleen getallen invullen.");
			
		} else {
			
			$.post('/', { action: 'processPercentage', q: q, val: $('[name=val]').val() }, function (response) {
				
				$('#qPercentage').html(response);
			
			});
			
		}
		
	} else {
		
		alert("U heeft nog geen antwoord ingevuld.");
		
	}
		
}

function _questionProcessRadio() {
	
	var question = $('[name=question]').val();
	
	var cnt = 0;
	var error = 0;
	
	var items = $('div#qQuestions #formRadio div');
	
	for (var i=0; i<items.length; i++) {
		
		if ($(items[i]).find('input:checked').length == 0) {
		
			error++;
			
		}
	
	};
	
	if (error == 0) {
		
		for (var i=0; i<items.length; i++) {
			
			var elem = $(items[i]).find('input:checked');
			
			for (var j=0; j<elem.length; j++) {
				
				if ($(elem[j]).val() == 0 || $(elem[j]).attr('yes') == '1') {
					
					cnt++;
					
				}
				
			}
		
		};
		
		$.post('/', { action: 'processRadio', question: question, count: cnt }, function (response) {
			
			if (response) {
				
				var content = response;
				
				$('#qQuestions').hide(300, function () {
				
					$('#qFeedback').html(content);
					
					$('#qFeedback').show(300);
				
				});
				
			} else {
				
				alert("Helaas, er kon geen advies worden gevonden");
				
			}
		
		});
		
	} else {
		
		alert("U heeft nog niet alle vragen beantwoord.");
		
	}		
	
}

function _questionInitMultiple() {
	
	if ($('div.question').length > 0) {
		
		$(this).find('li').each(function () {
			
			$(this).click(function () {
				
				var pointer = $(this).attr('pointer');
				
				if ($('feedback_'+pointer).length == 1) {
					
					var feedback = $('#feedback_'+pointer).val();
					
				} else {
				
					var feedback = $('#feedback_default').val();
					
				}
				
				$('div#feedback').html(feedback);
				
				$('div#inner').hide(500, function () { $('div#feedback').show(500); });
			
			});
		
		});
		
	}
	
}


function _questionInit() {

	/*
	if ($('div.question').length > 0) {
		
		$('div.question').each(function () {
			
			$(this).find('li').each(function () {
				
				$(this).click(function () {
					
					var pointer = $(this).attr('pointer');
					
					if ($('feedback_'+pointer).length == 1) {
						
						var feedback = $('#feedback_'+pointer).val();
						
					} else {
					
						var feedback = $('#feedback_default').val();
						
					}
					
					$('div#feedback').html(feedback);
					
					$('div#inner').hide(500, function () { $('div#feedback').show(500); });
				
				});
			
			});
		
		});
		
	}
	*/
	
	if ($('.question').length > 0) {
		
		var elem = $('.question');
		
		for (var i =0; i<elem.length; i++) {
		
			var question = $(elem[i]).accordion({
				
				active: '',
		        autoheight: false
			});
			
			$(elem[i]).find('ul.images li').each(function () {
				
				$(this).click(function () {
					
					var p = parseInt($(this).attr('pointer'));
					
					question.accordion("activate", p);						

				});
			
			});
		
		}
	
	}
	
	_questionInitMultiple();
	
}

/**
 * THEMES
 */

function _themeFetchContent(cid, cType) {

	var themeID = $('#themeID').val();
	var subthemeID = $('#subthemeID').val();
	
	if ( cid == null ) cid = '';
	if ( cType == null ) cType = '';
	
	if ( $('#ctype').val() != "") {
	
		cType = $('#ctype').val();
		$('#ctype').val('');
		
	}
	
	$('#themeContent').animate( { opacity: 0 }, 300, function () {
		
		$.post('/', { 
			
			action: 'fetchThemeContent',
			contentID: cid,
			cType: cType,
			themeID: themeID,
			subthemeID: subthemeID
			
			}, function (response) {
				
				if (response) {
					
					response = $.evalJSON(response);
					
					$('#themeContent').html(response.content);
					
					_popupInit();
					
					_questionInit();
					
					
						
				} else {
				
					$('#themeContent').html('<h1>Geen informatie beschikbaar</h1><p>Voor dit onderwerp is helaas nog geen informatie beschikbaar.</p>');
					
				}

				$('#themeContent').animate( { opacity: 1 }, 300 );
		
			});
		
	});
	
}


function _themeCreateNav(cType) {
	
	var themeID = $('#themeID').val();
	var subthemeID = $('#subthemeID').val();
	
	$.post('/', {
		
			action: 'fetchThemeNav', 
			themeID: themeID, 
			subthemeID: subthemeID, 
			cType: cType
			
		}, function (response) {
		
			if (response) {
			
				response = $.evalJSON(response);
				
				if (response.topnav) {
					
					$('#topNav').html(response.topnav);
					
					var elem = $('ul.topNav li');
					
					for (i=0; i<elem.length; i++) {
						
						$(elem[i]).click(function () {
							
							var items = $('#topNav li');
							
							for (j=0; j<items.length; j++) {
								
								$(items[j]).removeClass('active');
								
							}
	
							$(this).addClass('active');
							
							_themeHandleBottomNav();
							
							_themeFetchContent($(this).attr('cid'));
						
						});
						
					}
					
				}
				
			} else {
			
				$('#topNav').html('');
				
			}
			
			if ($('#contentID').val() != '') {
			
				var elem = $('ul.topNav li');
				
				for (i=0; i<elem.length; i++) {
					
					if ($(elem[i]).attr('cid') == $('#contentID').val()) {
						
						$(elem[i]).addClass('active');
						
					} else {
						
						$(elem[i]).removeClass('active');
						
					}
					
				}
				
				$('#contentID').val('');
				
			}
			
			_themeInitBottomNav();
	
		});	
	
}

function _themeInitBottomNav() {
	
	if ($('#topNav ul li').length > 1) {
		
		$('#bottomNav').show();
		
		if ($('#bottomNav').length == 1) {
				
			$('ul.bottomNav li.next').unbind('click');
			
			$('ul.bottomNav li.next').click(function () {
				
				if ($('ul.topNav li.active').next('li').length == 1) {
					
					var next = $('ul.topNav li.active').next('li');
					
					$('ul.topNav li.active').removeClass('active');
					
					$(next).addClass('active');
					
					_themeHandleBottomNav();
					
					_themeFetchContent($(next).attr('cid'), '');
					
				}
				
			});
			
			$('ul.bottomNav li.prev').unbind('click');
			
			$('ul.bottomNav li.prev').click(function () {
				
				if ($('ul.topNav li.active').prev('li').length == 1) {
					
					var prev = $('ul.topNav li.active').prev('li');
					
					$('ul.topNav li.active').removeClass('active');
					
					$(prev).addClass('active');
					
					_themeHandleBottomNav();
					
					_themeFetchContent($(prev).attr('cid'), '');
					
				}
			
			});
			
			_themeHandleBottomNav();
			
		}
		
	} else {
	
		$('#bottomNav').hide();
		
	}
	
}

function _themeHandleBottomNav() {
	
	if ($('ul.topNav li').length > 1) {
		
		if ($('ul.topNav li.active').next('li').length == 0) {
			
			$('ul.bottomNav li.next').hide(200);
			
		} else {
			
			$('ul.bottomNav li.next').show(200);
			
		}
		
		if ($('ul.topNav li.active').prev('li').length == 0) {
			
			$('ul.bottomNav li.prev').hide(200);
			
		} else {
			
			$('ul.bottomNav li.prev').show(200);
			
		}
		
	} else {
		
		alert("topNav NIET gevonden");
		
	}
	
}

function _themeInitMenuTypes() {
	
	$('ul.themeTypes li a').hover(function () {

		if (!$(this).hasClass('active')) { 
			
			var imgSrc = $(this).find('img').attr('src');
			imgSrc = imgSrc.replace('-trans.png', '-hover-trans.png');
				
			$(this).addClass("hover").stop().
				find('img').attr('src', imgSrc);
			
		}
			
	
	}, function () {
		
		if (!$(this).hasClass('active')) {
			
			var imgSrc = $(this).find('img').attr('src');
			imgSrc = imgSrc.replace('-hover-trans.png', '-trans.png');
			
			$(this).removeClass("hover").stop().
				find('img').attr('src', imgSrc);
			
		}
	
	});
	
}

function _fetchMoreInfo() {
		
	_themeFetchContent('', 'More info');
	_themeCreateNav('More info', '');
	
	var items = $('ul.themeTypes li');
	
	for (i=0; i<items.length; i++) {
		
		if ($(items[i]).find('a').attr('ctype') != 'More info') {
			
			$(items[i]).find('a').removeClass('active');
			
			var imgSrc = $(items[i]).find('img').attr('src');
			imgSrc = imgSrc.replace('-hover-trans.png', '-trans.png');
			
			$(items[i]).find('img').attr('src', imgSrc);
			
		} else {
			
			$(items[i]).find('a').removeClass('active');
			
			var imgSrc = $(items[i]).find('img').attr('src');
			imgSrc = imgSrc.replace('-trans.png', '-hover-trans.png');
			
			$(items[i]).find('img').attr('src', imgSrc);
			
		}
		
	}
	
}


function _themeSetSubmenu(cType) {
	
	var items = $('ul.themeTypes li');

	for (i=0; i<items.length; i++) {
		
		var t = $(items[i]).find('a').attr('ctype');
		
		if (t.toLowerCase() != cType) {
			
			$(items[i]).find('a').removeClass('active');
			
			var imgSrc = $(items[i]).find('img').attr('src');
			imgSrc = imgSrc.replace('-hover-trans.png', '-trans.png');
			
			$(items[i]).find('img').attr('src', imgSrc);
			
		} else {
			
			$(items[i]).find('a').addClass('active');
			
			var imgSrc = $(items[i]).find('img').attr('src');
			imgSrc = imgSrc.replace('-trans.png', '-hover-trans.png');
			
			$(items[i]).find('img').attr('src', imgSrc);
			
		}
		
	}
	
	
}

function _themeInit() {
	
	$('ul.themeTypes li').each(function () {
		
		$(this).find('a').click(function () {
			
			var items = $('ul.themeTypes li');
			
			for (i=0; i<items.length; i++) {
				
				$(items[i]).find('a').removeClass('active');
				
				var imgSrc = $(items[i]).find('img').attr('src');
				imgSrc = imgSrc.replace('-hover-trans.png', '-trans.png');
				
				$(items[i]).find('img').attr('src', imgSrc);
				
			}
			
			$(this).addClass('active');
							
			var imgSrc = $(this).find('img').attr('src');
			imgSrc = imgSrc.replace('-trans.png', '-hover-trans.png');
			
			$(this).find('img').attr('src', imgSrc);
			
			$('#activeType').val( $(this).attr('ctype') );
			
			_themeFetchContent('', $(this).attr('ctype'));
			_themeCreateNav($(this).attr('ctype'), '');
		
		});
	
	});
	
	/**
	 * Handle theme buttons
	 */
	
	$('ul.subThemeWrapper').each(function () {
		
		$(this).find('a').click(function () {
			
			$('#subthemeID').val($(this).attr('subtheme'));
			
			/**
			 * Remove active type items
			 */
			
			var items = $('ul.themeTypes li');
			
			for (i=0; i<items.length; i++) {
				
				$(items[i]).find('a').removeClass('active');
				
				var imgSrc = $(items[i]).find('img').attr('src');
				imgSrc = imgSrc.replace('-hover-trans.png', '-trans.png');
				
				$(items[i]).find('img').attr('src', imgSrc);
				
			}
			
			/**
			 * Fetch default theme content
			 */
			
			_themeFetchContent('', 'Content');
			
			/**
			 * Create theme navigation
			 */
			
			_themeCreateNav('Content');
			
			_themeHandleEvents($(this));
		
		});
	
	});
	
	if ( $('#ctype').val() != '' ) {
		
		_themeSetSubmenu($('#ctype').val());
		_themeFetchContent($('#ctype').val(), 'Content');
		
		
		$('#ctype').val('');
		
	}
	
	_themeCreateNav('Content');
	
	_themeInitMenuTypes();
	
	_popupInit();
	
	_questionInit();
	
}

function _themeHandleEvents(sender) {
	
	$('ul.subThemeWrapper li').each(function () {
						
		if ($(this).hasClass('left')) {
			
			if (sender.attr('subtheme') == $(this).find('a').attr('subtheme') && sender.attr('subtheme') != 1) {
			
				$(this).find('a').attr('subtheme', '1');
				$(this).find('a').attr('orig', $(this).find('img').attr('src'));
				
				$(this).find('img').attr('src', $(this).find('a').attr('main'));
				
			} else {
				
				if ($(this).find('a').attr('orig')) {
				
					$(this).find('a').attr('subtheme', '2');
					$(this).find('img').attr('src', $(this).find('a').attr('orig'));
					
				}
				
			}
			
		} else {
			
			if (sender.attr('subtheme') == $(this).find('a').attr('subtheme') && sender.attr('subtheme') != 1) {
				
				$(this).find('a').attr('subtheme', '1');
				$(this).find('a').attr('orig', $(this).find('img').attr('src'));
				
				$(this).find('img').attr('src', $(this).find('a').attr('main'));
				
			} else {
				
				if ($(this).find('a').attr('orig')) {
				
					$(this).find('a').attr('subtheme', '3');
					$(this).find('img').attr('src', $(this).find('a').attr('orig'));
					
				}
				
			}
			
		}
		
	});
	
}

$(document).ready(function () {
	
	_themeInit();

});
