$(document).ready(function(){
	search_bindall();
	shortlist_bindall();
	$("input#submit").bind('click', luur_submit);
	/*if ( window.document.luur_advancedsearch != null )
	{
		set_deps(window.document.luur_advancedsearch.institution.value);
	}*/
	var deps = $("#institution").val();
	if (typeof deps != "undefined")
	{
		set_deps(deps);
	}
});

function luur_submit()
{
	var deps = '';
	for (var i = 0; i < luur_departments.length; i++)
	{
		deps += luur_departments[i] + ',';
	}
	deps = deps.substring(0, deps.length-1);
	
	//window.document.luur_advancedsearch.institution.value = deps;
	$("#institution").val(deps);
}

function luur_dep_find(element)
{
	for (i = 0; i < luur_departments.length; i++)
	{
		if ( luur_departments[i] == element )
		{
			return i;
		}
	}
	return -1;
};

function luur_dep_remove(id)
{
	var elem = luur_dep_find(id)
	if ( elem != -1 )
	{
		luur_departments.splice(elem, 1);
	}
}


function search_bindall()
{
	$("a.dep").bind('click',browse_departments);
	$("input[class='dep']").each(function(){
		$(this).bind('click', checkbox_departments);
	});
}


function browse_departments()
{
	if ( $("div[id='" + this.id + "']>div.dep").size() > 0)
	{
		clear_tree(this.id);
		return;
	}
	var checked = 0;
	$("input[id=" + this.id + "]").each(function(){
		if (this.checked == true)
		{
			checked = 1;
		}
	});
	
	load_tree(this.id, checked);
}

function clear_tree(id)
{
	$("div[id='" + id + "']>div.dep").each(function(){
		 $(this).remove();
	});
	if (id == "0")
	{
		$("div.daddy").removeClass("luur_interactive");
	}
}

function load_tree(id, check)
{
	$("div[id='" + id + "']").load('/o.o.i.s?id=129&template=.luur_ajax.t', {control: 'departments', orgid: id, get_children: 1, checked: check, language: lang }, loaded_department);
	if(id==0)
	{
		$("div.daddy").addClass("luur_interactive");
	}
}

function checkbox_departments()
{
	if (this.id == "0")
	{
		if (this.checked == true)
		{
			clear_tree(this.id);
			luur_departments.length = 0;
			luur_departments.push(this.id);
		}
		else
		{
			luur_departments.length = 0;
			load_tree(this.id);
		}
	}
	else if ( this.checked == true )
	{
		$(this).parent("div.dep").find("input[type=checkbox]").each(function(){
			this.checked = true;
		});
		if ( luur_dep_find(this.id) == -1 )
		{
			luur_departments.push(this.id);
		}

		$(this).parent("div.dep").parents("div.dep").each(function(){
			var daddy = false;
			if ( this.id == "0" )
			{
				daddy = true;
			}
			var all_checked = true;
			if ( $("div[id=0]").find("div.dep").size() == 0)
			{
				all_checked = false;
			}
			$(this).children("div.dep").find("input[type=checkbox]").each(function(){
				if (this.checked == false)
				{
					all_checked = false;
				}
			});
			if (all_checked == true)
			{
				$(this).children("div.dep").find("input[type=checkbox]").each(function(){
					luur_dep_remove(this.id);
					/*
					var elem = luur_dep_find(this.id);
					if ( elem != -1 )
					{
						luur_departments.splice(elem,1);
					}
					*/
				});
			
				$(this).children("input[type=checkbox]").each(function(){
					this.checked = true;
				});
				luur_departments.push(this.id);
			}
			if ( all_checked == true && daddy == true )
			{
				$("input[id=0]").each(function(){
					this.checked = true;
				});
				luur_departments.length = 0;
				luur_departments.push(0);
			}
		});
	}
	else if ( this.checked == false )
	{
		luur_dep_remove(this.id);
		
		$(this).parent("div.dep").parents("div.dep").each(function(){
			var check = false;
			$(this).children("input[type=checkbox]").each(function(){
				check = this.checked;
			});
			if ( this.id == 0 && check == true )
			{
				$("div[id=0]>div.dep").each(function(){
					$("input[id=" + this.id + "]").each(function(){
						if (this.checked == true)
						{
							if ( luur_dep_find(this.id) == -1 )
							{
								luur_departments.push(this.id);
							}
						}
					});
				});
			}
			else if ( check == true )
			{
				$(this).children("div.dep").each(function(){
					$("input[id=" + this.id + "]").each(function(){
						if (this.checked == true)
						{
							if ( luur_dep_find(this.id) == -1 )
							{
								luur_departments.push(this.id);
							}
						}
					});
				});
			}
		});
		$(this).parents("div.dep").each(function(){
			$("input[id=" + this.id + "]").each(function(){
				this.checked = false;
				luur_dep_remove(this.id);
			});
		});
		$(this).parent("div.dep").find("input[type=checkbox]").each(function(){
			this.checked = false;
		});
	}

	/*
	$.ajax(
	{
		type: 'GET',
		url: '/o.o.i.s?id=129&template=.luur_ajax.t',
		data: 'control=departments&orgid' + this.id + '&language=' & lang + 'uncheck=1'
	});
	*/
	
	$("div#info").load('/o.o.i.s?id=129&template=.luur_ajax.t', { control:'departments', orgid: this.id });
}

function loaded_department()
{
	search_bindall();
	for (dep in luur_departments)
	{
		$("div[id=" + luur_departments[dep] + "]").find("input[type=checkbox]").each(function(){
			this.checked = true;
		});
	}
}

function set_settings(language, ref_id, departments)
{
	lang = language;
	references_id = ref_id;
}

function set_deps(departments)
{
	luur_departments = Array();
	luur_departments = departments.split(',');
	if ( luur_departments.length == 0 )
	{
		luur_departments.push(0);
	}
}





// -------------------------------------------------------------------------------------------- //




function shortlist_bindall()
{
	$(".luur_top>input[type=checkbox]").each(function(){
		$(this).bind('click', shortlist_click);
	});
	$("#shortlist_selectall").each(function(){
		$(this).bind('click', shortlist_selectall);
	});
	$("#shortlist_deselectall").each(function(){
		$(this).bind('click', shortlist_deselectall);
	});
}

function shortlist_selectall()
{
	$("#shortlist_deselectall").each(function(){
		this.checked = false;
	});
	var status = '';
	if (this.checked == true)
	{
		$(".luur_top>input[type=checkbox]").each(function(){
			this.disabled = true;
		});
		status = 'true';
	}
	else
	{
		$(".luur_top>input[type=checkbox]").each(function(){
				this.disabled = false;
		});
		status = 'false';
	}
	$.ajax(
	{
		type: 'GET',
		url: '/o.o.i.s?id=129&template=.luur_ajax.t',
		data: 'control=shortlist&selectall=' + status
	});
//	$("div#info").load("/o.o.i.s?id=129&template=.luur_ajax.t&control=shortlist&selectall=" + status);
}

function shortlist_deselectall()
{
	$("#shortlist_selectall").each(function(){
		this.checked = false;
	});
	$(".luur_top>input[type=checkbox]").each(function(){
		this.disabled = false;
	});
	var status = '';
	if (this.checked == true)
	{
		$(".luur_top>input[type=checkbox]").each(function(){
				this.checked = false;
		});
		status = 'true'
	}
	else
	{
		status = 'false';
	}
	$.ajax(
	{
		type: 'GET',
		url: '/o.o.i.s?id=129&template=.luur_ajax.t',
		data: 'control=shortlist&deselectall=' + status
	});
	$("div#info").load("/o.o.i.s?id=129&template=.luur_ajax.t&control=shortlist&deselectall=" + status);
}

/*
function shortlist_display()
{
	// plain text
	if (this.value == 1)
	{
		window.open('/o.o.i.s?id=' + references_id + '&shortlist=true', "", "width=600,height=700,scrollbars=yes");
	}
}
*/

function shortlist_click()
{
	$("input[id=shortlist_deselectall]").each(function(){
		this.checked = false;
	});
	var extra = '';
	var checked = 0;
	if (this.checked == true)
	{
		checked = 1;
	}
	if ( this.id == 'shortlist_selectall')
	{
		extra = 'selectall=true';
	}
	else if (this.id == 'shortlist_deselectall')
	{
		extra = 'deselectall=true';
	}
	else
	{
		extra = '&post_id=' + this.id + '&checked=' + checked;
	}
	$.ajax(
		{
			type: 'GET',
			url: '/o.o.i.s?id=129&template=.luur_ajax.t',
			data: 'control=shortlist' + extra
		}
	);
}





