/* Generic Javascript file, called by *all* CMS pages */

//Custom window opening routine, for opening a screen-centred window of a given width and height at a given URL.  This window is barebones/no-frills.
function contentPopup( w, h, url ) {
    //get monitor resolution
        var sw = screen.width;
        var sh = screen.height;
    //define coordinates to open at
        var leftpos = (sw / 2) - (w / 2);
        var toppos = (sh / 2) - (h / 2);
    //Open the window:
    //Note: IE7 produces an error at "moveTo()" - therefore revised implementation is to specify "left=" and "top=" in the window.open() args below:-
    window.open( url, 'CFRSContentPopupWindow', 'width='+w+',height='+h+'status=no,toolbar=no,menubar=no,resizable=yes,location=no,scrollbars=yes,left='+leftpos+',top='+toppos ); //some browsers are set to not let JavaScipt dictate the appearance of screen elements like the status bar - consider this when allocating sizing for the media file/window!
    return false;
}
 

        function newWin_addEvent(obj, type, fn) {
            if (obj.attachEvent) {
                obj['e' + type + fn] = fn;
                obj[type + fn] = function() { obj['e'+type+fn](window.event); }
                obj.attachEvent('on' + type, obj[type + fn]);
            } else
                obj.addEventListener(type, fn, false);
        }
        function newWinLinks(){
           var thishost = location.hostname;
           var links = document.getElementsByTagName('A');
           for( i=0; i<links.length; i++ ) {

              // don't check remaining conditions where rel="media" or rel="external",
              // this is a manually forced action:
              if( (links[i].rel!='media') && (links[i].rel!='external') ) {

                 //retrieve the hostname part of the url:
                 linkHostname = links[i].hostname.substr(0, links[i].hostname.indexOf(':'));

                 //some browsers ignore the protocol, so double check here:
                 if( linkHostname=='' ){
                    linkHostname=links[i].hostname; //back to original state.
                 }

                 // integrity check for rel settings - older safari versions seem to need it:
                 if( (links[i].rel!='media') && (links[i].rel!="external") ) {
                    // conditional checks go here - matching these conditions meens same window:
                    // hostname is unretrievable - shouldn't be encountered but prevents possible
                    // error on next condition...
                    if( linkHostname=='' ) {
                       continue;
                    }
                    else if( linkHostname==thishost ) { // if this is the same domain no new window.
                       continue;
                    }
                    else if( links[i].protocol=='mailto:' ) { // don't add new window link for mailto links.
                       continue;
                    }
                    else if( links[i].rel=='alternate' ) { // don't add new window links for alternate versions (same page)
                       continue;
                    }
                    else if( links[i].rel=='internal' ) { // manual force no new window link
                       continue;
                    }
                        
                    
                 }
              }

              //If we've got this far we are ready to make the current link open in a new window:
              links[i].target='_blank';

              //Give the link a tooltip (or append existing one):
              if( links[i].title!='' ) {
                 links[i].title += ' (opens in a new window)';
              }
              else {
                 links[i].title = 'opens in a new window';
              }

              //Add the "new window" icon to the end of the link - but only if the anchor doesn't contain an image already:
              if( links[i].getElementsByTagName('IMG').length<=0 ) {
                 var img = document.createElement("img");
                     
                 img.setAttribute( "src", "http://www.cambsfire.gov.uk/images/style/new-win-icon.gif" );
                     
                 img.setAttribute( "alt", "(opens in a new window)" );
                 img.style.verticalAlign = "middle";
                     
                 links[i].appendChild( img );
              }

           } //end for() through links.
        } //end func newWinLinks.

        //This function gets called as soon as this JS file is sourced in the HTML head - but it creates a window.onload() function that then calls the above function:
        newWin_addEvent(window, 'load', newWinLinks);