function fieldFocus(text, frmElm, type) {
	if (frmElm.value == text) {
		frmElm.value = '';
		try {
			frmElm.type = type;
		}
		catch(error) {}
	}
}

function fieldBlur(text, frmElm, type) {
	if (frmElm.value == '') {
		frmElm.value = text;
		try {
			frmElm.type = type;
		}
		catch(error) {}
	}
}


function submitOnEnter(frm, event) {
	if ((event.which && event.which == 13) || (event.keyCode && event.keyCode== 13)) {
		frm.submit();
	}
	return true;
}

function checkSubmitForm(fm_id) {
	var f = document.getElementById('frmElements' + fm_id);
	
	var checkResponse = $.ajax({
		type: "POST",
		url: "/inc/cfc/form.cfc",
		data: "method=captchaCheck&captcha=" + f.form_code.value + "&returnFormat=plain",
		async: false
	}).responseText;
	
	if (checkResponse == 'false') {
		f.form_code.value = '';
		f.form_code.focus();
		document.getElementById('captchaError').style.display = 'block';
		return false;
	}
	else {
		document.getElementById('captchaError').style.display = 'none';
		return true;
	}	 
}

function selectFirstItem() {
	document.getElementById('navigationLeft').style.display = "block";
}

function deselectFirstItem() {
	document.getElementById('navigationLeft').style.display = "none";	
}

function setFormField(f, text) {
	if (f.value == text) 
		f.value = '';
}

function resetFormField(f, text) {
	if (f.value == '')
		f.value = text;
}

function setOutlines() {
	var content = document.getElementById('content');
	var middle = document.getElementById('middleContent');
	// Marvin: -40px is de padding die er nog vanaf moet;
	middle.style.height = (content.offsetHeight-40) + 'px';
}

/*function showPhoto(pg_id, file, photoablumid, photografer, message) {
	if(photografer != ""){
		photografer = 'Foto gemaakt door: ' + photografer;
	}
	
	document.getElementById('photoHolder_' + photoablumid).innerHTML = '<img src="/inc/upload/photos/' + pg_id + '/photo_' + file + '" border="0" title="' + photografer + '" />';
	if(message != undefined){
		document.getElementById('textHolder_' + photoablumid).innerHTML = message;
	}
}*/

function highlightSearch(action){
	var tags = getElementsByClassName("searchField");
	if(!tags[1]){
		if(action=="over")tags[0].style.backgroundImage = "url(/inc/img/bg_search_over.png)";
		else tags[0].style.backgroundImage = "url(/inc/img/bg_search.png)";
	}
}

function getElementsByClassName(clss){
	var returnTags = new Array();	
	var tags = document.getElementsByTagName("*");
	for(var i=0;i<tags.length;i++){
		if(tags[i].className&&tags[i].className==clss)returnTags.push(tags[i]);
	}
	return returnTags;
}


function addListener(element, type, expression, bubbling){
	bubbling = bubbling || false;
	
	if(window.addEventListener) { // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else return false;
}

function getTargetId(event){
	
	if(event.target) { // FF
		return event.target.id;
	} else if(window.attachEvent) { // IE
		return event.srcElement.id;
	} else return false;
}
var t;
/* Expert Cells */
function showCellPopup(element, id) {
	clearTimeout(t);
	document.getElementById(element + id).style.display = 'block';
}

function goHideCellPopup(element, id) {
	document.getElementById(element + id).style.display = 'none';

}

function hideCellPopup(element, id) {
	t = setTimeout('goHideCellPopup("' + element + '", ' + id + ')',1000);
}

function resetCellCounter() {
	clearTimeout(t);
}



/* employees thumbnails */
function hideThumbOverlay(element) {
	$('#' + element).animate({
		opacity: 0
	}, 200);
	
	$('#trigger_' + element).animate({
		opacity: 0
	}, 200);
}

function showThumbOverlay(element) {
	$('#' + element).animate({
		opacity: 0.8
	}, 200);
	
	$('#trigger_' + element).animate({
		opacity: 1
	}, 200);
}




/* Photoalbum */
function photoAlbum(arrPhotos, photoPath, pa_id){
	
	/* Constructor : Photoalbum instellingen */
	this.arrPhotos = arrPhotos;
	this.photoPath = photoPath;
	this.photosPerPage = 5;
	this.paId = pa_id;	
	
	// FUNCTIE | Foto groot tonen
	this.showPhoto = function(id){
		var photoHolder = document.getElementById('photoholder_' + this.paId);	
		var photoInfo = document.getElementById('photoinfo_' + this.paId);	
		
		photoHolder.src = this.photoPath+ 'photo_' + this.arrPhotos[id][1];
		
		var photoInfoText = "";
		if(this.arrPhotos[id][2] != ''){
			photoInfoText += this.arrPhotos[id][2];
		}
		if(this.arrPhotos[id][2] != '' && this.arrPhotos[id][3] != ''){
			photoInfoText += " - ";	
		}
		if(this.arrPhotos[id][3] != ''){
			photoInfoText += this.arrPhotos[id][3];
		}
		photoInfo.innerHTML = photoInfoText;
		photoHolder.alt = photoInfoText;
	}
	
	// FUNCTIE | Thumbnails tonen
	this.showThumbs = function(pageId){
		
		// Standaard pagina
		pageId = pageId || 1;
		var loopFrom;
		var loopTo;
		
		var pageCount = Math.ceil(this.arrPhotos.length / this.photosPerPage);

		var photoStrip = document.getElementById('photostrip_'+this.paId);
		
		// Foto strip leegmaken
		photoStrip.innerHTML = "";
		
		// Pagina bereik uitrekenen
		if(this.arrPhotos.length > this.photosPerPage){
			loopFrom = (this.photosPerPage * pageId) - (this.photosPerPage - 1);
			if(pageCount == pageId && (this.arrPhotos.length % this.photosPerPage > 0)){
				loopTo = loopFrom + (this.arrPhotos.length % this.photosPerPage)-1;
			} else {
				loopTo = loopFrom + (this.photosPerPage - 1);								   
			}
		} else {
			loopFrom = 1;
			loopTo = this.arrPhotos.length;
		}

		// Thumbs uitloopen en afdrukken
		var objAlbum = new photoAlbum(this.arrPhotos, this.photoPath, this.paId);
		for(var i = loopFrom; i <= loopTo; i++){
			
			var photoThumb = document.createElement('img');
			photoThumb.id = "thumb_"+(i-1);
			photoThumb.className = "thumb";
			photoThumb.src = photoPath+'thumb_' + arrPhotos[i-1][1];
			photoThumb.alt = "";
			photoThumb.style.cursor = "pointer";
			addListener(photoThumb, 'click', function(e){ 
				photoId = getTargetId(e).split("_");
				photoId = photoId[1];
				objAlbum.showPhoto(photoId); 
			});
			
			photoStrip.appendChild(photoThumb);
		}
		
		// Pagina's uitlopen
		var photoPagesHolder = document.getElementById('photopages_holder_' + this.paId);
		var photoPages = document.getElementById('photopages_' + this.paId);	
		
		// Pagina's Leegmaken
		photoPages.innerHTML = "";
		
		// Checken of er wel pagina's zijn
		if(pageCount > 1){
			for(var i = 1; i <= pageCount; i++){
				var pageLink = document.createElement('span');
				pageLink.id = "link_"+i;
				pageLink.innerHTML = i+" ";
				if(i != pageId){
					pageLink.className = "pageLink";
					pageLink.style.cursor = "pointer";
					addListener(pageLink, 'click', function(e){ 
						pageId = getTargetId(e).split("_");
						pageId = pageId[1];
						objAlbum.showThumbs(pageId); 
					});
				} else {
					pageLink.className = "pageLink selected";
				}
				photoPages.appendChild(pageLink);
			}
		} else { // Anders uitzetten
			photoPagesHolder.style.display = "none";
		}
	}
}

$(function () {
    $('.logosunits').each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 20;

        var hideDelayTimer = null;

        var beingShown = false;
        var shown = false;
        var trigger = $('.trigger', this);
        var info = $('.tooltip', this).css('opacity', 0);
        var info = $('.tooltip', this).css('filter','alpha(opacity=0)');


        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                // don't trigger the animation again
                return;
            } else {
                // reset position of info box
                beingShown = true;

                info.css({
                    top: -110,
                    left: 11,
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 1,
                    filter: 'alpha(opacity=100)'
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                    info.css({filter:''})
                });
            }
			
            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0,
                    filter: 'alpha(opacity=0)'
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                    info.css({filter:''})
                });

            }, hideDelay);
    
            return false;
        });
    });
});

// Contact switch
$(function () {
    $('#contactunits a').each(function () { // For each link
		thisLink = this;
		$(this).click(function(e){ // Add a click listener
			clickedLink = this;
			$('#activeunit').val(clickedLink.className.replace('contact', ''));
			$('#contactdata div').each(function () { // For each data div
				if($(this).hasClass('active')){ // If the div is active
					$(this).fadeOut('fast', function(){ // Fade it out
						$(this).removeClass('active'); // Remove the active class
						$('#'+clickedLink.className).fadeIn('fast'); // Fade the new data div in
						$('#'+clickedLink.className).addClass('active');	// Add the class ID		 
					});
				}
			});
		});		
	});
});