/**
 * create Cinema.AD in SAPO.Cinema.namespace
 * Author Steven Koch @sapo
 */
if(!SAPO.Cinema || typeof(SAPO.Cinema) == 'undefined') {
   SAPO.namespace('Cinema');
}

SAPO.Cinema.AD = {

    /*URL to request mrec*/
    baseUrl: '/pub/mrecCinema.php?fromCallServer=true',
    //mrec iframe
    qSelIframeAd: '.mrec iframe',
    //mrec div
    elementMrec: 'div_mrec',
    //regiter Iframe element
    IFrameObj: null,


    /**
     * get pub from mrec to video player
     */
    getPubVideoSapo: function( ) {
    	return document.getElementsByTagName('iframe')[0].contentWindow.getPubVideoSapo( );
    },


    /**
     * content was refreshed, take some actions
     */
    contentRefreshed: function()
    {
	    // Refresh MREC iframe
      	var mRec = $$(this.elmIframeAd)[0];//prototype lib
      	if (mRec){
      		var src = mRec.src;
      		mRec.src = null;
      		mRec.src = src;
      	}else{
      		//ajax request
      		return this.callToServer();
      	}
    },


    /**
     * Request Server IFrame
     * Not save on historic timeline of browser!
     */
    callToServer: function() {
      if (!document.createElement) return true;//not done!

      var IFrameDoc;
      var URL = this.baseUrl + '&fromCallServer=true&rnd=' + Math.random();
      if (!this.IFrameObj && document.createElement) {
       try {
          var tempIFrame=document.createElement('iframe');
          tempIFrame.setAttribute('id','RSIFrame');
          tempIFrame.style.border='0px';
          tempIFrame.style.width='0px';
          tempIFrame.style.height='0px';
          this.IFrameObj = document.body.appendChild(tempIFrame);

          if (document.frames) {
            this.IFrameObj = document.frames['RSIFrame'];
          }
        } catch(exception) {
          iframeHTML='\<iframe id="RSIFrame" style="';
          iframeHTML+='border:0px;';
          iframeHTML+='width:0px;';
          iframeHTML+='height:0px;';
          iframeHTML+='"><\/iframe>';
          document.body.innerHTML+=iframeHTML;
          this.IFrameObj = new Object();
          this.IFrameObj.document = new Object();
          this.IFrameObj.document.location = new Object();
          this.IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
          this.IFrameObj.document.location.replace = function(location) {
            this.iframe.src = location;
          }
        }
      }
      //hack to Gecko browser
      if (navigator.userAgent.indexOf('Gecko') !=-1 && !this.IFrameObj.contentDocument) {
        setTimeout('callToServer()',10);
        return false;
      }
      if (this.IFrameObj.contentDocument) {
        // For NS6
        IFrameDoc = this.IFrameObj.contentDocument;
      } else if (this.IFrameObj.contentWindow) {
        // For IE5.5 and IE6
        IFrameDoc = this.IFrameObj.contentWindow.document;
      } else if (this.IFrameObj.document) {
        // For IE5
        IFrameDoc = this.IFrameObj.document;
      } else {
        return true;
      }
      IFrameDoc.location.replace(URL);
      return false;
    }
};

/**
 * push data(inrhtml) to mRec element
 */
function chargeMREC(inrhtml){
    try{
        var mRec = document.getElementById(SAPO.Cinema.AD.elementMrec);
        var oldAd = mRec.innerHTML;
        mRec.innerHTML = '';
        document.getElementById(SAPO.Cinema.AD.elementMrec).appendChild(inrhtml);
    }
    catch(err)
    {
        if(inrhtml.innerHTML!='')
            mRec.innerHTML = inrhtml.innerHTML;
        else
            mRec.innerHTML = oldAd;
    }
}


