/* Author: Sten Van den Bergh
Glue Webdesign
*/
$(function() {
	
	// Remove margin right on third item in grid
	$("div.three_column article:nth-child(3n+3)").css('marginRight', '0px');
	
	// Header gallery slideshow
	$(function() {
	    setInterval( "slideSwitch()", 5000 );
	});
	
	// Fancybox
	$("a[rel=fancy_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	// jCarousel
	$('#project_gallery').jcarousel({
		// Configuration
	});
	
	// External links
	$("a[href^='http:']:not([href*='" + window.location.host + "'])").each(function() {
		if( !$(this).hasClass('fancybox') )
		{
			$(this).click(function()
			{
			    window.open(this.href); // pop a new window
			    return false; // return false to keep the actual link click from actuating
			});
		}
	});
	
	// Form Validation
	$("#freeform, #newsletter_form").validate({
		errorPlacement: function(error, element) {
			element.parent().append(error);
		}
	});
	
	// Check column height for seprator border
	if($('.home_column').length > 0) {
		var colLeft = $("div.home_column:first-child").outerHeight(true);
		var colRight = $("div.home_column:last-child").outerHeight(true);
		
		if(colLeft > colRight) {
			$(".home_column:first-child").css({
				'borderRight' : '1px solid #a6a6a6', 
				'paddingRight' : '79px'
			});
		} else {
			$(".home_column:last-child").css({
				'borderLeft' : '1px solid #a6a6a6', 
				'paddingLeft' : '79px'
			});
		}
	} else {
		var colLeft = $(".page_column").outerHeight(true);
		var colRight = $("aside.last_column").outerHeight(true);
		
		if(colLeft > colRight) {
			$(".page_column").css({
				'borderRight' : '1px solid #a6a6a6', 
				'paddingRight' : '29px'
			});
		} else {
			$(".page_column").css({
				'marginRight' : '0px'
			});
			$("aside.last_column").css({
				'borderLeft' : '1px solid #a6a6a6', 
				'width' : '219px',
				'paddingLeft' : '30px'
			});
		}
	}

	$('#newsletter_form .email').focus(function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	$('#newsletter_form .email').focusout(function(){
		if(this.value == '') {
			this.value = this.defaultValue;
		}
	});
});

function slideSwitch() {
    var $active = $('#header_gallery .active');

    if ( $active.length == 0 ) $active = $('#header_gallery p:last');

    var $next =  $active.next().length ? $active.next()
        : $('#header_gallery p:first');

    $active.addClass('last_active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last_active');
    });
}












