var RequiredBooks = function ()
{
	var rpc = new Loader('book/');
	var semesters_list = $('required_semester');
	var subjects_list = $('required_subject');
	var courses_list = $('required_course');
	var sections_list = $('required_section');
	var display = $('required_menu_books');
	var books_display = $('required_menu_books_display');
	
	var booklist = new Array();
		
	var init = function ()
	{
		semesters_list.observe('change', LoadSubjects);
		subjects_list.observe('change', LoadCourses);
		courses_list.observe('change', LoadSections);
		sections_list.observe('change', LoadBooks);
		$('required_menu_books_close_x').observe('click', Hide);
		
		LoadSemesters();
		LoadSubjects();
	};
	
	var LoadSemesters = function ()
	{
		var semesters = rpc.Get('required_books/semesters', '', true);
		if(semesters.error != null)
		{
			return false;
		}
		
		var dropbox = new DropBox(semesters_list);
		dropbox.Populate({'opt':'value','val':'id','items':semesters});
	};
	
	var LoadSubjects = function ()
	{
		var params = 'semester='+semesters_list.value;
		var subjects = rpc.Get('required_books/subjects', params, true);
		if(subjects.error != null)
		{
			return false;
		}
		
		var dropbox = new DropBox(subjects_list);
		dropbox.Populate({'opt':'value','val':'id','items':subjects,'opt_one':'Select Subject','val_one':''});
	};
	
	var LoadCourses = function ()
	{
		if(subjects_list.options[0].value == '')
			subjects_list.options[0] = null;
		
		var params = 'semester='+semesters_list.value+'&subject='+subjects_list.value;
		var courses = rpc.Get('required_books/courses', params, true);
		if(courses.error != null)
		{
			return false;
		}
		
		var dropbox = new DropBox(courses_list);
		dropbox.Populate({'opt':'value','val':'id','items':courses,'opt_one':'Select Course','val_one':''});
	};
	
	var LoadSections = function ()
	{
		if(courses_list.options.length > 0 && courses_list.options[0].value == '')
			courses_list.options[0] = null;
		
		var params = 'semester='+semesters_list.value;
		params += '&subject='+subjects_list.value;
		params += '&course='+courses_list.value;
		
		var sections = rpc.Get('required_books/sections', params, true);
		if(sections.error != null)
		{
			return false;
		}
		
		var dropbox = new DropBox(sections_list);
		dropbox.Populate({'opt':'value','val':'id','items':sections,'opt_one':'Select Section','val_one':''});
	};
	
	var LoadBooks = function ()
	{
		if(sections_list.options.length > 0 &&  sections_list.options[0].value == '')
			sections_list.options[0] = null;
		
		var params = 'semester='+semesters_list.value;
		params += '&subject='+subjects_list.value;
		params += '&course='+courses_list.value;
		params += '&section='+sections_list.value;
		
		var books = rpc.Get('required_books/books', params, true);
		if(books.error != null)
		{
			return false;
		}
		
		Showbooks(books);
	};
	
	var Showbooks = function (books)
	{
		if(books.length > 0)
		{
			$$('a.search').each(function (link){ link.stopObserving('click', Search); });
			books_display.update('');
			booklist = new Array();
			for(var i=0; i<books.length; i++)
			{
				var book = books[i];
				
				booklist[book.RequiredID] = book;
				
				var div = new Element('DIV').addClassName('requried_book_container');
				
				switch(book.Code)
				{
					case 'R': div.addClassName('required'); break;
					case 'O': div.addClassName('optional'); break;
					case 'C': div.addClassName('recommend'); break;
					default : break;
				}
				
				var left = new Element('DIV').addClassName('left').update('Title:');
				
				var img = new Element('IMG').writeAttribute({'src':book.SmallImg});
				var picture = new Element('DIV').addClassName('picture').insert(img);
				var right = new Element('DIV').addClassName('right').update(book.Title);
				books_display.insert(div.insert(left).insert(picture).insert(right));
				
				if(book.Author != null && book.Author.length > 0)
				{
					left = new Element('DIV').addClassName('left').update('Author:');
					right = new Element('DIV').addClassName('right').update(book.Author);
					books_display.insert(div.insert(left).insert(right));
				}
				
				if(book.Edition != null && book.Edition.length > 0)
				{
					left = new Element('DIV').addClassName('left').update('Edition:');
					right = new Element('DIV').addClassName('right').update(book.Edition);
					books_display.insert(div.insert(left).insert(right));
				}
				
				if(book.ISBN != null && book.ISBN.length > 0)
				{
					left = new Element('DIV').addClassName('left').update('ISBN:');
					right = new Element('DIV').addClassName('right').update(book.ISBN);
					books_display.insert(div.insert(left).insert(right));
				}
				
				right = new Element('DIV').addClassName('right');
//				var a = new Element('A').update('Search for this book!');
//				a.writeAttribute({'id':book.RequiredID}).observe('click', Search).addClassName('search');
//				right.insert(a).insert('&nbsp;|&nbsp;');
				var a = new Element('A').update('Find me cheap books!').setStyle({'fontSize':'14px','fontWeight':'bold'});
				a.writeAttribute({
					'href':Documents.root+'browse/course/'+book.SubjectID+'/'+book.CourseCode
				});
				
				books_display.insert(div.insert(right.insert(a)));
			}
			
			display.setStyle({'display':'block'});
			var x = find_x_pos(sections_list) + sections_list.getWidth() + 20;
			var y = find_y_pos(semesters_list);// + sections_list.getHeight() - display.getHeight();
			display.setStyle({'left':x+'px','top':y+'px'});
		}
	};
	
	var Search = function ()
	{
		var id = this.id.split('_');
		id = id[0];
		$('title').value = booklist[id].Title;
		$('author').value = booklist[id].Author;
		$('isbn').value = booklist[id].ISBN;
		$('book_search_form').submit();
	};
	
	var Hide = function () { display.setStyle({'display':'none'}); };
	
	init();
};