﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Microsoft.Live.ClientControls");

Microsoft.Live.ClientControls.IDLoginStatusClientControl = function(element) {
    Microsoft.Live.ClientControls.IDLoginStatusClientControl.initializeBase(this, [element]);
    this.ClientOnLogin = null;
    this.ClientOnLogOut = null;
    this.ID = element.id;   
    
    //var clientact = document.getElementById(this.ID+"__clientaction"); 
            
    
}

Microsoft.Live.ClientControls.IDLoginStatusClientControl.prototype = {
    
    initialize: function() {
        Microsoft.Live.ClientControls.IDLoginStatusClientControl.callBaseMethod(this, 'initialize');                    
        
	    var clientval;
	    clientval = this.readCookie("action");
        
        if (clientval) {
          switch(clientval)
          {
                    case 'login' :
                        this.ClientLogin();                                         
                        break;
                    
                    case 'logout' :   
                        this.ClientLogOut();                                                    
                        break;
          }          
          this.clearCookie("action","");
        }
     
    },
    
    readCookie:function(name) {
	    var nameEQ = name + "=";
	    if (document.cookie) {
	        var ca = document.cookie.split(';');
	        for(var i=0;i < ca.length;i++) {
		        var c = ca[i];
		        while (c.charAt(0)==' ') c = c.substring(1,c.length);
		        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	        }
	    }
	    return null;
    },    
    
    
    
    clearCookie:function(name,value) {
    	var date = new Date();
	    date.setTime(date.getTime()+(-1*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	    document.cookie = name+"="+value+expires+"; path=/";
    },
    

    // events
    add_ClientOnLogin:function(handler)
    {
     if(handler!=null)
     {
      this.get_events().addHandler("clientlogin", handler);
     }
    },
    remove_ClientOnLogin:function(handler)
    {
     if(handler!=null)
     {
      this.get_events().removeHandler("clientlogin", handler);
     }
    }, 
      
    add_ClientOnLogOut:function(handler)
    {
     if(handler!=null)
     {
      this.get_events().addHandler("clientlogout", handler);   
     }
    },
    remove_ClientOnLogOut:function(handler)
    {
     if(handler!=null)
     {
      this.get_events().removeHandler("clientlogout", handler);
     }
    }, 
       
    ClientLogin:function()
    {      
      var h = this.get_events().getHandler('clientlogin');      
      if (h) 
       {
        var cancelEventArgs = new Sys.CancelEventArgs();
        h(this,cancelEventArgs);        
        
        //if (!cancelEventArgs.get_cancel()) 
        //this._sendAsyncEventMessage('login');
        //var clientact = document.getElementById(this.ID+"__clientaction");
        //clientact.value=" ";
       }        
     },   
     
     ClientLogOut:function()
     {           
      var h = this.get_events().getHandler('clientlogout');
      var cancelEventArgs = new Sys.CancelEventArgs();
      if (h) 
       {
        cancelEventArgs = new Sys.CancelEventArgs();
        h(this,cancelEventArgs);        
        //if (!cancelEventArgs.get_cancel()) 
        //this._sendAsyncEventMessage('logout');
        //var clientact = document.getElementById(this.ID+"__clientaction");
        //clientact.value=" ";
       }
          
     },
     
    dispose: function() {        
        //Add custom dispose actions here
        Microsoft.Live.ClientControls.IDLoginStatusClientControl.callBaseMethod(this, 'dispose');
    }
  
}

Microsoft.Live.ClientControls.IDLoginStatusClientControl.registerClass('Microsoft.Live.ClientControls.IDLoginStatusClientControl', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


Type.registerNamespace('Microsoft.Live.ServerControls');Microsoft.Live.ServerControls.Resource={};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();