/**
 * Author: P.A. Huisman 
 * SiteCLX Internet Development
 */
var siteclx = {
		component: {},
		js: '/themes/notenhoeve/js/',
		loaded: [],
		scripts: []
};


/**
 * Initialize components on dom ready
 */
$(document).bind("ready",
	function(){
		siteclx.initializeComponents ();
	}
);

/**
 * Initialize components.
 */
siteclx.initializeComponents = function (){
	
	siteclx.scripts = [];
	
	$ ('[class^="component-"]').each (function (){
		var component = this.className.replace (/.*?component-([^ ]+).*$/, '$1');
		$ (this).attr ('data-component', component);
	});
	
	$ ('[data-component]').each (function (){
		var element = $ (this);
		if (!element.data ('instantiated')){
			if (siteclx.component [element.attr ('data-component')] !== undefined){
				new siteclx.component [element.attr ('data-component')] (this);
				element.attr ('data-instantiated', true);
			}
		}
	});
	
	var scriptsToLoad = [];
	$.each(siteclx.scripts, function(){
		var s = this + '';
		if($.inArray(s, siteclx.loaded, -1) == -1){
			scriptsToLoad.push(s);
		}
	});
	
	yepnope({
		load: scriptsToLoad,
		complete: function(){
			$.merge(siteclx.loaded, scriptsToLoad);
			var x = function(){ $('body').trigger('scriptsLoaded'); };
			setTimeout(x, 800); // older ie needs timeout
		}
	});
	
	
};

/**
 * PhotoBlock component constructor
 */


siteclx.component.PhotoBlock = function (element){
	var self = this;

	var S4 = function() {
	   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	};
	
	this.element = $(element);
	this.element.find('a').attr('rel', S4()+S4()+S4()+S4() );
	this.initHandler = function(){self.init();}

	$('body').bind('scriptsLoaded', this.initHandler);
	siteclx.scripts.push(siteclx.js + 'libs/jquery.fancybox-1.3.4.pack.js');
	
};

/**
 * Fancybox for links in this photoblock element
 */
siteclx.component.PhotoBlock.prototype.init = function(){
	$('body').unbind('scriptsLoaded', this.initHandler);
	this.element.find('.photoblock').css({'visibility': 'visible', 'opacity': '0'}).animate({'opacity': 1}, 500);
	
	this.element.find('a').fancybox({
		'padding'		: 0,
		'overlayOpacity': 0.9,
		'overlayColor'	: '#dddddd',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	/*
	this.element.find('a').fancybox({
		'padding'		: 0,
		'overlayOpacity': 0.9,
		'overlayColor'	: '#dddddd',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});*/
};

/**
 * Slideshow component constructor
 */
siteclx.component.Slideshow = function (element){
	var self = this;
	this.element = $(element);
	
	this.initHandler = function(){self.init();}
	
	$('body').bind('scriptsLoaded', this.initHandler);
	siteclx.scripts.push(siteclx.js + 'libs/jquery.nivo.slider.pack.js');
};

/**
 * Init slider script en element
 */
siteclx.component.Slideshow.prototype.init = function (){
	$('body').unbind('scriptsLoaded', this.initHandler);
	var self = this;
	this.element.find('.slides').css({'visibility': 'visible', 'opacity': '0'}).animate({'opacity': 1}, 500);
	this.element.find('.slides').first().nivoSlider({
		directionNav:false,
        controlNav: false, 
        keyboardNav:false,
		pauseOnHover:false,
		pauseTime: 6000,
		effect: 'slideInLeft'
	});
};
