$(document).ready(function(){
	initSearch();
	initMenu();
	dynamicGenerateCopyRightYear("copyRightYear")
	$('.tab_titles li').click(function(){
		
		$(this).removeClass('hide');
		$(this).addClass('active');
		$(this).siblings().attr('class', 'hide');
		showTab($(this).attr('id'));
	});
	$('.maintab_tabs .tab').click(function(){
		$(this).removeClass('hide');
		$(this).addClass('active');
		$(this).siblings().attr('class', 'hide');
		var mainId = $(this).attr('id').replace('_tab', '');
		showTab('main', mainId);
	});
	$('.tabbox_tabs li').click(function(){
		$(this).removeClass('hide');
		$(this).addClass('active');
		$(this).siblings().attr('class', 'hide');
		var type = $(this).attr('id').replace('_tab', '');
		showTab(type);
	});
	if($("#logging_container").length>0){
		$("#completed_container").removeClass("x-hide-display");
		// here we will bind the event to the tabs
		$('.acs_logging_tabs li').click(function(){
			$(this).removeClass('hide');
			$(this).addClass('active');
			$(this).siblings().attr('class', 'hide');
			showTab($(this).attr('id'));
		});
	}
	/*
		creating the click function for business arrow
	*/
	if($("#business_con_arrow").length>0){
		$("#business_con_arrow").click(function(){
			var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("business_con_box");
			}else{
				return;
			}
		});
	}
	/*
		creating the click function for govPolicy arrow
	*/
	if($("#govPolicy_con_arrow").length>0){
		$("#govPolicy_con_arrow").click(function(){
				var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("govPolicy_con_box");
			}else{
				return;
			}
		});
	}
	/*
		creating the click function for sciTech arrow
	*/
	if($("#sciTech_con_arrow").length>0){
		$("#sciTech_con_arrow").click(function(){
			var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("sciTech_con_box");
			}else{
				return;
			}
		});
	}
	/*
		creating the click function for award arrow
	*/
	if($("#awards_con_arrow").length>0){
		$("#awards_con_arrow").click(function(){
			var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("awards_con_box");
			}else{	
				return;
			}
		});
	}
	/*
		creating the click function for people arrow
	*/
	if($("#people_con_arrow").length>0){
		$("#people_con_arrow").click(function(){
			var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("people_con_box");
			}else{
				return;
			}
		});
	}
	/*
		creating the click function for meeting arrow
	*/
	if($("#meetings_con_arrow").length>0){
		$("#meetings_con_arrow").click(function(){
			var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("meetings_con_box");
			}else{
				return;
			}
		});
	}
	/*
		creating the click function for obituaries arrow
	*/
	if($("#obituaries_con_arrow").length>0){
		$("#obituaries_con_arrow").click(function(){
			var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("obituaries_con_box");
			}else{	
				return;
			}
		});
	}
	
	if($("#letters_con_arrow").length>0){
		$("#letters_con_arrow").click(function(){
			var src=$(this).attr("src");
			var whichFuncs=swapDropDownImage(this,src);
			if(whichFuncs !=null){
				whichFuncs("letters_con_box");
			}else{	
				return;
			}
		});
	}
	
});
/*
	a function to swapping the up and down arrow
	@param: curImage - object of the current image to swap
	@param: src - if it is up image swap to down image or vice versa
	return type: either null or a function that will accept an arg of the element id and the function will act according
*/
function swapDropDownImage(curImage,src){
	var whichFuncs=null;
	if(curImage==null || src==""){
		return;
	}
	if(src=="/etc/designs/cen/images/arrow_right_light_blue.png"){
		$(curImage).attr("src","/etc/designs/cen/images/arrow_down_light_blue.png");
		whichFuncs=function(ele){
			$("#"+ele).slideDown("slow");
		}
		return whichFuncs;
	}else{
		$(curImage).attr("src","/etc/designs/cen/images/arrow_right_light_blue.png");
		whichFuncs=function(ele){
			$("#"+ele).slideUp("slow");
		}
		return whichFuncs;
	}
}

function showTab(type, mainId){
	switch(type){
		case 'latest_news_tab':
			$('#latest_news_outer').removeClass('x-hide-display');
			$('#news_feeds_outer').addClass('x-hide-display');
			break;
		case 'news_feeds_tab':
			$('#latest_news_outer').addClass('x-hide-display');
			$('#news_feeds_outer').removeClass('x-hide-display');
			break;
		case 'central_science_tab':
			$('#central_science').removeClass('x-hide-display');
			$('#blog_authors').addClass('x-hide-display');
			break;
		case 'blog_authors_tab':
			$('#central_science').addClass('x-hide-display');
			$('#blog_authors').removeClass('x-hide-display');
			break;
		case "logging_tab_completed":
			$("#error_container").addClass('x-hide-display');
			$("#completed_container").removeClass('x-hide-display');
			break;
			
		case "logging_tab_error":
			$("#completed_container").addClass('x-hide-display');
			$("#error_container").removeClass('x-hide-display');
			break;
			
		case 'main':
			$('#' + mainId).siblings().attr('class', 'x-hide-display');
			$('#' + mainId).removeClass('x-hide-display');
			break;
		default:
			var tabbox_view = '#popular_' + type;
			$(tabbox_view).removeClass('x-hide-display');
			$(tabbox_view).siblings().attr('class','x-hide-display');
			break;
	}
}

function dynamicGenerateCopyRightYear(id){
	if($("#"+id).length>0){
		// this mean the element exist
		var d=new Date();
		$('#'+id).text(d.getFullYear());
	}
}



