/***
Filename:       base.js
Date:           01/12/2007
Author:         James Condliffe
Description:    Global behaviours for Nisa Members site
***/

/***
Add class 'js' to body element as soon as possible
***/
(function()
{
    YAHOO.util.Event.onAvailable("nisamembers", applyJSClass);
    
    function applyJSClass()
    {
        YAHOO.util.Dom.addClass(YAHOO.util.Dom.get("nisamembers"), "js");
    }
        
})();

/***
Rollover images
***/
(function()
{
    YAHOO.util.Event.onDOMReady(init);

    function init()
    {
        var rollovers;
        
    	// Images can be either img or input elements
	    rollovers = YAHOO.util.Dom.getElementsByClassName("rollover", "img", document);
	    rollovers = rollovers.concat(YAHOO.util.Dom.getElementsByClassName("rollover", "input", document));
	
	    for (var i=0; i<rollovers.length; i++)
	    {
		    // Add the event handlers
		    YAHOO.util.Event.addListener(rollovers[i], "mouseover", roll);
		    YAHOO.util.Event.addListener(rollovers[i], "mouseout", roll);

		    // TODO: Image preloads
	    }    
    }
    
    function roll(e)
    {
        if(e.type == "mouseover")
        {
            this.src = this.src.replace( /^(.*)(\.gif|\.jpg|\.png)$/ , "$1"+"_roll"+"$2" );
        }
        else // remove _roll suffix
        {
            this.src = this.src.replace( /_roll/i , "" );
        }
    }

})();

/***
Open PDFs in new window
***/
(function()
{
    YAHOO.util.Event.onDOMReady(init);

    function init()
    {
        // Avoid applying this on the documents panel
        if(document.getElementById("panel6"))
        {
			// return; - old line;
			//return false;		// line ammended on 31/03/2008 by Andrei Vais
        }
        
	    // Grab our current base hostname
	    var baseHref;
	    var regexp = /(https?:\/\/)([^\/]+)/i;
	    matches = regexp.exec(window.location);	
	    baseHref = matches[2];
	    var pageBaseHRef = document.getElementsByTagName('base');
	    //alert("basehref: " + baseHref +"\nwindow.location: " + window.location +"\npageBaseHRef: " + pageBaseHRef[0].getAttribute("href"));
				    
	    // Grab the links	
	    var docLinks = document.getElementsByTagName("a");
	    	    
	    // Loop through the links
	    for (var i=0; i<docLinks.length; i++)
	    {
		    var anchor = docLinks[i];
		    
		    if (anchor.getAttribute("href")) // if it's a link
		    {
			    var href = anchor.getAttribute("href");
			    
			    if ((href.indexOf(".pdf") > -1) || (href.indexOf(".doc") > -1) || (href.indexOf(".txt") > -1))
			    {
			        anchor.onclick = function(){window.open(pageBaseHRef[0].getAttribute("href") + this.getAttribute('href').replace(pageBaseHRef[0].getAttribute("href"), "")); return false;};
			        
				    var tooltip = "";
				    if(anchor.getAttribute("title"))
				    {
				        tooltip = anchor.getAttribute("title") + " ";
				    }				    
				    tooltip += "(opens in new window)";
				    anchor.setAttribute("title", tooltip);
			    }
		    }
	    }
    }

})();


/***
Swap
***/
(function()
{
    YAHOO.util.Event.onDOMReady(init);

    function init()
    {
        // Avoid applying this on the documents panel
        if(document.getElementById("panel2"))
        {
        }
        
	    if(document.getElementById('latest-news'))
        {
            var newsUL = document.getElementById('latest-news');
            var picDiv = document.createElement('div');
            picDiv.setAttribute('id','storyBigPic');
            YAHOO.util.Dom.addClass(newsUL,'withLeftPhoto');
            newsUL.parentNode.insertBefore(picDiv,newsUL);
            
			var latestNewsLinks = document.getElementById("latest-news").getElementsByTagName("a");
			//var NewsItemImage = document.getElementById("NewsItemImage").firstChild;
			//var NewsItemImage_DefaultSRC = NewsItemImage.getAttribute("src");
			
			
			for (var i=0; i<latestNewsLinks.length; i++)
			{
				latestNewsLinks[i].onmouseover = function()
				{
					if(document.getElementById('storyBigPic').hasChildNodes())
					{
					    document.getElementById('storyBigPic').childNodes[0].setAttribute('src',this.childNodes[0].getAttribute('src'));
					}
					else//append it first
					{
					    var bigPic = document.createElement('img');
					    bigPic.setAttribute('alt', this.childNodes[0].getAttribute('title'));
					    bigPic.setAttribute('src',  this.childNodes[0].getAttribute('src')); 
					    document.getElementById('storyBigPic').appendChild(bigPic);
					}
					
					//remove all on classes and add only to hovered
					for (var j=0; j<latestNewsLinks.length; j++)
					{
					    YAHOO.util.Dom.removeClass(latestNewsLinks[j].parentNode,'on');
					}
					YAHOO.util.Dom.addClass(this.parentNode,'on');
					
				};
				
				if(i==0)
				{
				    var bigPic = document.createElement('img');
					bigPic.setAttribute('alt','testing alt');
					bigPic.setAttribute('src',  latestNewsLinks[i].childNodes[0].getAttribute('src')); 
					document.getElementById('storyBigPic').appendChild(bigPic);
					YAHOO.util.Dom.addClass(latestNewsLinks[i],'on');
				}

				latestNewsLinks[i].onmouseout = function()
				{
					//NewsItemImage.setAttribute("src", NewsItemImage_DefaultSRC);
					//YAHOO.util.Dom.removeClass(this,'on');
				};
			}

        }

    }

})();


/***
Resize eligible iframes
***/
(function()
{
    YAHOO.util.Event.onDOMReady(init);
    
    var dom = YAHOO.util.Dom;
    var ev = YAHOO.util.Event;
    
    function init()
    {
        var iFrames = dom.getElementsByClassName("dynamicheight", "iframe", document);
                
        for(var i = 0; i < iFrames.length; i++)
        {
            resize(iFrames[i], frames[iFrames[i].id].document);
        }
    }
    
    function resize(o, doc)
    {        
        dom.setStyle(o, "height", doc.body.scrollHeight + 50 + "px");
    }

})();


/***
Filter for COSHH documents
***/
(function()
{
    YAHOO.util.Event.onDOMReady(init);
    
    var util = YAHOO.util;
    
    var list;
    var listItems;
    
    function init()
    {
        var field = util.Dom.get("coshh-filter");
        list = util.Dom.get("coshh-docs");        
                
        if(!field || !list)
        {
            return;
        }
        
        listItems = list.getElementsByTagName("li");
        
        util.Event.addListener(field, "keyup", doFilter);
    }
    
    function doFilter(e)
    {        
        var searchVal = util.Dom.get("filter").value.toLowerCase();
        
        for(var i=0; i<listItems.length; i++)
        {            
            if((listItems[i].innerHTML.toLowerCase()).indexOf(searchVal) == -1)
            {
                util.Dom.addClass(listItems[i], "hidden");
            }
            else
            {
                util.Dom.removeClass(listItems[i], "hidden");
            }
        }
    }

})();


/***
Toggle My Consortium Box text
***/
(function()
{
    YAHOO.util.Event.onDOMReady(init);
    
    var dom = YAHOO.util.Dom;
    var ev = YAHOO.util.Event;
    
    function init()
    {
        if(document.getElementById("panel23"))
        {
			var consortium = YAHOO.util.Dom.getElementsByClassName("content", "div", document.getElementById("panel23"));

			for (var i=0; i<consortium.length; i++)
			{
				// Add the event handlers
				YAHOO.util.Event.addListener(consortium[i], "mouseover", toggleMe);
				YAHOO.util.Event.addListener(consortium[i], "mouseout", toggleMe);
			}    
        }
    }

	function toggleMe()
	{
		//alert("toggleMe");
		var consortiumText = YAHOO.util.Dom.getElementsByClassName("visitButton", "p", document.getElementById("panel23"));

		//alert(consortiumText.length);
		for (var i=0; i<consortiumText.length; i++)
		{
			if (YAHOO.util.Dom.getStyle(consortiumText[i], 'display') == 'none')
			{
				YAHOO.util.Dom.setStyle(consortiumText[i], 'display', 'block');
			}
			else
			{
				YAHOO.util.Dom.setStyle(consortiumText[i], 'display', 'none');
			}
		}
	}

})();

/* Set up SPEDI countdown */
/*(function()
{
    if(document.getElementById('CountdownClock'))
    {
        YAHOO.util.Event.onDOMReady(init);
    }
    var u = YAHOO.util;
    
    var EndDate = new Date("06/30/2010 00:00 AM");
    var DisplayDays = u.Dom.get("Days");
    var DisplayHours = u.Dom.get("Hours");
    var DisplayMinutes  = u.Dom.get("Minutes");

    function init()
    {        
        if (!DisplayDays || !DisplayHours || !DisplayMinutes)
        {
            return false;
        }
        Countdown();
    }
    
    function Countdown()
    {      
        var StartDate = new Date();
        var DateDiff = EndDate-StartDate;
        if(StartDate.getTimezoneOffset() != EndDate.getTimezoneOffset())
        {
            DateDiff=DateDiff+(StartDate.getTimezoneOffset()*60*1000);
        }
        var DateDiff_WholeDays = Math.floor(DateDiff/1000/3600/24);
        var DateDiff_WholeHours =  Math.floor((DateDiff - (DateDiff_WholeDays*24*3600*1000))/1000/3600);
        var DateDiff_WholeMinutes =  Math.floor((DateDiff - (DateDiff_WholeDays*24*3600*1000) - (DateDiff_WholeHours*3600*1000))/60/1000);

        if (DateDiff == 0)
        {
            //alert("SPEDI system is now closed");
        }
        else
        {
            DisplayDays.firstChild.nodeValue = DateDiff_WholeDays;
            DisplayHours.firstChild.nodeValue = DateDiff_WholeHours;
            DisplayMinutes.firstChild.nodeValue = DateDiff_WholeMinutes;
            //console.log(DateDiff_WholeDays + " : " + DateDiff_WholeHours + " : " + DateDiff_WholeMinutes)
            setTimeout(Countdown,60000);
        }
    }
    
    function calcremain(secs, num1, num2) {
      var s = ((Math.floor(secs/num1))%num2).toString();
      if (s.length < 2)
        s = "0" + s;
      return s;
    }


})();*/