function collectionRegister(collectionItemName)
{
	var collectionItemObject = document.getElementById(collectionItemName);

	if (typeof (collectionItemObject) == 'object')
	{
		counter = collectionObject.length;

		collectionObject[counter] = new Object();
		collectionObject[counter].name = collectionItemName;
		collectionObject[counter].height = 0;
		collectionObject[counter].offset = 0;

	}
}

function collectionCheck()
{
	WindowComplete ? collectionInit() : setTimeout("collectionCheck()",collectionCheckDelay);
}

function collectionInit()
{
	switch (collectionType)
	{
		case "ticker":
			for (i=0;i<collectionObject.length;i++)
			{
				var collectionItemObject = document.getElementById(collectionObject[i].name);

				if (typeof (collectionItemObject) == 'object')
				{
					height = collectionItemObject.offsetHeight;

					collectionObject[i].height = height;
					collectionObject[i].offset = collectionOffset;

					collectionItemObject.style.position = 'absolute';
					collectionItemObject.style.left = '0px';
					collectionItemObject.style.top = collectionOffset + 'px';

					collectionOffset += collectionDirection * (height + (collectionOffset == 0 ? 20 : 0));
				}
			}

			break;
	}

	setInterval("collectionRun()",collectionDelay)
}

function collectionRun()
{
	var i, j;

	switch (collectionType)
	{
		case "ticker":
			for (i=0;i<collectionObject.length;i++)
			{
				j = (collectionObject.length + i - 1) % collectionObject.length;

				collectionObject[i].offset -= collectionStep; Math.abs(collectionObject[i].offset) == collectionObject[i].height && (collectionObject[i].offset = collectionObject[j].offset + collectionDirection * collectionObject[j].height)

				eval('document.getElementById("collection" + i).style.' + collectionOrientation + ' = collectionObject[i].offset + "px";');
			}

			break;
		case "rotator":
			for (i=0;i<collectionObject.length;i++)
			{
				document.getElementById("collection" + i).style.display == "block" && (j = (collectionObject.length + i + 1) % collectionObject.length);
				document.getElementById("collection" + i).style.display = "none";
			}

			document.getElementById("collection" + j).style.display = "block";

			break;
	}
}


$(document).ready(function()
{
	if ($('.show').text() != '')
	{
		var showID = $('.show').text();
		
		openVideo($('#link_' + showID));
	}
	
	$('.videoLink').click(function()
	{
		if ($(this).hasClass('videoLink'))
		{
			$('.videoExitLink').html('Look!');
			$('.videoExitLink').addClass('videoLink');
			$('.activeVideo').removeClass('activeVideo');
			$('.videoExitLink').removeClass('videoExitLink'); //reset all exit links

			$('meta[property="og:title"]').attr('content', 'MusicDIRECTOR.nl: ' + jQuery(this).siblings('h2').text());
			$('meta[property="og:image"]').attr('content', 'http://' + document.domain + jQuery(this).parents('.label:first').find('img').attr('src'));
						
			openVideo(this);
		
			$(this).html('Close');
			$(this).removeClass('videoLink');
			$(this).addClass('videoExitLink');
		}
		else
		{
			closeVideo(this);
			
			$(this).html('Look!');
			$(this).removeClass('videoExitLink');
			$('.activeVideo').removeClass('activeVideo');
			$(this).addClass('videoLink');
		}

		return false;
	});
	
	$('.videoCloseLink').click(function()
	{
		closeVideo(this);
		
		$('.videoExitLink').html('Look!');
		$('.videoExitLink').addClass('videoLink');
		$('.videoExitLink').removeClass('videoExitLink');
		$('.activeVideo').removeClass('activeVideo');
		
		return false;
	});
});

function openVideo(videoLink)
{
	$('.videoHolder').attr('style', 'display:none;');

	var videoHolderID = $(videoLink).attr('rel'); //get rel from link	
	var videoHolder = $('#' + videoHolderID); //get video holder

	var splitLinkIds = $(videoLink).attr('id').split('_');
	var videoID = splitLinkIds[1];
	
	var videoText = $('#text_' + videoID);
	var videoTitle = $('#title_' + videoID);
	var videoSrc = $('#src_' + videoID);
	var videoImg = $('#img_' + videoID);
	var videoCopyLink = $('#copylink_' + videoID);
	
	$(videoTitle).parent().parent().addClass('activeVideo');
	
	//Set embed video in holder
	updateObject(videoHolderID, videoSrc, videoImg);
	
	//Set text to videoholder
	$('#' + videoHolderID + ' > .middle > div.text > p').html(videoText.html());
	//set title
	$('#' + videoHolderID + ' > .middle > div.text > h3').html(videoTitle.html());
	//set link
	$('#' + videoHolderID + ' > .middle > div.text > input').attr('value', videoCopyLink.html());
	
	//bind focus
	$('#' + videoHolderID + ' > .middle > div.text > input').focus(function()
	{
		this.select();
	});
	
	videoHolder.attr('style', 'display:block');
}

function closeVideo(videoLink)
{
	var videoHolderID = $(videoLink).attr('rel'); //get rel from link
	var videoHolder = $('#' + videoHolderID); //get video holder
	
	var object = $('#' + videoHolderID + '>.middle>.video>object');
	var objectID = $(object).attr('id');
	
	$(object).remove();
	$('#' + videoHolderID + '>.middle>.video').html(object);	
	
	videoHolder.attr('style', 'display:none;');
}

function updateObject(videoElID, videoSrc, videoImg)
{
	var object = $('#' + videoElID + '>.middle>.video>object');
	var objectID = $(object).attr('id');
	
	var flashvars = {};
	flashvars.file = videoSrc.html();
	flashvars.image=videoImg.html();
	flashvars.autostart=true;

	var params = {};
	params.wmode="opaque";
	params.allowscriptaccess = "always";
	params.allowfullscreen = "true";

	var attributes = {};

	$('.video').each(function()
	{
		var vidObj = $(this).html();
		
		$(this).html(vidObj); //reset video's
	})
	
	swfobject.embedSWF("/client/musicdirector/flash/mediaplayer.swf", objectID, "319", "240", "9.0.0","", flashvars, params, attributes);
}
