var utils = {};
utils.cookie = (function() {
    return {
      get : function ( cookieName ) {
        if ( ! cookieName ) { return cookieName; }
        var _val = ''
          , _all = document.cookie
          , _cookie = _all.indexOf(cookieName+'=');
        if( _cookie > -1 ) {
          var _start = _cookie+cookieName.length
            , _end = _all.indexOf(';', _start);
          _val = ( _end == -1 ) ? _all.substring(_start+1) : _all.substring(_start+1, _end);
        }
        return _val;
      },
      set : function ( cookieName, value, opts ) {
        if ( ! cookieName.length ) { return false; }

        var date = new Date();
        date.setTime(date.getTime()+( (opts.expires || 100 ) *24*60*60*1000));
        var expires = date.toGMTString();
        var cookie = cookieName
          , opts = opts || {}
          , val = value || ''
          , domain = opts.domain ? '; domain='+opts.domain : ''
          , path = opts.path || '/';
        document.cookie = cookie+ '=' +val+ '; expires=' +expires + domain+ '; path=' +path;
        return true;
      },
      remove : function ( cookieName, opts ) {
        if ( ! cookieName.length ) { return false; }
        var cookie = cookieName
          , opts = opts || {}
          , domain = opts.domain ? '; domain='+opts.domain : ''
          , path = opts.path || '/';
        document.cookie = cookie+ '=""; expires=Thu, 01-Jan-1970 00:00:01 GMT' +domain+ '; path=' +path;
        return true;
      }
    };
}());

/* ROTATE DEPPING PRESENTER INFO */
function rotateDepInfo() {
  if ( oaRotateTitle.active ) {
    oaRotateTitle.style.display = "none";
    oaRotateDepInfo.style.display = "block";
    oaRotateTitle.active = false;
  }
  else {
    oaRotateDepInfo.style.display = "none";
    oaRotateTitle.style.display = "block";
    oaRotateTitle.active = true;
  }
}
glow.onDomReady(function(){
  if ( document.getElementById( "js-rotate-dep-info" ) ) {
    oaRotateTitle    = document.getElementById( "js-rotate-title" );
    oaRotateTitle.active = true;
    oaRotateDepInfo  = document.getElementById( "js-rotate-dep-info" );
    window.setInterval( "rotateDepInfo();", 5000 );
  }
});
/* END ROTATE DEPPING PRESENTER INFO */

glow.onDomReady(function(){

  function popUpString( width, height ) {
      return  'width      = ' + width  + ',' + 'height = ' + height + ',' +
              'toolbar    = no, ' + 'personalbar = no, ' + 'location = no, ' + 'directories = no, ' +
              'statusbar  = no, ' + 'menubar     = no, ' + 'status   = no, ' + 'resizable   = yes,' +
              'left       = 60, ' + 'screenX     = 60, ' + 'top      = 100,' + 'screenY     = 100 ';
  }

  Service = {
    toggleOnAir : function( flag ) {
      var type     = ( flag == "expanded"  ) ? "collapsed" : "expanded";
      var cookie   = ( flag == "expanded"  ) ? "c"         : "e";
      var linkText = ( flag == "expanded"  ) ? "More"      : "Less";
      glow.dom.get( "#toggle-more"      )[0].className = "js-button-" + type;
      glow.dom.get( "#on-air-now > div" )[0].className = "js-"        + type;
      glow.dom.get( "#toggle-more"      )[0].innerHTML = linkText;
      utils.cookie.set("sn", cookie, { domain: 'bbc.co.uk', path: '/radio2',     expires : 365 } );
      utils.cookie.set("sn", cookie, { domain: 'bbc.co.uk', path: '/programmes', expires : 365 } );
    },
    buildMoreLink : function() {
      if ( glow.dom.get( "#on-air-now .js-collapsed" )[0] ) {
        var moreLink = glow.dom.create( '<li class="more"><a href="#" id="toggle-more" class="js-button-collapsed">More</a></li>' );
      }
      else {
        var moreLink = glow.dom.create( '<li class="more"><a href="#" id="toggle-more" class="js-button-expanded">Less</a></li>' );
      }
      glow.events.addListener( moreLink.get( "#toggle-more" )[0], "click", function() { //fetch from the nodeList
        ( this.className == "js-button-collapsed" ) ? Service.toggleOnAir( "collapsed" ) : Service.toggleOnAir( "expanded" );
        return false; //cancel default action.
      });
      moreLink.insertBefore( "#on-air-now .glance li.webcam" );
    },
    buildPopUpHandlers : function() {
      glow.events.addListener( ".webcam-console" , "click", function() { window.open( this.href,'webcam' , popUpString( 450, 425 ) ); return false; });
      glow.events.addListener( ".iplayer-console", "click", function() { window.open( this.href,'iPlayer', popUpString( 512, 270 ) ); return false; });
    },
    embedLiveText : function() {
      //hide the div
      glow.dom.get( "#on-air-now #live-text")[0].style.display = "none";
      if (glow.dom.get('#live-text').length > 0) {
        var myFlash = new glow.embed.Flash("http://www.bbc.co.uk/radio/livetext/version2/bin/PushClient.swf", "#live-text", "9", {
          width: "345px",
          height: "50px",
          id: "livetextpushclient",
          params: {
                allowScriptAccess: "always",
                bgcolor: "#ffffff",
                allowfullscreen: "false",
                wmode: "transparent",
            flashvars: {
                        config: "/radio2/include/livetext_config.xml"
            }
          }
        });
        myFlash.embed();
      }
      //reshow the div
      glow.dom.get( "#on-air-now #live-text")[0].style.display = "block";
    }
  }

  try {Service.embedLiveText();} catch (e) {}
  try {Service.buildMoreLink();} catch (e) {}
  try {Service.buildPopUpHandlers();} catch (e) {}

  //catch all iPlayer console links
  var iPlayerConsole = /iplayer\/\w+\/console/;
  for ( var i=0; i<document.links.length; i++ ) {
    if ( iPlayerConsole.test( document.links[i] ) ) {
      glow.events.addListener( document.links[i] , "click", function() { window.open( this.href,'webcam' , popUpString( 512, 270 ) ); return false; });
    }
  }


}); //end onDomReady 