var d2ptb_util = 
{
    "id" : 'd2ptb_util',

    "src_url" : "https://images.d2pass.com/images/js/D2PToolbarLoader_2010.js",
    "src_simple_url" : "https://images.d2pass.com/images/js/D2PToolbarLoader_2010_simple.js",
    "cache_url" : "http://smovie.heydouga.com/d2ptoolbar/cache.js",
    "css_url" : "http://smovie.heydouga.com/d2ptoolbar/cache.css",

    "$" : function( n )
    {
        return document.getElementById( n );
    },

    "queryLength" : function( )
    {
        return ((this.$(this.id).src.split("?",2))[1]).length;
    },

    "queryStRaw" :  function(ji, l)
    {
        if( l && l.split("?").length > 1 )
        {
            s = l.split("?");
            hu = s.slice( 1, s.length ).join( "?" );
        }
        else
        {
            hu = window.location.search.substring(1);
        }

        if( !hu ) return null;

        gy = hu.split("&");
        pos = 0;

        for (i=0;i<gy.length;i++) {
            ft = gy[i].split("=");
            if (ft[0] == ji) {
                return [ft.slice( 1, ft.length ).join( "=" ), ( pos + (ft[0]).length + 1 ), hu.length];
            }

            pos+=gy[i].length + 1;
        }

        return null;
    },

    "getCookie" :function(cookieName)
    {
        var search = cookieName + '=';
        if (document.cookie.length>0) {
                offset = document.cookie.indexOf(search);
                if (offset != -1){
                        offset += search.length;
                        end             = document.cookie.indexOf(';',offset);
                        if(end == -1)
                                end = document.cookie.length;
                        return unescape(document.cookie.substring(offset,end));
                }
        }

        return null;
    },

    "querySt" : function( n )
    {
        if( !this.$(this.id) || 0 >= this.$(this.id).src.length ||
            null == this.queryStRaw( n, this.$(this.id).src ) )
        {
            return null;
        }
        else
        {
            return this.queryStRaw( n, this.$(this.id).src )[0];
        }
    },

    "queryPos" : function( n )
    {
        if( !this.$(this.id) || 0 >= this.$(this.id).src.length ||
            null == this.queryStRaw( n, this.$(this.id).src ) )
        {
            return 0;
        }
        else
        {
            return this.queryStRaw( n, this.$(this.id).src )[1];
        }    
    },

    "querySubstr" : function( n, l )
    {
        if( !this.$(this.id) || 0 >= this.$(this.id).src.length ||
            null == this.queryStRaw( n, this.$(this.id).src ) )
        {
            return null;
        }
        else
        {
            var length = ( !l ) ? this.queryLength( ) : l;
            var pos = this.queryStRaw( n, this.$(this.id).src )[1];

            s  = this.$(this.id).src.split("?");

            return s.slice( 1, s.length ).join( "?" ).substr( pos, length - pos );
        }         

    },

    "base64list" : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',

    "base64encode" : function(s)
    {
      var t = '', p = -6, a = 0, i = 0, v = 0, c;
    
      while ( (i < s.length) || (p > -6) ) {
        if ( p < 0 ) {
          if ( i < s.length ) {
            c = s.charCodeAt(i++);
            v += 8;
          } else {
            c = 0;
          }
          a = ((a&255)<<8)|(c&255);
          p += 8;
        }
        t += this.base64list.charAt( ( v > 0 )? (a>>p)&63 : 64 )
        p -= 6;
        v -= 6;
      }
      return t;
    },

    "base64decode" : function(s)
    {
      var t = '', p = -8, a = 0, c, d;
    
      for( var i = 0; i < s.length; i++ ) {
        if ( ( c = this.base64list.indexOf(s.charAt(i)) ) < 0 )
          continue;
        a = (a<<6)|(c&63);
        if ( ( p += 6 ) >= 0 ) {
          d = (a>>p)&255;
          if ( c != 64 )
            t += String.fromCharCode(d);
          a &= 63;
          p -= 8;
        }
      }
      return t;
    },

   "loadFile" : function( filetype, filename, id )
   {
        var e;

        if (filetype == "js" )
        {
             e = document.createElement('script')
             e.setAttribute("type","text/javascript")
             if(id)e.setAttribute("id", id)
             e.setAttribute("src", filename)
        }
        else if (filetype=="css")
        {
            e = document.createElement("link")
            e.setAttribute("rel", "stylesheet")
            e.setAttribute("type", "text/css")
            e.setAttribute("href", filename)
        }

        document.getElementsByTagName("head")[0].appendChild( e )
    },

    "onloadloaded" : 0,

    "onload" : function( )
    {
        if( d2ptb_util.onloadloaded == 1 ) return;

        d2ptb_util.onloadloaded = 1;
    },

   "init" : function( )
   {

       if( window.addEventListener )
       {
           window.addEventListener("load", this.onload, false);
       }
       else if( window.attachEvent )
       {
           window.attachEvent("onload", this.onload);
       }
       else
       {
           window.onload = this.onload;
       }

       if( window.ActiveXObject )
       {
           this.loadFile( "css", this.css_url );
       }
    
       if( !this.getCookie('NetiA') && !this.querySt('nocache') )
       {
           this.loadFile( "js", this.cache_url );
       }

//       setTimeout( this.onload, 10000 );

       if( this.querySt('callback') || typeof initToolbar != 'undefined' )
       {
           try
           {
               if( typeof initToolbar != 'undefined' )
               {
                   initToolbar( );
               }

               if( this.querySt('callback') )
               {
                   eval( this.querySt('callback') );
               }
          }
          catch(e)
          {}
       }

   }

};


if( typeof d2p_toolbar_logout == 'undefined' )
{
    d2p_toolbar_logout = function( )
    {
        var url = ( d2ptb_util.querySt('logout') ) ? d2ptb_util.querySt('logout') : "/logout.php";

        if( url.match( /^javascript:/ ) )
        {
            try
            {
                eval( url.replace( /^javascript:/, '') );
                return;
            }
            catch(e)
            {
                var url = ( d2ptb_util.querySt('defaultlogout') ) ? d2ptb_util.querySt('defaultlogout') : "/logout.php";
            }
        }

        //delete NetiA Cookie
        document.cookie = 'NetiA=;path=/;domain=.'
                        + document.domain.replace( /^(www.|members.|mmovie.)/, '' )
                        + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";


        if( !url.match( /logout.php$/ ) )
        {
            top.window.location.href =  "http://www.d2pass.com/logout?pass=1&url=" + encodeURIComponent( url );
        }
        else if( d2ptb_util.querySubstr( "logouturl" ) )
        {
            if( d2ptb_util.querySubstr( "logouthasparams" ) )
            {
                top.window.location.href = url + "&url=" + document.URL;
            }
            else
            {
                top.window.location.href = url + "?url=" + document.URL;
            }
        }
        else
        {
            top.window.location.href = url;   
        } 
    }   

}

if( typeof d2p_toolbar_login == 'undefined' )
{
    d2p_toolbar_login = function( )
    {
        var login = ( d2ptb_util.querySt('login') ) ? d2ptb_util.querySt('login') : '/member/login.php';
        var query;

        if( login.match( /^javascript:/ ) )
        {
            try
            {
                eval( login.replace( /^javascript:/, '') );
                return;
            }
            catch(e)
            {
                var login = ( d2ptb_util.querySt('defaultlogin') ) ? d2ptb_util.querySt('defaultlogin') : "/logout.php";
            }
        }

        var url = ( d2ptb_util.querySt('ref') ) ? d2ptb_util.querySt('ref') : document.URL;

        if( 1 == d2ptb_util.querySt('encodeon') )
        {
            url = d2ptb_util.base64encode( url );
        }

        var query = d2ptb_util.querySubstr( "query" );

        if( query )
        {
            query = "?" + query + "&url=" + url;
        }
        else if( login.match( /login.php$/ ) )
        {
            query = "?url=" + url;
        }
        else
        {
            query = "";
        }

        var href = login + query;
        top.window.location.href = href;

    }   
}

if( typeof d2p_toolbar_site_join == 'undefined' )
{
    d2p_toolbar_site_join = function( )
    {
        var url = ( d2ptb_util.querySt('join') ) ? d2ptb_util.querySt('join') : "/join.html";

        if( url.match( /^javascript:/ ) )
        {
            try
            {
                eval( url.replace( /^javascript:/, '') );
                return;
            }
            catch(e)
            {
                var url = ( d2ptb_util.querySt('defaultjoin') ) ? d2ptb_util.querySt('defaultjoin') : "/logout.php";
            }
        }

        top.window.location.href = url;
    }
}

function initToolbar( )
{
    if( d2ptb_util.getCookie('NetiA') )
    {
        if( d2ptb_util.getCookie('d2p_free_member') || d2ptb_util.querySt('use_simple') == 1 )
        {
            document.write('<script language="javascript" src="'+d2ptb_util.src_simple_url+'"><\/script>' );
        }
        else
        {
            document.write('<script language="javascript" src="'+d2ptb_util.src_url+'"><\/script>' );
        }

    }
}

function D2ptbCacheCallback()
{
    if( d2ptb_util.querySt('nojoin') == 1 && d2ptb_util.$('d2p_joinBtn') )
    {
        d2ptb_util.$('d2p_joinBtn').innerHTML = '';
    }
}

d2ptb_util.init();

