window.addEvent('domready', function()
{	
	if($('categorydatelist'))
	{
		objCategorylist = new Categorylist();
	}
	
	if($('filterdropdown'))
	{
		objFiltertool = new Filtertool();
	}
});

var Filtertool = new Class(
{
	elFiltertoolDropdown:null,
	elFiltertoolButton:null,
	elFiltertoolInput:null,
	elFiltertoolSubmit:null,
	elFiltertoolResults:null,
	strSearchString:null,
	strSearchType:'all',
	initialize: function() 
	{
		this.elFiltertoolDropdown = $('filterdropdown');
		this.elFiltertoolButton = $('filtertoolbutton');
		this.elFiltertoolInput = $('filtertoolinput');
		this.elFiltertoolSubmit = $('filtertoolsubmit');
		this.elFiltertoolResults = $('filtertoolresults');
		
		var self = this;
		
		this.elFiltertoolButton.addEvent('click', function(event)
		{
			// cancel normal event
			event.stop();
			
			// open dropdown
			self.toggleDropdown();
			self.closeResults();
		});
		
		this.elFiltertoolSubmit.addEvent('click', function(event)
		{
			// cancel normal event
			event.stop();
			
			// open dropdown
			self.xhrSearch();
		});
	},
	urlencode: function(argStrData)
	{
		var strData = encodeURIComponent(argStrData);
		return strData.replace(/~/g,'%7E').replace(/%20/g,'+');	
	},
	getSearchType: function()
	{
		var self = this;
		
		$each($$('#filterdropdown input[type=radio]'), function(elRadio)
		{
			if(true == elRadio.checked)
			{
				self.strSearchType = elRadio.value;
			}
		});
		
		return this.strSearchType;
	},
	toggleDropdown: function()
	{
		if('block' == this.elFiltertoolDropdown.getStyle('display'))
		{
			this.closeDropdown();
		}
		else
		{
			this.openDropdown();
		}
	},
	openDropdown: function()
	{
		this.elFiltertoolDropdown.setStyle('display', 'block');
		this.elFiltertoolButton.src = 'inc/image/btn-filter-close.gif';
	},
	closeDropdown: function()
	{
		this.elFiltertoolDropdown.setStyle('display', 'none');
		this.elFiltertoolButton.src = 'inc/image/btn-filter-submit.gif';
	},
	openResults: function()
	{
		this.elFiltertoolResults.setStyle('display', 'block');
	},
	closeResults: function()
	{
		this.elFiltertoolResults.setStyle('display', 'none');
	},
	xhrSearch: function()
	{
		this.closeDropdown();
		
		this.strSearchString = this.elFiltertoolInput.value;
		
		var self = this;
		
		var objSearchRequest = new Request({
		method: 'post',
		url: 'xhr/filter/'}).send(
			'type=' + escape(this.getSearchType()) + 
			'&query=' + escape(this.strSearchString)
		);
		
		objSearchRequest.onSuccess = function(reponseText, responseXML)
		{
			var objResults = JSON.decode(reponseText);
			self.showResults(objResults);
		};
	},
	showResults: function(argObjResults)
	{
		this.elFiltertoolResults.empty();
		
		var self = this;
		
		if(0 < argObjResults.length)
		{
			$each(argObjResults, function(elResult, i)
			{
				var elTitle = new Element('a', {
				    'href': 'bekijk-documentaire/' + elResult.id,
					'html': '<h2>' + elResult.episodeTitle + '</h2>'
				});
				
				var elInfo = new Element('p', {
				    'html': elResult.series
				});
				
				var elLink = new Element('a', {
				    'href': 'bekijk-documentaire/' + elResult.id,
					'class': 'play'
				});
				
				var strClass = '';
				if(0 == i % 2)
				{
					var strClass = 'light ';
				}
				
				var elItem = new Element('li', {
						'class': strClass + 'mover',
						'events':
						{
					        'mouseover': function()
					        {
								this.addClass('current');
					        },
					        'mouseout': function()
					        {
					            this.removeClass('current');
					        }
					    }
					});
	
				elTitle.inject(elItem);
				elInfo.inject(elItem);
				elLink.inject(elItem);
				elItem.inject(self.elFiltertoolResults);
			});
			
			// show extra block if there are 10 results
			if(10 == argObjResults.length)
			{
				// show all results block
				var elTitle = new Element('a', {
					   'href': 'zoeken/' + this.urlencode(this.getSearchType()) + '/' + this.urlencode(this.strSearchString),										  
					   'html': '<h2>Bekijk alle resultaten</h2>'
				});
				
				var elInfo = new Element('p', {
				    'html': 'voor uw zoekopdracht naar "' + this.strSearchString + '"'
				});
				
				var elLink = new Element('a', {
					    'href': 'zoeken/' + this.urlencode(this.getSearchType()) + '/' + this.urlencode(this.strSearchString),
						'class': 'play'
					});
				
				var elItem = new Element('li', {
							'class': 'mover', 
							'class': 'all-results',							
							'events':
							{
						        'mouseover': function()
						        {
									this.addClass('current');
						        },
						        'mouseout': function()
						        {
						            this.removeClass('current');
						        }
						    }
						});
				
				elTitle.inject(elItem);
				elInfo.inject(elItem);
				elLink.inject(elItem);
				elItem.inject(this.elFiltertoolResults);
			}
		}
		else
		{
			var elTitle = new Element('h2', {
			    'html': 'Geen resultaat'
			});
			
			var elInfo = new Element('p', {
			    'html': 'Er zijn helaas geen documentaires gevonden.'
			});
			
			var elItem = new Element('li', {
				'class': 'light',
				'events':
				{
			        'mouseover': function()
			        {
						this.addClass('current');
			        },
			        'mouseout': function()
			        {
			            this.removeClass('current');
			        }
			    }
			});
			
			elTitle.inject(elItem);
			elInfo.inject(elItem);
			elItem.inject(self.elFiltertoolResults);
		}
		
		this.openResults();
	}
});

var Categorylist = new Class(
{
	intCurrentBlock:0,
	intBlockHeight:36,
	intBlockCount:0,
	intBlockDifference:2,
	objFx:null,
	initialize: function() 
	{
		var elCategoryList = $('categorydatelist');
		var arrCategories = elCategoryList.getElements('li');
		this.intBlockCount = arrCategories.length;

		var self = this;
		arrCategories.each(function(elItem, i)
		{
			if(elItem.hasClass('today'))
			{
				self.intCurrentBlock = i;
			}
		});
		
		if(0 != this.intCurrentBlock)
		{
			var intNewTop = (this.intBlockHeight * (this.intCurrentBlock - this.intBlockDifference)) * -1;
		}
		else
		{
			this.intCurrentBlock = 2;
		}
		
		this.setTop(intNewTop);
	},
	showPrevious:function()
	{
		if((this.intCurrentBlock - 2) > 0)
		{
			this.intCurrentBlock -= 1;
			var intNewTop = (this.intBlockHeight * (this.intCurrentBlock - this.intBlockDifference)) * -1;
			this.tweenTop(intNewTop);	
		}
	},
	showNext:function()
	{	
		if((this.intCurrentBlock + 3) < this.intBlockCount)
		{
			this.intCurrentBlock += 1;
			var intNewTop = (this.intBlockHeight * (this.intCurrentBlock - this.intBlockDifference)) * -1;
			this.tweenTop(intNewTop);	
		}
	},
	setTop:function(argIntNewTop)
	{
		 $('categorydatelist').setStyle('top', argIntNewTop);
	},
	tweenTop:function(argIntNewTop)
	{
		$('categorydatelist').tween('top', argIntNewTop);
	}
});

window.addEvent('domready',function(){
 
	$$('li.mover').each(function(li){
		li.addEvents({
			mouseover : function(){ this.addClass('current'); },
			mouseout : function(){ this.removeClass('current'); }
		});
	});
 
});

function loadTinyMCE(argStrBaseUrl)
{
	tinyMCE.init({
		theme 							: 'advanced',
		mode 							: 'textareas',
		plugins							: 'imagemanager',
		theme_advanced_toolbar_location : 'top',
		theme_advanced_buttons1 		: 'formatselect,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,link,unlink,|,image,code',
		theme_advanced_buttons2 		: '',
		theme_advanced_buttons3 		: '',
		content_css 					: 'inc/css/screen.css',
		cleanup_on_startup 				: true,
		cleanup							: true,
		relative_urls 					: true,
		apply_source_formatting			: true,
		force_br_newlines				: true,
		forced_root_block				: '',
		theme_advanced_blockformats		: 'h2, p',
		document_base_url 				: argStrBaseUrl
	});
}

function openPopup(argStrFile, argStrFrameName, argIntWidth, argIntHeight, argIntLeftStart, argIntTopStart, argIntScrollbars, argIntResizeable)
{
	if(argStrFrameName == '' || argStrFrameName == undefined)
	{
		argStrFrameName = 'popup';
	}
	if(argIntWidth == '' || argIntWidth == undefined)
	{
		argIntWidth = 530;
	}
	if(argIntHeight == '' || argIntHeight == undefined)
	{
		argIntHeight = 380;
	}
	if(argIntLeftStart == '' || argIntLeftStart == undefined)
	{
		argIntLeftStart = (screen.width/2) - (argIntWidth/2);
	}
	if(argIntTopStart == '' || argIntTopStart == undefined)
	{
		argIntTopStart = (screen.height/2) - (argIntHeight/2);
	}	
	if(argIntScrollbars == '' || argIntScrollbars == undefined)
	{
		argIntScrollbars = 'no';
	}
	if(argIntResizeable == '' || argIntResizeable == undefined)
	{
		argIntResizeable = 'no';
	}

	var strProperties = "width=" + argIntWidth + ", height=" + argIntHeight + ", left=" + argIntLeftStart + ", top=" + argIntTopStart + ", toolbar=no, titlebar=no, title=0, location=no, scrollbars=" + argIntScrollbars + ", status=no, resizable=" + argIntResizeable;
	
	window.open(argStrFile, argStrFrameName, strProperties);
}