//panel animation control ==========================================================

// this is set/reset by panel_gate() and panel_out() 
panel_reset = false;

function panel_out()
{
	// alert('firing panel out...');
	
	$('#slide_panel').animate(
		{
			left: -127
			// style: '',
			// className: '',
			// opacity: 0.4,
			// backgroundColor: ''
		},
		'slow'
	);
	
	if(panel_reset) { $("#subcategories").BlindRight(); } 
	panel_reset = false;
	
	$('#categories').animate({ opacity: 0 },'fast');
	// This seems to work
	// $('#subcategories').css('backgroundColor', 'transparent');

	// $('#subcategories').animate({ backgroundColor: 'transparent' },'fast');
	
}

function panel_in()
{
	
	// alert('firing panel_in()');
	
	// if(panel_reset)
	// {
	// 	$('#slide_panel').animate(
	// 		{
	// 			left: 30,
	// 			// style: '',
	// 			// className: '',
	// 			opacity: 1,
	// 			// backgroundColor: ''
	// 		},
	// 		'slow'
	// 	);	
	// }
	// else
	// {
		$('#slide_panel').animate(
			{
				left: 30
				// style: '',
				// className: '',
				// opacity: 0.4,
				// backgroundColor: ''
			},
			'fast'
		);
	// }

	// $("#subcategories").BlindLeft();
	// BlindLeft, BlindRight, BlindToggleHorizontally	
	
	$('#categories').animate({ opacity: 1 },'slow');
	// This seems to work
	// $('#subcategories').css('backgroundColor', '#FFF');
	
	// $('#subcategories').animate({ backgroundColor: '#FFF' },'slow');
	
}

function panel_gate(trig)
{
	// alert('got ' + trig.href);

	var trigger = trig.href;

	// if( $(trig).attr('href') =="#services_index" ) // this does not work in ie 7 .. detecting the presence of #services_index instead
	if(trigger.search(/#services_index/) != -1)
	{
		
		// alert('in the panel_gate() if()');
		
		$("#subcategories").BlindLeft();
		
		// bc_reset(); // reset the breadcrumbs
		
		// send send in the slide panel showing only the cat list
		panel_reset = true;
		panel_in(); 
			
		//$('#slide_panel').show();
	}
	else 
	{	
		
		// alert('in the panel_gate() else');
		
		$('#slide_panel').hide();
		// $('#slide_panel').attr({ left: -110 } );
		// alert('done running hide');
	}
}

// data loaders ==========================================================
function golink(url)
{
	window.open(url,'findit_launched');
	// window.open('url to open','window name','attribute1,attribute2')
	// Below is a list of the attributes you can use:
	//    1. width=300 :: Use this to define the width of the new window.
	//    2. height=200 :: Use this to define the height of the new window.
	//    3. resizable=yes or no :: Use this to control whether or not you want the user to be able to resize the window.
	//    4. scrollbars=yes or no :: This lets you decide whether or not to have scrollbars on the window.
	//    5. toolbar=yes or no :: Whether or not the new window should have the browser navigation bar at the top (The back, foward, stop buttons..etc.).
	//    6. location=yes or no :: Whether or not you wish to show the location box with the current url (The place to type http://address).
	//    7. directories=yes or no :: Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).
	//    8. status=yes or no :: Whether or not to show the window status bar at the bottom of the window.
	//    9. menubar=yes or no :: Whether or not to show the menus at the top of the window (File, Edit, etc...).
	//   10. copyhistory=yes or no :: Whether or not to copy the old browser window's history list to the new window. 
}

function download_file(id){
	// pass the id into mod_download_file.php which increments the files hit count
	window.open("http://www.csuchico.edu/vpbf/findit/findit/_lib/php/mod_download_file.php?fid=" + id,'file_download');
}

function tab_load_department(id)
{
	// load this id in to the departmet tab area
	load_department_detail(id);
	// switch to the department tab
	$('#tab_container').triggerTab(4);
}

function load_department_detail(id)
{
	// alert( 'laoding department ' + id);
	
	$.ajax({
		type: "POST",
		url: '_lib/php/mod_dept_detail.php',
		data: 'id=' + id,
		dataType: "html",
		timeout: 30000,
		error: function(arg1,arg2){ 
			alert('An error occured while loading the department you chose. Please try again later.'); 
		},
		success: function(data){
			// $('#department_detail').fadeOut('slow');
			$('#department_detail').html('');
			$('#department_detail').html(data);
			$('#department_detail').fadeIn('slow');
		},
		complete: function(arg1,arg2){
			//alert( 'complete() reports -> args:['+arg1+'],['+arg2+']'); 
		}
	});
}

function load_department_forms(id)
{	
	$.ajax({
		type: "POST",
		url: '_lib/php/mod_dept_forms.php',
		data: 'id=' + id,
		dataType: "html",
		timeout: 30000,
		error: function(arg1,arg2){ 
			alert('A fatal error occured while loading the forms for the department you chose. Please try again later.'); 
		},
		success: function(data){
			// $('#department_forms').fadeOut('slow');
			$('#department_forms').html('');
			$('#department_forms').html(data);
			$('#department_forms').fadeIn('slow');
		},
		complete: function(arg1,arg2){
			//alert( 'complete() reports -> args:['+arg1+'],['+arg2+']'); 
		}
	});
}

function search(form)
{
	// get the search_text
	var query = (form == 'home_search') ? document.getElementById('home_search_text').value : document.getElementById('main_search_text').value; 
	// var query = $('#search_text').attr('value'); // returns undefined if blank ... hurmph!
	
	blank_error_msg = 'I can&rsquo;t serach a blank query ... sorry';
	
	if(query.replace(/^\s+$/,'') != '') // if search is not blank
	{
		locations = ''; 

		if(form == 'home_search') // if at home form -> create the locations string and fill the form fields (checkboxes) of the main search form
		{
			$('.home_search_section').each(function()
			{
				if(this.checked)
				{
					locations += this.value + ',';
					$('#main_' + this.value).attr({checked: 'checked'}); // check the target forms control			
				}
				else if(!this.checked) // uncheck the target forms control
				{
					// check the main search form also
					$('#main_' + this.value).attr({checked: ''});
				}	
			});
		}
		else // juast create the locations string
		{
			$('.main_search_section').each(function()
			{
				if(this.checked)
				{
					locations += this.value + ',';
				}	
			});
		}
		
		if(form == 'home_search') // if @ home form set the search string in the main search form and trigger the search tab
		{
			$('#main_search_text').attr({value: query}); 
			$('#tab_container').triggerTab(3);
		}
			
		locations = locations.substring(0,(locations.length - 1)); // trim off the last ","
		
		// perform the search -> send the query return the results into '#search_results'
		args = 'q=' + query + '&s=' + locations;

		$.ajax({
			type: "POST",
			url: '_lib/php/mod_search.php',
			data: args,
			dataType: "json",
			timeout: 30000,
			error: function(arg1,arg2){ 
				alert( 'A fatal error ocurred while searching. Please try again later.'); 
			},
			success: function(data){
			
				// extract json vars
				var ret = data.op[0].ret; 
				var msg = data.op[0].msg; 
				var department_results = data.op[1].dept;
				var document_results = data.op[2].docu;
				
				// report the error message returned from the server 
				if(!ret) 
				{ 
					report(msg,{cls:'apperr', type:'sticky', inject: 'search_message'}); 
				}
				else // load the results into the search results panel
				{
					report(msg,{cls: 'message', inject: 'search_message'}); 
					$('#dept_results').html(department_results);
					$('#docu_results').html(document_results);
				}
			},
			complete: function(arg1,arg2){
				//alert( 'complete() reports -> args:['+arg1+'],['+arg2+']'); 
			}
		});
	}
	else
	{
		
		if(form == 'home_search') report(blank_error_msg,{cls: 'apperr', inject: 'home_search_message'}); 
		else report(blank_error_msg,{cls: 'apperr', inject: 'search_message'});
	}

}

function clear_search(form)
{
	// clear the search text
	$('#'+form+'_text').attr({value: ''});
	$('.'+form+'_section').each(function()
	{
		this.checked = false;
	});
	
	var reset_html = '<div class="hint"><img src="_lib/img/icon_hint.jpg" alt="foo" class="leftfloat" height="40" width="40"><p>Input what you are looking for into the form above, click on the &ldquo;Find It!&rdquo; button when your ready to search.</p><p>Options:</p><ul><li>You can limit your search by choosing only a specific area to search in by using the <strong>Departments, Forms & Procedures and Services</strong> checkboxes</li><li>You can search for individual words or phrases by enclosinging the phrase inside quotes &ldquo;like this&rdquo;. </li></ul></div>';
	
	// reset the search pane
	$('#dept_results').html(reset_html);
	$('#docu_results').html('');	
}

function load_services(c_num,el)
{
	
	// load the service area with the services for category "s"
	$.ajax({
		type: "POST",
		url: '_lib/php/mod_load_services.php',
		data: 'for_cat=' + c_num,
		dataType: "html",
		timeout: 30000,
		error: function(arg1,arg2){ 
			alert('An error occured while loading the service list for the categry you chose. Please try again later.'); 
		},
		success: function(data){
			$('#cat_services_list').html(data);
		},
		complete: function(arg1,arg2){
			//alert( 'complete() reports -> args:['+arg1+'],['+arg2+']'); 
		}
	});
	
	// alert('loading services for category ' + c_num);
	panel_out();

	// get teh text to send into load_bc_cat(), etc...
	var service = $(el).html();

	// set the instruction to match the chosen service
	$('#service_departments').html('<div class="hint"><img src="_lib/img/icon_hint.jpg" width="40" height="40" alt="foo" class="leftfloat" /><p>Choose a service from the <strong>' + service + '</strong> list.</p></div>');
	// set the instruction to match the chosen service
	$('#service_documents').html('<div class="hint"><img src="_lib/img/icon_hint.jpg" width="40" height="40" alt="foo" class="leftfloat" /><p>Choose a service from the <strong>' + service + '</strong> list.</p></div>');


	// set the category name breadcrumb to be the same as the text of the anchor tag that triggered load_service()
	load_bc_cat(service);

}

function load_atoz()
{
	// reset the breadcrumb text .. hide it!
	$('#bc_cat').fadeOut('slow').html('');
	$('#bc_service').fadeOut('slow').html('');

	// load the service area with the services for category "s"
	$.ajax({
		type: "POST",
		url: '_lib/php/mod_load_all_services.php',
		data: null,
		dataType: "html",
		timeout: 30000,
		error: function(arg1,arg2){ 
			alert('An error occured while loading the service list. Please try again later.'); 
		},
		success: function(data){
			// load the service list into the panel
			$('#cat_services_list').html('');
			$('#cat_services_list').html(data);
			$('#cat_services_list').fadeIn('fast');
		},
		complete: function(arg1,arg2){
			//alert( 'complete() reports -> args:['+arg1+'],['+arg2+']'); 
		}
	});
}

function get_service_members(s_num,el)
{
	panel_out();
	// set the category name breadcrumb to be the same as the text of the anchor tag that triggered load_service()
	load_bc_service($(el).html());
	
	// load the service area with the services for category "s"
	$.ajax({
		type: "POST",
		url: '_lib/php/mod_service_members.php',
		data: 'sid=' + s_num + '&sname=' + $(el).html(), // being lazy here!
		dataType: "json",
		timeout: 30000,
		error: function(arg1,arg2){ 
			alert('An error occured while loading the document list for the service that you chose. Please try again later.'); 
		},
		success: function(data){
			
			var ret = data.op[0].ret; 
			var msg = data.op[0].msg; 
			var department_results = data.op[1].dept;
			var document_results = data.op[2].docu;
			
			// report the error message returned from the server 
			if(!ret) 
			{ 
				report(msg,{cls:'apperr', type:'sticky', inject: 'service_member_messgae'}); 
			}
			else // load the results into the correct areas
			{
				// just disguard the message for now
				// report(msg,{cls: 'message', inject: 'service_member_messgae'}); 
				
				$('#service_departments').html('');
				$('#service_departments').html(department_results);
				$('#service_departments').fadeIn('fast');
				
				$('#service_documents').html('');
				$('#service_documents').html(document_results);
				$('#service_documents').fadeIn('fast');
			}
		},
		complete: function(arg1,arg2){
			//alert( 'complete() reports -> args:['+arg1+'],['+arg2+']'); 
		}
	});
	
}

// reset the breadcrumbs and open the category panel
function bc_reset()
{
	// alert('firing bc_reset()');
	panel_in();
	
	// reset the document display area.
	$('#service_departments').html('<div class="hint"><img src="_lib/img/icon_hint.jpg" width="40" height="40" alt="foo" class="leftfloat" /><p>Choose a category from the <strong>Service Categories</strong> list.</p></div>');
	// reset the document display area.
	$('#service_documents').html('<div class="hint"><img src="_lib/img/icon_hint.jpg" width="40" height="40" alt="foo" class="leftfloat" /><p>Choose a category from the <strong>Service Categories</strong> list.</p></div>');
	
	$('#bc_cat').fadeOut('slow').html('');
	$('#bc_service').fadeOut('slow').html('');
}

// breadcrumb functions ===================================================

// beacuse of rushed execution phase (read no pre programming planning!) the following two functions share this global!
// last_cat_text = '';

function load_bc_cat(cat_text)
{
	// set the last_cat_text var
	// last_cat_text = cat_text; 	
	// set the cat html -> 	fade it in
	$('#bc_cat').html(' &nbsp;&gt;&nbsp; ' + cat_text).show();
	// reset the service breadcrumb
	$('#bc_service').fadeOut('slow').html('');
}

function load_bc_service(svs_text)
{
	// $('#cat_services_list > h2').html();
	// check for setting the services breadcrumb without a category breadcrumb ... fix if you need to
	// if($('#bc_cat').html() == '') $('#bc_cat').html(' &nbsp;&gt;&nbsp; ' + last_cat_text).show();
	if($('#bc_cat').html() == '') $('#bc_cat').html(' &nbsp;&gt;&nbsp; ' + $('#cat_services_list > h2').html()).show();
	// set the services breadcrumb
	$('#bc_service').html(' &nbsp;&gt;&nbsp; ' + svs_text).show();	
}

