$(document).ready(function() {
	jQuery('div.videos').Carousel();
	jQuery('div.updates').MessageLoader();	
	jQuery('div.vids').ImageLoader(600, 450, 54);
	jQuery('div.disco').ImageLoader(600, 450, 65);	
	jQuery('div.foldscreen').FoldOut();
	jQuery('ul.list-toggle').ListToggler('h2','div.description');
	//jQuery('div.discography').Discography();
	jQuery('ul.accordion').accordion({ 
		header: 'h4',
		collapsible:true,
		active: false
	});
	getDiscoPlayer();
	initDatePicker();
	findExternalLinks();
	
	Shadowbox.init({
		handleOversize: "drag",
		modal: true
	});
	
	//IE6 Only functions
	jQuery.each(jQuery.browser, function(i, val) {
		if (i == "msie" && parseInt(jQuery.browser.version.substr(0, 3)) < 7) {
			setFirstChild('div.footer ul.metamenu li');
		}
	});
	
});

//(function(){
	/*
	 * Load Discography
	 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
	 * @since 11 apr 2011
	 * @version 1.0
	 * @copyright eFocus
	 *
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
/*
	$.fn.Discography = function(){
		var elContainer = jQuery(this);
		var arrAlbumLinks = jQuery('a.load-album');

		arrAlbumLinks.click(function(){
			var uId = jQuery(this).find('span.uid').html();
			var url = jQuery(this).find('span.url').html();

		tx_gm_discographygetAlbumWithSongs(uId)
			$.ajax({
				type: "POST",
				url: url,
				//data: "xajax=getAlbumWithSongs&xajaxr=1&xajaxargs[]=" + uId,
				complete: function(msg){
					jQuery('ul.accordion').accordion({
						header: 'h4',
						collapsible: true,
						active: false
					});
					getDiscoPlayer();
				}
			});
			
				
			return false;
		});
	}
})(jQuery); */


(function(){
	/*
	 * jQuery loadMessages
	 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
	 * @since 11 apr 2011
	 * @version 1.0
	 * @copyright eFocus
	 *
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
	$.fn.MessageLoader = function(){
		var elContainer = jQuery(this);
		var arrFoldScreenLinks = jQuery('a.message-load');
		var counter = 5;

		arrFoldScreenLinks.click(function(){
			tx_gm_updatesgetMoreUpdates(counter)
			counter = counter += 5;

			return false;
		});
	
	}
})(jQuery);

(function(){
	/*
	 * jQuery set FoldOut Screen
	 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
	 * @since 25 maa 2011
	 * @version 1.0
	 * @copyright eFocus
	 *
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
	$.fn.FoldOut = function(){
		return this.each(function(){
			var elContainer = jQuery(this);
			var arrFoldScreenLinks = jQuery('a[rel|=foldscreen]');
			var arrFoldScreenClose = jQuery('a[rel|=fclose]')
			var isEnabled = true;
			
			arrFoldScreenClose.click(function(el){
				jQuery('#mediaspace').addClass("hidden");
				//sendEvent('STOP');
				elContainer.animate({
					height: '0'
				}, 'fast', function(){
					isEnabled = true;
				});			
			});	
			
			arrFoldScreenLinks.click(function(el,i){
				if (isEnabled) {
					$("html, body").animate({ scrollTop: 0 }, "slow");
					elContainer.animate({
						height: '100%'
					}, 600, function(){
						elContainer.effect("bounce", {
							distance: 20,
							times: 1
						}, 1000, function(){
							jQuery('#mediaspace').removeClass("hidden");
							var elLink = el.currentTarget;
							setJWPlayer(elLink.href, "mediaspace");
							isEnabled = false;	
						})
					})
				} else {
						var elLink = el.currentTarget;
						setJWPlayer(elLink.href, "mediaspace");	
				}
				
				return false;
			});
		})
	};
})(jQuery);

(function(){
	/*
	 * jQuery image loader, flashy pops them up while loading...
	 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
	 * @since 4 apr 2011
	 * @version 1.0
	 * @copyright eFocus
	 * 
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
	$.fn.ImageLoader = function(intFade, intDelay, intImageSize){
		return this.each(function(){
			
			//tx_gm_discography[album]
			
			var elContainer = jQuery(this);
			var images = elContainer.find('img');
			var i = 0;
			
			//reset the images first
			images.css({
				opacity: 0,
				height: 0		
			});
			
			images.each(function(){
				var image = jQuery(this);
				setTimeout(function(){
					 image.animate(
					 { 
					 	height: intImageSize +"px",
						opacity: 1 
					 }, intFade,
					 function(){
						image.parent().addClass("loaded");
					});					
				}, i)
				i += intDelay
			});

		});
	};
})(jQuery);

(function(){
	/*
	 * ListToggler 
	 * @param elHeader string to click on
	 * @param elOpen string to open
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
	$.fn.ListToggler = function(elHeader, elOpen){
		return this.each(function(){
			var elContainer = jQuery(this);
			var url = window.location.href;

			jQuery(this).find(elOpen).hide();
			jQuery(this).find('li').addClass("close");
						
			fnOpen = function(el){
				el.parent('li').removeClass("close");	
				el.parent('li').addClass("open");
				el.toggle('slow');	
			}
			
			fnClose = function(el){
				el.parent('li').removeClass("open");	
				el.parent('li').addClass("close");
				el.toggle('slow');						
			}
			
			if(url.indexOf("#") != -1){
				var id = window.location.href.split("#")[1];
				var el = jQuery(this).find('li[name*="' + id + '"]');
				fnOpen(el.find(elOpen));	
			}
			
			elContainer.find(elHeader).click(function() {
				var elArea = jQuery(this).parent().find(elOpen);
				
				if(elArea.css("display") == "none"){
					fnOpen(elArea);
				} else {			
					fnClose(elArea);			
				}
				
				return false;
			}).next();
		});
	}
})(jQuery)



/**
 * Prepare the JWPlayer
 * 
 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
 * @version 1.0, 05 apr, 2011
 * @param string href url of youtube video
 * @param string id of the space element 
 * @return void
 */
function setJWPlayer(href, space){
	var so = new SWFObject('/fileadmin/templates/js/jwplayer/player.swf', 'mpl', '720', '406', '9');
	so.addParam('allowfullscreen', 'true');
	so.addParam('allowscriptaccess', 'always');
	so.addParam('wmode', 'transparent');
	/*so.addParam('mode',[
		{type: 'html5'},
        {type: 'flash', src: 'player.swf'},
        {type: 'download'}
	]),*/
	so.addVariable('file', href);
	so.addVariable('feature', 'youtube_gdata_player');
	so.addVariable('backcolor', '000000');
	so.addVariable('frontcolor', 'FFFFFF');
	so.addVariable('lightcolor', '999999');
	so.addVariable('screencolor', '000000');
	so.addVariable('controlbar', 'over');
	so.addVariable('autostart','true');
	so.write(space);
}

function setJWPlayerMp3(href, space){
	var so = new SWFObject('/fileadmin/templates/js/jwplayer/player.swf', 'ply', '445', '24', '9');
	so.addParam('allowfullscreen', 'true');
	so.addParam('allowscriptaccess', 'always');
	so.addParam('wmode', 'transparent');
	/*so.addParam('mode',[
		{type: 'html5'},
        {type: 'flash', src: 'player.swf'},
        {type: 'download'}
	]),*/
	so.addVariable('file', href);
	so.addVariable('backcolor', '000000');
	so.addVariable('frontcolor', 'FFFFFF');
	so.addVariable('lightcolor', '999999');
	so.addVariable('screencolor', '000000');
	so.write(space);
}

/**
 * Prepare the JWPlayer for Disco page
 * 
 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
 * @version 1.0, 05 apr, 2011
 * @return void
 */
function getDiscoPlayer(strId){
	var arrSongLinks = jQuery('h4.load-song');
	arrSongLinks.click(function(){
		var href = jQuery(this).find('span').text();
		var uid = jQuery(this).parent().find('div.uid').text();		
		if(href.length > 1){
			setJWPlayerMp3(href, 'player_'+uid);	
		}
	});	
}

/**
 * Find all external links, and add class External
 *
 * @author Mirjam Verloop, <mirjam.verloop@efocus.nl>
 * @author Lee Boonstra, <lee.boonstra@efocus.nl> 
 * @version 1.0, 6 mei, 2011
 * @return void
 */
function findExternalLinks() {
	var allExternalLinks = jQuery('a[href^="http://"]');
	var thisDomain = window.location.host;
	allExternalLinks.each(function(i) {
		//console.log(jQuery(this).attr('rel'))
		if (!jQuery(this).attr('href').indexOf(thisDomain) !== -1 && jQuery(this).attr('rel')!=="foldscreen") {
			jQuery(this).attr('rel','external');
		}
	});
	initExternalLinks();
} 

/**
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author Mirjam Verloop, <mirjam.verloop@efocus.nl>
 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
 * @version 1.0, 19 jul, 2010
 * @requires <a href="http://www.efocus.nl/" class="external">eFocus</a>
 * @return void
 */
function initExternalLinks() {
	var arrExternalLinks = jQuery('a[rel|=external]');
	if (arrExternalLinks.length > 0) {
		arrExternalLinks.each(function() {
				$(this).click(function(e) {
				window.open($(this).attr('href'));
				return false;
			});
		});	
	}
}

/**
 * Incase the list item has no previous list item, add class
 * 
 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
 * @version 1.0, 11 jan, 2011
 * @param list - ul item
 * @return void
 */
function setFirstChild(list){
	$(list).children().each(function(){
		if ($(this).prev().length == 0) {
			$(this).addClass('first-child');
		}
	});
}


/**
 * init datepicker
 * 
 * @author Phi Son Do, <phison.do@efocus.nl>
 * @version 1.0, 19 apr, 2011
 * @param list - ul item
 * @return void
 */
function initDatePicker(){
	$( "#date" ).datepicker();

	$.datepicker.regional['nl'] = {
	closeText: 'Sluiten',
	prevText: '�?',
	nextText: '→',
	currentText: 'Vandaag',
	monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
	'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
	monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
	'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
	dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
	dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
	dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
	weekHeader: 'Wk',
	dateFormat: 'dd-mm-yy',
	firstDay: 1,
	isRTL: false,
	showMonthAfterYear: false,
	yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['nl']);

}
