/**
 * Muziekgebouw Actiesites
 *
 * common js
 *
 * @author AK, 2010
 * 
 */


// delclare global vars
;var peppered;


// cufon
//Cufon.replace('#intro .title .main, .text .superStrong');


(function($) { // jQuery closure

// globals
// ...

// peppered object
peppered = {

//				verwijs_url = $this.attr('deeplink'),			url:  verwijs_url,
videos: {
	
	init: function() {
		var _this = this;
		
		swfobject.registerObject("test", "9.0.0", "/flash/expressInstall.swf");

		
		$('#trailers li.video a').each(function() {
			var $this = $(this),
				href = $this.attr('href'),
				vId = href.split('v=')[1].split('&')[0],
				title = $this.attr('title')
			;
			_this.setMeta($this, vId);

			$this.click(function() {
				_this.play(vId, title, $this.data());
				return false;
			});
		});
		
	},
	
	play: function(vId, title, data) {
		//console.log(data.referral.length)
		var _this = this,
			vidSrc = "http://www.youtube.com/v/" + vId + "&hl=nl_NL&fs=1&autoplay=1&rel=0",
			arrTitle;
		
		if (ytplayer) {
			ytplayer.loadVideoById(vId);
			arrTitle = title.split('|');
			if (arrTitle.length > 1) {
				title = '<span class="h3">' + arrTitle[0] + '</span><br /><span class="h4">' + arrTitle[1] + '</span>';
			}
			else {
				title = '<span class="h3">' + arrTitle[0] + '</span>';
			}
			if (data.referral.length > 0) {
				title = '<a href="' + data.referral + '">' + title + '</a>'; 
			}
			
			$('#trailers h3').html(title);
		}
		
		return;
		
		
		$.pDialog({
			id: 'video',
			type: 'swf',
			title: 'YouTube',
			verwijs_url: verwijs_url,
			swfobject_config: {
				file: vidSrc,
				width: 476,
				height: 300,
				version: '9.0.0',
				expressInstallSwfurl: '/flash/expressInstall.swf'
			},
			width: 500,
			height: 350,
			closeOnOutsideClick: true,
			hideTitleBar: true,
			resizable: false,
			centeringTopMargin: 10,
			keepInDom: false,
			buttsons: {
				'Sluiten': {
					action: 'cancel'
				}
			}
		});
		

	},
	
	setMeta: function($elm, vId) {
		var _this = this;
		// http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#Retrieve_video_entry
		
		$.getJSON('http://gdata.youtube.com/feeds/api/videos/' + vId + '?v=2&alt=json-in-script&callback=?', function(data, textStatus) {
			//console.log(data.entry.media$group)
			var thumb = data.entry.media$group.media$thumbnail[0],
				// 164x93
				w = 70,
				h = Math.ceil((thumb.height/thumb.width) * w),
				img = new Image(w, h),
				s, m, date
			;
			
			img.src = thumb.url;
			$elm.find('.img').html(img);
			
			s = data.entry.media$group.yt$duration.seconds;
			m = Math.floor(s/60);
			s = Math.floor(s%60);
			
			if (s < 10) { s = '0' + s }
			
			date = (data.entry.published.$t).split('T', 1)[0].split('-');
			
			$elm//.find('.title').text(data.entry.title.$t)
				.parent()
				.find('.viewCount').text(data.entry.yt$statistics.viewCount)
				.end()
				//.find('.published').text($.datepicker.formatDate('dd/mm/yy', new Date(date[0], date[1], date[2])))
				//.end()
				.find('.runningTime .time').text(m + ':' + s)
			;			
		});
	}
	
} /* /videos */



};	/* /peppered */




})(jQuery);

function onYouTubePlayerReady(playerId) {
	console.log(playerId);
  //  ytplayer = document.getElementById("test");
    ytplayer = swfobject.getObjectById('test');
    console.log(ytplayer);
  }

