/*--- inputs actions original---*/
/*
function initInputs(){
	var arr = new Array("input", "textarea")
	for (var j=0; j<arr.length; j++){
		var inputs = document.getElementsByTagName(arr[j]);
		for (var i=0; i<inputs.length; i++){
			if((inputs[i].type == 'text' || inputs[i].type == 'password') || inputs[i].tagName == 'TEXTAREA'){
				inputs[i]._val = inputs[i].value;
				inputs[i].onfocus = function(){
					this.parentNode.className += " focus";
					if(this.value == this._val) this.value = '';
				}
				inputs[i].onblur = function(){
					this.parentNode.className = this.parentNode.className.replace("focus", "");
					if(this.value == '') this.value = this._val;
				}
			}
		}
	}
}
*/
/*--- inputs actions changed: should not empty the boxes by clicking. It should just happen for searchbox ---*/
function initInputs(){
	var arr = new Array("input", "textarea")
	for (var j=0; j<arr.length; j++){
		var inputs = document.getElementsByTagName(arr[j]);
		for (var i=0; i<inputs.length; i++){
		
			if((inputs[i].type == 'text' || inputs[i].type == 'password') || inputs[i].tagName == 'TEXTAREA'){
				inputs[i]._val = inputs[i].value;
				inputs[i].onfocus = function(){
					this.parentNode.className += " focus";
					if((this.id == 'search' && this.value == 'met trefwoorden, vakgebied, ics code') || this.id == 'login_username' || this.id == 'login_password'){
						if(this.value == this._val) this.value = '';
					}
				}
				inputs[i].onblur = function(){
					this.parentNode.className = this.parentNode.className.replace("focus", "");
					if((this.id == 'search' && this._val == 'met trefwoorden, vakgebied, ics code') || this.id == 'login_username' || this.id == 'login_password'){
						if(this.value == '') this.value = this._val;
					}
				}
			}
		}
	}
}

function popups(){
	var _holder = $('.popups');
	var _d = 400;
	_holder.each(function(){
		var _items = $(this).find('li');
		_items.each(function(){
			var _this = $(this);
			_this.popup = $(this).find('.popup:eq(0)');
			_this.h = _this.popup.show().height();
			_this.f = false;
			_this.popup.css({top: 10});
			_this.pCont = _this.popup.find('.c:eq(0)');
			_this.popup.h = _this.pCont.height();
			_this.pCont.css({height: 0, position: 'relative'});
			_this.popup.hide();
			_this.mouseenter(function(){
				_this.popup.show();
				_this.popup.animate({top: -_this.h/2 + 24},{duration:_d, queue: false});
				_this.pCont.animate({height: _this.popup.h},{duration:_d, queue: false});
			}).mouseleave(function(){
				if(!_this.f){
					_this.popup.animate({top: 10},{duration:_d, queue: false});
					_this.pCont.animate({height: 0},{duration: _d,queue: false,complete: function(){
						_this.popup.hide();
					}});
				}
			});
			_this.popup.mouseenter(function(){
				_this.f = true;
			}).mouseleave(function(){
				_this.f = false;
			})
		})
	})
}

//* jquery tabs *********************************************************************************
var _couter = 0;
jQuery.fn.jqueryTabs = function(_options){
	// defaults options
/*
		event: 'click','mouseover','mouseleave'
*/
	var _options = jQuery.extend({
		event: 'click',
		tabTitle: 'ul.tabcontent',
		tabNavHolder: false,
		useCloseIcon: false,
		customNav: null,
		onChange: null,
		selected: 0,
		tabNavClass: 'tabset'
	},_options);

	return this.each(function(){
		var _this = $(this);
		var closeIcon = $("#close-icon");
				
		_this.css('position','relative');
		var _tabs = _this.children();
		_tabs.addClass('tab');
		var _tabNav='';
		var _a = _options.selected, _prev = -1;
		_tabs.removeClass('active').eq(_a).addClass('active');
		if (_options.useCloseIcon) closeIcon.hide();

		// build tabset navigation
		_tabs.each(function(_ind){
			$(this).attr('id','tab'+_couter+'-'+_ind);
			var _title = $(this).find(_options.tabTitle);
			_tabNav += '<li><a href="#tab'+_couter+'-'+_ind+'">'+_title.html()+'</a></li>';
			_title.remove();

			//hide tab
			if(_a == _ind){
				_this.css({
					height: _tabs.eq(_a).outerHeight(true)+'px'
				})
				$(this).css({
					position: 'absolute',
					overflow: 'hidden',
					left: '0'
				});
				
			}else{
				$(this).css({
					position: 'absolute',
					overflow: 'hidden',
					left: '-9999px'
				});
				
			}
		});
		_tabNav = '<ul class="'+_options.tabNavClass+'">' + _tabNav + '</ul>';
		var _tabSet = $(_tabNav);
		var _tabLinks = _tabSet.find('li');

		//insert Navigation
		if(!_options.tabNavHolder) _this.before(_tabSet)
		else $(_options.tabNavHolder).append(_tabSet);
		_tabLinks.eq(_a).addClass('active');

		//custom navigation build
		if (jQuery.isFunction(_options.customNav)){
			_options.customNav.apply(_tabSet);
		}

		if (_options.useCloseIcon) {
			closeIcon.click(function() {
				_options.onChange.apply(_tabLinks.eq(_a));
				closeIcon.hide();
			});
		}
		
		_tabLinks.click(function(){
			if(!$(this).hasClass('active')){
								
				_prev = _a;
				_a = _tabLinks.index(this);
								
				//on Change
				if (jQuery.isFunction(_options.onChange)){
					_options.onChange.apply(_tabLinks.eq(_a));
				}

				if($.browser.msie){
					var _next = $(this.getElementsByTagName('a')[0].hash);
					var _cur;
					_tabLinks.eq(_prev).each(function(){
						_cur = $(this.getElementsByTagName('a')[0].hash);
					})
				}else{
					var _next = $($(this).find('a:eq(0)').attr('href'));
					var _cur = $(_tabLinks.eq(_prev).find('a:eq(0)').attr('href'));
				}
				_next.css({
					left: '0'
				}).addClass('active');
				if(_tabLinks.eq(_prev).hasClass('active')){
					_cur.css({
						left: '-9999px'
					}).removeClass('active');
				}
				var _h = _next.outerHeight(true);
				_this.css({height: 80 + _h + "px"});
				_tabLinks.removeClass('active').eq(_a).addClass('active');
				if (_options.useCloseIcon) closeIcon.show();
				
			}else{
				
				if (_options.useCloseIcon) closeIcon.hide();
				
				_a = _tabLinks.index(this);
				//on Change for active tab
				if (jQuery.isFunction(_options.onChange)){
					_options.onChange.apply(_tabLinks.eq(_a));
				}
				
			}
			return false;
		});
		_couter++;
	})
}

function initPage(){
	initInputs();
	popups();
	$('.carusel-overlay').css({opacity: 0});

	//init Content tabs
	$('ul.tabs-frame').jqueryTabs({
		tabTitle: 'div.tab-title',
		tabNavClass: 'tabs-nav tabset'		
	});
	//init tabs in header 
	$('ul.tabs-holder').jqueryTabs({
		tabTitle: 'div.tab-title',
		tabNavHolder: '#header-holder div.nav',
		selected: -1,
		useCloseIcon: true,
		customNav: function(){
			$(this).find('li:first').attr('id','first').prepend('<em>&nbsp;</em>');
			$(this).find('li:last').attr('id','last').append('<em>&nbsp;</em>');
		},
		onChange: function(){
			var _this = $(this);
			if(_this.hasClass('active')){
				if(_this.parents('.carousel:eq(0)').length){
					_this.parents('.carousel:eq(0)').animate({bottom: -170},400,function(){
						_this.removeClass('active');
						var _cur;
						if($.browser.msie){
							_this.each(function(){
								_cur = $(this.getElementsByTagName('a')[0].hash);
							})
						}else{_cur = $(_this.find('a:eq(0)').attr('href'));}
						_cur.css({left: '-9999px'})
					});
					$('.carusel-overlay').fadeTo(400,'0',function(){
						$(this).hide();
					});
				}
			}else{
				if(_this.parents('.carousel:eq(0)').length) {
					_this.parents('.carousel:eq(0)').animate({bottom: -1},400);
					$('.carusel-overlay').show().fadeTo(400,'0.5');
				}
			}
		}
	});
	
	SimplyButtons.init();
	
	// Facuuradres gelijk houden aan bezoekadres
	$('#bezoekadres_land').change(function() {
		if ($('#factuuradres_gelijkaanbezoekadres').is(':checked')) {
			$('#factuuradres_land').val($("#bezoekadres_land").val());
		}
	});
	$('#bezoekadres_postcode').change(function() {
		if ($('#factuuradres_gelijkaanbezoekadres').is(':checked')) {
			$('#factuuradres_postcode').val($("#bezoekadres_postcode").val());
		}
	});
	$('#bezoekadres_huisnummer').change(function() {
		if ($('#factuuradres_gelijkaanbezoekadres').is(':checked')) {
			$('#factuuradres_huisnummer').val($("#bezoekadres_huisnummer").val());
		}
	});
	$('#bezoekadres_huisnummertoevoeging').change(function() {
		if ($('#factuuradres_gelijkaanbezoekadres').is(':checked')) {
			$('#factuuradres_huisnummertoevoeging').val($("#bezoekadres_huisnummertoevoeging").val());
		}
	});
	$('#bezoekadres_straat').change(function() {
		if ($('#factuuradres_gelijkaanbezoekadres').is(':checked')) {
			$('#factuuradres_straat').val($("#bezoekadres_straat").val());
		}
	});
	$('#bezoekadres_woonplaats').change(function() {
		if ($('#factuuradres_gelijkaanbezoekadres').is(':checked')) {
			$('#factuuradres_woonplaats').val($("#bezoekadres_woonplaats").val());
		}
	});
	$('#factuuradres_gelijkaanbezoekadres').change(function() {
		if ($('#factuuradres_gelijkaanbezoekadres').is(':checked')) {
			$('#factuuradres_land').attr('readonly','readonly').val($("#bezoekadres_land").val());
			$('#factuuradres_postcode').attr('readonly','readonly').val($("#bezoekadres_postcode").val());
			$('#factuuradres_huisnummer').attr('readonly','readonly').val($("#bezoekadres_huisnummer").val());
			$('#factuuradres_huisnummertoevoeging').attr('readonly','readonly').val($("#bezoekadres_huisnummertoevoeging").val());
			$('#factuuradres_straat').attr('readonly','readonly').val($("#bezoekadres_straat").val());
			$('#factuuradres_woonplaats').attr('readonly','readonly').val($("#bezoekadres_woonplaats").val());
			
		} else {
			$("#factuuradres_land").removeAttr("readonly");
			$("#factuuradres_postcode").removeAttr("readonly");
			$("#factuuradres_huisnummer").removeAttr("readonly");
			$("#factuuradres_huisnummertoevoeging").removeAttr("readonly");
			$("#factuuradres_straat").removeAttr("readonly");
			$("#factuuradres_woonplaats").removeAttr("readonly");
		}
	});
	//factuur adres stuff
	
	/*closeAll();
	$("#factuur_naar input").click(function () {
		//switch
		switch ($('#factuur_naar input:checked').val()) {
			case 'P':
				open([0,2]);
			break;
			case 'W':
				open([0,1,2]);
			break;
		}
	
	});
	*/
	//welk radio is checked?
	//if($("#factuur_naar input:checked").val() == 'P') open([0,2]);
	//if($("#factuur_naar input:checked").val() == 'W') open([0,1,2]);
	
	//neem over van bezoek adres
	$("#neem_over").click(function (){
		if ($("#neem_over").is(":checked")){
			$('#factuur_land').attr('readonly','readonly').val($("#bezoek_land").val());
			$('#factuur_postcode').attr('readonly','readonly').val($("#bezoek_postcode").val());
			$('#factuur_huisnr').attr('readonly','readonly').val($("#bezoek_huisnr").val());
			$('#factuur_huisnr_toevoeging').attr('readonly','readonly').val($("#bezoek_huisnr_toevoeging").val());
			$('#factuur_straat').attr('readonly','readonly').val($("#bezoek_straat").val());
			$('#factuur_woonplaats').attr('readonly','readonly').val($("#bezoek_woonplaats").val());
			
		} else {
			$("#ter_attentie_van").removeAttr("readonly");
			$("#factuur_land").removeAttr("readonly");
			$("#factuur_postcode").removeAttr("readonly");
			$("#factuur_huisnr").removeAttr("readonly");
			$("#factuur_huisnr_toevoeging").removeAttr("readonly");
			$("#factuur_straat").removeAttr("readonly");
			$("#factuur_woonplaats").removeAttr("readonly");
		}		
	});
	
	/*
	 * GSA Keymatch-specific javascript
	 */
	if($(".search-result").length > 0) {
		
		var keymatchUrl = $("#keymatchUrl").text();
		
		var amountOfKeymatches = $(".keymatch").length;
		if(amountOfKeymatches > 0) {	
			
			var listOfNormIds = new Array();
			
			$(".keymatch").each(function() {
				
				var classList = $(".keymatch").attr('class').split(/\s+/);
				$.each( classList, function(index, item) {
					if(item.indexOf('norm') >= 0) {
						
						var normId = item.substring(4, item.length);
						listOfNormIds.push(normId);
					}
				});
				
				for(i = 0; i < listOfNormIds.length; i++) {
					var url = keymatchUrl + '?contentid='+listOfNormIds[i];
					
					if(document.location.toString().indexOf("redactie-") > 0){
						url = url.replace('http://','');
						url = "http://redactie-"+url;
					}
					$.get(url,function(html){
						$(".search-result .head").fadeIn().after(html);
					});
				}
			});
		}
	}
}

//factuur divs, will be used to display and hide divs according to radiobuttons checked
var factuurDivs = [
	"#cisf-ter-attentie-van",
	"#cisf-adres",
	"#cisf-uw-referentie"
];
var factuurHtmls = [];

function closeAll(){
	var i=0;
	$(factuurDivs).each(function() {
		var daDiv = factuurDivs[i];
		if($(daDiv).html() != ''){
			factuurHtmls[i] = $(daDiv).html();
		}
		$(daDiv+" input").attr("disabled","disabled");
		$(daDiv).hide();
		//$(daDiv).empty();
		i++;
	});
}

function factuurOpen(nr){
	closeAll();//closeall first
	for(i = 0 ; i < nr.length ; i ++){
		//$(factuurDivs[nr[i]]).html(factuurHtmls[nr[i]]);
		$(factuurDivs[nr[i]]+" input").removeAttr("disabled");
		$(factuurDivs[nr[i]]).show();
	}
}

function validateDutchZipCode() {
	var land = $("#bestelAfleverLand").val();
	var zip = $("#bestelAfleverPostCode").val();
	
	if (land == "NL") {
		
	}		
	
}

if (window.addEventListener) window.addEventListener("load", initPage, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initPage);

$(document).ready(function() {
	var lastSearchTerm = getCookie("searchTerm");
	$("#norm_zoeken_q").val(lastSearchTerm);
	$("#cursus_zoeken_q").val(lastSearchTerm);
	$("#artikelen_zoeken_q").val(lastSearchTerm);
	var newSearchTerm = $("#search").val();
	if (newSearchTerm != 'met trefwoorden, vakgebied, ics code') {
		createCookie("searchTerm", newSearchTerm, 1);
	} else {
		createCookie("searchTerm", "", 1);
	}
	
});																												

