/**
* Class to handle basic JS operations on sub module pages (such as activating and controlling drop down
* content(
*
* @author Mike Pritchard (mike@adastrasystems.com)
*/
var mtrSubModule = {

	currentPageNo : 0,
	pagePostID : -1,
	nextPageURL : '',
	prevPageURL : '',
	
	moduleID : 0,
	submoduleID : 0,
	
	// //////////////////////////////////////////////////////////////////
	
	init : function(module_id, submodule_id, current_page_no, page_post_id, next_page_url, prev_page_url, code_url){
	
		mtrSubModule.moduleID = module_id;
		mtrSubModule.submoduleID = submodule_id;
		mtrSubModule.currentPageNo = current_page_no;
		mtrSubModule.pagePostID = page_post_id;
		mtrSubModule.nextPageURL = next_page_url;
		mtrSubModule.prevPageURL = prev_page_url;
		
		// Setup remote command url
		RemoteService.commandURL = code_url + "/code/php/RemoteServices.php";		
		
		// Make any content that should be those funky drop-down content thingies
		$('.expandable').each(function(){			
			
			var header = $(this).html();
			
			var content = $(this).next().html();			
			
			var tag = 'div';
			
			if ($(this).next().is('ul')){
				tag = 'ul';
			}
			else if ($(this).next().is('p')) {
				tag = 'p';
			}
			
			// Clear content
			$(this).next().html("");
									
			var large = '';
			if (header.length > 150){
				large = 'largebanner';
			}
			
			var txt = "";
			txt += "<div class='expandable'>\n"
			txt += "    <div class='expandable_banner plus "+large+"'>\n";
			txt += "        <h3 class='basic_banner_text_small'>"+header+"</h3>\n";
			txt += "    </div>\n";
			txt += "    <div class='expandable_banner_content'>\n";
			txt += "        <"+tag+">"+content+"</"+tag+">\n";
			txt += "    </div>\n";
			txt += "</div>\n";
			 
			$(this).html(txt); 
		});

		
		// Activate the expandables (add a click listener)		
		$('.expandable_banner').click(function() {mtrSubModule.toggleExpandable(this);});

		// Add click listeners for next/back buttons		
		$('.next_button').click(function() {mtrSubModule.gotoNext();});
		$('.prev_button').click(function() {mtrSubModule.gotoPrev();});
		
	},

	// //////////////////////////////////////////////////////////////////

	gotoNext : function(){
		
		// Check for any questions that may have been answered (if this page
		// has any questions
		
		mtrQuestionProcessor.processQuestions(mtrSubModule.pagePostID, mtrSubModule.moduleID, function(resp){mtrSubModule.onAnswersSaved(mtrSubModule.nextPageURL)});
		
		// Update user progress 
		RemoteService.updateUserModuleProgress(mtrSubModule.submoduleID, mtrSubModule.currentPageNo);
    	
	},

	// //////////////////////////////////////////////////////////////////

	gotoPrev : function(){
	
		//mtrQuestionProcessor.processQuestions(mtrSubModule.pagePostID, mtrSubModule.moduleID);
		mtrQuestionProcessor.processQuestions(mtrSubModule.pagePostID, mtrSubModule.moduleID, function(resp){mtrSubModule.onAnswersSaved(mtrSubModule.prevPageURL)});
    		
	},

	// //////////////////////////////////////////////////////////////////

	onAnswersSaved : function(pageURL){
		//alert('changing page');
		window.location = pageURL;		
	},
		
	// //////////////////////////////////////////////////////////////////

	/**
	* Toggle the expandable banner, either slide it up or down based on its current
	* state.
	* @param obj the DOM object that was clicked
	*/
	toggleExpandable : function(obj){

		// Is this one open or closes
		var isClosed = $(obj).hasClass('plus');
				
		// Close all
		$('.expandable_banner').each(function(){	

			if ($(this).hasClass('minus')){
				$(this).parent().children(".expandable_banner_content").slideUp('fast');
				$(this).removeClass('minus');
				$(this).addClass('plus');
			}
		});
		
		// If this one is closed, then open it
		if (isClosed){
			$(obj).parent().children(".expandable_banner_content").slideDown('fast');
			$(obj).removeClass('plus');
			$(obj).addClass('minus');
		}
		
/*		var exp = $(obj).parent();
		
		// Is it open or closed?
		if ($(obj).hasClass('plus')){
			// It's closed, so slide the content down
			$(obj).parent().children(".expandable_banner_content").slideDown('fast');
			$(obj).removeClass('plus');
			$(obj).addClass('minus');
		}
		else {
			$(obj).parent().children(".expandable_banner_content").slideUp('fast');
			$(obj).removeClass('minus');
			$(obj).addClass('plus');
		}
*/		
				
	}	
}
