/**
 * Muziekgebouw Actiesites
 *
 * common js
 *
 * @author AK, 2010
 * 
 */


// delclare global vars
;var peppered;


// twitter gebruikt het //
function isEmpty(str) { if (typeof str == 'undefined' || str === null || str === '' || str == 0) { return true; } return false; }


// cufon
Cufon.replace('#intro .title .main, .text .superStrong');



(function($) { // jQuery closure

// globals
// ...

// peppered object

peppered = {


common: {
	
	pageEnhance: function(config) {
		
		// reflecting bg
		var bgImgConfig = config.bgImgConfig,
			rhf = 1/3
		;
		
		document.write('<div id="bgReflect"><img src="' +  bgImgConfig.src + '" width="' + bgImgConfig.w + '" height="' + bgImgConfig.h + '" /></div>');
		$(function(){ $('body').css('background-image', 'none') });
		$('#bgReflect img').reflect({ opacity: 1, height: rhf });
		
		// set #outerWrap minHeight
		var minH = bgImgConfig.h + Math.floor(bgImgConfig.h * rhf);
		$('#outerWrap').css('minHeight', minH);
		
	},
	
	pageEnhanceIE: function() {
		
		// rounded corners (http://jquery.malsup.com/corner/)
		if (typeof $.fn.corner == 'function') {
			$('#menu li, #share, div.block a.all').corner('3px');
			$('#sponsors h2').corner('4px top');
			$('html').addClass('corner-active');
		}
		
		// drop shadow (<> filters in conditional css)
		$('div.block').each(function() {
			var $this = $(this);
			$this.wrapInner('<div class="block-content"/>').append('<div class="block-dropshadow"/>').addClass('dropshadow');
		});
		
	}
	
},	/* /common */


theirTips: {

	/**
	 * init
	 * Enables hover popup
	 * uses hoverIntent: http://cherne.net/brian/resources/jquery.hoverIntent.html
	 */
	init: function() {
	
		var $quotes = $('#theirTips li q');
		
		$quotes.hide(); // needed 4 IE7
		
		$('#theirTips li a').hoverIntent({
			over: function() {
				var $this = $(this),
					$quote = $this.find('q')
				;
				
				$quotes.addClass('noscr').hide();
				$quote.removeClass('noscr').show();
			},
			out: function() {
				var $this = $(this),
					$quote = $this.find('q')
				;
				$quote.addClass('noscr').hide();
			},
			timeout: 350
		});
	}

},	/* /theirTips */


ourTips: {

	/**
	 * init
	 * Enables hover popup
	 * uses hoverIntent: http://cherne.net/brian/resources/jquery.hoverIntent.html
	 */
	init: function() {
		
		$.metadata.setType("attr", "data");
		var $infoPops = $('#ourTips li .info').hide(); // hide needed for IE7
		
		$('#ourTips li a').hoverIntent({
			over: function() {
				var $this = $(this),
					$infoPop = $this.find('.info'),
					$img = $this.find('img'),
					$parent = $this.parent()
				;
				
				$parent.css('zIndex', 1).siblings().css('zIndex', 0);
				
				//var hoverImgSrc = $img.attr('src').replace(/img(.*)_34\.jpg/, 'img$1_39.jpg');
				var hoverImgSrc = $parent.metadata().hoverImg;
				//console.log($parent.metadata().hoverImg);
				$infoPops.addClass('noscr').hide();	
				$infoPop.removeClass('noscr').css('background-image', 'url(' + hoverImgSrc + ')').show();
			},
			out: function() {
				var $this = $(this),
					$infoPop = $this.find('.info')
				;
				$infoPop.addClass('noscr').hide();		
				//$this.parent().css('zIndex', 0);
			},
			timeout: 350
		});
	}

},	/* /ourTips */

//				verwijs_url = $this.attr('deeplink'),			url:  verwijs_url,
videos: {
	
	init: function() {
		var _this = this;
		
		$('#videos li.video a').each(function() {
			var $this = $(this),
				href = $this.attr('href'),
				verwijs_url = $this.attr('deeplink'),
				vId = href.split('v=')[1].split('&')[0]
			;
			_this.setMeta($this, vId);

			$this.click(function() {
				_this.play(vId,verwijs_url);
				return false;
			});
		});
		
	},
	
	play: function(vId,verwijs_url) {
		var _this = this;
		
		var vidSrc = "http://www.youtube.com/v/" + vId + "&hl=nl_NL&fs=1&autoplay=1&rel=0";
		
		$.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) {
			var thumb = data.entry.media$group.media$thumbnail[4],
				// 164x93
				w = 164,
				h = Math.ceil((thumb.height/thumb.width) * 164),
				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 */


shoppingBasket: {

	init: function() {
		$("#shoppingBasket.empty").hide();	
		$('form.formInWinkelmandje').submit(function() {
			var id=$('form.formInWinkelmandje input[name="voorstelling_id"]').attr("value");
			
		    $.post("/index.php?ajax=true", $(this).serialize(), 
				function(data) {
		    		// NB this ajax call is returning much more data (is for the normal site), 
		    		// only use count here.
		    		
					$("#shoppingBasket").slideDown("normal", function() {
						$("#shoppingBasket .submit").fadeIn(200);
					});
					if (data.count == 1) {
						$("#basketCount").text("1 concert");
					} else {
						$("#basketCount").text(data.count + " concerten");
					}
		    	}, 'json'
			);
			return false;
		});
	}

} /* /shoppingBasket */


};	/* /peppered */


})(jQuery);

