﻿// ipac_custom.js
// various iPAC custom functions
// documentation for using balloon popups  http://www.wormbase.org/wiki/index.php/Balloon_Tooltips

	//default balloon initialization code
	var isBalloon = false;
    var balloon             = new Balloon;
    balloon.minWidth		= 400;
    balloon.maxWidth		= 608;
    balloon.balloonTextSize = '80%';
    balloon.delayTime       = 250;
    balloon.balloonImage    = '/images/blue_balloons/balloon.png'
    balloon.shadow			= 20;
    balloon.padding			= 30;
    balloon.upLeftStem    = '/images/blue_balloons/up_left.png';
	balloon.downLeftStem  = '/images/blue_balloons/down_left.png';
	balloon.upRightStem   = '/images/blue_balloons/up_right.png';
	balloon.downRightStem = '/images/blue_balloons/down_right.png';
	balloon.stemHeight  = 30;
	balloon.stemOverlap = -1;
	balloon.closeButton   = '/images/blue_balloons/close.png';
	//----------------
	
	// gets URL from the browser's address bar
	function getURL()
	{
	    if (ipacURL!="")
	        return ipacURL;
	    var url = window.location.href;
		var pos = url.indexOf("http://");
		if (pos==0) {
			pos = url.indexOf("/", pos+7);
			if (pos)
				url = url.substring(pos, url.length)			
		}
		return url;
	}
	
	var CurrUrl = getURL();
	//----------------
	
	// redirects to spell check page
	function StartSpelling() { window.location.href='/4DACTION/web_SpellCheck/Bookbag=' + Bookbag; }

    // handles easy search button click --- obsolete as button click submits the form 
    function EasySearch(tbID)
    {
        el = document.getElementById(tbID);
        window.location.href='/4DCGI/Gen_2002/EasySearch/q='+el.value + "/Lang=En/Bookbag=" + Bookbag;
    }
    
    // handles key presses inside Easy Search and detects enter key -- obsolete as these fields handled by the form
    function checkEnter(el, e, action) 
    {
        if (e.keyCode == 13) { 
            switch(action)
            {
                case "GoPage":
					SubmitWithParameter("GoPage", el.value);
                    break;
                case "EasySearch":
                    EasySearch(e.srcElement.id);
                    break;
            }
            return false;
        }
    }
    
    // handles onmouseover and click for the iPAC Settings button (click is handled by the form now)
    function Preferences(e, target)
    {
		if (target=="balloon")
		{
			isBalloon = true;
			var whiteBalloon = new Balloon;
			whiteBalloon.delayTime = 500;
			whiteBalloon.showTooltip(e,"url:/4daction/web_Preferences/Balloon/BookBag=" + Bookbag,1,300);
		}
		else
			window.location.href="/4daction/web_Preferences/ReturnUrl='" + CurrUrl + "'/BookBag=" + Bookbag;
    }
    
    // same as above function, diferent setings page, added for testing new interface --- currently not used
    function SearchPreferences(e, target)
    {
    		isBalloon = true;
			var whiteBalloon = new Balloon;
			whiteBalloon.delayTime = 500;
			whiteBalloon.showTooltip(e,"url:/4daction/web_SearchPreferences/Balloon/BookBag=" + Bookbag,1,300);
    }
    
    // refreshes current page with added parameter, used by iPAC Settings in balloon popup
    function SubmitWithParameter(Param, Value)
    {
		var url=ReplaceInURL(CurrUrl, Param, Value);
		if(Bookbag!="")
			url=ReplaceInURL(url, "BookBag", Bookbag);
		window.location.href=url;
    }
    
    // helper function to replace value of a parameter in an URL 
    function ReplaceInURL(url, name, value)
    {
		var pos1 = url.toLocaleLowerCase().indexOf(name.toLocaleLowerCase() + "=", 0);
		if (pos1>=0) {
			var pos2 = url.indexOf("/", pos1);
			if (pos2>=0)
				url = url.substring(0, pos1) + name + "=" + value + url.substring(pos2, url.length);
			else
				url = url.substring(0, pos1) + name + "=" + value;
		}
		else
			url += "/" + name + "=" + value;
			
		return url;
    }
    
    // handles clicks on an option in iPAC Settings in balloon popup
    function OptionClicked(el)
	{
		if (isBalloon) {
			SubmitWithParameter(el.name,el.value);
		}
	}
	
	// shows the default balloon popup, used when hovering over Title link or Book cover in search results.
	function RecordDetailsBalloon(e,url)
	{
	    if (BallonOn=="On")
	        balloon.showTooltip(e,'url:' + url + '/Display=balloon',1);
	}
