//Needs prototype
var UtilityService=Class.create(
{
    initialize:function()
    {},

    getElementFromResponse:function(response,attr,attrValue)
    {// [attr=attrValue works as a selector]
        var tempE=new Element("div");
        tempE.update(response.responseText.stripScripts());
        var eMatches=tempE.select("["+attr+"='"+attrValue+"']");
        if(eMatches.size()>0)
            return eMatches.first();
        else
            return null;
    },

    //Interface effects
    fadeIn:function(element,duration)
    {//time in seconds
        if(duration<=0)
            return;

        var fPS=10;//frequency/updates per second
        var frequency=1/fPS;
        var nrOfExecs=duration*fPS*1.0;
        var increase=1/nrOfExecs;

        element.setOpacity(0.0);
        element.show();

        var periodicalExecuter=new PeriodicalExecuter(
           function()
           {
               var stopPE=UtilityService.prototype.execFadeIn(element,increase);
               if(stopPE)
               {
                   periodicalExecuter.stop();
               }
           }.bind(this,periodicalExecuter,element,increase)
           ,frequency);
    },

    execFadeIn:function(element,increase)
    {
        var oldOpacity=element.getStyle("opacity");
        oldOpacity=oldOpacity*1.0;
        var newOpacity=oldOpacity+increase;
        var stopPE=false;

        //to control overflow
        if(newOpacity>1.0)
        {
            newOpacity=1.0;
            stopPE=true;
        }

        element.setOpacity(newOpacity);
        return stopPE;
    },

    fadeOut:function(element,duration,removeAfterFade)
    {//time in seconds
        if(duration<=0)
        {
            element.remove();
            return;
        }
        var fPS=10;//frequency/updates per second
        var frequency=1/fPS;
        var nrOfExecs=duration*fPS*1.0;
        var diff=-1/nrOfExecs;

        element.setOpacity(1.0);
        var periodicalExecuter=new PeriodicalExecuter(
           function()
           {
               var stopPE=UtilityService.prototype.execFadeOut(element,diff);
               if(stopPE)
               {
                   element.hide();
                   periodicalExecuter.stop();
                   if(removeAfterFade)
                   {
                       element.remove();
                   }
               }

           }.bind(this,periodicalExecuter,element,diff)
           ,frequency);
    },

    execFadeOut:function(element,diff)
    {
        var oldOpacity=element.getStyle("opacity");
        oldOpacity=oldOpacity*1.0;
        var newOpacity=oldOpacity+diff;
        var stopPE=false;

        //to control overflow
        if(newOpacity<0.0)
        {
            newOpacity=0.0;
            stopPE=true;
        }

        element.setOpacity(newOpacity);
        return stopPE;
    },

    getNodeValue: function(tree, el){
      return tree.getElementsByTagName(el)[0].firstChild.nodeValue;
    },

    getFirstMatching:function(element,attr,attrValue)
    {
        element=$(element);
//        if(element instanceof HTMLDocument)
//           element=$(element.body);
        if(element == document)
           element=$(element.body);
   
        var eMatches=element.select("["+attr+"='"+attrValue+"']");
        if(eMatches.size()>0)
            return eMatches.first()
        else
            return null;
    }   
}

);