/**
* OOM Verzekeringen
*
* This script relies on the Fabrique js library.js v 1.1
*/
function setupHomepage() {
	if (!hasClass(document.body, 'home'))
		return false;

	// Setup javascript enhanced homepage
	var insurancetypeC =
      getElByClass('insurancetype-container');

	for (var i = 0; i < insurancetypeC.length; i++) {
		addClass(insurancetypeC[i], 'insurancetype-enhanced');
	}

	setupMainCases();
	setupHomeActivitiesNav();
}


/**
* Setup main cases behaviour
*/
function setupMainCases() {
	var mainCases = getElByClass('case-main');
	for (var i = 0; i < mainCases.length; i++) {
		addClass(mainCases[i], 'case-hidden');

		addEvent(mainCases[i], 'mouseover', function() {
			removeClass(this, 'case-hidden');
		});

		addEvent(mainCases[i], 'mouseout', function() {
			addClass(this, 'case-hidden');
		});
	}
}


/**
* Setup home activities navigation
*/
function setupHomeActivitiesNav() {
	var activitiesNav = getEl('activities-nav');
	var insuranceFor = getEl('insurance-for');
	var activitiesNavItems = activitiesNav.getElementsByTagName('a');

	if (!activitiesNav || !insuranceFor || (activitiesNav && !(activitiesNavItems.length > 0)))
		return false;

	var activitiesFormId = 'insuranceinfo-picker';
	var activitiesForm = document.createElement('form');
	activitiesForm.id = activitiesFormId;
	activitiesForm.action = '#FIXME';
	activitiesForm.method = 'post';

	var activitiesSelect = document.createElement('select');
	activitiesSelect.name = 'activities';
	activitiesSelect.id = 'activities';

	var activitiesSubmit = document.createElement('button');
	var activitiesSubmitTxt = document.createTextNode('Ga naar pagina');
	activitiesSubmit.appendChild(activitiesSubmitTxt);
	activitiesSubmit.setAttribute('type', 'submit');

	for (var i = 0; i < activitiesNavItems.length; i++) {
		var activity = document.createElement('option');
		activity.value = activitiesNavItems[i].href;
		var activityTxt = document.createTextNode(activitiesNavItems[i].innerHTML);
		activity.appendChild(activityTxt);
		addEvent(activity, 'click', function() {
			getEl(activitiesFormId).action = this.value;
		});

		activitiesSelect.appendChild(activity);
		if (i == 0)
			activitiesForm.action = activity.value;
	}

	addEvent(activitiesSelect, 'change', function() {
		document.location = this.value;
	});

	activitiesForm.appendChild(activitiesSelect);
	activitiesForm.appendChild(activitiesSubmit);
	insuranceFor.parentNode.appendChild(activitiesForm);

	activitiesNav.style.display = 'none';
}




/**
* Setup Special hyperlinks
*/
function setupSpecialAnchors() {
	var anchors = document.getElementsByTagName('A');
	for (var i = 0; i < anchors.length; i++) {
		// Setup print handlers
		if (hasClass(anchors[i], 'print'))
			addEvent(anchors[i], 'click', function() {
				print();
				return false;
			});

		// Setup external hyperlinks
		if (hasClass(anchors[i], 'external'))
			anchors[i].target = '_blank';

		// [Added by Centric] Setup lightbox
		if (hasClass(anchors[i], 'lightbox')) {
			setupLightbox(anchors[i]);
		}

		// Setup info hoverlinks fix for ie6
		if (browser.winIE6Down && hasClass(anchors[i], 'extra-explanation'))
			setupInfolink(anchors[i]);
	}
}


/**
* Setup lightbox frame
*/
function setupLightbox(premiumCalc) {
	premiumCalc.onclick = function() {
		window.scrollTo(0, 0);
		var dPremiumCalc = new Dialog();
		var content = document.createElement('IFRAME');
		content.frameBorder = false;
		content.name = 'lightbox-frame';
		content.id = 'lightbox-frame';
		if (premiumCalc.href) {
			content.src = premiumCalc.href;
		}
		dPremiumCalc.addContent(content);
		dPremiumCalc.render();
		return false;
	}
}


/**
* Setup info hoverlinks
* (IE6 hack)
*/
function setupInfolink(infolink) {
	infolink.onmouseout = function() {
		rerenderEl(infolink.childNodes[infolink.childNodes.length - 1]);
		rerenderEl(infolink.childNodes[infolink.childNodes.length - 2]);
		rerenderEl(infolink.childNodes[infolink.childNodes.length - 3]);

		return false;
	}
}

/**
* Setup info hoverlinks fot accordion
* (IE6 hack)
*/
function showHideInfoLink(infolink) {
	if (browser.winIE6Down) {
		rerenderEl(infolink.childNodes[infolink.childNodes.length - 1]);
		rerenderEl(infolink.childNodes[infolink.childNodes.length - 2]);
		rerenderEl(infolink.childNodes[infolink.childNodes.length - 3]);

		return false;
	}
}

/**
* Setup Accordion
*/
function setupAccordion() {
	// Loop through al the questions on the page
	var accordionTitles = getElByClass('accordion-title');
	for (var i = 0; i < accordionTitles.length; i++) {
		addClass(accordionTitles[i].parentNode, 'accordion-closed');
		addEvent(accordionTitles[i], 'click', function() {
			toggleAccordion(this.parentNode);
			//ie6 reflow meta-nav HACK
			if (browser.winIE6Down) {
				rerenderEl(getEl('meta-nav'));
			}
		});
	}
}
// Show/hide the tab
function toggleAccordion(el) {
	hasClass(el, 'accordion-closed') ?
    removeClass(el, 'accordion-closed') :
    addClass(el, 'accordion-closed');
}


/**
* Check whether dialog document type is actually a lightbox
*/
function unsetDialog() {
	if (!hasClass(document.body, 'lightbox'))
		return false;

	if (typeof parent.frames['lightbox-frame'] !== 'undefined')
		return false;

	removeClass(document.body, 'lightbox');
}


/**
* Setup Tabs
*/
var tabs = new Array();
function setupTabs() {
	//Loop through the available tabshandles
	var tabsEl = getElByClass('tabs');
	for (var i = 0; i < tabsEl.length; i++) {
		//Loop through the tabs within a tabhandle
		var tabHandles = tabsEl[i].getElementsByTagName('A');
		for (var j = 0; j < tabHandles.length; j++) {
			// Register tabs and their event handlers
			tabs.push(tabHandles[j]);
			addEvent(tabHandles[j], 'click', function() {
				showTab(getTabId(this));
			});

			// Modify the tabs identifiers against pagejumping
			var tabContent = getEl(getTabId(tabHandles[j]));
			if (tabContent && typeof tabContent !== 'undefined')
				tabContent.id += '_enhanced';
		}

		//Determine which tab to display at startup
		var startTab = document.location.hash;
		if (startTab) {
			startTab = startTab.substring(startTab.indexOf('#') + 1);
			getEl(startTab + '_enhanced') ?
        showTab(startTab) :
        showTab(getTabId(tabHandles[0]));
		}
		else {
			showTab(getTabId(tabHandles[0]));
		}
	}
}
//Show tab
function showTab(tabId) {
	if (!tabs || (typeof tabs == 'undefined'))
		return false;

	for (var i = 0; i < tabs.length; i++) {
		// Check whether tab with the identifier actually exists
		var tab = getEl(getTabId(tabs[i]) + '_enhanced');
		if (!tab || (typeof tab == 'undefined'))
			continue;

		// Show/hide tab
		if (tabId !== getTabId(tabs[i])) {
			addClass(tab, 'tab-hidden');
			removeClass(tabs[i], 'current');
		}
		else {
			removeClass(tab, 'tab-hidden');
			addClass(tabs[i], 'current');
		}
	}

	//ie6 reflow meta-nav HACK
	if (browser.winIE6Down) {
		rerenderEl(getEl('meta-nav'));
	}
	//end ie6 HACK
}
// Get the hash from a complete anchor-url
function getTabId(tabAnchor) {
	return tabAnchor.href.substring(tabAnchor.href.indexOf('#') + 1)
}


/**
* Lightbox class
*/
function Dialog(id) {
	this.dId = 'lightbox';
	this.dContainer = '';
	this.dBackground = '';

	// Set default id
	this.dId = id ? id : 'lightbox';

	// Set default background
	var bg = document.createElement('DIV');
	bg.id = 'lightbox_bg';
	addClass(bg, this.dId + '_bg');
	var bgComment = document.createComment('//');
	bg.appendChild(bgComment);
	this.dBackground = bg;

	// Set content container
	var container = document.createElement('DIV');
	container.id = this.dId;
	this.dContainer = container;

	// Setup titlebar
	var titleBar = document.createElement('P');
	titleBar.id = 'titlebar';
	// Setup closer
	var close = document.createElement('A');
	close.dDialog = this;
	addEvent(close, 'click', function() {
		this.dDialog.destruct();
		return false;
	});
	close.appendChild(document.createTextNode('Sluiten'));
	titleBar.appendChild(close);
	this.addContent(titleBar);
}

Dialog.prototype.addContent = function(el) {
	this.dContainer.appendChild(el);
}

Dialog.prototype.render = function() {
	//HACK
	if (!browser.winMozilla) {
		this.dBackground.style.width = '100%';
		this.dBackground.style.height = '100%';
		var container = getEl('container');
		container.style.overflow = 'hidden';
	}
	if (browser.winIE) {
		document.getElementsByTagName('HTML')[0].style.overflow = 'hidden';
	}
	document.body.style.overflow = 'hidden';
	document.body.appendChild(this.dBackground);
	document.body.appendChild(this.dContainer);
}

Dialog.prototype.destruct = function() {
	//HACK
	if (!browser.winMozilla) {
		var container = getEl('container');
		container.style.overflow = 'visible';
	}
	if (browser.winIE) {
		document.getElementsByTagName('HTML')[0].style.overflow = '';
	}
	document.body.removeChild(this.dContainer);
	document.body.removeChild(this.dBackground);
	document.body.style.overflow = 'visible';
}


/**
* BirthDates object
*/
var BirthDates = {
	id: 'people-birthdates',
	people: new Array(''),
	setup: function() {
		// Don't perform when no birthdates are present
		if (!getEl(BirthDates.id))
			return false;

		BirthDates.get();
		BirthDates.render();
	},
	add: function(BirthDateValue) {
		BirthDates.people.push(BirthDateValue);
	},
	get: function(nr) {
		BirthDates.people = new Array('');
		if (!nr)
			nr = -1;
		// Loop through al the people birthdates on the page
		var birthDates = getElByClass('birthdate-c');
		for (var i = 0; i < birthDates.length; i++) {
			if ((i + 1) !== nr)
				BirthDates.add(getEl('birthdate' + (i + 1)).value);
			getEl(BirthDates.id).removeChild(birthDates[i]);
		}
	},
	remove: function(nr) {
		BirthDates.get(nr);
		BirthDates.render();
	},
	render: function() {
		var newBirthDate = getEl('new-birthdate-c');
		if (newBirthDate)
			getEl(BirthDates.id).removeChild(newBirthDate);

		for (var i = 1; i < BirthDates.people.length; i++) {
			/*
			Creating DOM nodes for a single birthdatefield resulting in:
			<div class="f-text-c birthdate-c">
			<label for="birthdate1">Geboortedatum 1</label>
			<input id="birthdate1" type="text" name="birthdate[]">
			<a class="button"><span>x</span></a>
			</div>
			*/
			var birthDateEl = document.createElement('DIV');
			addClass(birthDateEl, 'f-text-c birthdate-c');
			var birthDateLabel = document.createElement('LABEL');
			birthDateLabel.setAttribute('for', 'birthdate' + i);
			birthDateLabel.appendChild(document.createTextNode('Geboortedatum ' + i));
			var birthDateInput = document.createElement('INPUT');
			birthDateInput.type = 'text';
			birthDateInput.name = 'birthdate[]';
			birthDateInput.id = 'birthdate' + i;
			birthDateInput.value = BirthDates.people[i];
			var birthDateDelete = document.createElement('A');
			addClass(birthDateDelete, 'button');
			var birthDateDeleteSpan = document.createElement('SPAN');
			birthDateDeleteSpan.appendChild(document.createTextNode('x'));
			birthDateDelete.appendChild(birthDateDeleteSpan);
			birthDateDelete.birthDateC = i;
			addEvent(birthDateDelete, 'click', function() {
				BirthDates.remove(this.birthDateC);
				return false;
			});

			birthDateEl.appendChild(birthDateLabel);
			birthDateEl.appendChild(birthDateInput);
			birthDateEl.appendChild(birthDateDelete);
			getEl(BirthDates.id).appendChild(birthDateEl);
		}

		/*
		Creating DOM nodes for a new birthdatefield resulting in:
		<div class="f-text-c" id="new-birthdate-c">
		<input id="birthdate-new" type="text" name="birthdate[]" value="dd-mm-jjjj">
		<a class="button"><span>+</span></a>
		</div>
		*/
		var newBirthDate = document.createElement('DIV');
		addClass(newBirthDate, 'f-text-c');
		newBirthDate.id = 'new-birthdate-c';
		var newBirthDateInput = document.createElement('INPUT');
		newBirthDateInput.type = 'text';
		newBirthDateInput.name = 'birthdate[]';
		newBirthDateInput.id = 'birthdate-new';
		newBirthDateInput.value = 'dd-mm-jjjj';
		var newBirthDateAdd = document.createElement('A');
		addClass(newBirthDateAdd, 'button');
		addEvent(newBirthDateAdd, 'click', function() {
			BirthDates.get();
			BirthDates.add(getEl('birthdate-new').value);
			BirthDates.render();
		});
		var newBirthDateAddSpan = document.createElement('SPAN');
		newBirthDateAddSpan.appendChild(document.createTextNode('+'));
		newBirthDateAdd.appendChild(newBirthDateAddSpan);

		newBirthDate.appendChild(newBirthDateInput);
		newBirthDate.appendChild(newBirthDateAdd);
		getEl(BirthDates.id).appendChild(newBirthDate);
	}
}


/**
* Formlogic object
*/
var Formlogic = {
	formrules: new Array(),
	add: function(obj) {
		Formlogic.formrules.push(obj);
	},
	setup: function() {
		for (var i = 0; i < Formlogic.formrules.length; i++) {
			var ruleTrigger = getEl(Formlogic.formrules[i].id);
			if (ruleTrigger) {
				ruleTrigger.show = Formlogic.formrules[i].show;
				ruleTrigger.hide = Formlogic.formrules[i].hide;
				Formlogic.process(ruleTrigger);
				ruleTrigger.onfocus = function() {
					Formlogic.process(this);
				}
				ruleTrigger.onclick = ruleTrigger.onfocus;
			}
		}
	},
	process: function(rule) {
		for (var k = 0; k < rule.show.length; k++) {
			var el = getEl(rule.show[k]);
			if (el) {
				el.style.display = rule.checked ? 'block' : 'none';
			}
		}
		for (var l = 0; l < rule.hide.length; l++) {
			var el = getEl(rule.hide[l]);
			if (el && rule.checked) {
				el.style.display = 'none';
			}
		}
	}
}


/**
* Rerender element
*/
function rerenderEl(el) {
	if (!el) {
		return false;
	}
	el.parentNode.appendChild(el);
}


/**
* Fix a png-image
* Supersleight parts: http://24ways.org/2007/supersleight-transparent-png-in-ie6
*/
function fixPng(img) {
	if (!browser || !img)
		return false;

	if (browser.winIE6Down) {
		img.parentNode.style.width = img.offsetWidth;
		img.parentNode.style.height = img.offsetHeight;
		img.parentNode.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop src="' + img.src + '")';
	} else {
		img.style.visibility = 'visible';
	}
}
function fixBgPng(obj) {
	var mode = 'scale';
	var bg = obj.currentStyle.backgroundImage;
	var src = bg.substring(5, bg.length - 2);
	if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
		mode = 'crop';
	}
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
	obj.style.backgroundImage = 'url(/css/images/x.gif)';
};


/**
* Fix all png-images
*/
function setupPngsFixer() {
	if (!browser.winIE6Down)
		return false;

	fixPng(getEl('logo'));
}


/**
* Setup height of the content layer against layout overlap
* Warning: this is a hack
*/
function resizeContent() {
	// var content = getEl('content');
	//       var nav = getEl('nav');
	//
	//       if(!content || !nav)
	//         return false;
	//
	//       if(nav.clientHeight > content.clientHeight)
	//         content.style.height = nav.clientHeight + 'px';
}


// Create Flash objects
// Homepage caroussel video
createFlashObject({
	id: 'casesmovie',
	parentId: 'cases-video',
	uri: '/css/swf/slideshow.swf',
	params: {
		'scale': 'noscale',
		'salign': 'lt',
		'wmode': 'opaque',
		'FlashVars': 'xmlSrc=/scripts/nl-xml/slideshow.xml'
	},
	className: 'flashObject',
	width: 920,
	height: 394,
	requiredVersion: 8
});


/**
* On DOMContentLoaded events
*/
onLoaded(function() {
	addClass(document.body, 'js-enhanced');
});
onLoaded(createFlashObjects);
onLoaded(setupHomepage);
onLoaded(setupAccordion);
onLoaded(setupTabs);
onLoaded(setupPngsFixer);
onLoaded(setupSpecialAnchors);
onLoaded(BirthDates.setup);
onLoaded(unsetDialog);
//onLoaded(resizeContent);

