/* ------------------------------------------------- */
/* On document ready
/* ------------------------------------------------- */
$(document).ready(function($) {
	// Init Default IE fixes
	defaultIEFixes.init();
	// Page backstretch funcionality
	backstretch.init();
	// Menu functionality
	menu.init();
	// Quicklinks funcionality
	quicklinks.init();
	// Quicklinks funcionality
	list.init();
	// Filter init
	if($('#filter').length) filter.init();
	// Init form validation
	$('form').validate();
	// Init uniform
	$("select, input:checkbox, input:radio, input:file").uniform();
	// Init scrollpane
	$('.scroll-pane').jScrollPane();
	// Init table helper
	table.init();
	// Init scrollables
	$('#scrollable').scrollable({easing:'easeInOutCirc',speed:800});
	// Vertical/Horizontal align
	$('.valign').vAlign();
	$('.halign').hAlign();
});

var quicklinks = {
	
	quick_links: $('#quicklinks li'),
	
	init: function () {
		var qu = this;
		qu.events();
	},
	
	events: function () {
		var qu = this;
		qu.quick_links.click(function() {
			var href = $('h2 a', $(this));
			window.location = href.attr('href');
		});
	}
}

/* ------------------------------------------------- */
/* Home backstretch functions
/* ------------------------------------------------- */
var backstretch = {
	
	images: $.data(document.body, 'images'),
	position: 0,
	
	init: function() {
		ho = this;
		if(ho.images.length > 0) $.backstretch(ho.images[ho.position], {speed:500});
		if(ho.images.length > 1) ho.events();
	},
	
	events: function() {
		ho = this;
		setInterval('ho.nextSlide()', 7500); // WAS 15000
	},
	
	nextSlide: function() {
		ho = this;
		pos = ho.position++;
		if(pos >= (ho.images.length - 1)) ho.position = 0;
		$('#backstretch img').fadeOut(100, function() {
			$('#backstretch img').attr("src", ho.images[ho.position]).fadeIn(100);
		})
		$(window).trigger("resize");
	}
	
}

/* ------------------------------------------------- */
/* Menu functions
/* ------------------------------------------------- */
var menu = {
	
	menu: $('#main-menu-wrapper'),
	a: $('#main-menu>ul>li>a'),
	btn: $('#menu-action-btn'),
	
	init: function() {
		me = this;
		// Set submenu's
		me.a.each(function(index) {
			var childUl = $('ul', $(this).parent());
			if(childUl.length) childUl.css('left', ($(this).outerWidth()));
		});
		// Let the main-menu peep
		me.menu.animate({'left':'-450px'}, 0, 'easeInOutExpo');
		me.btn.fadeIn(800);
		// Start events
		me.events();
	},
	
	events: function() {
		me = this;
		// Rollover Menu-button
		me.btn.mouseenter(function() {
			me.btn.fadeOut(400);
			me.menu.stop().animate({'left':'0px'}, 800, 'easeInOutExpo');
		});
		// Roll out Main-menu hide
		me.menu.mouseleave(function() {
			me.btn.fadeIn(400);
			me.menu.stop().animate({'left':'-450px'}, 800, 'easeInOutExpo');
		})
	}
	
}

var list = {
	
	listItems: $('#list-wrapper ul li'),
	
	init: function() {
		li = this;
		li.events();
	},
	
	events: function() {
		li = this;
		li.listItems.click(function() {
			var href = $('a.go-to', $(this));
			if(href.length) window.location = href.attr('href');
		});
	}
	
}

/* --------------------------------------------------------- */
/* Table functions
/* --------------------------------------------------------- */
var table = {
	
	rows: $('table tr').not('.uitverkocht'),
    links: $('table tr td a.link'),
	
	init: function() {
		ta = this;
		ta.rows.each(function() {
			var href = $('a.go-to', $(this));
			if(href.length) {
				$(this).css({'cursor':'pointer'});
				$(this).hover(function() {
					$(this).addClass('hover');
				}, function() {
					$(this).removeClass('hover');
				});
			}
		});
		ta.events();
	},
	
	events: function() {
		ta = this;
		ta.rows.click(function(e) {
            if(e.originalTarget.className != 'link'){
                var href = $('a.go-to', $(this));
                if(href.length) {
                    window.location = href.attr('href');
                }
            }
		})
	}
	
}

/* --------------------------------------------------------- */
/* Filter (jump-menu) functions
/* --------------------------------------------------------- */
var filter = {

	filter: $('#filter'),

	init: function() {
        fi = this;
		fi.events();
	},

	events: function() {
        fi = this;

        /*fi.filter.change(function() {
            var val = fi.filter.val();
            if (val != '') {
                window.location = val;
            }
        })*/
	}

}

/* ------------------------------------------------- */
/* Default IE Fixes for 6/7/8
/* Fixes for last/first child
/* ...
/* ------------------------------------------------- */
var defaultIEFixes = {
	
	init: function() {
		ie = this;
		// First determine if we have IE
		if($.browser.msie && ($.browser.version < 9)) {
			// Last/first-child hack
			// Add elements to your discretion
			ie.firstLastChildHack('#main-menu ul li');
			// Here comes the following hack
		}
	},
	
	firstLastChildHack: function(el) {
		$(el).first().addClass('first-child');
		$(el).last().addClass('last-child');
	}
}

/* ------------------------------------------------- */
/* Commen log functions avoids pesky alerts in FF
/* if FireBug isn't enabled!
/* http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
/* ------------------------------------------------- */
window.log = function(){
	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	if(this.console) { console.log(Array.prototype.slice.call(arguments)); }
};

// Vertical and horizontal align
// http://snipplr.com/view.php?codeview&id=12566
(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var h = $(this).height();
	var oh = $(this).outerHeight();
	var mt = (h + (oh - h)) / 2;	
	$(this).css("margin-top", "-" + mt + "px");	
	$(this).css("top", "50%");
	$(this).css("position", "absolute");	
	});	
}
})(jQuery);

(function ($) {
$.fn.hAlign = function() {
	return this.each(function(i){
	var w = $(this).width();
	var ow = $(this).outerWidth();	
	var ml = (w + (ow - w)) / 2;	
	$(this).css("margin-left", "-" + ml + "px");
	$(this).css("left", "50%");
	$(this).css("position", "absolute");
	});
}
})(jQuery);
