/**
 * @author at
 */
$(document).ready(function(){
	PageData.init();
	Selection.init();
	FileListTable.init();
	Merkliste.init();
	TextSearch.init();
	
	if( 2 == PageData.get( "mode" ) && 0 == Merkliste.size() ) {
		PageData.set( "mode" , 1 );
		PageData.submit();
	}
	
	ScrollPosition.restore();
});


var ScrollPosition = {
	save: function() {
		if( $.browser.msie ) {
			var ie_version_regex = /MSIE 7\.0/i;
			var tmp = navigator.appVersion.match(ie_version_regex);
			if(tmp && tmp.length > 0)
				PageData.set( "scroll_position", document.documentElement.scrollTop );
			else
				PageData.set( "scroll_position", document.body.scrollTop );
		}
		else {
			PageData.set( "scroll_position", window.pageYOffset );
		}
	},
	
	restore: function() {
		var scroll_position = PageData.get("scroll_position");
		PageData.set( "scroll_position", 0 );
		if( scroll_position ) {
			window.scrollTo(0, scroll_position);
		}
	}
};

var TextSearch = {
	init: function() {
		document.suche.onsubmit = function() {
			PageData.set( "result_page_number", 0 );
			PageData.set( "search_by", "fulltext" );
			PageData.set( "mode", 1 );
			PageData.submit();
			return false;
		};
		
		this._setValue( PageData.get("text_search") );
	},
	
	reset: function() {
		this._setValue("");
	},
	
	toString: function() {
		return escape( this._getValue() );
	},
	
	_trim: function( str ) {
		return (str.replace(/\s+$/,"").replace(/^\s+/,""));
	},
	
	_getValue: function() {
		return this._trim( document.getElementsByName( "vollText" )[0].value );
	},
	
	_setValue: function( newValue ) {
		document.getElementsByName( "vollText" )[0].value = newValue;
	}
};

var FileListTable = {
	init: function() {
		$(".file_list_page_nr_clickable").click( function() {
			PageData.set( "result_page_number", $(this).attr("page") );
			PageData.submit();
		});
		
		$(".page_resize_btn_enabled").click( function() {
			PageData.set( "result_page_number", 0 );
			PageData.set( "result_page_size", $(this).attr("size") );
			PageData.submit();
		});
		
		$(".sortable").click( function() {
			PageData.set( "sort_order", $(this).attr("sort_order") );
			PageData.submit();
		});
		
		$(".view_switch").click( function() {
			PageData.set( "detail_level", $(this).attr("detail_level") );
			PageData.submit();
		});
		
		$(".single_doc_download_action").click( function() {
			PageData.set( "action", "get_document" );
			PageData.set( "document_id", $(this).attr("document_id") );
			PageData.submit();
		});
	}
};

var PageData = {
	init: function() {
	},
	
	get: function( name ) {
		return g_page_data[name];
	},
	
	set: function( name, value ) {
		g_page_data[name] = value;
	},
	
	submit: function() {
		switch( this.get("search_by") ) {
			case "fulltext":
				Selection.reset();
				if( this.get( "text_search" ) != TextSearch._getValue() )
					this.set( "result_page_number", 0 );
			break;
			case "selection":
			default:
				TextSearch.reset();
				if( this.get( "selectedCategories" ) != Selection.toString() )
					this.set( "result_page_number", 0 );
			break;
		}
		
		this.set( "selectedCategories", Selection.toString() );
		this.set( "text_search", TextSearch.toString() );
		this.set( "merkliste", Merkliste.toString() );
		
		var param = [];
		$.each(g_page_data, function( name, value ) {
			param.push( name + "=" + value );
		});
		this.set( "action", "" );
		document.location.href = "index.php?" + param.join("&");
	}
};

var Selection = {
	exists: function() {
		return "undefined" != typeof g_selection_data;
	},
	
	init: function() {
		if( !this.exists() )
			return;
		
		this.appendPulldown( $("#selection_container_1"), g_selection_data.A.children, 0,
			function( node_map, container, node_list, pd_depth, on_change_callback ) {
				container.find("select").each( function( i, element ) {
					if( i > pd_depth )
						$(element).remove();
				});
				
				Selection.appendPulldown( container, node_map[this.value].children, pd_depth + 1, on_change_callback );
			}
		);
		
		this.appendPulldown( $("#selection_container_2"), g_selection_data.B.children, 0,
			function( node_map, container, node_list, pd_depth, on_change_callback ) {
				$("#selection_container_3").find("select:first").remove();
			
				var children = [];
				if( this.value > 0 ) {
					children = node_map[this.value].children;
				}
				else {
					$(node_list).each( function( i, node ) {
						children = children.concat(node.children);
					});
				}
				
				Selection.appendPulldown( $("#selection_container_3"), children, 0, function() {} );
			}
		);
		
		this.fromString( PageData.get( "selectedCategories" ) );
		
		$("#search_result_btn").click( function() {
			PageData.set( "result_page_number", 0 );
			PageData.set( "search_by", "selection" );
			PageData.submit();
		});
	},
	
	reset: function() {
		this.fromString("0;0;0");
	},
	
	toString: function() {
		function _get( container ) {
			var sel = [];
			container.find("select").each( function( i, e ) {
				sel.push( e.options[e.selectedIndex].value );
			});
			return sel.join(".");
		}
		
		return _get($("#selection_container_1")) + ";" + _get($("#selection_container_2")) + ";" + _get($("#selection_container_3"));
	},
	
	fromString: function( data ) {
		data = new String( data );
		$.each( data.split(";"), function( i1, selection ) {
			$.each( selection.split("."), function( i2, id ) {
				var pd = $("#selection_container_"+(i1+1)).find("select:eq("+i2+")").get(0);
				if( pd && pd.options ) {
					$.each( pd.options, function( i3, opt ) {
						if( id == opt.value ) {
							opt.selected = true;
							$(pd).change();
						}
					});
				}
			});
		});
	},
	
	appendPulldown: function( container, node_list, pd_depth, on_change_callback ) {
		if( 0 == node_list.length )
			return;
		
		var pd = document.createElement("select");
		var node_map = [ g_selection_data.null_element ];
		
		if( node_list.length > 0 ) {
			node_list = (new Array()).concat(node_list);
			node_list.unshift( node_map[0] );
		}
		
		$(node_list).each( function( i, node ) {
			pd.options[i] = new Option( node.title, node.id );
			node_map[node.id] = node;
		});
		
		$(pd).change( function() {
			on_change_callback.call( this, node_map, container, node_list, pd_depth, on_change_callback );
		});
		
		container.append( pd );
		$(pd).change();
	}
};

var Merkliste = {
	merkliste: [],
	
	init: function() {
		if(g_page_data.merkliste.length )
			this.merkliste = g_page_data.merkliste.split( ";" );
		
		$(".merkliste_add").click( function() {
			Merkliste.add( $(this).attr("file_id") );
			ScrollPosition.save();
			PageData.submit();
		});
		
		$(".merkliste_remove").click( function() {
			Merkliste.remove( $(this).attr("file_id") );
			ScrollPosition.save();
			PageData.submit();
		});
		
		$("#show_merkliste_btn").click( function() {
			PageData.set( "mode", 2 );
			PageData.submit();
		});
		
		$("#show_merkliste_back_btn").click( function() {
			PageData.set( "mode", 1 );
			PageData.submit();
		});
		
		$("#start_download_btn").click( function() {
			PageData.set( "action", "download" );
			PageData.submit();
		});
	},
	
	size: function() {
		return this.merkliste.length;
	},
	
	add: function( file_id ) {
		this.merkliste.push( file_id );
	},
	
	remove: function( file_id ) {
		var arr = [];
		for( var n=0; n < this.merkliste.length; n++ )
			if( file_id != this.merkliste[n] )
				arr.push( this.merkliste[n] );
		this.merkliste = arr;
	},
	
	toString: function() {
		return this.merkliste.join( ";" );
	}
};

