$(function() {
	
	// PNG-fix
	$('body').supersleight();
	
	/* logo home link */
	$('div#logo').click(function() {
		document.location.href = baseUrl;
	});
	
	$('div#mainmenu div.mainmenu_item').mouseenter(function() {
		$('a',this).eq(0).addClass('is_active');
		$fo = $('div.foldout',this);
		var le = $(this).offset().left;
		var wi = $fo.width();
		var bo = $('body').width();
		var exc = Math.max(0,le+wi-bo);
		$('div.foldout').css('left','-'+exc+'px');
		$('div.foldout',this).show();
	});
	$('div#mainmenu div.mainmenu_item').mouseleave(function() {
		$('a',this).eq(0).removeClass('is_active');
		$('div.foldout',this).hide();
	});
	
	/* leftcol menu collapse inactive */
	$("div#leftcol ul ul").hide();
	$("div#leftcol ul li.is_active ul").show();
	$("div#leftcol ul:has(li.is_active)").show();
	
	/* external links */
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
	
	/* search */
//	$('div#search input').click(function() {
//		if($(this).data('is_clicked') != true && this.value == this.defaultValue) {
//			this.value = '';
//			$(this).data('is_clicked', true);
//		}
//	});
	
	/* cufon replacements */
	Cufon.replace('h1')('h2')('div#toptext1')('div#toptext2');
	
	/* contactform left */
	$('div#contactpanel').each(function() {
		$cp = $(this);
		// panel effect:
		$('div.contactbutton',$cp).click(function() {
			$('form',$cp).not($(this).siblings('form')).hide();
			$(this).siblings('form').toggle();
		});
		
		$('input.text,textarea',$cp).each(function() {
			this.title = this.defaultValue;			
		});
		
		// default content:
		$('input.text,textarea',$cp).focus(function() {
			$(this).addClass('hasvalue');
			if(this.value == this.defaultValue) {
				this.value = '';
			}
		});
		$('input.text,textarea',$cp).blur(function() {
			if(this.value == '') {
				this.value = this.defaultValue;
				$(this).removeClass('hasvalue');
			}
		});
		
		// submitting:
		$('input.submit').click(function() {
			var frm = this.form;
			var valflds = ['naam','bedrijfsnaam','email'];
			var valid = true;
			for(var i=0;i<valflds.length;i++) {
				$fld = $('input[name="'+valflds[i]+'"]',frm).eq(0);
				$fld.removeClass('error');
				if($fld.get(0).value == '' || $fld.get(0).value == $fld.get(0).defaultValue) {
					$fld.addClass('error');
					valid = false;
				}
			}
			if(valid) {
				$.post(frm.action, $(frm).serialize(),function(data) {
					$('div.forms',$cp).hide();
					$('div.thankyou').html(data).show();
				});
			}
		});
	});
	
	/* homepage slider */
	if($('div#home_frame')) {
		var currentN = 0;
		$('div#home_frame ul li').eq(0).addClass('active');
		window.setInterval(function() {
			// find next inactive sibling and fades them in-out
			$nowActive = $('div#home_frame ul li.active');
			$nextActive = $nowActive.next('li').eq(0);
			if($nextActive.length == 0) {
				$nextActive = $('div#home_frame ul li').eq(0);
			}
			$nextActive.addClass('active');
			$nowActive.removeClass('active');
		},8000);
		$('div#home_frame').click(function() {
			try {
				window.location = $('li.active > a').get(0).href;
			} catch (e) {}
		});
	}
	
	/* referentie overzichts pagina */
	$('ul#projectlist li').click(function() {
		try {
			$firstA = $('a',this);
			var href = $firstA.get(0).href;
			if($firstA.get(0).rel == 'external') {
				window.open(href);				
			} else {
				document.location.href = href;
			}
		} catch (e) {}
	});
});
