/**
 * Voorstellingoverzicht & detail js
 * @author arjen
 */

// voorstellingen genre/maand select

initSelect('formVoorstellingenFilter-genre');
initSelect('formVoorstellingenFilter-maand');


/**
 * Voorstellingen list items
 * 
 */
$('#content ul.voorstellingenLijst li.voorstelling:not(".voorstellingMultiple")').not($('li.voorstellingMultiple li.voorstelling')).hover(function(){
	$(this).addClass('voorstellingHover');
	$(this).find('a.voorstellingDetailLink').addClass('hover');
	
}, function(){
	$(this).removeClass('voorstellingHover');
	$(this).find('a.voorstellingDetailLink').removeClass('hover');
});

$('#content ul.voorstellingenLijst li.voorstelling:not(".voorstellingMultiple,.voorstellingVacant")').not($('ul.voorstellingMultiple li.voorstelling')).click(function(){
	sHref = $(this).find('a.voorstellingDetailLink').attr('href');
	window.location.href = sHref;
});
$('#content ul.voorstellingenLijst li.voorstelling:not(".voorstellingMultiple,.voorstellingVacant")').not($('ul.voorstellingMultiple li.voorstelling')).find('input').click(function(e){
	e.stopPropagation();
});

$('#content ul.voorstellingenLijst li.voorstelling input.textSubmit').hover(function(){
	$(this).parents('li.voorstelling').find('a.voorstellingDetailLink').removeClass('hover');
},function(){
	$(this).parents('li.voorstelling').find('a.voorstellingDetailLink').addClass('hover');
});

$('#voorstKalenderLink').removeClass('hidden');
$('#voorstKalenderLink').addClass('voorstellingenKalenderLink');

// genre/maand dropdowns	
$('#formVoorstellingenFilter input.submit').addClass('hidden');
$('#formVoorstellingenFilter label').addClass('hidden');
//$('#formVoorstellingenFilter').css({top: '-80px'});
$('#formVoorstellingenFilter').addClass('fx');

if ($('body').is('#bodyVoorstellingen'))
	$('#subColumn2').css('padding-top', '3em');
else
	$('#subColumn').css('padding-top', '0em');

	// onchange handler via selectaccessible.js!

	
/**
 * Voorst. detail audio/video
 * @author AK
 * 
 */
function closeMediaPlayer() {
	$('.flashMediaPlayer object, .flashMediaPlayer').remove();
	$('.pageContent .mediafragmenten').remove();
	$('.pageContent').addClass('content');
	$('.pageContent').removeClass('mediaShow');
	$('#voorstellingActies li a').removeClass('active');
	$('.omschrijvingWrap').show();
}

/**
 * spawnPlayer
 */
function spawnPlayer(type, file, title) {
	if (type == 'video') {
		var iMovieW = 390;
		var iMovieH = 345;
		var iMovieFlashVer = 9;
	}
	else if (type == 'audio') {
		var iMovieW = 390;
		var iMovieH = 50
		var iMovieFlashVer = 7;
	}
	
	// remove & add container for swf
	$('#voorstellingDetail-flash'+type.ucfirst()).remove();
	$('.pageContent').prepend('<div class="flashMediaPlayer" id="voorstellingDetail-flash'+type.ucfirst()+'"></div>');
	
	// swfobject
	var flashvars = {playMe: true, myFile: file, title: title.toUpperCase(), closeFunction: "closePlayerFromFlash"};
	var params = {wmode: "opaque", bgcolor: '#262626'};
	//var attributes = {id: "flash"+type.ucfirst(), styleclass: "flashMediaPlayer"};
	var attributes = { styleclass: "flashMediaPlayer" };
	swfobject.embedSWF("/flash/"+type+"player.swf", "voorstellingDetail-flash"+type.ucfirst(), iMovieW, iMovieH, "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
	if ($('#voorstellingDetail-flash'+type.ucfirst()))
		return true;
	return false;
}	
	
// bind click events fragmenten
$('.pageContent .mediafragmenten').each(function(){
	sType = $(this).attr('id').replace('Fragmenten', '');
	$(this).find('a').not('.backlink').click(function(){
		var sFile = $(this).attr('href');
		var sTitle = $(this).text();
		$(this).parent().siblings().children('a').removeClass('active');
		$(this).addClass('active');
		if (spawnPlayer(sType, sFile, sTitle))
			return false;
	});
});


// remove fragmenten list, and generate video anchor
oFragmenten = new Object();
$('.pageContent .mediafragmenten').each(function(){
	sType = $(this).attr('id').replace('Fragmenten', '');
	$(this).children('h3').hide();
	oFragmenten[sType] = $(this).clone(true);
	$(this).remove();
	$('#voorstellingActies ul').append('<li id="voorstellingActies-'+sType+'"><a href="#" class="mediaToggle" id="show'+sType.ucfirst()+'Anchor">'+sType+'</a></li>');
});


// onclick mediaToggle
$('#voorstellingActies a.mediaToggle').each(function(){
	$(this).click(function(){
		
		sType = $(this).attr('id').replace('show', '').replace('Anchor', '').toLowerCase();
		
		// gajemee active -> reget page
		if (document.location.search.indexOf('act=gajemee') != -1) {
			document.location.href = document.location.pathname+'?act='+sType;
			return false;
		}
		
		// active?
		if ($(this).is('.active')) {
			closeMediaPlayer();
			$(this).removeClass('active');
			return false;
		}
		
		$('#audioFragmenten, #videoFragmenten').remove();
		$('.flashMediaPlayer object, .flashMediaPlayer').remove();
		$('.pageContent .omschrijvingWrap').hide();
		$('.pageContent').append(oFragmenten[sType]);
		oFragmenten[sType] = $('#'+sType+'Fragmenten').clone(true);
		$('.pageContent').addClass('mediaShow');
		$('.pageContent').removeClass('content');
		$('.pageContent .mediafragmenten').append('<p><a href="javascript:closeMediaPlayer();" class="backlink">terug naar tekst</a></p>');
		$('#voorstellingActies li a').removeClass('active');
		$(this).addClass('active');
		$('#' + sType + 'Fragmenten ul li:first a').click();
		
		// force set 'ga je mee' anchor to ga je mee form
		$('#voorstellingActie-gajemee a').attr('href', document.location.pathname+'?act=gajemee');
		
		return false;
	});
});

// trigger click mediaToggle on right 'act' queryvar
if (document.location.search.indexOf('act=video') != -1) {
	$('#showVideoAnchor').click();
}
else if (document.location.search.indexOf('act=audio') != -1) {
	$('#showAudioAnchor').click();
}


/**
 * == voorstellingen kalendervorm ==
 * 
 * @author AK
 * 
 */

// put popup info into wrapper

$('ul.voorstellingenLijstKalender li.voorstelling:not(".voorstellingMultiple,.voorstellingVacant")').each(function(){
	$(this).wrapInner('<div class="voorstellingWrap"></div>');

	oDatum = $(this).find('.voorstellingDatum').clone(true);
	$(this).find('.voorstellingDatum').remove();
	$(this).prepend(oDatum);
	
	oImg = $(this).find('img').clone(true);
	$(this).find('img').remove();
	$(this).prepend(oImg);
});


/*
 * Dimension:
 * 	popup: 260*130
 * 	v. item: 128*128
 */

// hover items
var iPopTimeoutId;
var oLastHoveredItem = null;
var oHoveredItem = null;

$('#content ul.voorstellingenLijstKalender > li.voorstelling:not(".voorstellingVacant")').hover(function(){
	//$.log('hoverIn li');
		
	window.clearTimeout(iPopTimeoutId);	
	oHoveredItem = $(this);
	
	// need to create voorstellingPopup?
	if (!$('#voorstellingPop').length) {
		//$(this).find('.voorstellingWrap').hide();
		$('body').append('<div id="voorstellingPop"></div>');		
	}
	else
		$('#voorstellingPop').removeClass('voorstellingPopMultiple');	
	
	// set popup contents
	if ($(this).is('.voorstellingMultiple')) {
		oWrap = $(this).find('.voorstellingWrap').clone(true);
		//$.log(oWrap.html());
	}
	else {
		oWrap = $(this).find('.voorstellingWrap').clone(true);
	}
	
	$('#voorstellingPop').html(oWrap);
	$('#voorstellingPop .voorstellingWrap:last').css({borderWidth: 0});
	
	if ($(oWrap).length > 1)
		$('#voorstellingPop').addClass('voorstellingPopMultiple');	
	
	oLastHoveredItem = this;
	//$.log(oLastHoveredItem);
	
	// popup hover events
	oThisItem = $(this);
		
	$('#voorstellingPop').hover(function(e) {
		//$.log('hoverIn popup');
		oHoveredItem = oThisItem;
		oThisItem.addClass('voorstellingHover');		
		window.clearTimeout(iPopTimeoutId);			
	}, function(e) {
		//$.log('hoverOut popup');
		oThisItem.removeClass('voorstellingHover');		
		//$('#voorstellingPop').remove();
	});
	
	// items in popup hover
	$('#voorstellingPop .voorstellingWrap').hover(function(){
		$(this).addClass('voorstellingPopHover');
		$(this).find('a.voorstellingDetailLink').addClass('hover');
	}, function() {
		$(this).removeClass('voorstellingPopHover');
		$(this).find('a.voorstellingDetailLink').removeClass('hover');
	});
	
	$('#voorstellingPop .voorstellingWrap').click(function(){
		sHref = $(this).find('a.voorstellingDetailLink').attr('href');
		window.location.href = sHref;
	});
	
	// calculate popup offset & set it
	oOffset = $(this).offset();	
	iOffsetY = $(this).find('.voorstellingDatum').height();
	iOffsetX = $(this).find('.voorstellingDatum').width();
	iVoorstellingPopWidth = $('#voorstellingPop').width();
	iVoorstellingPopHeight = $('#voorstellingPop').height();
	
	iPopLeft = oOffset.left-iVoorstellingPopWidth+iOffsetX;
	if (oOffset.left < 260)
		iPopLeft = oOffset.left;
	
	iPopTop = oOffset.top+iOffsetY;	
	//if ($('#bodyWrap').height() - iPopTop + iVoorstellingPopHeight < 0)
	
	$('#voorstellingPop').css({ left: iPopLeft, top: iPopTop });
	
	// create some space for large popups
	if (iVoorstellingPopHeight + iPopTop > $('#bodyWrap').height()) {
		iPadding = (iVoorstellingPopHeight + iPopTop) - $('#bodyWrap').height();
		$('#bodyWrap').css({marginBottom: iPadding});
	}
},
function(){
	//$.log('hoverOut li');	
	iPopTimeoutId = window.setTimeout(function(){
		//$.log('remove');
		$('#voorstellingPop').remove();
		oHoveredItem = null;
	}, 50);
});

var SwitchPoP = function(oItem) {
	html = $(oItem).find('.voorstellingWrap').html();
	$('#voorstellingPop').html(html);
}

/**
 * x-fade
 */

$('li.voorstellingMultiple').each(function(){
	var iInterval = 2000;
	var oItems = $(this).children('ul').children('li.voorstelling');
	$(oItems).not(oItems[0]).addClass('hide');

	function xFade(){
		//$.log('xfade');		
		iCurrent = iNext;
		iNext = iCurrent + 1;		
		if (iNext > iNumItems - 1) 
			iNext = 0;		
		$(oItems[iCurrent]).animate({
			opacity: 'hide'
		}, iInterval / 2);
		$(oItems[iNext]).animate({
			opacity: 'show'
		}, iInterval / 2, '', function(){
			if ($(oHoveredItem).get(0) == $(oItems[iNext]).get(0)) {
				oHoveredItem = oItems[iCurrent];
				//SwitchPoP($(oItems[iNext]));
			}			
			setTimeout(xFade, iInterval);
		});
	}

	var iNumItems = $(oItems).length;
	var iCurrent = -1;
	var iNext = 0;
	var ixFadeTimer = setTimeout(xFade, iInterval);	
});


/**
 * eigenschappen IR
 */
$('#vEigenschappen .eigenschap-parel').addClass('IR IRActive eigenschap-parel-IR').append('<span class="IR-aid"></span>')
$('#vEigenschappen .eigenschap-kijk').addClass('IR IRActive eigenschap-kijk-IR').append('<span class="IR-aid"></span>')
 