var Access;

(function($) {
  Access = {
	
    vCookieName: '_hmm_ms_access',
    
    vPageCookieName: '_hmm_ms_page',
    
    vOptions: { path: '/', expires: 30 },
    
    vPageName: '',
    
    vRestricted: new Array(),
    
    init: function()
    {
//      this.vRestricted.push('whats-new');
//      this.vRestricted.push('product-capabilities');
//      this.vRestricted.push('videos');
//      this.vRestricted.push('video-ethics');
//      this.vRestricted.push('video-leadership');
      this.vRestricted.push('whitepapers');
//      this.vRestricted.push('webinars');
      
      /** Get Page Name **/
      this.vPageName = this.getPageName();
      
      /** Execute Check **/
      Access.hasAccess();
    },
    
    getPageName: function()
    {
      vPath = location.pathname;
      vPageName = vPath.split('/').pop().replace('.html','');
      return vPageName;
    },
    
    hasAccess: function()
    {
      for(var vPage in this.vRestricted)
      {
        if(this.vRestricted[vPage] == this.vPageName)
        {
          Access.hasCookie();  
        }
      }
    },

    hasCookie: function()
    {
      if(!$.cookie(this.vCookieName))
      {
    	Access.deleteCookie();
    	Access.setPageCookie();
        location.href = 'login.html';
      }
    },

    setCookie: function()
    {
      $.cookie(this.vCookieName,'true',this.vOptions);
    },
    
    setPageCookie: function()
    {
      $.cookie(this.vPageCookieName,this.vPageName + '.html',this.vOptions);
    },

    deleteCookie: function()
    {
      $.cookie(this.vCookieName,null);
    },
    
    processLogin: function()
    {
      $("#invalid-login").hide();
      
      $.ajax({
    	url: 'http://assessment.hbsp.org/microsite_authentication/index.php',
    	dataType: 'jsonp',
    	jsonp: 'jsonp_callback',
        data: "emailaddress=" + $("#login_emailaddress").val() + "&password=" + $("#login_password").val() + "&action=auth&siteid=1"   
      });
    },
    
    processResponse: function(data)
    {
      if(data.results.status == "success")
      {
        Access.setCookie();
        Access.redirectToPage();
      }	
      else
      {
        $("#invalid-login").show();
      }
    },
    
    redirectToPage: function()
    {
    	vRedirect = $.cookie(this.vPageCookieName);
    	if(vRedirect == '' || vRedirect == null || vRedirect == undefined)
        {
    		vRedirect = 'index.html';
        }
        location.href = vRedirect;
    }
    
  } // end Access
})(jQuery);
