// JavaScript Document

$(document).ready(function() {

	//ALL PAGES//
	//Search form on top of each page
	$('#search_button').click(function(){
		if($('#pubmed').attr('checked')=='checked'){
			$('#site').attr('checked', 'false');
			var form_url = 'http://www.ncbi.nlm.nih.gov/entrez/query.fcgi';
			$('#pubmed').attr('name', 'db');
			$('#input').attr('name', 'term');
			$("#search_form").attr("action", form_url).submit();
		}else if($('#site').attr('checked')){
			var form_url = 'http://www.google.com/search';
			$('#site').attr('name', 'sitesearch');
			$("#search_form").attr("action", form_url).submit();
		}		
	});
	
	$('#input').bind('keypress', function(e) {
        if(e.keyCode==13){
			if($('#pubmed').attr('checked')){
				$('#site').attr('checked', 'false');
				var form_url = 'http://www.ncbi.nlm.nih.gov/entrez/query.fcgi';
				$('#pubmed').attr('name', 'db');
				$('#input').attr('name', 'term');
				$("#search_form").attr("action", form_url).submit();
			}else if($('#site').attr('checked')){
				var form_url = 'http://www.google.com/search';
				$('#site').attr('name', 'sitesearch');
				$("#search_form").attr("action", form_url).submit();
			}
			return false;
        }
	});
	
	//Header image links to home page on every page
	$('#header').click(function(){ document.location.href='http://www.accelmed.org/index.php'; });
	
	//After Login on any page, return to the page you were currently on before login
	var return_page = location.href.split('/').pop();
	$('#return_page').attr('value', return_page);

	//Buttons: when pressed, add class pressed
	//$('.button').mousedown(function(){
		//$(this).addClass('pressed');
	//});
	//$('.button').mouseup(function(){
	//	$(this).removeClass('pressed');
	//});
	$('.ddl').mousedown(function(){
		$(this).addClass('ddl_pressed');
	});
	$('.ddl').mouseup(function(){
		$(this).removeClass('ddl_pressed');
	});
	

//INDEX PAGE//
//Index page Logged in Poll area
$('#close').click(function(){
	$('#poll_area').hide();
	var sid = $('.survey_id').html();
	var uid = $('.user_id').html();
	$.post("survey_optout.php", { uid: uid, sid: sid });
	$('#myarea').show();
});

//Index Page Program list toggle
var page = window.location.pathname;
page = page.replace(".php","");
page = page.replace("/","");
$.post("program_list.php", { type: 'all', page: page },
	function(data){
		$('#program_list').html('');
		$('#program_list').append(data);
	});
	$('#all_progs').addClass('selected');
	$('#all').attr('checked', 'checked');
$('#all_progs').click(function(){
	$.post("program_list.php", { type: 'all', page: page },
		function(data){
			$('#program_list').html('');
			$('#program_list').append(data);
		});
		$('#audioweb_progs').removeClass('selected');
		$('#live_progs').removeClass('selected');
		$(this).addClass('selected');
		$('#all').attr('checked', 'checked');
});
$('#audioweb_progs').click(function(){
	$.post("program_list.php", { type: 'audioweb', page: page },
		function(data){
			$('#program_list').html('');
			$('#program_list').append(data);
		});
		$('#all_progs').removeClass('selected');
		$('#live_progs').removeClass('selected');
		$('#audioweb_progs').addClass('selected');
		$('#all').attr('checked', false);
		$('input#audioweb').attr('checked', 'checked');
});
$('#live_progs').click(function(){
	$.post("program_list.php", { type: 'live', page: page },
		function(data){
			$('#program_list').html('');
			$('#program_list').append(data);
		});
		$('#audioweb_progs').removeClass('selected');
		$('#all_progs').removeClass('selected');
		$(this).addClass('selected');
		$('#live').attr('checked', 'checked');
});

////Index page Menu
//default state is under Programs tab
/* Old menu slider code
$('.arrow').css('left', 450);
	
$('#who').mouseover(function(){
	$('.arrow').animate({"left": "0"}, "fast");
	$('.arrow').css('left', 0);
});
$('#education').mouseover(function(){
	$('.arrow').animate({"left": "120px"}, "fast");	
	$('.arrow').css('left', 120);
});
$('#experience').mouseover(function(){
	$('.arrow').animate({"left": "265px"}, "fast");
	$('.arrow').css('left', 265);
});
$('#partners').mouseover(function(){
	$('.arrow').animate({"left": "370px"}, "fast");
	$('.arrow').css('left', 370);
});
$('#programs').mouseover(function(){
	$('.arrow').animate({"left": "450px"}, "fast");
	$('.arrow').css('left', 450);
});
*/

//Index Page Menu Bar change background color
$('#header li').mouseover(function(){
	$('#header li').each(function(){ $(this).removeClass('current_page_item'); });
	$(this).addClass('current_page_item');
});

//Index page Sub Menu
$('.sub-menu').hide();
$('#who').hover(function(){
	$('.sub-menu').hide();
	$('#who_sub').show();
});
$('#education').hover(function(){
	$('.sub-menu').hide();
	$('#education_sub').show();
});
$('#experience').hover(function(){
	$('.sub-menu').hide();
	$('#experience_sub').show();
});
$('#partners').hover(function(){
	$('.sub-menu').hide();
	$('#partners_sub').show();
});
$('#programs').hover(function(){
	$('.sub-menu').hide();
	$('#programs_sub').show();
});

//INDEX PAGE

	
//Calendar year change
var years = new Array();
var d = new Date();
years[d.getFullYear()]= d.getFullYear();
for(var i=1; i<10; i++){
	years[d.getFullYear()+i]= d.getFullYear()+i;
	years[d.getFullYear()-i]= d.getFullYear()-i;
}
$('#cal_arrow_right').click(function(){
	display_year = $('#cal_year').html();
	display_year = years[display_year]+1;
	$('#cal_year').html(display_year);
	$.post("calendar.php", { year: display_year, month: 1 },
		function(data){
			$('.events').html('');
			$('.events').append(data);
		});
	$('#calendar-body li').each(function(){
			$(this).removeClass('month-selected');
	});
	$('#calendar-body li#1').addClass('month-selected');
});

$('#cal_arrow_left').click(function(){
	display_year = $('#cal_year').html();
	display_year = years[display_year]-1;
	$('#cal_year').html(display_year);
	$.post("calendar.php", { year: display_year, month:1 },
		function(data){
			$('.events').html('');
			$('.events').append(data);
		});
	$('#calendar-body li').each(function(){
		$(this).removeClass('month-selected');
	});
	$('#calendar-body li#1').addClass('month-selected');
});

//PROGRAM PAGE//
//Faculty modal
$('.faculty').click(function(){
	var id = $(this).attr('id');
	var text_len = $( "#" + id + "_extra_info p").text().length;
	var modal_height = 350 + (((text_len-600)/110)*14);
	$( "#" + id + "_extra_info").show().css({'z-index' : '1000'});
	$( "#" + id + "_extra_info").animate({
		marginTop: '0', /* The next 4 lines will vertically align this image */ 
		marginLeft: '0',
		top: '30%',
		left: '25%',
		width: '600px', /* Set new width */
		//height: modal_height + 'px', /* Set new height */
		padding: '20'
		}, 800); /* this value of "200" is the speed of how fast/slow this hover animates */
});

//Program Page, Toggle System Requirements on right bar.
$('#requirements').hide();
$('#reqs').click(function(){
	$('#requirements').toggle();
	if($('#reqs span').html()=='+'){
		$('#reqs span').html('-');
	}else {
		$('#reqs span').html('+');
	}
});
//Program Page, Toggle Credit Instructions on right bar.
$('#credit_instructions').hide();
$('#cinstructions').click(function(){
	$('#credit_instructions').toggle();
	if($('#cinstructions span').html()=='+'){
		$('#cinstructions span').html('-');
	}else {
		$('#cinstructions span').html('+');
	}
});

function expandtoPrint(){ $('#requirements').show();$('#credit_instructions').show(); }
window.onbeforeprint=expandtoPrint;
function reverttoHidden(){ $('#requirements').hide();$('#credit_instructions').hide(); }
window.onafterprint=reverttoHidden;

//Program info box toggle
$('.info_header').click(function(){
	$('.info_body').toggle();
	if($('.info_header span').html()=='+'){
		$('.info_header span').html('-');
	}else {
		$('.info_header span').html('+');
	}
});

//Calendar and Social Media location redirects
$('#google_cal').click(function(){
	window.location = $(this).find('a').attr('href');
});
$('#ical').click(function(){
	window.location = $(this).find('a').attr('href');
});
$('#outlook').click(function(){
	window.location = $(this).find('a').attr('href');
});
$('#facebook').click(function(){
	function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}
	//fbs_click();
	window.location = $(this).find('a').attr('href');
	//window.location = $(this).find('a').attr('href');
});
$('#linkedin').click(function(){
	window.location = $(this).find('a').attr('href');
});
$('#twitter').click(function(){
	window.location = $(this).find('a').attr('href');
});
$('#googleplus').click(function(){
	
});
//Calendar and Social Media hover image swap
$('#google_cal').mouseover(function(){
	$(this).removeClass('google_cal');
	$(this).addClass('google_hover');
});
$('#google_cal').mouseout(function(){
		$(this).addClass('google_cal');
		$(this).removeClass('google_hover');
});
$('#ical').mouseover(function(){
	$(this).removeClass('ical');
	$(this).addClass('ical_hover');
});
$('#ical').mouseout(function(){
		$(this).addClass('ical');
		$(this).removeClass('ical_hover');
});
$('#outlook').mouseover(function(){
	$(this).removeClass('outlook');
	$(this).addClass('outlook_hover');
});
$('#outlook').mouseout(function(){
		$(this).addClass('outlook');
		$(this).removeClass('outlook_hover');
});
$('#facebook').mouseover(function(){
	$(this).removeClass('facebook');
	$(this).addClass('facebook_hover');
});
$('#facebook').mouseout(function(){
		$(this).addClass('facebook');
		$(this).removeClass('facebook_hover');
});
$('#linkedin').mouseover(function(){
	$(this).removeClass('linkedin');
	$(this).addClass('linkedin_hover');
});
$('#linkedin').mouseout(function(){
		$(this).addClass('linkedin');
		$(this).removeClass('linkedin_hover');
});
$('#twitter').mouseover(function(){
	$(this).removeClass('twitter');
	$(this).addClass('twitter_hover');
});
$('#twitter').mouseout(function(){
		$(this).addClass('twitter');
		$(this).removeClass('twitter_hover');
});
$('#googleplus').mouseover(function(){
	$(this).removeClass('googleplus');
	$(this).addClass('googleplus_hover');
});
$('#googleplus').mouseout(function(){
		$(this).addClass('googleplus');
		$(this).removeClass('googleplus_hover');
});

//CORPORATE PAGES//
//Left Menu Bar on Coporate Pages
	$('.left-menu ul.sub').hide();
	//find current page and add class 'selected' and make purple
	$('.left-menu ul.sub li').each(function(){
			var menulink = $(this).find('a').attr('href');
			//var filename = location.href.split('/').pop();
			var filename = location.href;
			filename = filename.split("?")[0];
			if(filename==menulink){
				$(this).find('a').addClass('selected');
				$(this).parent().show();
				$(this).parents('li').addClass('selected');
			}	
	});
	//Shows the sub section
	$('.left-menu li').click(function(){
		$(this).find('ul.sub').show();
	});


//Leadership Page image popups and overlay
function removeHeight() {
	$("ul.thumb li").each(function(){
		if( $(this).attr('class') != 'arrows'){
		$(this).css({'z-index' : '0'});
		$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
		.animate({
			marginTop: '0', /* Set alignment back to default */
			marginLeft: '0',
			top: '0',
			left: '0',
			width: '90px', /* Set width back to default */
			height: '120px', /* Set height back to default */
			padding: '0px'
		}, 400);
		}
	});
}

function picsSlide(action){
	if(action=='left'){
		$('.last').hide();
		$('.first').show();
	}else if(action=='right'){
		$('.first').hide();
		$('.last').show();
	}else if(action=='initial'){
		$('.last').hide();
	}
}

picsSlide('initial');
$('#right_arrow').mouseover(function(){
	//$('.first').hide();
	//$('.last').animate({opacity: 'toggle', width: 'toggle'}, 10);
	
	//$('.first').fadeOut(200, function(){
	//	$('.last').fadeIn(500);
	//});    

	picsSlide('right');
});
$('#left_arrow').mouseover(function(){
	//$('.last').hide();
	//$('.first').animate({opacity: 'toggle', width: 'toggle'}, 10);
	
	//$('.last').fadeOut(200, function(){
	//	$('.first').fadeIn(500);
	//});
	
	picsSlide('left');
});

function hideAll() {
	$('#tabs-1').hide();
	$('#tabs-2').hide();
	$('#tabs-3').hide();
	$('#tabs-4').hide();
	$('#tabs-5').hide();
	$('#tabs-6').hide();
	$('#tabs-7').hide();
	$('#tabs-8').hide();
	$('#tabs-9').hide();
}
hideAll();
$("ul.thumb li").mouseover(function() {
	removeHeight();
	if($(this).attr('class') != 'arrows'){
	$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
	$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
		.animate({
			marginTop: '-87px', /* The next 4 lines will vertically align this image */ 
			marginLeft: '-70px',
			top: '50%',
			left: '50%',
			width: '140px', /* Set new width */
			height: '180px', /* Set new height */
			padding: '0px'
		}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
		
		var id= $(this).find('a').attr("href"); hideAll(); $(id).show();
	}
});
$('body').click(function(){
	removeHeight();	
});


function staff_modal(id){
	alert('modal');
	$( "#" + id + "_extra_info").show().css({'z-index' : '1000'});
	$( "#" + id + "_extra_info").animate({
		marginTop: '0', /* The next 4 lines will vertically align this image */ 
		marginLeft: '0',
		top: '38%',
		left: '40%',
		width: '450px', /* Set new width */
		height: '575px', /* Set new height */
		padding: '20px'
		}, 800); /* this value of "200" is the speed of how fast/slow this hover animates */
}

$('.extra_info_modal').hide();
$('.staff_img').click(function(){
	$("ul.thumb li").each(function(){
		$(this).css({'z-index' : '0'});
	});
	$(this).parent().click(function(e) {
		e.preventDefault();
	});
	var href = $(this).parent().attr('href');
	$( href + "_extra_info").show().css({'z-index' : '1000'});
	$( href + "_extra_info").animate({
		marginTop: '0', /* The next 4 lines will vertically align this image */ 
		marginLeft: '0',
		top: '38%',
		left: '40%',
		width: '450px', /* Set new width */
		//height: '575px', /* Set new height */
		padding: '20px'
		}, 800); /* this value of "200" is the speed of how fast/slow this hover animates */
});

$('.staff').click(function(){
	$("ul.thumb li").each(function(){
		$(this).css({'z-index' : '0'});
	});
	var id = $(this).parent().parent().attr('id');
	$( "#" + id + "_extra_info").show().css({'z-index' : '1000'});
	$( "#" + id + "_extra_info").animate({
		marginTop: '0', /* The next 4 lines will vertically align this image */ 
		marginLeft: '0',
		top: '38%',
		left: '40%',
		width: '450px', /* Set new width */
		//height: '575px', /* Set new height */
		padding: '20px'
		}, 800); /* this value of "200" is the speed of how fast/slow this hover animates */
});
$('.extra_info_modal span.close').click(function(){
	$('.extra_info_modal').hide();
});

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


//Press Release Page
$('.press').click(function(){
	var id = $(this).parent().parent().attr('id');
	$( "#" + id + "_extra_info").show().css({'z-index' : '1000'});
	$( "#" + id + "_extra_info").animate({
		marginTop: '0', /* The next 4 lines will vertically align this image */ 
		marginLeft: '0',
		top: '-4%',
		left: '8%',
		width: '450px', /* Set new width */
		//height: '830px', /* Set new height */
		padding: '0'
		}, 800); /* this value of "200" is the speed of how fast/slow this hover animates */
});
//////////////////////
//Admin Left Menu Bar
$('#profile_menu li').each(function(){
	var menulink = $(this).find('a').attr('href');
	var filename = location.href;
	filename = filename.split("?")[0];
	if(filename==menulink){
		$(this).addClass('selected');
	}	
});

//Preferences Page


////


//Clinical Experience- Advisory Boards Form in Modal
$('#collaborate').click(function(){
	$("#dialog-collaborate-form").dialog( "open" );
});
//Clinical Experience- Advisory Boards Form in Modal
$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-collaborate-form" ).dialog({
		autoOpen: false,
		height: 500,
		width: 480,
		modal: true
	});
	
//Portfolio in a Modal
$('#portfolio').click(function(){
	$("#dialog-portfolio").dialog( "open" );
});
//Clinical Experience- Advisory Boards Form in Modal
$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-portfolio" ).dialog({
		autoOpen: false,
		height: 750,
		width: 920,
		modal: true
	});
	
	
//Innovative Education Overview
//Subject accordions
	$('.accordion p').click(function(){
		$('.accordion p').each(function(){
			$(this).next().hide();
			if($(this).next().is(":visible")){
				$(this).find('span').html('-');
			}else {
				$(this).find('span').html('+');
			}
		});
		$(this).next().toggle('slow');
		if($(this).next().is(":visible")){
			$(this).find('span').html('-');
		}else {
			$(this).find('span').html('+');
		}
		return false;
	}).next().hide();
	$('.accordion p').first().next().toggle();	
	
});


function ValidateLogin(theForm){
  if (theForm.username.value == "")
  {
    alert("Please enter a value for the \"User Name\" field.");
    theForm.username.focus();
    return (false);
  }
  if (theForm.password.value == "")
  {
    alert("Please enter a value for the \"Password\" field.");
    theForm.password.focus();
    return (false);
  }
   return (true);
}

