jQuery.noConflict();

jQuery(document).ready(function($) {

    /**
     * === Document archive and Quicksand plugin code ===
     * 
     * NOT: This could be written as a JQuery class, but since prototype also is included, 
     * there are confilcts.
     * 
     */   
    var IvarArchive = {
    	/**
    	 * Adds all necessary listeners for IvarArchive to run.
    	 */
    	init: function(){
    		IvarArchive.addClickEventsOnTopFolderLinks();
    	    IvarArchive.addAjaxClickEventHandlers();
    	    IvarArchive.addItemClickListeners($$('.documentarchive-item'));
    	},
    	
    	/**
	     * Add click event for top folder links
	     */
	    addClickEventsOnTopFolderLinks: function(){
	    	$('.archive-filter li.top-folder-link a').click(function(event) {
	    		var link = $(this);
	    		
	    		IvarArchive.toggleSubFoldersSelectedClass(link.attr('data-folderid'));
	    		IvarArchive.clearSelectedClass($$('.archive-filter li.sub-folder-link'));
	    		
	    		var parent = link.parent();
	    		if(parent.hasClass('alle-link') && !parent.hasClass('search-enabled')){
	    			IvarArchive.addSelectedClass($$('.archive-filter ul.sub li')[0]);
	    		}
	    	});
	    },
	    
	    /**
	     * Add click event for quicksand
	     */
	    addAjaxClickEventHandlers: function(){
	    	$('.archive-filter li.folder-link a').click(function(event) {
	    		var link = $(this);
	    		
	    		/**
	    		 * Load with ajax
	    		 */
	    		$.get(link.attr('data-href'), function(data) {
	    			IvarArchive.runQuicksandPlugin(data);
	    		}); 
	    		
	    		IvarArchive.removeInfoContent();
	    		IvarArchive.toggleFilterButtons(link.parent());
	    		event.preventDefault();  
	    	});
	    },
	    
	    /**
	     * Updates the newly loaded data into the quicksand plugin. 
	     */
	    runQuicksandPlugin: function(data){
	    	var container = $('.documentarchive-overview').quicksand(
				$(data).children(),
				{
					duration: 300,
					useScaling: false
				},			
				function(){ // callback after render
					IvarArchive.addItemClickListeners(container.children());    			
				}
			);
	    	
	    	return container;
	    },
    		
		/**
		 * Replaces the archive info with the new html
		 */
		loadFileInfo: function(url){
			$.get(url, function(data) {
				$('.archive-info').replaceWith(data);
			});    	
		},
    
	    /**
	     * Removes all content from the info container.
	     */
	    removeInfoContent: function(){
	    	$('.archive-info').empty();    	
	    },
	    
	    /**
	     * Adds click listeners to the newly loaded files.
	     */
	    addItemClickListeners: function(items){
	    	$(items).each(function(index, item){
	    		$(item).bind("click", function(event){
	    			IvarArchive.loadFileInfo($(this).attr('data-href'));
	    	        event.preventDefault();
				});
	    	});
	    },
	    
	    /**
	     * Removed selected class on all buttons but adds on the clicked.
	     */
	    toggleFilterButtons: function(selectedButton){
	    	var clickedTop = $(selectedButton).hasClass('top-folder-link');
	    	var links = null;
	    	if(clickedTop){
	    		links = $$('.archive-filter li.top-folder-link');
	    	}
	    	else {
	    		links = $$('.archive-filter li.sub-folder-link');
	    	}
	    	
	    	IvarArchive.clearSelectedClass(links);
	    	IvarArchive.addSelectedClass(selectedButton);
	    },
	
	    toggleSubFoldersSelectedClass: function(topFolderId){
	    	$$('.archive-filter ul.sub').each(function(ul){
	    		var listElement = $(ul); 
	    		
	    		if(listElement.attr('data-topfolderid') == topFolderId){
	    			IvarArchive.addSelectedClass(listElement);    			
	    		}
	    		else {
	    			IvarArchive.removeSelectedClass(listElement);    			
	    		}
	    	});    	
	    },
	    
	    addSelectedClass: function(element){
	    	$(element).addClass('selected');    	
	    },
	    
	    removeSelectedClass: function(element){
			$(element).removeClass('selected');
	    },
	    
	    clearSelectedClass: function(elements){
	    	elements.each(function(element){
	    		IvarArchive.removeSelectedClass(element);
    		});    	
	    }	    
    };
    IvarArchive.init();
    
});// end dom ready
