/**
 * @author johanrombouts
 */

YTPlaylistService = new function(){
	
	var moving=false;
	
	
	
	this.init = function(){
		$('body').prepend('<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/playlists/5351C0DA47637ACC?&v=2&alt=jsonc&callback=YTPlaylistService.jsonObj" ></script>');
	}
	
	this.randOrd = function(){
		return (Math.round(Math.random())-0.5); 
	}
	
	this.shuffle = function(o){ //v1.0
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
	};
	
	this.jsonObj = function(data){
		var liList ='';
		var arrList= new Array();
		for(var y=0; y<data.data.items.length; y++){
			try{
				if (data.data.items[y].video.thumbnail.sqDefault) {
				arrList.push('<img src="' + data.data.items[y].video.thumbnail.sqDefault + '" title="' + data.data.items[y].video.title +'" id="'+ data.data.items[y].video.id+'" />')  ;
				}
			}catch(errorObject){}
		}
		var shufList = this.shuffle(arrList);
		
		$('#YTPlaylists').append('<div id="waterwheelCarousel">'+ shufList.toString().replace(/\,/g, ' ') +'</div>')
		
		$("#waterwheelCarousel").waterwheelCarousel("horizontal",{
			startingItem: 3,
			startingWaveSeparation: 0,
			startingItemSeparation: 300,
			itemSeparationFactor: .3,
			centerOffset: 160,
			itemDecreaseFactor: .8,
			flankingItems: 1,
			clickedCenter: function(jObj){
				var vid = jObj.attr('id');
				var embedCode = '<div id="ytplayer" class="ellapsed"><object id="ytplayerControl" class="ellapsed"><param name="movie" value="http://www.youtube.com/v/' + vid + '&amp;hl=nl_NL&amp;fs=1&amp;autoplay=1&amp;enablejsapi=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + vid + '&amp;hl=nl_NL&amp;fs=1&amp;autoplay=1&amp;enablejsapi=1" id="ytplayerControl" class="ellapsed" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"></embed></object><script>function onYouTubePlayerReady(playerId){ytplayerCtrl = document.getElementById("ytplayerControl"); ytplayerCtrl.addEventListener("onStateChange", "onytplayerStateChange");}function onytplayerStateChange(newState) {if (newState == 0){ $("#'+ vid +'").next().trigger("click");}}</script></div>';
				this.moving=false;
				var imgsrc = jObj.attr('src');
				var imgcode = '<div id="imgplayer" class="collapsed"><img src="'+ imgsrc +'" class="collapsed"/></div>';
				jObj.css({
					zIndex:-1
				});
				$('#playerPlaceholder').html(imgcode);
				$('#imgplayer').switchClass('collapsed','ellapsed',500);
				$('#imgplayer img:first').switchClass('collapsed','ellapsed',500);
				var st = setTimeout(
					function(){
						$('#playerPlaceholder').html('<div id="imgplayer" class="ellapsed"><img src="'+ imgsrc +'" class="ellapsed"/></div>' + embedCode);
					}
				,500);	
			},
			movedToCenter: function(jObj){
				if (!this.moving == true) {
					//alert(this.moving);
					this.moving = true;
					$('#playerPlaceholder').children('#ytplayer').html('');
					$('#imgplayer').switchClass('ellapsed','collapsed',500);
					$('#imgplayer img:first').switchClass('ellapsed','collapsed',500);
					var jjObj = jObj.attr('id');
					var st = setTimeout(
						function(){
							$('#' + jjObj).trigger('click');
						}	
					, 1000);
				}
			}
	    });
		
		
		

	}
	
	
	
}	
