/** 
 * Netlash Homepage
 * 
 * @author Bram Van Damme	<bram@netlash.com>
 * @author Bert Pattyn		<bert@netlash.com>
 */

/**
 * JS_NETLASH Object
 */
if (!JS_NETLASH) { var JS_NETLASH = new Object(); }


/**
 * JS_NETLASH - general JS object
 */
JS_NETLASH.hpHandler = {
	
	/**
	 * Datamembers
	 */
 	debug: false,
		
		
	/**
	 * init - hook the clicks
	 * 
	 * @return void
	 */
	init: function() {
		
		// init the slideshow
		JS_NETLASH.hpHandler.slideshow.init();
		
	},
	
	
	/**
	 * end of object
	 */
	_eoo			: true

}
	
	
	
	

/**
 * JS_NETLASH - hpHandler - slideshow
 */
JS_NETLASH.hpHandler.slideshow = {
		
	/**
	 * Datamembers
	 */
 	waitTime			: 4000,		// time between fades (viz. how long to show a slide)
	fadeTime			: 700,		// time to fade from one image to the other
	
	timeId				: null,		// the timer
	slides				: null,		// the slides
 	numSlides			: 0,		// number of slides
	curSlide			: 0,		// current slide
	numLoaded			: 0,		// number of images preloaded
		
		
	/**
	 * init - hook the clicks
	 * 
	 * @return void
	 */
	init: function() {
		// debug
		if (JS_NETLASH.hpHandler.debug)	console.log('JS_NETLASH.hpHandler.slideshow.init');
		
		// get the slides
		JS_NETLASH.hpHandler.slideshow.slides 	= $('#homeShowcase ul.thumbs li > a');
		JS_NETLASH.hpHandler.slideshow.numSlides	= JS_NETLASH.hpHandler.slideshow.slides.length;
		
		// hide the even slide
		$('#homeShowcase-even').fadeOut(10).css('display','none');

		
		// hook the clicks on the numbers
		JS_NETLASH.hpHandler.slideshow.slides.bind('click', function(e) {
			// don't jump
			e.preventDefault();
			
			// Don't double fade!
			if ($('#homeShowcase img:animated').length > 0)	return;
														
			// remove selected class from current slide number
			$(JS_NETLASH.hpHandler.slideshow.slides.get(JS_NETLASH.hpHandler.slideshow.curSlide)).parent().removeClass('selected');
			
			// define the new curSlide
			JS_NETLASH.hpHandler.slideshow.curSlide = parseInt($(this).parent().attr('id').substr(14))-1;
			
			// now show it!
			JS_NETLASH.hpHandler.slideshow.transSlide();
			
		});
					
		// hook the click on the "more" link
		$('#homeShowcase p.more a').css('zIndex', 200).bind('click', function(e) {
			
			// don't jump if we're fading!
			if ($('#homeShowcase img:animated').length > 0) {
				e.preventDefault();	
			}
			
		});
			
		// preload the images
		JS_NETLASH.hpHandler.slideshow._preload();
		
	},
			
			
	/**
	 * _preload - preload all slides
	 * @return void
	 */
 	_preload: function() {
		
		// array of Image instances we'll be using				
		var extImg = [];
		
		// loop all slides
		for (var i = 0; i < JS_NETLASH.hpHandler.slideshow.slides.length; i++) {
		
			// new image ref
			extImg[i] 		= new Image();
		
			// attach image
			extImg[i].src 	= $(JS_NETLASH.hpHandler.slideshow.slides.get(i)).attr('rel');
			
			// now wait for it to load
			if (extImg[i].complete) { // image already in cache (IE)
				JS_NETLASH.hpHandler.slideshow._loaded();
			} else {
				extImg[i].onerror = function() { // couldn't load (404)
					JS_NETLASH.hpHandler.slideshow._loaded();
				}
				extImg[i].onload = function() { // image was loaded
					JS_NETLASH.hpHandler.slideshow._loaded();
				}
			}
			
		}
		
	},
		
		
	/**
	 * _loaded - an image was loaded
	 * 
	 * @return void
	 */			
	_loaded: function() {
		
		// increment numLoaded
		JS_NETLASH.hpHandler.slideshow.numLoaded++;
		
		// start slideshow if all images are loaded
		if (JS_NETLASH.hpHandler.slideshow.numLoaded == JS_NETLASH.hpHandler.slideshow.numSlides) {
			JS_NETLASH.hpHandler.slideshow.timeId = window.setTimeout(JS_NETLASH.hpHandler.slideshow.nextSlide, JS_NETLASH.hpHandler.slideshow.waitTime);
		}
		
	},
		
		
	/**
	 * nextSlide - advances to the next slide
	 * 
	 * @return void
	 */
	nextSlide: function() {
		
		// debug
		if (JS_NETLASH.hpHandler.debug)	console.log('JS_NETLASH.hpHandler.slideshow.nextSlide');
							
		// Don't double fade
		if ($('#homeShowcase img:animated').length > 0)	return;
			
		// remove selected class from curr slide					
			$(JS_NETLASH.hpHandler.slideshow.slides.get(JS_NETLASH.hpHandler.slideshow.curSlide)).removeClass('selected');
		
		// if we're at the last slide, go back to 0
			if (JS_NETLASH.hpHandler.slideshow.curSlide == JS_NETLASH.hpHandler.slideshow.numSlides-1)
				JS_NETLASH.hpHandler.slideshow.curSlide = 0;
			else
				JS_NETLASH.hpHandler.slideshow.curSlide++;
		
		// go to next slide
			JS_NETLASH.hpHandler.slideshow.transSlide();
		
	},


	/**
	 * prevSlide - returns to the previous slide
	 *
	 * @return void
	 */
	prevSlide			: function() {
		
		// debug
			if (JS_NETLASH.hpHandler.debug)	console.log('JS_NETLASH.hpHandler.slideshow.prevSlide');
							
		// Don't double fade
			if ($('#homeShowcase img:animated').length > 0)	return;
			
		// remove selected class from current item
			$(JS_NETLASH.hpHandler.slideshow.slides.get(JS_NETLASH.hpHandler.slideshow.curSlide)).removeClass('selected');
		
		// if we're at the first slide, go back to the last one
			if (JS_NETLASH.hpHandler.slideshow.curSlide == 0)
				JS_NETLASH.hpHandler.slideshow.curSlide = JS_NETLASH.hpHandler.slideshow.numSlides-1;
			else
				JS_NETLASH.hpHandler.slideshow.curSlide--;
		
		// go to next slide
			JS_NETLASH.hpHandler.slideshow.transSlide();
			
	},
	
	
	/**
	 * transSlide - transition to the new slide
	 * 
	 * @param int slideNum
	 * @return void
	 */
	transSlide: function() {
						
		// debug
		if (JS_NETLASH.hpHandler.debug)	console.log('JS_NETLASH.hpHandler.slideshow.gotoSlide %d', JS_NETLASH.hpHandler.slideshow.curSlide);
		
		// shortcut
		var slide	= $(JS_NETLASH.hpHandler.slideshow.slides.get(JS_NETLASH.hpHandler.slideshow.curSlide));
		
		// clear prev timeout
		window.clearTimeout(JS_NETLASH.hpHandler.slideshow.timeId);
		
		// remove selected class form all links
		JS_NETLASH.hpHandler.slideshow.slides.parent().removeClass('selected');
		
		// add selected class to current item
		slide.parent().addClass('selected');
		
		// set title and subtitle
		var title = slide.attr('title').split(' - ')[0];
		var subtitle = slide.attr('title').split(' - ')[1];
		if(subtitle == undefined) subtitle = '';
		
		// update link
		$('#homeShowcase a.readMore').attr('href', slide.attr('href'));
		$('#homeShowcase .bigImage img').parent().attr('href', slide.attr('href'));
		
		// update title
		$('#homeShowcase .description .title h2').text(title);
		$('#homeShowcase .description .title h4').text(subtitle);

		// define what to fade out and in
		var fOut	= $('#homeShowcase .bigImage img:visible');
		var fIn		= $('#homeShowcase .bigImage img:hidden');
		
		// debug
		if (JS_NETLASH.hpHandler.debug)	console.log('fOut = ' + fOut.attr('id'));
		if (JS_NETLASH.hpHandler.debug)	console.log('fIn = ' + fIn.attr('id'));
		
		// animate
		fOut.css('zIndex', 100)
			.fadeOut(JS_NETLASH.hpHandler.slideshow.fadeTime, function() { 
				$(this).css('display','none'); 
			});
		
		fIn.css('zIndex', 50)
			.attr('src', slide.attr('rel'))
			.attr('alt', title)
			.attr('title', title)
			.css('display','block')
			.fadeIn(JS_NETLASH.hpHandler.slideshow.fadeTime, function(e) {
				// when we're done fading, go to the next slide (but wait for it of course)
				JS_NETLASH.hpHandler.slideshow.timeId 	= window.setTimeout(JS_NETLASH.hpHandler.slideshow.nextSlide, JS_NETLASH.hpHandler.slideshow.waitTime);
			});	
			
		
	},
	
		
	/**
	 * end of object
	 */
	_eoo: true

}
	
	
	
	

/**
 * Gjeefmosjette
 */
jQuery(function($) {
	JS_NETLASH.hpHandler.init();
});
