// JavaScript Document

$(function () { // same as $(document).ready(function () { })
	timer = 0;
	clear_all();
	show_browsebar(); // and page
	
	// add pre-text ///////////////////////////////////////////////////////////////////
	$('#browse_tr LI.btn SPAN').each(function(){ $(this).html('T'+$(this).html()); });
	$('#browse_to LI.btn SPAN').each(function(){ $(this).html('T'+$(this).html()); });
	$('#browse_co LI.btn SPAN').each(function(){ $(this).html('T'+$(this).html()); });
	$('#browse_cu_tr LI.btn SPAN').each(function(){ $(this).html('C'+$(this).html()); });
	$('#browse_cu_to LI.btn SPAN').each(function(){ $(this).html('C'+$(this).html()); });
	$('#browse_cu_co LI.btn SPAN').each(function(){ $(this).html('C'+$(this).html()); });
	$('#browse_re_tr LI.btn SPAN').each(function(){ $(this).html('R'+$(this).html()); });
	$('#browse_re_to LI.btn SPAN').each(function(){ $(this).html('R'+$(this).html()); });
	$('#browse_re_co LI.btn SPAN').each(function(){ $(this).html('R'+$(this).html()); });
	
	
///////////////////////////////////////////////////
// SET BUTTON ACTIONS /////////////////////////////
///////////////////////////////////////////////////
	// search button
	$('#search_in').keyup( function(e){
		if( timer > 0 ) clearTimeout( timer );
		
		if( e.which == 13 ){ prep_search(); $('#search_in').blur(); }
		else if( $('#search_in').val() != "" ) timer = setTimeout( "prep_search()", 2000 );
	});
	
	
	// browser tabs
	$('#browse_sec A').click( function(){
		if( $(this).hasClass('sel') ){
			$('#browse_sec A').removeClass('sel');
			$(this).blur();
			hide_browsebar();
		}else{
			$('#browse_sec A').removeClass('sel');
			$(this).addClass('sel').blur();
			location.hash = $(this).attr('href');
			show_browsebar();
		}
	});
	
/*	// co buttons
	$('#browse_co LI.btn[co_toID]').click(function () {
		$(this).addClass('selected');
		select_co( $(this).attr('id').substring(3), 1 );
	});*/
			
});


/////////////////////////////////////////////////////////////////
// GENERAL FUNCTIONS ////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
function show_sec(sec){
	$('#browse_sec A').removeClass('sel');
	$('#browse_sec A[href="#'+sec+'"]').addClass('sel').blur();
	location.hash = sec;
	show_browsebar();
}
function clear_all(){
	$.modal.close();
	$('#page_container').hide();
	$('#container').html('');
	$('.browse_to, .browse_co').hide();
	$('.selected').removeClass('selected');
	$('#browse_container').show();
}
function show_browsebar(){
	h = location.hash;
	h = h.substr(1);
	
	if( h.substr(0,5) == "query" ){
			//find subsection
			h2 = h.indexOf("#");
			if( h2 > 0 ){
				h2 = h.substr( h2+1 );
				get_search(h2);
			}
	}
	if( h.substr(0,6) == "course" ){
			//find subsection
			h2 = h.indexOf("#");
			if( h2 > 0 ){
				h2 = h.substr( h2+1 );
				select_co(h2,1);
			}
	}
	if( h == "faqs" || h.substr(0,4) == "faqs" || h == "links" || h == "contact-us" ){ get_page(h); }
	if( h == "" ) h = "training";
	
	$('#browse_sec A[href="#'+h+'"]').addClass('sel');
	$('.browse-list').hide(); $('#browse_'+h).show(); 
	$('.selected').removeClass('selected');
	$('.browse_to, .browse_co').hide();
	$('#browse_container').show();
	if( h.substr(0,5) == "track" ){
			//find section
			h2 = h.indexOf("#");
			if( h2 > 0 ){
				h2 = h.substr( h2+1 );
				//find track
				h3 = h2.indexOf("#");
				if( h3 > 0 ){
					h3 = h2.substr( h3+1 );
					h2 = h2.substr( 0,h2.indexOf("#") );
					sel_tr(h3,h2,1);
				}
			}
	}
	if( h.substr(0,5) == "topic" ){
			//find section
			h2 = h.indexOf("#");
			if( h2 > 0 ){
				h2 = h.substr( h2+1 );
				//find track
				h3 = h2.indexOf("#");
				if( h3 > 0 ){
					h3 = h2.substr( h3+1 );
					h2 = h2.substr( 0,h2.indexOf("#") );
					sel_to(h3,h2,1);
				}
			}
	}
}
function hide_browsebar(){
	if( navigator.appName == "Microsoft Internet Explorer" ){
		$('#browse_container').hide();
		$('#browse_to, #browse_co').hide();
		$('.selected').removeClass('selected');
	}else{
		$('#browse_container').slideUp(300, function() { 
				$('#browse_to, #browse_co').hide();
				$('.selected').removeClass('selected');
		});
	} 
}
function clear_search(theText) {
 if (theText.value == theText.defaultValue) {
	 theText.value = ""
	 $(theText).addClass("unblur");
 }
}
function show_search(theText) {
		theText.value = theText.defaultValue;
		$(theText).removeClass("unblur");
}
function sel_sec( sec_name ){
		$('#browse_sec A').removeClass('sel');
		$('#browse_sec A[href="#'+sec_name+'"').addClass('sel').blur();
		location.hash = '#'+sec_name;
		show_browsebar();
}
function sel_tr( trID, sec, leaveMdl ){
	sel_sec( sec );
	if(leaveMdl!=1) $.modal.close();
	$('.browse_to, .browse_to LI, .browse_co, .browse_co LI').hide(); //hide substructure
	$('.selected').removeClass('selected'); //remove highlights
	$('#browse_'+sec+' .browse_tr LI[trid="'+trID+'"]').addClass('selected'); //highlight track
	$('#browse_'+sec+' .browse_to LI[to_trID="'+trID+'"], #browse_'+sec+' .browse_to').show(); //show topics
	
	$('.no_topics').hide();
	if( $('#browse_'+sec+' .browse_to LI[to_trID="'+trID+'"]').length < 1 ) $('.no_topics').show();//fadeIn('fast');
}
function sel_to( toID, sec, leaveMdl ){
	sel_tr( find_tr( toID, sec ), sec, leaveMdl ); //for direct links to topics, redo track select
	$('#browse_'+sec+' .browse_to LI[toid="'+toID+'"]').addClass('selected'); //highlight topic
	$('#browse_'+sec+' .browse_co LI[co_toID="'+toID+'"], #browse_'+sec+' .browse_co').show(); //show resources
	
	$('.no_courses').hide();
	if( $('#browse_'+sec+' .browse_co LI[co_toID="'+toID+'"]').length < 1 ) $('.no_courses').show();//fadeIn('fast');
}
function find_tr(toID, sec){ return $('#browse_'+sec+' .browse_to LI[toid="'+toID+'"]').attr('to_trID'); }

function select_tr( trID,leaveMdl ){
	if(leaveMdl!=1) $.modal.close();
	tpcDisplayed = $('#browse_to').css('display');
	$('.browse_to, .browse_co').hide(); 
	$('.selected').removeClass('selected');	
	$('#bTR'+trID).addClass('selected');
	$('#browse_to LI').hide();
	if( tpcDisplayed == "none" )
		$('#browse_to LI[to_trID="'+trID+'"], #browse_to').show();//fadeIn('fast');
	else
		$('#browse_to LI[to_trID="'+trID+'"], #browse_to').show();
	
	if( $('#browse_to LI[to_trID="'+trID+'"]').length < 1 ) $('#no_topics').show();//fadeIn('fast');
	if( $('#browse_container').css('display') == "none" )
		if( navigator.appName == "Microsoft Internet Explorer" ) $('#browse_container').show();
		else $('#browse_container').slideToggle(300);
}
function select_to( toID,leaveMdl ){
	if(leaveMdl!=1) $.modal.close();
	crsDisplayed = $('#browse_co').css('display');
	select_tr( get_tr(toID),leaveMdl );
	$('#bTO'+toID).addClass('selected');
	$('#browse_co LI').hide();
	if( tpcDisplayed == "none" )
		$('#browse_co LI[co_toID="'+toID+'"], #browse_co').show();//fadeIn('fast');
	else
		$('#browse_co LI[co_toID="'+toID+'"], #browse_co').show();
		
	if( $('#browse_co LI[co_toID="'+toID+'"]').length < 1 ) $('#no_courses').show();//fadeIn('fast');
}
function select_co( coID,leaveMdl ){
	// leave when editing
	if(leaveMdl!=1)	$.modal.close();
	
	$('#container').html("");
	get_course( coID,leaveMdl );
	$('#container').show();
	$('#browse_sec A').removeClass('sel');
	hide_browsebar();
}
function get_course( coID, leaveMdl ){
	if(leaveMdl!=1) $.modal.close();
	$.ajax({
		method: "get", url: "/index.php/home/course/"+coID,
		beforeSend: function(){ 
			$('#container').html('<div id="ajax_loader"><img src="/imgs/ajax_loader.gif"/></div>').hide();
			$('#container').show();//fadeIn('slow');
		},
		complete: function(){  },//$("#loading").hide("fast");}, //stop showing loading when the process is complete
		success: function(html){ $("#container").html(html); //show the html inside .content div
		}
	});
}
function get_resource(reID){
	$.modal.close();
	$.ajax({
		method: "get", url: "/index.php/home/resource/"+reID,
		beforeSend: function(){ 
			//$('#container').html('<div id="ajax_loader"><img src="/imgs/ajax_loader.gif"/></div>').hide();
			//$('#container').show();//fadeIn('slow');
			$.modal('<div id="ajax_loader"><img src="/imgs/ajax_loader.gif"/></div>');
		},
		complete: function(){  },//$("#loading").hide("fast");}, //stop showing loading when the process is complete
		success: function(html){ $.modal.close(); $.modal(html); // $("#container").html(html); //show the html inside .content div
		}
	});
}
function prep_search(){
	$('#browse_sec LI').removeClass('sel');
	get_search( $('#search_in').val() );
}
function get_search(qry, re_only){
	$('#page_container').hide();
	$('#container').html("");
	
	hide_browsebar();
	$('#browse_sec A').removeClass('sel');
	
	$.modal.close();
	$.ajax({
		method: "get", url: "/index.php/home/search/"+qry+"/"+re_only,
		beforeSend: function(){ 
			$('#container').html('<div id="ajax_loader"><img src="/imgs/ajax_loader.gif"/></div>').hide();
			$('#container').show();//fadeIn('slow');
		},
		complete: function(){  },//$("#loading").hide("fast");}, //stop showing loading when the process is complete
		success: function(html){ $("#container").html(html); //show the html inside .content div
		}
	});
}
function get_tr( toID ){
	return $('.browse_to LI[toid="'+toID+'"]').attr("to_trid");
}	
function submit_newsletter(){
	$('#page01_content').slideUp(300, function(){ $('#page01_content').html("Thanks for signing up."); });
	$('#page01_content').fadeIn();
	return true;
}
function get_page(p){
	//find subsection
	p2 = p.indexOf("#");
	if( p2 > 0 ){
		p2 = p.substr( p2+1 );
	}
	if( p.substr(0,4) == "faqs" ) p = "faqs";
	
	$('#page_container').hide();
	$('#container').html("");
	
	hide_browsebar();
	$('#browse_sec A').removeClass('sel');
	
	$.modal.close();
	$.ajax({
		method: "get", url: "/index.php/home/page/"+p,
		beforeSend: function(){ 
			$('#container').html('<div id="ajax_loader"><img src="/imgs/ajax_loader.gif"/></div>').hide();
			$('#container').show();//fadeIn('slow');
		},
		complete: function(){  },//$("#loading").hide("fast");}, //stop showing loading when the process is complete
		success: function(html){
			$("#container").html(html);   //show the html inside .content div
			if( p2 != -1 ){
				location.hash = "#"+p2;
			}
		}
	});
}