$(document).ready(function() {
	$("a.ui-icon-zoomin").fancybox({
		'titleShow'     : false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	$(".jaarverslagContent").parent("#main-content").addClass("mijnjaarverslag");
});
function buttonEmptyHide (){
					if($('ul#trashed li').length == 0) {
							$('#trash').addClass('empty');
							$('a.leegmaken').hide();
						}
					
					if ($('ul#trashed li').length != 0) {
							$('#trash').removeClass('empty');
							$('a.leegmaken').show();
						}
					};
$(function() {
			 //var hoofdstuk1 = $.cookie('cookiehoofdstuk1');
			 //var hoofdstuk2 = $.cookie('cookiehoofdstuk2');
			 //var hoofdstuk3 = $.cookie('cookiehoofdstuk3');
			 //var hoofdstuk4 = $.cookie('cookiehoofdstuk4');
			 
			 //var hallo = 'ul#trashed li#';
			 var recycle_icon = '<a href="link/to/recycle/script/when/we/have/js/off" class="ui-icon ui-icon-refresh">Recycle image</a>';
			 
			/* if (hoofdstuk1 == 'true') {
  			 var html1 =  $('ul#gallery li#hoofdstuk1');
	  			$(html1).appendTo('ul#trashed');
				$('ul#gallery li#hoofdstuk1').remove();
				$(hallo+'hoofdstuk1').find('a.ui-icon-trash').remove();
				$(hallo+'hoofdstuk1').append(recycle_icon);
				$(hallo+'hoofdstuk1').animate({ width: '60px' }).find('img').animate({ height: '36px' });
				$('form input.hoofdstuk1').attr('value', 'true');
			 };*/

			$.fn.appendChapterTo = function(list){
				var chapterToAppend = this;
				var currentListItem = list.find('> li:first');
				
				while(chapterToAppend.attr('id') > currentListItem.attr('id')){
					currentListItem = currentListItem.next();
				};

				if (!(currentListItem.get(0))){
					chapterToAppend.appendTo(list);
				}else{
					chapterToAppend.insertBefore(currentListItem);
				}
				return this;
//				if(currentListItem){
//					chapter.insertBefore(currentListItem);
//				}else{
//					chapter.appendTo(list);
//				}
				
			};
			
			$('ul#gallery li.firstSection li').each(function(){
				var thisid	= $(this).attr('id');
				var selected = $.cookie('cookie' + thisid);
				if (selected == 'true'){
					//$(this).appendTo('ul#trashed');
					
					$(this).appendChapterTo($('ul#trashed'));
					
					$('ul#trashed li#' + thisid).find('a.ui-icon-trash').remove();
					$('ul#trashed li#' + thisid).append(recycle_icon);
					$('ul#trashed li#' + thisid).animate({ width: '210px' }).find('img').animate({ height: '35px', width: '32px' });
					$('form input.' + thisid).attr('value', 'true');
					$('ul#gallery li.firstSection li#' + thisid).remove();
				};
			});
			
			$('ul#gallery li.secondSection li').each(function(){
				var thisid	= $(this).attr('id');
				var selected = $.cookie('cookie' + thisid);
				if (selected == 'true'){
					//$(this).appendTo('ul#trashed');
					
					$(this).appendChapterTo($('ul#trashed'));
					$('ul#trashed li#' + thisid).find('a.ui-icon-trash').remove();
					$('ul#trashed li#' + thisid).append(recycle_icon);
					$('ul#trashed li#' + thisid).animate({ width: '210px' }).find('img').animate({ height: '35px', width: '32px' });
					$('form input.' + thisid).attr('value', 'true');
					$('ul#gallery li.secondSection li#' + thisid).remove();
				};
			});
			$('ul#gallery li.thirdSection li').each(function(){
				var thisid	= $(this).attr('id');
				var selected = $.cookie('cookie' + thisid);
				if (selected == 'true'){
					//$(this).appendTo('ul#trashed');
					
					$(this).appendChapterTo($('ul#trashed'));
					$('ul#trashed li#' + thisid).find('a.ui-icon-trash').remove();
					$('ul#trashed li#' + thisid).append(recycle_icon);
					$('ul#trashed li#' + thisid).animate({ width: '210px' }).find('img').animate({ height: '35px', width: '32px' });
					$('form input.' + thisid).attr('value', 'true');
					$('ul#gallery li.thirdSection li#' + thisid).remove();
				};
			});
				
				var $gallery = $('#gallery li.firstSection'), $trash = $('#trash'), $trashed = $('#trashed');
				
				$('#trashed li').draggable({
					cancel: 'a.ui-icon',
					revert: 'invalid', 
					helper: 'clone',
					cursor: 'move'
				});
				
				$trash.droppable({
					accept: '#gallery li.firstSection li, #gallery li.secondSection li, #gallery li.thirdSection li',
					activeClass: 'ui-state-highlight',
					drop: function(ev, ui) {
						deleteImage(ui.draggable);
					}
				});
				
				// VOOR SECTIE 1
				$('#gallery li.firstSection ul li').draggable({
					cancel: 'a.ui-icon',
					revert: 'invalid', 
					containment: $('#demo-frame').length ? '#demo-frame' : 'document',
					helper: 'clone',
					cursor: 'move',
					zIndex: '2002'
				});

				$gallery.droppable({
					accept: '#trash li',
					activeClass: 'dropIt',
					drop: function(ev, ui) {
						recycleImage(ui.draggable);
					}
				});
				
				
				//VOOR SECTIE 2
				$('#gallery li.secondSection ul li').draggable({
					cancel: 'a.ui-icon',
					revert: 'invalid', 
					containment: $('#demo-frame').length ? '#demo-frame' : 'document',
					helper: 'clone',
					cursor: 'move',
					zIndex: '2002'
				});

				$('#gallery li.secondSection').droppable({
					accept: '#trash li',
					activeClass: 'dropIt',
					drop: function(ev, ui) {
						recycleImage(ui.draggable);
					}
				});
				////
				//VOOR SECTIE 3
				$('#gallery li.thirdSection ul li').draggable({
					cancel: 'a.ui-icon',
					revert: 'invalid', 
					containment: $('#demo-frame').length ? '#demo-frame' : 'document',
					helper: 'clone',
					cursor: 'move',
					zIndex: '2002'
				});

				$('#gallery li.thirdSection').droppable({
					accept: '#trash li',
					activeClass: 'dropIt',
					drop: function(ev, ui) {
						recycleImage(ui.draggable);
					}
				});
				////
				var recycle_icon = '<a href="/OranjeFonds/lib/jaarverslag2009/pdf/Oranje_Fonds_Jaarverslag2009.pdf" class="ui-icon ui-icon-refresh">Recycle image</a>';
				
				function deleteImage($item) {
					$item.fadeOut(function() {
						var trashed 	= $('ul#trashed');				   
						var $list 		= $('ul',$trash).length ? $('ul',$trash) : $($trashed).appendTo($trash);
						var chapterid 	= this.getAttribute('id');
						
//						$item.appendChapterTo($list);
						//$item.find('a.ui-icon-trash').remove();
						$item.appendChapterTo($list).fadeIn(function() {
						$item.animate({ width: '210px' }).find('img').animate({ height: '35px', width: '32px' });
							$('form input.' + chapterid).attr('value', 'true');
							var test = $('form input.' + chapterid)
							$.cookie('cookie'+ chapterid,'true');					
						});
						$item.find('a.ui-icon-trash').css({'backgroundImage':'url(/oranjefonds/images/jaarverslag/delete.jpg)', 'backgroundRepeat':'no-repeat', 'textIndent':'-9999px', 'width':'27px', 'height':'28px', 'marginTop':'1px'}).attr("title","verwijder hoofdstuk");
						$item.find('a.ui-icon-refresh').css({'backgroundImage':'url(/oranjefonds/images/jaarverslag/delete.jpg)', 'backgroundRepeat':'no-repeat', 'textIndent':'-9999px', 'width':'27px', 'height':'28px', 'marginTop':'1px'});
						$('a.ui-icon-refresh').attr("title","Verwijder hoofdstuk"); buttonEmptyHide();
					});
					
				}
				
				// image recycle function
				var trash_icon = '<a href="/OranjeFonds/lib/jaarverslag2009/pdf/Oranje_Fonds_Jaarverslag2009.pdf" title="Voeg hoofdstuk toe" class="ui-icon ui-icon-trash">Delete image</a>';
				
				function recycleImage($item) {					
					$item.fadeOut(function() {
										   
					   	var chapterid 	= this.getAttribute('id');
					   	//alert (chapterid);
						//$item.find('a.ui-icon-refresh').remove();
						//alert ($(this).html());
						//var sectie1 = 
						var section1 = 'sectie1';
						var section2 = 'sectie2';
						var section3 = 'sectie3';
						
						var ul1 = $('#gallery li.firstSection ul');
						var ul2 = $('#gallery li.secondSection ul');
						var ul3 = $('#gallery li.thirdSection ul');
						
						if ($(this).hasClass(section1)) { 
						$item.css('width','84px').find('img').css({height: '80px', width: '74px', display: 'block'}).end().appendChapterTo(ul1).fadeIn();
						}
						
						if ($(this).hasClass(section2)) {
						$item.css('width','84px').find('img').css({height: '80px', width: '74px', display: 'block'}).end().appendChapterTo(ul2).fadeIn();
						}
						if ($(this).hasClass(section3)) {
						$item.css('width','84px').find('img').css({height: '80px', width: '74px', display: 'block'}).end().appendChapterTo(ul3).fadeIn();
						}
						
						$('form input.' + chapterid).attr('value', 'false');
						$.cookie('cookie'+ chapterid,'false');
						
						$item.find('a.ui-icon-trash').css({'backgroundImage':'url(/oranjefonds/images/jaarverslag/add.jpg)', 'backgroundRepeat':'no-repeat', 'textIndent':'-9999px', 'width':'27px', 'height':'28px', 'marginTop':'8px'}).attr("title","voeg hoodstuk toe");
						$item.find('a.ui-icon-refresh').css({'backgroundImage':'url(/oranjefonds/images/jaarverslag/add.jpg)', 'backgroundRepeat':'no-repeat', 'textIndent':'-9999px', 'width':'27px', 'height':'28px', 'marginTop':'8px'}).attr("title","voeg hoodstuk toe"); buttonEmptyHide();
					});
					
				}
					$('a.leegmaken').click(function(ev) {
						$("ul#trashed li").each(function(){
							recycleImage($(this));
						});
						return false;
   					});
				// image preview function, demonstrating the ui.dialog used as a modal window
				/*function viewLargerImage($link) {
					var src = $link.attr('href');
					var title = $link.siblings('img').attr('alt');
					var $modal = $('img[src$="'+src+'"]');

					if ($modal.length) {
						$modal.dialog('open')
					} else {
						var img = $('<img alt="'+title+'" width="384" height="288" style="display:none;padding: 8px;" />')
							.attr('src',src).appendTo('body');
						setTimeout(function() {
							img.dialog({
									title: title,
									width: 400,
									modal: true
								});
						}, 1);
					}
				}*/
				
				// resolve the icons behavior with event delegation
				$('ul.gallery li').click(function(ev) {
					var $item = $(this);
					var $target = $(ev.target);
					
					if ($target.is('ul#gallery li a.ui-icon')) {
						deleteImage($item);
					} else if ($target.is('a.ui-icon-zoomin')) {
						viewLargerImage($target);
					} else if ($target.is('ul#trashed li a.ui-icon')) {
						recycleImage($item);
					}
					return false;
				});
				
			});
			$(document).ready(function() {
			   	// set tooltip text == title attr
				buttonEmptyHide();
				$('ul.gallery h5').attr('title', function(){
					return $(this).text();	
				});
				
				$('ul.gallery h5').tooltip({cssClass:"tooltip-h5"});

				// make the text short
				$('ul.gallery li h5').each(function(){
				var body =	$(this).text();
				var str1 = body.substring(0, 11); 
					if( body.length > 11){
						$(this).text(str1+'..');
						}
				});
				
			$('form.jaarverslag button').hover(function() {
					$(this).addClass('hover');
				},
				function(){
					$(this).removeClass('hover');
				});
			});
			// SCROLLING SIDEBAR //
			$(document).ready(function() {			   
				var $scrollingDiv = $(".floating");
 				
				$(window).scroll(function(){	
					$scrollingDiv.addClass('now');					  
					
					var topOffset 	= $(window).scrollTop();
					var trashHeight = $('#trash').height(); 

						if (topOffset <= 350) {
							$(".floating").removeClass('now');
							}
						if (topOffset <= 580 && trashHeight <= 198  ) {	
						$scrollingDiv
							.stop()
							.animate({"marginTop": ($(window).scrollTop() + 0) + "px"}, "slow" );							
						}
				});
			});
			//END OF SCROLLING SIDEBAR

			
			/*$('ul.hoofdmenu li').mouseover(function() {
				var windowh = $(window).height();
				
				if (typeof openmenu.get(0) != 'undefined') {
					var menuh = openmenu.height();
					var openmenut = openmenu.offset().top;
					var totalm = menuh + openmenut;
					var total = windowh - totalm;
		
					if (menuh + openmenut > windowh) {
						openmenu.animate({ top: total + "px" }, 500);
					}
				}
    		});*/