﻿var Site = {

	start: function() {
		Site.addSearchText();
		Site.checkEmptyLists();
		Site.addAjaxLists();
		if($('table.tbl_directory a.toggle_switch').length) { Site.animateDirectoryTable(); }
	},
	
	animateDirectoryTable: function() {
		
		$('table.tbl_directory a.toggle_switch').bind( 'click', function(event) { // Show content row
																		 	event.preventDefault();
																		  	var toggle_switch = $(this);
																			var containerRow = toggle_switch.parent().parent().parent().parent();
																			var contentRow = containerRow.next('tr');
																			containerRow.css('background-color', contentRow.css('background-color', '#D7EDC0'));
																			contentRow.toggle(0);
																			//contentRow.fadeIn();
																			$parent().('table.tbl_directory a.hide_switch').show();
																			$parent()('table.tbl_directory a.toggle_switch').hide();
																			
																		 });
		
		$('table.tbl_directory a.hide_switch').bind( 'click', function(event) { // Hide content row
																		 	event.preventDefault();
																		  	var hide_switch = $(this);
																			var containerRow = hide_switch.parent().parent().parent().parent();
																			var contentRow = containerRow.next('tr');
																			containerRow.css('background-color', contentRow.css('background-color', '#fff'));
																			//var contentRow = hide_switch.parent().parent().parent();
																			//contentRow.next('tr').css('background-color', '#fff');
																			contentRow.hide();
																			//contentRow.fadeOut();
																			$('table.tbl_directory a.hide_switch').hide();
																			$('table.tbl_directory a.toggle_switch').show();
																			
																		 });
		
		$('table.tbl_directory a.stats_switch').bind( 'click', function(event) { // Show content row
																		 	event.preventDefault();
																			$('.show_personnel').hide();
																			$('.show_stats').toggle();
																		 });
		

		
		$('table.tbl_directory a.personnel_switch').bind( 'click', function(event) { // Show content row
																		 	event.preventDefault();
																			$('.show_stats').hide()
																			$('.show_personnel').toggle();
		 });

	},
	
	addSearchText: function() {
		var sText = 'Search...';
		var box = $(".ms-sbplain:text");
		
		if( box.length ) {
			box.attr({ value: sText });
			
			box.focus( function() {	if( box.attr('value') == sText ) { box.attr( 'value', '' ); }});
			
			box.blur( function() { if( box.attr('value') == '' ) { box.attr( 'value', sText ); }});
		}
	},
	
	checkEmptyLists: function() {
		if( $('select:empty').length) { $('select:empty').css('display','none'); }
	},
	
	showAjaxLoader: function() {
		$("#loading").show();
	},
	
	hideAjaxLoader: function() {
		$("#loading").fadeOut('slow');
	},
	
	showList: function(el) {
		el.fadeIn('slow');
	},
	
	hideList: function(el) {
		el.hide();
	},
	
	addAjaxLists: function() {
		
		if($('#selectMain').length) {
		
			$('#selectMain').chainSelect('#selectSecond','json.php', {
									 
				before: function( target ) { // before request hide the target combobox and display the loading message 
					Site.showAjaxLoader();
					Site.hideList($(target));
				},
				
				after: function( target ) { // after request show the target combobox and hide the loading message
					Site.hideAjaxLoader();
					Site.showList($(target));
					Site.checkEmptyLists();
				}
				
			});
		
		}
	
		if($('#selectSecond').length) {
			$('#selectSecond').chainSelect('#selectThird','json.php', {
										   
				before: function( target ) { 
					Site.showAjaxLoader();
					Site.hideList($(target));
				},
				
				after: function( target ) { 
					Site.hideAjaxLoader();
					Site.showList($(target));
					Site.checkEmptyLists();
				}
				
			});
		}
		
		if($('#selectThird').length) {
			$('#selectThird').chainSelect('#selectFourth','json.php', {
										   
				before: function( target ) { 
					Site.showAjaxLoader();
					Site.hideList($(target));
				},
				
				after: function( target ) { 
					Site.hideAjaxLoader();
					Site.showList($(target));
					Site.checkEmptyLists();
				}
				
			});
		}
	}

}

$(document).ready( Site.start );

