$(document).ready(function() {

	/*
		create activityItem carrousel
	*/

	website.activityItem.carrousel = function(nMediaItems) {
		
		// Check if carrousel html is present. If not, quit.
		if($('#activityItem-carrousel').length == 0)
			return;

		var pub = {};

		var timer;
		var currentItem;
		var justHadVimeo = false;
        var bVimeoIsPlaying = false;

        /*
            Vimeo implementation code
         */

        /**
         * Utility function for adding an event. Handles the inconsistencies
         * between the W3C method for adding events (addEventListener) and
         * IE's (attachEvent).
         */
        var addEvent = function(element, eventName, callback) {
            if (element.addEventListener) {
                element.addEventListener(eventName, callback, false);
            }
            else {
                element.attachEvent('on' + eventName, callback);
            }
        };

        var vimeoPlayerReady = function() {
            console.log('vimeo player ready!');

        };

        var vimeoPlayerOnPlay = function() {
            console.log('vimeo player: play!');

            // set boolean for timer checks
            bVimeoIsPlaying = true;

            // pause the countdown while playing
            clearInterval(timer);
        };

        /*
            /Vimeo implementation code
        */
		
		/*
			constructor
		*/

		init = function() {
			
			// set initial item
			currentItem = 0;

			// nav to and thereby force positioning of first item			
			scrollTo(0);
			
			
			// initial vimeo embeds (gets redone in the scrollTo)
			// includes a rather quick n dirty parser for multiple video's
			
			$('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container > .video').each(function(index) {
				
				var attr = $(this).attr('vimeoid');

				if (typeof attr !== 'undefined' && attr !== false) {
    				$(this).html('<iframe src="http://player.vimeo.com/video/'+($($('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container > .video')[index])).attr('vimeoId')+'?api=1&amp;color=00ab4e&title=0&amp;byline=0&amp;portrait=0&amp;autoplay=0&amp;loop=1" width="559" height="314" frameborder="0" style="display: block; background-color: #000;"></iframe>');
				}

				attr = $(this).attr('youtubeid');

				if (typeof attr !== 'undefined' && attr !== false) {
	    			$(this).html('<iframe width="559" height="314" src="http://www.youtube.com/embed/'+$(this).attr('youtubeid')+'?hd=1&wmode=transparent" frameborder="0" allowfullscreen style="z-index: -1;"></iframe>');
				}

				
			});			
	
			// set eventhandling
			setEventHandlers();
			
			// setup timer (gets stopped on user interaction
			timer = setInterval(function() {
	
				if (currentItem == nMediaItems-1) {
					scrollTo(0);
					
				} else {
					scrollTo(currentItem + 1);
				}
				
			}, 4600);
			
		}


		/*
			private methods
		*/
		
		var setEventHandlers = function() {

			// bind the clicks on the thumbnails
			for (i=0;i<nMediaItems;i++) {
			
				$($('#activityItem-carrousel > .carrousel-content > .info-carrousel > center > .thumb > .item')[i]).bind('click', function() {
					
					scrollTo($(this).attr('nr'));
					clearInterval(timer);
				});
			}
			
			// bind a mouseenter event on the video element(s) so the timers stops
			$('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container > .video').bind('mouseenter', function() {

				clearInterval(timer);
			});		
		}

		var scrollTo = function(nId) {
			
			// update currentItem
			currentItem = nId;

			var carrouselWidth = 577;
			var imageWidth = 559;

			$('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel').animate( {
				   'margin-left' : -(imageWidth * (nId))
			   },{'queue': false, 'duration': 400, 'easing': 'easeOutExpo', 'complete' : function() {
			   
				   	// update vimeo embeds
				   	if (justHadVimeo == true) {
							   		
						$('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container > .video').each(function(index) {							
							var attr = $(this).attr('vimeoid');

							if (typeof attr !== 'undefined' && attr !== false) {
			    				$(this).html('<iframe src="http://player.vimeo.com/video/'+($($('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container > .video')[index])).attr('vimeoId')+'?api=1&amp;color=00ab4e&title=0&amp;byline=0&amp;portrait=0&amp;autoplay=0&amp;loop=1" width="559" height="314" frameborder="0" style="display: block; background-color: #000;"></iframe>');
							}

							attr = $(this).attr('youtubeid');

							if (typeof attr !== 'undefined' && attr !== false) {
			    				$(this).html('<iframe width="559" height="314" src="http://www.youtube.com/embed/'+$(this).attr('youtubeid')+'?hd=1&wmode=transparent" frameborder="0" allowfullscreen style="z-index: -1;"></iframe>');
							}

							
						});	
				   		justHadVimeo = false;
				   	};
				   	
				   	// if current = vimeo set 		var justHadVimeo = true;
				   	if ( $($($('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container')[nId]).children()[0]).attr('class') == 'video' ) {
				   		justHadVimeo = true;
				   	};				   				   
			   	}
			   
			   });

			// position image in it's container
			var currentImageWidth = $($('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container')[nId]).children()[0].width;
			var currentImageHeight = $($('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container')[nId]).children()[0].height;
		
			$($($('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container')[nId]).children()[0]).css('margin-left', (559-currentImageWidth)/2);
			$($($('#activityItem-carrousel > .carrousel-header > .image-carrousel-border > .image-carrousel > .image-container')[nId]).children()[0]).css('margin-top', (313-currentImageHeight)/2);

			// update thumbnails
			$('#activityItem-carrousel > .carrousel-content > .info-carrousel > center > .thumb > .item').css('border-color', '#ffffff');
			$($('#activityItem-carrousel > .carrousel-content > .info-carrousel > center > .thumb > .item')[nId]).css('border-color', '#40c07a');

			$('#activityItem-carrousel > .carrousel-content > .info-carrousel > center > .thumb > .pointer').css('visibility', 'hidden');
			$($('#activityItem-carrousel > .carrousel-content > .info-carrousel > center > .thumb > .pointer')[nId]).css('visibility', 'visible');

             /*
                Bind Vimeo listeners
             */

            // bind the vimeo ready event to the iframe(s)
            var vimeoPlayers = $('.video').children();

            for (var i = 0, length = vimeoPlayers.length; i < length; i++) {
               player = vimeoPlayers[i];
               //$f(player).addEvent('ready', vimeoPlayerReady);
               $f(player).addEvent('play', vimeoPlayerOnPlay);
            };

            // reset the vimeo boolean
            bVimeoIsPlaying = false;
		};
		
		// call constructor
		init();

		// expose public methods / objects
		return pub;

	};
});
