// Basic functionality such as printing and bookmarking...

$(document).ready(function() {
	$("a#print_page").click(function() {
		window.print();
		return false;
	});
	
	$("a#bookmark_link").click(function() {
		//var url = 'http://choice.ichameleon.com';
		var url = location.href;
		var title = document.title;
		//var title = 'CSIP: Choice Medicines';
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title); }
		else if(window.opera && window.print) { // Opera Hotlist
			return true; 
		}
	});
	
	// Thi sneeds updating to maybe use the title attribute of the link (just the term) so that each instance of the pop-up can be unique
	$("a.glossary").mouseover(function(e) {
		var tempX = 0;
		var tempY = 0;	
		var elementHeight = 0;
		var elementWidth = 0;
		var thisTerm = $(this).attr("title");
		var thisDiv = "div#"+thisTerm;
		var position = $(this).position();
		//alert(position['left']);
		
		elementWidth = $(this).width();
		elementHeight = $(this).height();

		//tempX = (e.pageX-(elementWidth+(elementWidth/2)));
		tempX = e.pageX-60;//position['left']-(elementWidth*2);
		tempY = e.pageY//position['top']+elementHeight;
		
		if (tempX < 0){tempX = 0}
		if (tempY < 0){tempY = 0}

		$(thisDiv).css("top",tempY + 'px');
		$(thisDiv).css("left",tempX + 'px');
		$(thisDiv).offset(position);
		$(thisDiv).css("display","block");

		$(thisDiv).mouseover(function(){
			$(thisDiv).css("display","block");
		});
		$(thisDiv).mouseout(function(){
			$(thisDiv).css("display","none");
		});
		$(this).mouseout(function(){
			$(thisDiv).css("display","none");
		});
		return false;
	});
	

	// automate shortcuts
	$("select.conditions").change(function(){
		$("select.conditions option:selected").each( function(){
			if( $(this).val() > 0 ){
				document.location = "/"+ $('input#local').val() +"/conditions/"+ $(this).val() +"/";
			}
		});
	});
	$("select.meds").change(function(){
		$("select.meds option:selected").each( function(){
			if( $(this).val() > 0 ){
				document.location = "/"+ $('input#local').val() +"/medications/"+ $(this).val() +"/";
			}
		});
	});
	// admin shortcuts
	$("select.adminconditions").change(function(){
		$("select.adminconditions option:selected").each( function(){
			if( $(this).val() > 0 ){
				//document.location = "/"+ $('input#local').val() +"/conditions/"+ $(this).val() +"/";
				document.location = "/admin/object/?action=edit&type=condition&id="+ $(this).val();
			}
		});
	});
	$("select.adminclass").change(function(){
		$("select.adminclass option:selected").each( function(){
			if( $(this).val() > 0 ){
				//document.location = "/"+ $('input#local').val() +"/conditions/"+ $(this).val() +"/";
				document.location = "/admin/object/?action=edit&type=class&id="+ $(this).val();
			}
		});
	});
	$("select.adminmeds").change(function(){
		$("select.adminmeds option:selected").each( function(){
			if( $(this).val() > 0 ){
				//document.location = "/"+ $('input#local').val() +"/medications/"+ $(this).val() +"/";
				document.location = "/admin/object/?action=edit&type=medication&id="+ $(this).val();
			}
		});
	});
	$("select.admingloss").change(function(){
		$("select.admingloss option:selected").each( function(){
			if( $(this).val().length > 0 ){
				//document.location = "/"+ $('input#local').val() +"/medications/"+ $(this).val() +"/";
				document.location = "/admin/object/?action=edit&type=glossary&id="+ $(this).val();
			}
		});
	});
	
	
	// clear search boxes when clicked on...
	$("input#main_keywords").focus(function(){
		if( $("input#main_keywords").val()=='Enter keywords' ){
			$("input#main_keywords").val('');		
		}
	});
	$("input#main_keywords").blur(function(){
		if( $("input#main_keywords").val()<='' ){
			$("input#main_keywords").val('Enter keywords');
		}
	});
	$("input#keywords").focus(function(){
		if( $("input#keywords").val()=='Enter keywords' ){
			$("input#keywords").val('');							
		}
	});	
	$("input#keywords").blur(function(){
		if( $("input#keywords").val()<='' ){
			$("input#keywords").val('Enter keywords');
		}
	});	
	
});