// resize functie (deel code van supersized.js)
	$.fn.resizenow = function() {
		return this.each(function() {
			var imgH = $(this).children().height(),
				imgW = $(this).children().width(),
				biwH = $(window).height(),
				biwW = $(window).width(),
				ratio = imgH/imgW;

			if ((biwH/biwW) > ratio){
				$(this).height(biwH);
				$(this).width(biwH / ratio);
				$(this).children().height(biwH);
				$(this).children().width(biwH / ratio);
			} else {
				$(this).width(biwW);
				$(this).height(biwW * ratio);
				$(this).children().width(biwW);
				$(this).children().height(biwW * ratio);
			}
			$(this).children().css('left', (biwW - $(this).width())/2);
			$(this).children().css('top', (biwH - $(this).height())/2);

			return false;
		});
	};
// form-value
	jQuery.fn.toggleVal = function(focusClass) {
		this.each(function() {
			$(this).live('focus blur', function(event) {
				if (event.type == 'focus') {
					// clear value if current value is the default
					if($(this).val() == this.defaultValue) { $(this).val(""); }
				
					// if focusClass is set, add the class
					if(focusClass) { $(this).addClass(focusClass); }
				} else {
					// restore to the default value if current value is empty
					if($(this).val() == "") { $(this).val(this.defaultValue); }
				
					// if focusClass is set, remove class
					if(focusClass) { $(this).removeClass(focusClass); }
				}
			});
		});
	};
// preload
	$.fn.preloader = function(options){
		var defaults = {
			delay:0,
			preload_parent:this,
			check_timer:300,
			ondone:function(){ },
			oneachload:function(image){  },
			fadein:300 
		};

		var options = $.extend(defaults, options),
			root = $(this),
			images = root.find("img").css({"visibility":"hidden",opacity:0}),
			timer,
			counter = 0,
			i=0,
			checkFlag = [],
			delaySum = options.delay ,

		init = function(){
			timer = setInterval(function(){
				if(counter>=checkFlag.length){
					clearInterval(timer);
					options.ondone();
					return;
				}
				for(i=0;i<images.length;i++){
					if(images[i].complete==true){
						if(checkFlag[i]==false){
							checkFlag[i] = true;
							options.oneachload(images[i]);
							counter++;
							
							delaySum = delaySum + options.delay;
						}
						$(images[i]).css("visibility","visible").delay(delaySum).animate({opacity:1},
						options.fadein,
						function(){ $(this).parent().removeClass("preloader");});
					}
				}
			},options.check_timer)
		};

		images.each(function(){
			if($(this).parent(options.preload_parent).length==0)
				$(this).wrap("<div class='preloader' />");
			else
				$(this).parent().addClass("preloader");
			checkFlag[i++] = false;
		}); 
		images = $.makeArray(images); 

		var icon = jQuery("<img />",{
			id : 'loadingicon' ,
			src : '/images/load.gif'
		}).hide().appendTo("body");

		timer = setInterval(function(){
			if(icon[0].complete==true){
				clearInterval(timer);
				init();
				icon.remove();
				return;
			}
		},100);
	}

// external links
	function externalLinks() {
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
				anchor.target = "_blank";
		}
	};
	window.onload = externalLinks;
// facebookfeed

	/*
	
	message			de ingevoerde tekst
	picture			als intern (op FB) -> staat er een plaat bij
	source			bijv. http://www.youtube.com/v/lsm3_50qIzA&autoplay=1
	link				de link
	name				linknaam
	caption			linkalias
	description			beschrijving van de link
	icon				FB-icon i.e. wallpost or video
	type				link || video || status || photo
	
	*/

	function displayFacebookFeed(result) {
		$.each(result.data, function (i, item) {
			var type = item.type;
			var icon = item.icon;
			if(!icon){
				icon = '/images/status.jpg';
			}
			var username = item.from.name;
			var source = item.source;
			/*if(source){
				var splitLink1 = source.split('v/'),
					splitLink2 = splitLink1[1].split('&'),
					finalsource = '<img src="http://img.youtube.com/vi/'+splitLink2[0]+'/2.jpg" alt="videothumb" width="185" height="139" />';
				
			} else {
				*/var finalsource = name;
			//}
			var pageURL = "http://nl-nl.facebook.com/muziekweek";
			var delink = item.link;
			var name = item.name;
			var linkname = item.caption;
			if(!linkname){
				linkname = item.properties;
			}
			var picture = item.picture;
			var date = $.timeago(item.created_time);
			var body = item.message;
			
			if(!body){
				body = '';
			}
			if(type == 'link'){
				$(".facebookfeed ul").append(
					'<li><span class="fb_icon"><img src="'+icon+'" /></span><a href="'+pageURL+'" target="_blank">'+username+'</a> '+body+'<br /><a href="'+delink+'" target="_blank">'+name+'</a><br /><span class="caption">'+linkname+'</span><br /><span class="date">'+date+'</span></li>'
				);
			}
			if(type == 'video'){
				$(".facebookfeed ul").append(
					'<li><span class="fb_icon"><img src="'+icon+'" /></span><a href="'+pageURL+'" target="_blank">'+username+'</a> '+body+'<br />Watch video on <a href="'+delink+'" target="_blank">'+ linkname+'</a> &raquo;<br /><span class="caption">'+linkname+'</span><br /><span class="date">'+date+'</span></li>'
				);
			}
			if(type == 'status'){
				$(".facebookfeed ul").append(
					'<li><span class="fb_icon"><img src="'+icon+'" /></span><a href="'+pageURL+'" target="_blank">'+username+'</a> '+body+'<br /><span class="date">'+date+'</span></li>'
				);
			}
			if(type == 'photo'){
				$(".facebookfeed ul").append(
					'<li><span class="fb_icon"><img src="'+icon+'" /></span><a href="'+pageURL+'" target="_blank">'+username+'</a> '+body+'<br /><a href="'+delink+'" target="_blank"><img src="'+picture+'" alt="" style="display:block;" /></a><span class="date">'+date+'</span></li>'
				);
			}
		});
	}
	String.prototype.format = function () {
		var s = this,
		i = arguments.length;
		while (i--) {
			s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
		}
		return s;
	};


	$(document).ready(function(){
// png voor IE
		$('body').pngFix();
// enter does submit if IE
		if ($.browser.msie){
			$('input').keydown(function(e){
				if (e.keyCode == 13) {
					$(this).parents('form').submit();
					return false;
				}
			});
		}
// dotted linkline
		$('a').focus(function() {this.blur();});
// fancy tickets
$(".fancyticket").fancybox({
		'width': 960,
		'height': 550,
		'padding':0,
		'autoScale': false,
		'transitionIn': 'fade',
		'transitionOut': 'fade',
		'type'	: 'iframe'
	});
// plaatje is link
		$('a img').hover(function(){
			$(this).stop().animate({'opacity':.7}, 100);
		}, function(){
			$(this).stop().animate({'opacity':1}, 400, function(){
				if ( $.browser.msie ){
					this.style.removeAttribute('filter');
				}
			});
		});
// als links niet in markup mogen ivm worst-in-blik.
		$('a.verborgen').each(function(){
			var hiddenLink = $(this).text(),
				splitLink = hiddenLink.split('$'),
				emailLink = splitLink[0]+'@'+splitLink[1]+''+splitLink[2]+'.'+splitLink[3];
			$(this).attr('href', 'mailto:'+emailLink);
			$(this).text(emailLink);
			$(this).removeClass('verborgen');
		});
// values van label in input
// form als t-shirt
		$('label.hidden').each(function(){
			var tekst = $(this).text();
			$(this).next('input').val(tekst);
			$(this).hide();
		});
// focus op input
		$('textarea, input[type="text"]').addClass("idleField");
		// textarea
		$('textarea').focus(function() {
			$(this).removeClass("idleField").addClass("focusField");
		});
		$('textarea').blur(function() {
			$(this).removeClass("focusField").addClass("idleField");
		});
		//input type=text
		$('input[type="text"], textarea').live('focus', function() {
			$(this).removeClass("idleField").addClass("focusField");
			var defVal = $(this).prev('label').text();
			if (this.value == this.defaultValue){ 
				this.value = '';
			}
			if(this.value != this.defaultValue){
				this.select();
			}
		});
		$('input[type="text"], textarea').live('blur', function() {
			$(this).removeClass("focusField").addClass("idleField");
			if ($.trim(this.value) == ''){
				this.value = (this.defaultValue ? this.defaultValue : '');
			}
		});
// printbutton
		$('#printwindow').click(function(){
			window.print();
			return false;
		});
// javascript is on...
		$('.bg-image')
			.addClass('wrapped-image')
			.removeClass('bg-image')
			.wrap('<div class="bg-image-wrap" />');
// ...then we can call the resize function for the .bg-image
		$('.bg-image-wrap').resizenow();
		$('.bg-image-wrap').preloader();
// twitter feed
		$(".twitterfeed").twit("muziekweek", {
			limit: 1,
			label: 'Twitter',
			title: ''
		});
// last.fm feed
		$('.lastfmfeed').lastfm('Muziekweek','recenttracks','e0de46d270197ba1e7878e99a5b54085', {
			noimage: '/images/noimage.gif'
		});
		$('.lastMore').live('click', function(){
			if($('.lastfmfeed').find('li.hidden').size() == 1){
				$('p.lastNavi').css('display','none');
			}
			$('.lastfmfeed').find('li.hidden').slice(0,1).removeClass('hidden').css({'display':'block', 'opacity':0}).animate({'opacity':1}, 500);
		});
		$('.lastTop10').live('click', function(){
			$('.lastfmfeed').find('li.hidden').removeClass('hidden').css({'display':'block', 'opacity':0}).animate({'opacity':1}, 500);
			$('p.lastNavi').css('display','none');
		});
// flickr feed
		$('.flickrfeed').flickrfeed('58827951@N03','', {
			limit: 6
		});
// facebook feed
		$.ajax({
			url: 'https://graph.facebook.com/180736388633633/feed?access_token=217821654917956|d4674c31550460f4553e3416.1-1412556297|uUA7xeJAtRDxjFXOpIvFWLhbmf8&limit=1&callback=?',
			dataType: 'json',
			success: displayFacebookFeed
		});
		var clicks = 1;
		$('.facebookMore').live('click', function(){
			var fbUlH = parseInt($('.facebookfeed').children('ul').height());
			$('.facebookfeed').children('ul').css('min-height', fbUlH);
			$('.facebookfeed').find('li').addClass('old').animate({'opacity':0}, 350, function(){ $('.facebookfeed').find('li.old').remove() })
			$('.facebookfeed').find('p.facebookNavi').animate({'opacity':0}, 150, function(){
				$.ajax({
					url: 'https://graph.facebook.com/180736388633633/feed?access_token=217821654917956|d4674c31550460f4553e3416.1-1412556297|uUA7xeJAtRDxjFXOpIvFWLhbmf8&limit='+clicks+'&callback=?',
					dataType: 'json',
					success: displayFacebookFeed
				});
				
			
				$('.facebookfeed').find('p.facebookNavi').animate({'opacity':1}, 150, function(){
					if ( $.browser.msie ){
						this.style.removeAttribute('filter');
					}
				});
			});
			clicks = clicks+1;
			return false;
		});
	});

// na browser resize, afbeelding ook resizen
	$(window).bind("resize", function(){
		$('.bg-image-wrap').resizenow();
	});
