﻿Type.registerNamespace('EvNet.UI');
EvNet.UI.PageGate=function(element)
{
  
  EvNet.UI.PageGate.initializeBase(this,[element]);
  this._installFrame=null;
  this._rootPath='/PageGate';
  this._promptPath='installprompt.html';
  this._template='default';
  this._locale='en';
  this._promptInterval=30;
  this._silverlightVersion='2.0';
  this._visitImageUrl='';
  this._installLink='http://go.microsoft.com/fwlink/?LinkID=92799';
  this._closeLink='';
  this._frameLoadedDelegate=null;
  this._closeButtonClickedDelegate=null;
  this._windowResizeDelegate=null;
  this._windowScrollDelegate=null;
  this.__cookieName='SLInstallPostponed';
  this.__dialogHasBeenShown=false;
  
}

EvNet.UI.PageGate.prototype=
{  
  get_installFrame:function()
  {
    return this._installFrame;    
  }
  ,set_installFrame:function(value)
  {    
    this._installFrame=value;    
  }
  ,get_rootPath:function()
  {    
    return this._rootPath;   
  }
  ,set_rootPath:function(value)
  {    
    this._rootPath=value;    
  }
  ,get_promptPath:function()
  {   
    return this._promptPath;    
  }
  ,set_promptPath:function(value)
  {    
    this._promptPath=value;    
  }
  ,get_template:function()
  {    
    return this._template;    
  }
  ,set_template:function(value)
  {    
    this._template=value;    
  }
  ,get_locale:function()
  {    
    return this._locale;    
  }
  ,set_locale:function(value)
  {    
    this._locale=value;    
  }
  ,get_promptInterval:function()
  {    
    return this._promptInterval;    
  }
  ,set_promptInterval:function(value)
  {    
    this._promptInterval=value;    
  }
  ,get_silverlightVersion:function()
  {    
    return this._silverlightVersion;    
  }
  ,set_silverlightVersion:function(value)
  {    
    this._silverlightVersion=value;    
  }
  ,get_visitImageUrl:function()
  {    
    return this._visitImageUrl;    
  }
  ,set_visitImageUrl:function(value)
  {    
    this._visitImageUrl=value;    
  }
  ,get_installLink:function()
  {    
    return this._installLink;    
  }
  ,set_installLink:function(value)
  {    
    this._installLink=value;    
  }
  ,get_closeLink:function()
  {    
    return this._closeLink;    
  }
  ,set_closeLink:function(value)
  {    
    this._closeLink=value;    
  }
  ,initialize:function()
  {    
    EvNet.UI.PageGate.callBaseMethod(this,'initialize');
    this._frameLoadedDelegate=Function.createDelegate(this,this._frameLoaded);
    $addHandler(this.get_installFrame(),'load',this._frameLoadedDelegate);
    this._onLoad();    
  }
  ,dispose:function()
  {    
    var iframe=this.get_installFrame();
    var closeButton;

    if(this._frameLoadedDelegate!=null)
    {      
      $removeHandler(iframe,'load',this._frameLoadedDelegate);
      this._frameLoadedDelegate=null;
      closeButton=iframe.contentWindow.document.getElementById('PageGateCloseBtn');
      if(closeButton!=null&&this._closeButtonClickedDelegate!=null)
      {        
        $removeHandler(closeButton,'click',this._closeButtonClickedDelegate);
        this._closeButtonClickedDelegate=null;        
      }      
    }
    
    if(this._windowScrollDelegate!=null)
    {      
      $removeHandler(window,'scroll',this._windowScrollDelegate);
      this._windowScrollDelegate=null;      
    }
    
    if(this._windowResizeDelegate!=null)
    {      
      $removeHandler(window,'resize',this._windowResizeDelegate);
      this._windowResizeDelegate=null;      
    }
    
    this._installFrame=null;
    this._rootPath=null;
    this._promptPath=null;
    this._template=null;
    this._locale=null;
    this._promptInterval=null;
    this._silverlightVersion=null;
    this._visitImageUrl=null;
    this._installLink=null;
    this._closeLink=null;
    this.__cookieName=null;
    EvNet.UI.PageGate.callBaseMethod(this,'dispose');
    
  }
  ,_onLoad:function()
  {
    var elementID=this.get_element().id;
    if(this._getBrowserIsSupported())
    {      
      if(!Silverlight.isInstalled(this.get_silverlightVersion())&&!this.__dialogHasBeenShown)
      {        
        if(!this._hasCookie())
        {          
          this._showDialog();
          this._setCookie();          
        }        
      }
      
      if(!(Silverlight.available||Silverlight.ua.Browser!='MSIE'))
      {        
        TimeoutDelegate=function()
        {          
          $find(elementID)._onLoad();          
        }        
        setTimeout(TimeoutDelegate,3000);        
      }      
      else
      {        
        this.get_element().style.display='none';
        $find('overlay').hide();        
      }      
    }    
  }
  ,_showDialog:function()
  {
    this.get_installFrame().src=String.format('{0}/templates/{1}/{2}/{3}',this.get_rootPath(),this.get_template(),this.get_locale(),this.get_promptPath());    
  }
  ,_frameLoaded:function(sender,e)
  {    
    var iframe=this.get_installFrame();
    var visitImage=iframe.contentWindow.document.getElementById('PageGateViewImage');
    var installButton=iframe.contentWindow.document.getElementById('PageGateInstallBtn');
    var closeButton=iframe.contentWindow.document.getElementById('PageGateCloseBtn');
    if(!String.isNullOrEmpty(this.get_visitImageUrl()))
        visitImage.src=this.get_visitImageUrl();
    if(!String.isNullOrEmpty(this.get_installLink()))
        installButton.href=this.get_installLink();
    this._closeButtonClickedDelegate=Function.createDelegate(this,this._closeButtonClicked);
    Sys.UI.DomEvent.addHandler2(closeButton,'click',this._closeButtonClickedDelegate,iframe.contentWindow);
    this._windowResizeDelegate=Function.createDelegate(this,this._windowChange);
    $addHandler(window,'resize',this._windowResizeDelegate);
    if(Silverlight.ua.Browser=='MSIE')
    {      
      var ieVersion=parseFloat(navigator.appVersion.split("MSIE")[1]);
      if(ieVersion>=6&&ieVersion<7)
      {        
        this._windowScrollDelegate=Function.createDelegate(this,this._windowChange);
        $addHandler(window,'scroll',this._windowScrollDelegate);        
      }      
    }
    
    $removeHandler(iframe,'load',this._frameLoadedDelegate);
    this._frameLoadedDelegate=null;
    $find('overlay').show();
    this.get_element().style.display='block';
    this.__dialogHasBeenShown=true;
    this._alignPopup();
    
  }
  ,_closeButtonClicked:function(sender,e)
  {    
    this.get_element().style.display='none';
    $find('overlay').hide();
    if(!String.isNullOrEmpty(this.get_closeLink()))
        this.get_installFrame().src=this.get_closeLink();
    return false;    
  }
  ,_windowChange:function(sender,e)
  {    
    this._alignPopup();    
  }
  ,_alignPopup:function()
  {    
    var element=this.get_element();
    var bMeasurements=Globals.getBrowserMeasurements();
    var eMeasurements=Globals.getElementSize(element);
    element.style.left=((bMeasurements.window.width/2)-(eMeasurements.width/2))+'px';
    element.style.top=((bMeasurements.window.height/2)-(eMeasurements.height/2))+'px'; 
  }
  ,_getBrowserIsSupported:function()
  {    
    return Silverlight.ua.Browser!='Unsupported';    
  }
  ,_hasCookie:function()
  {    
    if(this.get_promptInterval()>-1)
    {      
      var hasCookie=false;
      var cookie=''+document.cookie;
      var start=cookie.indexOf(this.__cookieName);
      var cookieValue='';
      if(!(cookie==''||start==-1))
      {        
        var end=cookie.indexOf(';',start);
        if(end==-1)
            end=cookie.length;
        cookieValue=unescape(cookie.substring(start+this.__cookieName.length+1,end));
      }
      
      if(cookieValue=='')
        return false;
      else
        return true;      
    }    
    else
        return false;    
  }
  ,_setCookie:function()
  {    
    if(this.get_promptInterval()>-1)
    {      
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+this.get_promptInterval());
      document.cookie=String.format('{0}=ok;path=/;expires={1}',this.__cookieName,exdate.toGMTString());      
    }    
  }  
}

EvNet.UI.PageGate.registerClass('EvNet.UI.PageGate',Sys.UI.Control);
Sys.UI.DomEvent.addHandler2=function Sys$UI$DomEvent$addHandler(element,eventName,handler,contentWindow)
{
  
  var e=Function._validateParams(arguments,[
          {    
            name:"element",domElement:true
          }
          ,
          {
            
            name:"eventName",type:String
          }
          ,
          {
            
            name:"handler",type:Function
          }
          ,
          {
            
            name:"contentWindow",type:Object,optional:true,mayBeNull:true
          }
  ]);
  if(e)throw e;
  if(!element._events)
  {    
    element._events= { };    
  }
  
  var eventCache=element._events[eventName];
  if(!eventCache)
  {    
    element._events[eventName]=eventCache=[];    
  }
  
  var browserHandler;
  if(element.addEventListener)
  {    
    browserHandler=function(e)
    {      
      return handler.call(element,new Sys.UI.DomEvent(e));      
    }
      
    element.addEventListener(eventName,browserHandler,false);
  }
  else if(element.attachEvent)
  {    
    browserHandler=function()
    {      
      var w=(contentWindow==null)?window.event:contentWindow
        return handler.call(element,new Sys.UI.DomEvent(w.event));      
    }
    
    element.attachEvent('on'+eventName,browserHandler);    
  }
  
  eventCache[eventCache.length]= { handler:handler,browserHandler:browserHandler
  };
  
}

if(Sys!=undefined)Sys.Application.notifyScriptLoaded();
