$(document).ready(function() {
	page.init();
	media.init();
	uploader.init();
	lightbox.init();
});

var page = {
	init : function() {
		$("img.homeimage").mouseover(function () 
		{
			$(this).attr('src','images/headers/header_01_home.gif');
		});
		$("img.homeimage").mouseout(function () 
		{
			$(this).attr('src','images/headers/header_01.gif');
		});
	}
}

var media = {
	init : function() {
		var cur_item = 0;
		$('span[rel|=galleryitem]').each(function()
		{
			$(this).bind('click', function()
			{
				$('object[id|=galleryitem]').each(function(){
					try{
						$(this).sendEvent('STOP',null);
					}catch(ex){
						
					}
				});
				
				$('div[rel|=galleryitem]').css('display', 'none');
				$('div[rel='+$(this).attr('rel')+']').css('display', 'block');
				
				//$('#gallery_display').html($('div[rel='+$(this).attr('rel')+']').html());
			});
			cur_item++;
		});
		$("div#medianavwrap").scrollable(
		{
			size:6,
			items:'#medianav',
			item:'span'
		}).mousewheel();
	}
}

var uploader = {
	init: function(){
		if($("#swfupload-control").length == 1)
		{
			$(function(){
				$('#swfupload-control').swfupload({
					upload_url: "processMultimediaUpload.php",
					file_post_name: 'uploadfile',
					post_params : { 'mediaAlbum_id' : mediaAlbum_id, 'PHPSESSID' : session_id, 'pagename' : pagename, 'subpagename' : subpagename},
					file_size_limit : "204800",
					file_types : "*.jpg;*.jpeg;*.png;*.gif",
					file_types_description : "Media bestanden",
					file_upload_limit : 500,
					flash_url : "include/plugins/swfupload/swfupload.swf",
					button_image_url : 'include/plugins/swfupload/images/wdp_buttons_upload_114x29.png',
					button_width : 114,
					button_height : 29,
					button_placeholder : $('#multimediauploadbutton')[0],
					debug: false,
					assume_success_timeout : 3
				})
				.bind('fileQueued', function(event, file){
					var listitem='<li id="'+file.id+'" >'+
						'File: <em>'+file.name+'</em> ('+Math.round(file.size/1024)+' KB) <span class="progressvalue" ></span>'+
						'<div class="progressbar" ><div class="progress" ></div></div>'+
						'<p class="status" >Bezig</p>'+
						'<span class="cancel" >&nbsp;</span>'+
						'</li>';
					$('#multimediauploadlog').append(listitem);
					$('li#'+file.id+' .cancel').bind('click', function(){
						var swfu = $.swfupload.getInstance('#swfupload-control');
						swfu.cancelUpload(file.id);
						$('li#'+file.id).slideUp('fast');
					});
					// start the upload since it's queued
					$(this).swfupload('startUpload');
				})
				.bind('fileQueueError', function(event, file, errorCode, message){
					//This throws an error.
	                switch (errorCode) {
	                case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
	                	alert('Het bestand: '+file.name+' is te groot');
	                	break;
	                case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
	                	alert('Bestanden van 0 bytes kunnen niet geupload worden.');
	                	break;
	                case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
	                    alert('Het bestandsformaat van '+file.name+' is niet toegestaan.');
	                    break;
	                case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
	                	alert('Je probeert te veel bestanden toe te voegen.\n' + (message === 0 ? 'Je hebt het maximaal aantal bestanden bereikt.' : 'Je kunt er nog ' + message + ' selecteren'));
	                    break;
	                default:
	                	if (file !== null) {
	                		alert('Onbekende fout');
	                	}
	                    break;
	                }
				})
				.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
					$('#queuestatus').text('Geselecteerde bestanden: '+numFilesSelected+' / Te verwerken bestanden: '+numFilesQueued);
				})
				.bind('uploadStart', function(event, file){
					$('#multimediauploadlog li#'+file.id).find('p.status').text('Bezig met uploaden...');
					$('#multimediauploadlog li#'+file.id).find('span.progressvalue').text('0%');
					$('#multimediauploadlog li#'+file.id).find('span.cancel').hide();
				})
				.bind('uploadProgress', function(event, file, bytesLoaded){
					//Show Progress
					var percentage=Math.round((bytesLoaded/file.size)*100);
					$('#multimediauploadlog li#'+file.id).find('div.progress').css('width', percentage+'%');
					$('#multimediauploadlog li#'+file.id).find('span.progressvalue').text(percentage+'%');
				})
				.bind('uploadSuccess', function(event, file, serverData){
					var item=$('#multimediauploadlog li#'+file.id);
					item.find('div.progress').css('width', '100%');
					item.find('span.progressvalue').text('100%');
					item.addClass('success').find('p.status').html('Klaar!!!');
				})
				.bind('uploadComplete', function(event, file){
					// upload has completed, try the next one in the queue
					$(this).swfupload('startUpload');
				})
				.bind('uploadError', function(event, file, errorCode, message){
					if(errorCode != -280)
					{
						//var item=$('#multimediauploadlog li#'+file.id);
						alert('Probleem met het uploaden van: '+file.name);
					}
				})
			})
		}
	}
}

var lightbox = {
	init : function() {
		$('a[rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
	}
}
