$(document).ready(function(){
	$('.loginFormInput').bind('focus', function(){
		$(this).removeClass('inactive');
		$(this).val('');
		
		//remove text background from password field
		if($(this).attr('name') == 'password'){
			$(this).removeClass('passwordBackground');
		}
		
	});
	
	$('.loginFormInput').bind('blur', function(){
		var value = $(this).val();
		var name = $(this).attr('name');
		if (value == '') {
			$(this).addClass('inactive');
			switch (name) {
				case "username": $(this).val('E-mailadres'); break;
				case "password": $(this).val(''); $(this).addClass('passwordBackground'); break;
			}
		}
	});
	
	$('.createAccountLink, .popupContent').colorbox({
		transition: 'none',
		width: '518px', 
		height: '440px',
		iframe:true
	});
	
	$('.forgotPassLink').colorbox({
		transition: 'none',
		width: '518px', 
		height: '296px',
		iframe:true
	});	
	
	$('.saveBundle').colorbox({
		transition: 'none',
		width: '518px', 
		height: '288px',
		iframe:true
	});	
	
	$('.closeButton').bind('click',function(){
		parent.$.fn.colorbox.close();
	});
	$('.removeButton').bind('click', function(){
		removeItem(this);
	});
	
	function removeItem(item) {
		var keyNo = $(item).attr('id').substring(9);
		$.getJSON("/site_files/custom_modules/mijnToolkits/ajax/ajaxCalls.php", { action: 'remove', key: keyNo }, function(data){
			$('#totalDownloadbundelSize').html(data.newsize);
			$(item).parent().fadeOut('slow', function(){
				$(item).remove();
			});
			if(data.newsize=='0'){
				$('#downloadLinks').hide();
			}
		});
		
	}
	
	$('.addToDownloadBundelButton').bind('click', function(i, button){
		var button = this;
		var specs = $(button).attr('rel');
		specs = specs.split("|");
		var filename = specs[0];
		var title = specs[1];
		var toolkit = specs[2];

		$.getJSON("/site_files/custom_modules/mijnToolkits/ajax/ajaxCalls.php", { action: 'add', filename: filename, title: title, toolkit: toolkit }, function(data){
			if (data.error != true) {
				// past gewoon 
				$('#totalDownloadbundelSize').html(data.totalsize);
				var newCount = $('#downloadbundelContents li').length;
				if (data.filesizeKB < 1024) {
					var filesize = Math.ceil(data.filesizeKB)+' kb';
				} else {
					var filesize = Math.ceil(data.filesize)+' mb';
				}
				var newListItem = $('<li><div class="removeButton" id="download_'+newCount+'"></div><a href="#">'+ title +'</a> ['+ filesize +']</li>');
				$(newListItem).find('.removeButton').bind('click', function(){
					removeItem(this);
				});
				$('#downloadbundelContents').append(newListItem);
				var newLeft = $(newListItem).position().left;
				var newTop = $(newListItem).position().top;
				var newWidth = $(newListItem).width();
				var newHeight = $(newListItem).height();
				$(newListItem).css('opacity','0');
				var oldLeft = $(button).position().left;
				var oldTop = $(button).position().top;
				var animationDing = $('<div class="animationDing" style="position: absolute; height: '+$(button).height()+'px; width: '+$(button).width()+'px; left: '+oldLeft+'px; top: '+oldTop+'px"></div>');
				$(button).after(animationDing);
				$(animationDing).animate({ 
			        "left": newLeft+"px",
			        "top": newTop+"px",
			        "width": "155px",
			        "height": newHeight+"px"
			    }, 1500, function() {
			    	$(animationDing).fadeOut('slow', function(){
			    		$(animationDing).remove();
			    		
			    	});
			    	$(newListItem).animate({
			    		'opacity': '100'
			    	});
			    });
				$(button).remove();
			} else {
				// past niet meer in de 100mb!
				var melding = $('<div class="sizeMelding"><img src="/site_files/img/close_error.png" alt="" />De downloadbundel kan maximaal 100mb aan downloads bevatten en er is nog maar '+(100-data.currentsize)+'mb beschikbaar. Dit document is '+Math.ceil(data.filesize)+'mb.</div>');
				$(button).before(melding);
				$(melding).find('img').bind('click', function(){
					$(melding).dequeue().remove();
				});
				$(melding).fadeTo(3000,100).fadeOut(5000, function(){
					$(this).remove()
				});
			}
			$('#downloadLinks').show();
	    });
		return false;
	});
	
	function bundleSaved(){
		alert('a');
		$.fn.colorbox.close();
	}
	
});

