﻿var videoAdSOAP;
var VideoAdSOAP = function(videoUrl)
{
    this.videoContentUrl = videoUrl;
    this.videoAd = null;
    this.getVideoAd = getVideoAd;
    this.sr = null;
    this.createSOAPRequest = createSOAPRequest;
    this.send = send;
    this.processResponse = processResponse;
    this.process = process;
    //SOAPClient.SendRequest(sr, processResponse); //Send request to server and assign a callback
    this.processResponseEndFunction = null;
    this.setProcessResponseEndEventListener = setProcessResponseEndEventListener;
    this.createSOAPRequest(this.videoContentUrl);
    videoAdSOAP = this;       
}
function createSOAPRequest()
{
   var soapBody = new SOAPObject("GenerateAdByMediaUrlAndSize"); //Create a new request object
    soapBody.ns = "http://tempuri.org/";
    soapBody.appendChild(new SOAPObject("mediaUrl")).val(escape(this.videoContentUrl));
    soapBody.appendChild(new SOAPObject("width")).val($(adDiv).width());
    soapBody.appendChild(new SOAPObject("height")).val($(adDiv).height());
    //Create a new SOAP Request
    this.sr = new SOAPRequest("http://tempuri.org/GenerateAdByMediaUrlAndSize", soapBody); //Request is ready to be sent
    //Lets send it
    //SOAPClient.Proxy = "http://player.mvertex.co.il/player/layerads/proxy.aspx";
    //SOAPClient.Proxy = "http://demo.com:34627/TestJquery/proxy.aspx";
    //if(mediaVertex_proxy == undefined)
      //  mediaVertex_proxy = "http://player.mvertex.co.il/player/layerads/proxy.aspx";
    SOAPClient.Proxy = "http://player.mvertex.co.il/player/layerads/proxy.aspx";
   SOAPClient.SOAPServer  = "http://player.mvertex.co.il/MediaWebService/AdWebService.asmx"; //Specify web-service address or a proxy file
    // SOAPClient.SendRequest(this.sr, processResponse); 
}

function send()
{
    //SOAPClient.SendRequest(this.sr, this.processResponse); 
    this.process();
}

function getVideoAd()
{
    //return "<iframe name='iframeName' id='iframeName' allowtransparency='true' width='" + $(adDiv).width() + "' height='" + $(adDiv).height() + "' marginheight='0' scrolling='no'  frameborder='0' src=\"http://player.mvertex.co.il/player/netoAds.aspx?width=" + $(adDiv).width() + "&height=" + $(adDiv).height() + "\"></iframe>";
    return this.videoAd;
}
function process()
 {
    var videoAd = new VideoAd();
    videoAd.setStartTime(8000);
   videoAd.setDuration(12000);
   var escapedVideoUrl = escape(this.videoContentUrl);
   //videoAd.setAdCode("<iframe name='iframeName' id='iframeName' allowtransparency='true' width='404' height='67' marginheight='0' scrolling='no'  frameborder='0' src=\"http://player.mvertex.co.il/player/netoAds.aspx?width=404&height=67&cuntentUrl=" + escapedVideoUrl + "\"></iframe>");
   videoAd.setAdCode("<iframe name='iframeName' id='iframeName' allowtransparency='true' width='404' height='67' marginheight='0' scrolling='no'  frameborder='0' src=\"http://advertex.mvertex.co.il/AdVertex/netoAds.aspx?width=404&height=67&contentUrl=" + escapedVideoUrl + "\"></iframe>");
   videoAd.setWidth($(adDiv).width());
   videoAd.setHeight($(adDiv).height());
   videoAdSOAP.videoAd = videoAd;
   //alert(responseMessage.EndTime[0].Text-responseMessage.StartTime[0].Text);
   //alert(videoAdSOAP.processResponseEndFunction);
   videoAdSOAP.processResponseEndFunction();
   
   
} 

function processResponse(respObj)
 {
 
    var videoAd = new VideoAd();
  try {
    //alert(respObj);
    //respObj is a JSON equivalent of SOAP Response XML (all namespaces are dropped)
    //... do something with response
    //Notice how I set references to the returned values. SOAP Response node names are kept the same, and become arrays
  var responseMessage = respObj.Body[0].GenerateAdByMediaUrlAndSizeResponse[0].GenerateAdByMediaUrlAndSizeResult[0];
    //Let's set those textbox values (using jQuery)
    //$("#result").html("That div is " + responseMessage.LocationX[0].Text);
   } 
   catch(e) 
   { 
        alert("Unable to resolve ZIP Code"); 
   }
   videoAd.setStartTime(parseInt(responseMessage.StartTime[0].Text));
   videoAd.setDuration(parseInt(responseMessage.EndTime[0].Text-responseMessage.StartTime[0].Text));
   videoAd.setAdCode(responseMessage.AdUrl[0].Text);
   videoAd.setWidth(responseMessage.Width[0].Text);
   videoAd.setHeight(responseMessage.Height[0].Text);
   videoAdSOAP.videoAd = videoAd;
   //alert(responseMessage.EndTime[0].Text-responseMessage.StartTime[0].Text);
   //alert(videoAdSOAP.processResponseEndFunction);
   videoAdSOAP.processResponseEndFunction();
   
   
} 

function setProcessResponseEndEventListener(processResponseEndFunction)
{
    this.processResponseEndFunction =processResponseEndFunction;
    
}


