//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;
    },

    writeToLog:function(message)
    {
        try{console.debug(message);}catch(exception){}
    },

    getCaption:function(logicalName,defaultCaption)
    {
        var caption="["+logicalName+"]";
        var captionFound=false;
        //Uses a global variable captionTable that will hold captions loaded through xsl
        if(!Object.isUndefined(captionHash) && captionHash!=null && captionHash instanceof Hash)
        {
            var captionInHash=captionHash.get(logicalName);
            if(captionInHash!=null)
            {
                caption=captionInHash;
                captionFound=true;
            }
        }
        if(!captionFound && !Object.isUndefined(defaultCaption))
            return defaultCaption;

        return caption;
    },

    //Resizes the window so that the document matches the new dimensions
    resizeWindowEnhanced:function(newInnerWidth,newInnerHeight,considerScrollbarsOffset)
    {
        try{
        var decorationOffsetWidth=window.outerWidth-window.innerWidth;
        var decorationOffsetHeight=window.outerHeight-window.innerHeight;
        var scrollbarOffset=(considerScrollbarsOffset)?20:0;

        window.resizeTo(scrollbarOffset+newInnerWidth+decorationOffsetWidth,scrollbarOffset+newInnerHeight+decorationOffsetHeight);
        }catch(ex){}
    }
}

);