/**
 *	fires when the DOM is loaded
 *
 */
window.addEvent('domready', function(event) {
	
	// cufon init
	replaceFonts();
	
	// tweetmeme counter
	tweetMeme();
	
	// sliders
	frontpageSlider();
	archiveSlider();
	
	// image rollovers
	imageRollover();
	
	// shadowbox if there is a rel attribute for loadtime performance
	$$('a').each( function(e) {
		var rel = e.getProperty('rel');		
		if ( $chk(rel) ) {			
			if ( rel.test("shadowbox") ) {
				shadowBoxInit();
			}
		}
	});
    
    startMoorquee();
});

function startMoorquee() {
    var obj;
    
    obj = new mooquee($('slider_nav'), {
        direction: 'left',
        speed: 20,
        marWidth: 710
    });
}

/** 
 *	Activate cufon
 *
 */
function replaceFonts() {
	Cufon.replace(
		'#nav ul li div.mb a, h1, h2, h3, .button_large'
		, { 
		fontFamily:'Interstate',hover: true
	});	
}

/**
 *	archive slider
 *
 */
function archiveSlider() {
	if ( !$chk( $('showcase') ) )
		return false;
	
	if ( !$('showcase').hasClass('category') )
		return false;
	
	// default slide
	var currentSlider 	= 1;
	
	// total amount of slides
	var sliderAmt		= $$('.slide').length;
	
	// enumerate slides
	var i = 1;
	
	// set autoloop
	var autoloop = true;
	
	// autoloop
	( function() {
		if ( autoloop ) {
			currentSlider++;
			
			if ( currentSlider > sliderAmt )
				currentSlider = 1;
			
			var element = $('featured_nav').getElement('li.control_' + currentSlider + ' a');
			
			startSlideArchive(element);
		}
	}).periodical(5000);
	
	$$('.slide').each( function(e) {
		e.addClass('slide_' + i);
	
		// set active slide
		if (i == currentSlider)
			e.addClass('slide_active');
		else {
			e.setStyles({
				'opacity': 0,
				'display': 'block'
			});
		}
		i++;
	});
	
	// enumerate control
	var i = 1;
	
	$('featured_nav').getElements('li.control').each( function(e) {
		e.addClass('control_' + i);
		
		if (i == 1) 
			e.addClass('active');
			
		i++;
		
		e.getElement('a').addEvent('click', function(event) {
			// stop propagation
			event.stop();
			
			// stop the autoloop forever
			autoloop = false;
			
			startSlideArchive(this);
		});
	});
}

function startSlideArchive(e) {
	// scope 
	var li = e.getParent('li');
	
	var nr = li.get('class').replace('control control_', '').toInt();
	
	// get active slide
	var activeSlide = $('slider').getElement('.slide_active');
	
	if ( !activeSlide.hasClass('slide_' + nr) ) {
		currentSlider = nr;
		
		fxFadeOut = new Fx.Tween(activeSlide);
		
		fxFadeOut.start('opacity', 0).chain( function() {
			// remove active
			$('featured_nav').getElements('li.active').removeClass('active');
			activeSlide.removeClass('slide_active');
			
			// set active
			li.addClass('active');
			
		});	
		
		var fadeInSlide = $('slider').getElement('.slide_' + currentSlider);
		fadeInSlide.addClass('slide_active');
		
		fxFadeIn = new Fx.Tween(fadeInSlide);
		
		fxFadeIn.start('opacity', 1);
	}
}

/**
 *	frontpage slider
 *
 */
function frontpageSlider() {
	if ( !$chk( $('showcase') ) )
		return false;
	
	if ( !$('showcase').hasClass('home') )
		return false;
			
	// default slide
	var currentSlider 	= 1;
	
	// total amount of slides
	var sliderAmt		= $$('.slide').length;
	
	// enumerate slides
	var i = 1;
	
	$$('.slide').each( function(e) {
		e.addClass('slide_' + i);
	
		// set active slide
		if (i == currentSlider)
			e.addClass('slide_active');
		else {
			e.setStyles({
				'opacity': 0,
				'display': 'block'
			});
		}
		i++;
	});
	
	// set autoloop
	var autoloop = true;
	
	// autoloop
	( function() {
		if ( autoloop ) {
			currentSlider++;
			
			if ( currentSlider > sliderAmt )
				currentSlider = 1;
						
			startSlideFrontpage(currentSlider);
		}
	}).periodical(5000);
	
	// set slide action
	$('slider_nav').getElements('a').each( function(e) {		
	
		e.addEvent('click', function(event) {
			// stop propagation
			event.stop();
			
			// stop the autoloop
			autoloop = false;
			
			// get slider
			if ( e.hasClass('next') ) {
				currentSlider++;
				
				if (currentSlider > sliderAmt)
					currentSlider = 1;
			} else if ( e.hasClass('prev') ) {
				currentSlider--;
				
				if (currentSlider == 0)
					currentSlider = sliderAmt;
			} else
				currentSlider = e.get('text').toInt();
			
			startSlideFrontpage(currentSlider)
		});
	});
}

function startSlideFrontpage(currentSlider) {
	// get active slide
	var activeSlide = $('slider').getElement('.slide_active');
	
	if ( !activeSlide.hasClass('slide_' + currentSlider) ) {
		fxFadeOut = new Fx.Tween(activeSlide);
		
		fxFadeOut.start('opacity', 0).chain( function() {
			var fadeInSlide = $('slider').getElement('.slide_' + currentSlider);
			
			// remove active
			activeSlide.removeClass('slide_active');
		
			// set active
			fadeInSlide.addClass('slide_active');
			
			fxFadeIn = new Fx.Tween(fadeInSlide);
			
			fxFadeIn.start('opacity', 1);
		});
	}
}

/** 
 *	Tweetme Counter
 *
 */
function tweetMeme() {
	
	if ( !$chk( $("retweet-sidebar") ) )
		return false;
	
	Request.tweetmeme = new Class({
		// return json data with extended information of a place / location.
		Extends: Request.JSONP,
		options: {
			url: "http://api.tweetmeme.com/url_info.jsonc?url={location}"
		},
		initialize: function(location, options) {
			this.parent(options);
			this.options.url = this.options.url.substitute({
				location: location
			});
		},
		success: function(data, script) {
			this.parent(data, script);
		} 
	});

	// example use
	// <span class="retweet_count" rel="http://www.google.nl"></span>
	// get retweets for all posts on a page and update the counter. URL in the rel property of the markup.
	$$(".retweet_count").each(function(el) {
		var url = el.get("rel");
		new Request.tweetmeme(url, {
			log: true,
			onSuccess: function(data) {
				if (data.status == 'success') {
					if (data.story.url_count == 1)
						el.set("html", data.story.url_count+" Tweet");
					else
						el.set("html", data.story.url_count+" Tweets");
					
				}
				else {
					el.set("html", "Geen Tweet");
				}
			}
		}).send();
	});
}

/** 
 *	image Rollover
 *
 */
function imageRollover() {

	$$('img.rollover').each(function(img) {	
		
		var src = img.getProperty('src');
		
		img.addEvent('mouseenter', function() { 
			img.setProperty('src', img.getProperty('hover')); 
		});
		
		img.addEvent('mouseleave', function() { 
			img.setProperty('src',src); 
		});	
	});
}

/** 
 *	image Rollover
 *
 */
function shadowBoxInit() {

	Shadowbox.init({
		animate: false,
		animateFade: false
	});
}
